From patchwork Tue Oct 13 01:34:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 11834689 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6C342697 for ; Tue, 13 Oct 2020 01:34:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2E76621D7F for ; Tue, 13 Oct 2020 01:34:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=arista.com header.i=@arista.com header.b="FGKDY4+X" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2E76621D7F Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=arista.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 8BA31940007; Mon, 12 Oct 2020 21:34:24 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7FABF94000B; Mon, 12 Oct 2020 21:34:24 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 676A4940007; Mon, 12 Oct 2020 21:34:24 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0152.hostedemail.com [216.40.44.152]) by kanga.kvack.org (Postfix) with ESMTP id 29C2494000A for ; Mon, 12 Oct 2020 21:34:24 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id AB01E181AE865 for ; Tue, 13 Oct 2020 01:34:23 +0000 (UTC) X-FDA: 77365182006.04.bear15_050ef6a271ff Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin04.hostedemail.com (Postfix) with ESMTP id 9086580061E1 for ; Tue, 13 Oct 2020 01:34:23 +0000 (UTC) X-Spam-Summary: 1,0,0,efc1ff3fdb2e088d,d41d8cd98f00b204,dima@arista.com,,RULES_HIT:41:69:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1543:1711:1730:1747:1777:1792:2393:2553:2559:2562:2890:2910:3138:3139:3140:3141:3142:3355:3865:3866:3867:3870:3871:3872:3874:4118:4250:4321:5007:6261:6653:6742:6743:9036:9592:10004:11026:11473:11658:11914:12043:12114:12296:12297:12438:12517:12519:12555:12679:12683:12895:12986:13255:13894:14181:14394:14721:21080:21220:21444:21450:21451:21627:21990:30054:30069:30070:30079:30090,0,RBL:209.85.128.67:@arista.com:.lbl8.mailshell.net-62.2.0.100 66.100.201.201;04yfu1tj1w7g8zjd8wfzqo5g43pgbop84yw1n4dh6bw78tkk1rxuq4iu8h9axow.p8kw8dmnn8wo7fr9ij9mdnyp8otjk79cgzsfqoh79jfd9e7mp6hb9wnzxgof6c1.q-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: bear15_050ef6a271ff X-Filterd-Recvd-Size: 7077 Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by imf04.hostedemail.com (Postfix) with ESMTP for ; Tue, 13 Oct 2020 01:34:22 +0000 (UTC) Received: by mail-wm1-f67.google.com with SMTP id e23so12248154wme.2 for ; Mon, 12 Oct 2020 18:34:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=N0dBjMGQZwr5fzpQZu0PyDC9RTW1+FomX32MrbIIN2c=; b=FGKDY4+X3JgwoUoN1C/3HtoPUDzAwaeKRnm9weDnUEBjEuPWSOeILJ5WitdE7ezOUP Z8h6z8QjmHrQkKSfvP/CqOKrMe2VeWaoWuM1DTGhWnLuZk1g88dKU5Im4GTOJwORpW2f UuV18UZCz944g3qypRZ2XQhlg6JzVZmLhMr5Bs/tnrm1RAUdEgB/WdGaLZGTdqqhIMgV pfG53+RblNOsJA22ZNjqtEkKPrKzJBc7Og2rE+hr0YIrlaORjXWV2alA7tDY1jhP1fno 43O1Nu5NaFD+pRDCg3zxhKfu0Owyg1VkXKC8ZVHZFjGsiuAaTBtUv+EuEgHOsI8dOIdW j1Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N0dBjMGQZwr5fzpQZu0PyDC9RTW1+FomX32MrbIIN2c=; b=b8QonqZko8MabQ6AN+IGaZWQnP/Ot//qtAfSzKtp9118PFC6kagLIRLEfRpIJuDwW0 Dchxr0ByYNyd7KMAjpU3AtuqtjyITaI6B81kxTezbbcfVtVZmnox2dVTH2R+i9WcgTqG t5U/i2YezgrcbS5IqDRImQVEWTWVCr9hdswS+1Bcm1xzkKpZlycmiEoJFiODsWZZeU9e Zbxmqxhon2L+h81esD382i6cNNyZl2EapL+bWnZbcSLxtIAPRkRKtiWsvMZcWATe3OcZ DSrcUgmX4IipJg5m1V04dbM+3Idnngll684wS3d4jMaSvGsFMYk9RbGan9tX3kGCNZdX fyIg== X-Gm-Message-State: AOAM531XuGSP9T4SdzR8QZPyXFnFQf4qsVMs+RHS8VCGNfF0uScRHzVv B3r86jCMEXQ8g+f3RFUYvhludg== X-Google-Smtp-Source: ABdhPJzYLde4t7k258XJEoIwmIIg3SOE4QcEX3b/iLBAwsA51BD5ldG7hvxtYYzYqpyGbgSwcygcoQ== X-Received: by 2002:a7b:c0c8:: with SMTP id s8mr12752537wmh.78.1602552862013; Mon, 12 Oct 2020 18:34:22 -0700 (PDT) Received: from localhost.localdomain ([2a02:8084:e84:2480:228:f8ff:fe6f:83a8]) by smtp.gmail.com with ESMTPSA id d23sm24825325wmb.6.2020.10.12.18.34.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Oct 2020 18:34:21 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Dmitry Safonov , Alexander Viro , Andrew Morton , Andy Lutomirski , Brian Geffon , Catalin Marinas , Dan Williams , Dave Jiang , Hugh Dickins , Ingo Molnar , "Kirill A. Shutemov" , Mike Kravetz , Minchan Kim , Russell King , Thomas Bogendoerfer , Thomas Gleixner , Vishal Verma , Vlastimil Babka , Will Deacon , linux-aio@kvack.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 2/6] mm/mremap: For MREMAP_DONTUNMAP check security_vm_enough_memory_mm() Date: Tue, 13 Oct 2020 02:34:12 +0100 Message-Id: <20201013013416.390574-3-dima@arista.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201013013416.390574-1-dima@arista.com> References: <20201013013416.390574-1-dima@arista.com> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Currently memory is accounted post-mremap() with MREMAP_DONTUNMAP, which may break overcommit policy. So, check if there's enough memory before doing actual VMA copy. Don't unset VM_ACCOUNT on MREMAP_DONTUNMAP. By semantics, such mremap() is actually a memory allocation. That also simplifies the error-path a little. Also, as it's memory allocation on success don't reset hiwater_vm value. Fixes: commit e346b3813067 ("mm/mremap: add MREMAP_DONTUNMAP to mremap()") Signed-off-by: Dmitry Safonov --- mm/mremap.c | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/mm/mremap.c b/mm/mremap.c index 03d31a0d4c67..c248f9a52125 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -365,11 +365,19 @@ static unsigned long move_vma(struct vm_area_struct *vma, if (err) return err; + if (unlikely(flags & MREMAP_DONTUNMAP && vm_flags & VM_ACCOUNT)) { + if (security_vm_enough_memory_mm(mm, new_len >> PAGE_SHIFT)) + return -ENOMEM; + } + new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT); new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff, &need_rmap_locks); - if (!new_vma) + if (!new_vma) { + if (unlikely(flags & MREMAP_DONTUNMAP && vm_flags & VM_ACCOUNT)) + vm_unacct_memory(new_len >> PAGE_SHIFT); return -ENOMEM; + } moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len, need_rmap_locks); @@ -398,7 +406,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, } /* Conceal VM_ACCOUNT so old reservation is not undone */ - if (vm_flags & VM_ACCOUNT) { + if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) { vma->vm_flags &= ~VM_ACCOUNT; excess = vma->vm_end - vma->vm_start - old_len; if (old_addr > vma->vm_start && @@ -423,34 +431,16 @@ static unsigned long move_vma(struct vm_area_struct *vma, untrack_pfn_moved(vma); if (unlikely(!err && (flags & MREMAP_DONTUNMAP))) { - if (vm_flags & VM_ACCOUNT) { - /* Always put back VM_ACCOUNT since we won't unmap */ - vma->vm_flags |= VM_ACCOUNT; - - vm_acct_memory(new_len >> PAGE_SHIFT); - } - - /* - * VMAs can actually be merged back together in copy_vma - * calling merge_vma. This can happen with anonymous vmas - * which have not yet been faulted, so if we were to consider - * this VMA split we'll end up adding VM_ACCOUNT on the - * next VMA, which is completely unrelated if this VMA - * was re-merged. - */ - if (split && new_vma == vma) - split = 0; - /* We always clear VM_LOCKED[ONFAULT] on the old vma */ vma->vm_flags &= VM_LOCKED_CLEAR_MASK; /* Because we won't unmap we don't need to touch locked_vm */ - goto out; + return new_addr; } if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) { /* OOM: unable to split vma, just get accounts right */ - if (vm_flags & VM_ACCOUNT) + if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) vm_acct_memory(new_len >> PAGE_SHIFT); excess = 0; } @@ -459,7 +449,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, mm->locked_vm += new_len >> PAGE_SHIFT; *locked = true; } -out: + mm->hiwater_vm = hiwater_vm; /* Restore VM_ACCOUNT if one or two pieces of vma left */