From patchwork Fri Aug 16 16:09:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bert Karwatzki X-Patchwork-Id: 13766575 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5BA43C531DC for ; Fri, 16 Aug 2024 16:10:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E37BE6B0361; Fri, 16 Aug 2024 12:10:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DE6386B0362; Fri, 16 Aug 2024 12:10:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C86D56B0363; Fri, 16 Aug 2024 12:10:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id AACAA6B0361 for ; Fri, 16 Aug 2024 12:10:15 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3BE1FC010E for ; Fri, 16 Aug 2024 16:10:15 +0000 (UTC) X-FDA: 82458595590.11.589764A Received: from mout.web.de (mout.web.de [217.72.192.78]) by imf29.hostedemail.com (Postfix) with ESMTP id 15F3E12001F for ; Fri, 16 Aug 2024 16:10:12 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b=dxxA5BNK; dmarc=pass (policy=quarantine) header.from=web.de; spf=pass (imf29.hostedemail.com: domain of spasswolf@web.de designates 217.72.192.78 as permitted sender) smtp.mailfrom=spasswolf@web.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723824553; a=rsa-sha256; cv=none; b=5bJQaMZEcrXN6Box4tXwOfNAB4HI4/yZzZIPqM/AVp4EQYtvztMqbgWh685hzIX4rpnAeu FQmBn6hdGhEdqOmdQXP8SI9Sni9A5JhejJlJJRH3vAkVv9X13QDOiyjuxWgpEQH0JqT0VN Jk5W5PIWSZFXTPpXFQhrOuVswFxZBx8= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b=dxxA5BNK; dmarc=pass (policy=quarantine) header.from=web.de; spf=pass (imf29.hostedemail.com: domain of spasswolf@web.de designates 217.72.192.78 as permitted sender) smtp.mailfrom=spasswolf@web.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723824553; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=AmchI/mG/89rB0ctm/Lc5xogMtZgADTp8F1OlxCErWc=; b=JNjxRJ3jVzm5SsgrY0wRZeNvlcrQVvtE4zpvFVSD8MZhABmHAMY6Wy0vXmFnptChNvuqaX u/aWsL1qA1l9UiNIGw1DRAReI+YbpnQ6JAt6Zbi03y7MsyGW3ec4qhjRqmOCz/9+GkpnAn rsv4fc4bFOCA9DRYk7ipe274hNdsbt0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1723824604; x=1724429404; i=spasswolf@web.de; bh=AmchI/mG/89rB0ctm/Lc5xogMtZgADTp8F1OlxCErWc=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-ID: MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=dxxA5BNKt5E5dGXUu/1mShVE24nJUcfB78loaTD872vuFvtgyz1RKXh5gGQ+rXLe 6/jIMEbGg7qrdkxNW2GOrzcFgnO2XadlpkOPb/yMGugpY0siNzA0fJz/0hWgMnzFo NAhqRBhN0dBPrtGJDOaU+ORNGMGYcqwGUer5HcwDY76cBhtuSbDz0D18WAwPCO2nd n3OCNvgWxKElISQzNYPqN/RcSoKJE2PotgKGJzAN/ihnS1PIt7UOW0dIa+pgs6pHB Tscg0WkYEkYiGXtl8nTaL/Hn+Ram82RnyLcRB5X8a28FdhfyOA0NvUeOtThSA1lo9 UcK2W7OR6vCGd+VByA== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from localhost.localdomain ([84.119.92.193]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MCol0-1sW9q012sC-008eIT; Fri, 16 Aug 2024 18:10:04 +0200 From: Bert Karwatzki To: "Liam R . Howlett" Cc: Bert Karwatzki , Suren Baghdasaryan , Vlastimil Babka , Lorenzo Stoakes , Matthew Wilcox , sidhartha.kumar@oracle.com, "Paul E . McKenney" , Jiri Olsa , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Kees Cook , Jeff Xu , "Liam R . Howlett" , linux-security-module@vger.kernel.org, Lorenzo Stoakes Subject: [PATCH v5.1 16/19] mm/mmap: Use vms accounted pages in mmap_region() Date: Fri, 16 Aug 2024 18:09:59 +0200 Message-ID: <20240816161000.2805-1-spasswolf@web.de> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Provags-ID: V03:K1:JGM+UFz9mnK+dk5IHlGfFo+pKmuhMOGrj45PRVD26wBmNPDV41E lK47NqDqgqByTLZbGjYLjaYF+HFeVqqH/jmbrgXKSwbT8tLJn2FGSghTWWgW9LWw56x/gGU NNerZhrasjoetmqnt1SJQaXnvW5iyvlwe0Xodks8udK7iOARL+PrfoXpZNb3pDsiOf4cB+z fRtx6+prJocut62CHYCcQ== UI-OutboundReport: notjunk:1;M01:P0:rRzJMlqlChs=;NhoU0uBcvC7hw644J1GISkdkjoa UIvvOWBiilyu7LnRug6QLpm/RgZ/hH+yerngyKY0lGQgc0y4nGLLIveat4s8TFN7BsQ8IKXfv 3xSVBIbpET/rHH2OC1V42Og6SN/2YMwkUswzPPSk8YJqbd+au4igW1UP2ygvcs4Fdp3Rw/0kv eAPCU0zQR8qvGZuB12l61QtciYO+bWnndY6TS2beIRbjHKS0N0GksyA+EgY6/iDYDIMrq+5SA 91xMW26mQjg2SjGhH9aXZ6/Xty8G6/woRKj1pzl6gtqsw5YBwQshxYrO6cfhZXo7DgIVdiYsN El4T8+HK61YDFDeyqyMohwjboLa/WOYcBV6Qu1opAiTdSiqp/3KyEbEu/pQOL2fvDPo5vNOTO 2Nj9TAfp0n5KmCjp/Jt6JjZ3ualApxvhz574v7J3aL60m1rwXQA5+7uScdpC9iGyc7yBrqCok 2uRSWVzPFiWb71knOWLJeL4OYEWALEZXP0fr749sRLq1KsAxPvJmFE1DCd2ZnPfimo9wZGn5Y MPkdNRtfQVvXm/b9t7BljR2JiC3yhZtuxQ3442Memu7bs2qgfQdOmHPsrAUKnUFtn7VFO+8Lt jCtt7NI7PjcRvWrSX+dIC7IUWtlQMKbzU7jr01p6w6gq/K+dSnbFWmGFbAck4yepCoRh6Yd4p Ouw1oIGQBNfYGqRkYdCWr4KrhOCHolJ9/eb2YsCe65AykVfO6382Rs4lQ+L4zxg/VMcN0clIZ qv6d9VValj2uGFeOds1AHlMXKY347kOABfeYVn0niwcP1QKnZMJGSfHhQwyQDXL0t/YSKhR0k V6pM+pZVK52KQ4GGjJq+uD3A== X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 15F3E12001F X-Stat-Signature: u7tadef17r87c4ajbnheps38ay17s1jb X-Rspam-User: X-HE-Tag: 1723824612-443697 X-HE-Meta: U2FsdGVkX1+a/s/TChEb9/OBWymSr5PjjuUjuMBPlIbNcsYO4NW2qZmhHQoAUfKoPU67qTJNTVtEPNLi+jDadDtRcNm8I1yfpNTsByQIS5UJi+RotfPC2lEGDqSZDGgBHufMd6izgS5nmRk8AGdm1QRqjaRFOkXBoU+s4SCEbP4M5/oBMr9RSkF5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+SYx8bshq9QkzvEjkzSAKFB+gzObkxXEMdZl8YLKzAPKy+LMFICqq6m32m5HLv0opKYO7P2kJYGim1PaEYzu0NVULDKaljoSDCIxSsNhlFVKf6uDcj636bFM/SHS9Tp319p3w== 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: List-Subscribe: List-Unsubscribe: Change from nr_pages variable to vms.nr_accounted for the charged pages calculation. This is necessary for a future patch. This also avoids checking security_vm_enough_memory_mm() if the amount of memory won't change. Signed-off-by: Liam R. Howlett Cc: Kees Cook Cc: linux-security-module@vger.kernel.org Reviewed-by: Kees Cook Reviewed-by: Lorenzo Stoakes Reviewed-by: Suren Baghdasaryan --- mm/mmap.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) -- 2.45.2 diff --git a/mm/mmap.c b/mm/mmap.c index 57fb1c82a852..d0942b1a62f0 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1416,9 +1416,10 @@ unsigned long mmap_region(struct file *file, unsigned long addr, */ if (accountable_mapping(file, vm_flags)) { charged = pglen; - charged -= nr_accounted; - if (security_vm_enough_memory_mm(mm, charged)) + charged -= vms.nr_accounted; + if (charged && security_vm_enough_memory_mm(mm, charged)) goto abort_munmap; + vms.nr_accounted = 0; vm_flags |= VM_ACCOUNT; } From patchwork Fri Aug 16 16:10:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bert Karwatzki X-Patchwork-Id: 13766576 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40CA8C3DA4A for ; Fri, 16 Aug 2024 16:10:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CD7F66B02D0; Fri, 16 Aug 2024 12:10:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C86726B02D2; Fri, 16 Aug 2024 12:10:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4FFD6B02D3; Fri, 16 Aug 2024 12:10:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 968006B02D0 for ; Fri, 16 Aug 2024 12:10:25 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4B1491C05C0 for ; Fri, 16 Aug 2024 16:10:25 +0000 (UTC) X-FDA: 82458596010.09.B06BEC1 Received: from mout.web.de (mout.web.de [212.227.17.12]) by imf15.hostedemail.com (Postfix) with ESMTP id 4FFC7A0025 for ; Fri, 16 Aug 2024 16:10:23 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b=MxouhEGH; spf=pass (imf15.hostedemail.com: domain of spasswolf@web.de designates 212.227.17.12 as permitted sender) smtp.mailfrom=spasswolf@web.de; dmarc=pass (policy=quarantine) header.from=web.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723824610; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=Jt4AnDxa2v6eX4uy5aSPJq3B4MFKZYmw5hpJKYIii18=; b=4qV0WNXxo0c0n/tsa3k6xG9r+Oxz6U9xcIFTkXf+Bt3I+eAWq9yOqNrG2/rChcDwDCefQ1 UEysqg/186U/96UwPSiZixymKpSOFznCYL8vKUnyVIg+HVgy2oDXaZNrzNU4sC/r8zXTCc dVvH9X69fRxsU/m0Xa7I95VU9PD+kHQ= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b=MxouhEGH; spf=pass (imf15.hostedemail.com: domain of spasswolf@web.de designates 212.227.17.12 as permitted sender) smtp.mailfrom=spasswolf@web.de; dmarc=pass (policy=quarantine) header.from=web.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723824610; a=rsa-sha256; cv=none; b=0LNTaWiWsgiY16z810BmWfote9b2qeWsow+jNT9zolwKRLYxbBlCLNFz1nOal/6rwXn+Vx 1Z/jKr+x3CuB2rNnh2Im4sjpcKewJojWl4EmydTQcCClgSQ+2j1samIIecpHgZuZ0dgd8K L3Cw/TEMQnJr97KbJpA+1Ic3DFhL1hw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1723824614; x=1724429414; i=spasswolf@web.de; bh=Jt4AnDxa2v6eX4uy5aSPJq3B4MFKZYmw5hpJKYIii18=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-ID: MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=MxouhEGH9zQ6cxHiav2M2R3Acaoqrud4CaCithT3Ew3Q0P6JhrAV4nE/lmfLaw4f wo04FFwJWEi4ag11Z3XZ5GfwHFY84o71uiXKBD0z4qja50wtU7tTm3gXEm1SnGa6a osxGYi+DVAauuml38waCGmM16pjlz6rCvegzE+mbOl7TgXkfnjxRh92qwkiyesUiH lD3u06kf9KnJPGsim1AQnD+DXoftqCeXv30rDTqkCLCDcOKRaEK0xns205vzTOYLS sNEtDXYTzRSJ+q+BTmEZvDM+yiRRxV5OAhQZHd05cLCiKilrAoRH2yPlfqXA0vY/B XSciziARwlzCA1AHaw== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from localhost.localdomain ([84.119.92.193]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MFJnX-1sPFwV22DR-000iNq; Fri, 16 Aug 2024 18:10:14 +0200 From: Bert Karwatzki To: "Liam R . Howlett" Cc: Bert Karwatzki , Suren Baghdasaryan , Vlastimil Babka , Lorenzo Stoakes , Matthew Wilcox , sidhartha.kumar@oracle.com, "Paul E . McKenney" , Jiri Olsa , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Kees Cook , Jeff Xu , "Liam R . Howlett" Subject: [PATCH v5.1 17/19] mm/mmap: Move can_modify_mm() check down the stack Date: Fri, 16 Aug 2024 18:10:09 +0200 Message-ID: <20240816161011.2825-1-spasswolf@web.de> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Provags-ID: V03:K1:/tV0rt25aiRoq8ZeGhSC0y+MFw2Llz1W+RtwQXZFZS8ngpSyUwR ZlYPrOs/5YpuIlkc4v9mISTRyh9FIgZuLuz4zRDC5WAuBRlyBNyaYiLwKoBViTLOXlSmOli GUqpe01cAngade7VPcZd1EqoFxPaUELhrATcqcXc1JuSljq+WgsANILkPZpf7xvhUl9c871 a5CLtKPrKTA2qruM7TDvg== UI-OutboundReport: notjunk:1;M01:P0:ZLsp+uOi0sI=;LqAopercSIFXhWS3BGil7aB/SAJ FaNIYjzqw7t+vJnAznefAIq5b6ZlKE7eas1V6s2sFhWNKFcZSvnsJYRT39Q5jNuHQSgST+1m+ sWtiL/Ck5APRGSetgJtG/qw2jQUqIL/Ro0bv5WcioNotj4xNpEZPPWaHJ8uYyImq8NLZCFm6F 9n0yX86IpIthnt1qb9povfIHQIdhofw7akBEgRtSwrX6GS3GnU9XpdgFH9ood+iYUciXSDn/Y ThGeGQhNMeOY5OG26fVZIpEWOEDlnA0Ec+WdMJSN9fR5aoXx8HAbrUKoBxdyXHY2ikYBft54s N+pJFpshxyTAHrQ+caSBGw010Um3nuYK9EjZ6vdOuNZs5e3H8DTAMuN8nv1c09nQIu9Ezyn6h L7bp8Gf8pDpBpErenkg/3Drj7a5kCzK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CtuLogxvitYCEMeYXbCKcjQyWcVVmSKeJubbebihejPrP0+cxQgwTtZU5VuvG/paPfPZ6v/nFAt3yUWJ66GgsSxAX/VBKQ2Wx/jzVZ1s4itPUxO6/ZzW25hayGMmCbTey3dVtk4qdoMT+sA2ueZE2B+WSDXNfrJkDlEjFy6WY2pZA5/bfAM0OcA7MUWU4VZgqGmqhYrlws+LnHnUJh/jN4VT1yRf94U2NaTlZ+aVSCg5VEKD2AyrjCYPOgQ9tQhF2vxxmRB0z+ZSNTXHpLMrs6kyxxhX+pkFSpSPi1kcnksddmYeatoZD3T52J8/3oCWLbZS0iqraFfzxakrHsp/oOG04moXbL4J9ZJLcI8R/EI5PFj5iBXqr9YRIvb7h9fqGmX21B2BQCn3OJZz5bWpPeetdwbdknC+hgIRc/Rtj9U6Nwq0JAohzUB5JftlUJ+cp9khp/Qx8HBB9 Iq/a7fPR wYhllYBQzm/DhJTzYUzP956XD+3+5W5/ZVM8eTl/lerTV+K0x9xmsrj5fbfVreZQn5sFeqx/GvGQjc0Mkb2EUs0fXJ+wh3PvWdQpjkytU/t0QIm75iXoTzNMGMJxhgrRUlDIIfbHQfWXYIZIvlmosClzYRGzDqxDWPiLpAUu1KwGduoTfTcPoeRel8qFcu8Lq3s7+Dlh1UTeCV5JrIMhZSZsXIwg6N1tBd+XVGmtcwavC+zbxW57KjHPz6tKt8BB/Mb+xA8rdUZVgHm+FcAlgI6J6w8EyzKRsKHyFexoJYP5VFtNdre8Fdumn9AY7q8UqWZ0IPyHGfkNsRBTKIZyeKAXt8ejwOmuUssLPRsq8eXHyLRCTVcPM9J1bEw== 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: List-Subscribe: List-Unsubscribe: Without an arch_unmap() call so high in the call stack, the check for mseal'ed vmas can be moved lower as well. This has the benefit of only actually checking if things are msealed when there is anything to check. That is, we know there is at least one vma that is in the way and needs to be checked. Only call the can_modify_mm() in do_vmi_align_munmap() and the MAP_FIXED case of mmap_region(). Signed-off-by: Liam R. Howlett Cc: Jeff Xu --- mm/mmap.c | 13 ++++--------- mm/vma.c | 11 ++++------- 2 files changed, 8 insertions(+), 16 deletions(-) -- 2.45.2 diff --git a/mm/mmap.c b/mm/mmap.c index d0942b1a62f0..ae74d0674b6d 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1387,13 +1387,15 @@ unsigned long mmap_region(struct file *file, unsigned long addr, if (!may_expand_vm(mm, vm_flags, pglen - nr_pages)) return -ENOMEM; - if (unlikely(!can_modify_mm(mm, addr, end))) - return -EPERM; /* Find the first overlapping VMA */ vma = vma_find(&vmi, end); init_vma_munmap(&vms, &vmi, vma, addr, end, uf, /* unlock = */ false); if (vma) { + /* Prevent unmapping a sealed VMA. */ + if (unlikely(!can_modify_mm(mm, addr, end))) + return -EPERM; + mt_init_flags(&mt_detach, vmi.mas.tree->ma_flags & MT_FLAGS_LOCK_MASK); mt_on_stack(mt_detach); mas_init(&mas_detach, &mt_detach, /* addr = */ 0); @@ -1766,13 +1768,6 @@ int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, { struct mm_struct *mm = vma->vm_mm; - /* - * Check if memory is sealed, prevent unmapping a sealed VMA. - * can_modify_mm assumes we have acquired the lock on MM. - */ - if (unlikely(!can_modify_mm(mm, start, end))) - return -EPERM; - return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, unlock); } diff --git a/mm/vma.c b/mm/vma.c index 86757443a7a2..f267b2552c8b 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -957,6 +957,10 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, struct vma_munmap_struct vms; int error; + /* Prevent unmapping a sealed VMA. */ + if (unlikely(!can_modify_mm(mm, start, end))) + return -EPERM; + init_vma_munmap(&vms, vmi, vma, start, end, uf, unlock); error = vms_gather_munmap_vmas(&vms, &mas_detach); if (error) @@ -1007,13 +1011,6 @@ int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, if (end == start) return -EINVAL; - /* - * Check if memory is sealed, prevent unmapping a sealed VMA. - * can_modify_mm assumes we have acquired the lock on MM. - */ - if (unlikely(!can_modify_mm(mm, start, end))) - return -EPERM; - /* Find the first overlapping VMA */ vma = vma_find(vmi, end); if (!vma) { From patchwork Fri Aug 16 16:10:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bert Karwatzki X-Patchwork-Id: 13766577 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64826C531DC for ; Fri, 16 Aug 2024 16:10:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E5E7F6B034A; Fri, 16 Aug 2024 12:10:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E0D906B0365; Fri, 16 Aug 2024 12:10:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C883E6B0367; Fri, 16 Aug 2024 12:10:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A6A776B034A for ; Fri, 16 Aug 2024 12:10:34 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 224B1140130 for ; Fri, 16 Aug 2024 16:10:34 +0000 (UTC) X-FDA: 82458596388.16.C589601 Received: from mout.web.de (mout.web.de [212.227.15.4]) by imf06.hostedemail.com (Postfix) with ESMTP id 3761C180017 for ; Fri, 16 Aug 2024 16:10:31 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b=qZjuni5L; dmarc=pass (policy=quarantine) header.from=web.de; spf=pass (imf06.hostedemail.com: domain of spasswolf@web.de designates 212.227.15.4 as permitted sender) smtp.mailfrom=spasswolf@web.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723824572; a=rsa-sha256; cv=none; b=swKL0hWeGiZYkhD0npXlQ+9RF/81G1qu+G84dY6QccaqVBG6qq3fz4ozzw43dyKrzcODnK reuY40Yn2sUGY7WwxudmhD4HsrD+LNtOzDpO2xqjV50YN4lvXFn308Okyrh2s8G/BID2La lL8TjNP7pXx41FCsrB1TV7nlx+Od3zo= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b=qZjuni5L; dmarc=pass (policy=quarantine) header.from=web.de; spf=pass (imf06.hostedemail.com: domain of spasswolf@web.de designates 212.227.15.4 as permitted sender) smtp.mailfrom=spasswolf@web.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723824572; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=AqH8ZahU0Ew/z25cFE4au3x3zvZRx/o0pPBf330Utj0=; b=Hw7Ua3YwKsNCVgP7wGQMgdNnebCRdGTuRHyjpON36g7nc9ZYF+NCJ6SLM9FustmfpxC/ax zjODkgS47IzAp7Vxw6QeDzUx9fvCJmHN+K+/KRmRnxlsJdE1ZolyGbqGwsbwwwRSFH+jxs /XZCFFkfy4F5pkKvtfYwLhAHOprPitw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1723824624; x=1724429424; i=spasswolf@web.de; bh=AqH8ZahU0Ew/z25cFE4au3x3zvZRx/o0pPBf330Utj0=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-ID: MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=qZjuni5LCs4oYuVCZusa6gqTmhASB26+km+807lc16rRQCXxjNmVnGenGI353vWZ YUaFhqmZxlGsp9npjX3QqYw/U1u8tPFj33qXVOaffgyanCdqmipDqeRa7x6iGXHKV O9IwLuk8F6hnFzqM4rwY2T8yoLwZsZpJJv3U3zRNf7Ly2l7R27497HXNRSy8vDu2s DjUrlK0mHhGSggk2WKCVba7PUKnW7JlFVqfQrEH66pZDzriYX95lJF4+w5mlSDEA7 g2jywPtKZRB24Jkap7rpYFsXZmbEvy1ifI8I4UYV7Kyt7i7lwxvM6sIfp4ORqH577 qKHlkvP2kbmKJRzddA== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from localhost.localdomain ([84.119.92.193]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1MKuKH-1sswPS2QWr-00Lcmp; Fri, 16 Aug 2024 18:10:23 +0200 From: Bert Karwatzki To: "Liam R . Howlett" Cc: Bert Karwatzki , Suren Baghdasaryan , Vlastimil Babka , Lorenzo Stoakes , Matthew Wilcox , sidhartha.kumar@oracle.com, "Paul E . McKenney" , Jiri Olsa , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Kees Cook , Jeff Xu , "Liam R . Howlett" Subject: [PATCH v5.1 18/19] ipc/shm, mm: Drop do_vma_munmap() Date: Fri, 16 Aug 2024 18:10:19 +0200 Message-ID: <20240816161020.2845-1-spasswolf@web.de> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Provags-ID: V03:K1:pG5/9OccDc0jmknnLiMC90N3TcGHFcL+ezavX79zPFVjOUla0KP 8ZdVhdU/iTbUmFt/RfpHAzG2yCA9a4ZiOpoksJz7cBSVm85f3uHKhKAIaRorQspNBFnzeGm bDzqstp3/21sQy5rU4iBk8fZQbv3hyPxbSvdW25SfKghV/VOT4cFF9eUMZPkEeieMrujo6j v/eoITaqZ+PtCqam4e0Rw== UI-OutboundReport: notjunk:1;M01:P0:eYlzR9waZA8=;X2ciPsgPNvjP5nI2TnpdJPGQpTl ZGbpSOwDaVm5k/sSYOr2VTEIHeTvCjtWQ2bvND3mBcq4ssJ4Be+fMKt1Z4bB4RiT2RM4RfsZV BuyBUSKFYD47YkkHMm6TqgHiwrQZAEVb8QQXnYl534UVjW4WC09jHMSJVKt6cgaCu6n/jJf9/ FWa0xEThkn8DjzmmMKgqR8yXouWYgP7O6YToci1pn/Zyimqf6JcN8vzVs8jOKPDFJdw0bUJjX 63JiWjWs778Ybdm5x1QJupMw0hYyxAzNbctfeH3rDNuuk/0qTAuF2NMBNnPfl21lF/0UUvUd6 xewZ8FhVtfAr/A6lUPAS4FAepYzao8RKRaNoYwjQ4eUx+sPj/DQRLr0Q0ZPVNPPKlvSkLWyGa o5gtJLqgMr4sSc3SArDITKaGf2N3fbEom82k4iK8q9ILREJVeijK3Mwgmsj8KOh/u2NXOTnWh 1G6gJg2RZJZhaINrW5tAn2dLjepplmhCGNdDeSI2XIaZCZZ1lO4JAydRwJ39x9GTME6mpPQtj TyFnRYi2Kwf52MKLCLpOWm9eJQ326jKjXQfCUZknw1+IP7meLbuTWxKzdZRd5p7kzfIhTDpkB /ZrViptLTW5MuilUD4Z1nNGGeVckAvz7nUiKAMN3WZmIrj3cQ7GeKz9uAzIPltCyTK0hBaTPW oIusx3li8L3Dl2ReYZdSAzjDx1t37H2jET/faXZ3xrH7gRe3DbcrC8xMAlQskJseQD6D4JoZI DlftLjMzMoH43BGtPbki3ST2Ip5yC281Uy16uPt2qM2kp6eYq7/dBTidKNMBsUxbJvRejE+5+ nGPF7cZj2glANQ3dcMJAzDHg== X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 3761C180017 X-Stat-Signature: t9g4d3pmxfdic5ehatbiwnai988ctc9k X-Rspam-User: X-HE-Tag: 1723824631-751125 X-HE-Meta: U2FsdGVkX18AYCXDH/q2nsUJFooFD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MonXdn5P1fDGOFToUy9J7l7CqFfkrm6F3+Ul2JBohPfh4xWgEMzpOD02SKll/PjnQl7kaxeI/hq7PQBYXnjWLjZvPyPwa9c66Qy3wBi2b9pdlhCYbKl9XR1XnN6mCGdiNjyr3Dt8fLGEi+wKUnxo= 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: List-Subscribe: List-Unsubscribe: The do_vma_munmap() wrapper existed for callers that didn't have a vma iterator and needed to check the vma mseal status prior to calling the underlying munmap(). All callers now use a vma iterator and since the mseal check of can_modify_mm() has been moved to do_vmi_align_munmap() and the vmas are aligned, this function can just be called instead. do_vmi_align_munmap() can no longer be static as ipc/shm is using it and it is exported via the mm.h header. Signed-off-by: Liam R. Howlett --- include/linux/mm.h | 6 +++--- ipc/shm.c | 8 ++++---- mm/mmap.c | 31 ++++--------------------------- 3 files changed, 11 insertions(+), 34 deletions(-) -- 2.45.2 diff --git a/include/linux/mm.h b/include/linux/mm.h index 653287396808..2dccdc7ba09a 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3292,14 +3292,14 @@ extern unsigned long do_mmap(struct file *file, unsigned long addr, extern int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf, bool unlock); +extern int do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, + struct mm_struct *mm, unsigned long start, + unsigned long end, struct list_head *uf, bool unlock); extern int do_munmap(struct mm_struct *, unsigned long, size_t, struct list_head *uf); extern int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior); #ifdef CONFIG_MMU -extern int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, - unsigned long start, unsigned long end, - struct list_head *uf, bool unlock); extern int __mm_populate(unsigned long addr, unsigned long len, int ignore_errors); static inline void mm_populate(unsigned long addr, unsigned long len) diff --git a/ipc/shm.c b/ipc/shm.c index 3e3071252dac..99564c870084 100644 --- a/ipc/shm.c +++ b/ipc/shm.c @@ -1778,8 +1778,8 @@ long ksys_shmdt(char __user *shmaddr) */ file = vma->vm_file; size = i_size_read(file_inode(vma->vm_file)); - do_vma_munmap(&vmi, vma, vma->vm_start, vma->vm_end, - NULL, false); + do_vmi_align_munmap(&vmi, vma, mm, vma->vm_start, + vma->vm_end, NULL, false); /* * We discovered the size of the shm segment, so * break out of here and fall through to the next @@ -1803,8 +1803,8 @@ long ksys_shmdt(char __user *shmaddr) if ((vma->vm_ops == &shm_vm_ops) && ((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) && (vma->vm_file == file)) { - do_vma_munmap(&vmi, vma, vma->vm_start, vma->vm_end, - NULL, false); + do_vmi_align_munmap(&vmi, vma, mm, vma->vm_start, + vma->vm_end, NULL, false); } vma = vma_next(&vmi); diff --git a/mm/mmap.c b/mm/mmap.c index ae74d0674b6d..d2f47cd66650 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -169,11 +169,12 @@ SYSCALL_DEFINE1(brk, unsigned long, brk) goto out; /* mapping intersects with an existing non-brk vma. */ /* * mm->brk must be protected by write mmap_lock. - * do_vma_munmap() will drop the lock on success, so update it - * before calling do_vma_munmap(). + * do_vmi_align_munmap() will drop the lock on success, so + * update it before calling do_vma_munmap(). */ mm->brk = brk; - if (do_vma_munmap(&vmi, brkvma, newbrk, oldbrk, &uf, true)) + if (do_vmi_align_munmap(&vmi, brkvma, mm, newbrk, oldbrk, &uf, + /* unlock = */ true)) goto out; goto success_unlocked; @@ -1747,30 +1748,6 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, return ret; } -/* - * do_vma_munmap() - Unmap a full or partial vma. - * @vmi: The vma iterator pointing at the vma - * @vma: The first vma to be munmapped - * @start: the start of the address to unmap - * @end: The end of the address to unmap - * @uf: The userfaultfd list_head - * @unlock: Drop the lock on success - * - * unmaps a VMA mapping when the vma iterator is already in position. - * Does not handle alignment. - * - * Return: 0 on success drops the lock of so directed, error on failure and will - * still hold the lock. - */ -int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, - unsigned long start, unsigned long end, struct list_head *uf, - bool unlock) -{ - struct mm_struct *mm = vma->vm_mm; - - return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, unlock); -} - /* * do_brk_flags() - Increase the brk vma if the flags match. * @vmi: The vma iterator From patchwork Fri Aug 16 16:10:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bert Karwatzki X-Patchwork-Id: 13766578 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0C27AC531DC for ; Fri, 16 Aug 2024 16:10:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92E2E8D008C; Fri, 16 Aug 2024 12:10:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8DE1A8D007E; Fri, 16 Aug 2024 12:10:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7CC478D008C; Fri, 16 Aug 2024 12:10:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 58E468D007E for ; Fri, 16 Aug 2024 12:10:44 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0A2E1A0125 for ; Fri, 16 Aug 2024 16:10:44 +0000 (UTC) X-FDA: 82458596808.19.B4AFA1B Received: from mout.web.de (mout.web.de [217.72.192.78]) by imf14.hostedemail.com (Postfix) with ESMTP id E4CCB10000D for ; Fri, 16 Aug 2024 16:10:41 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b="Hm/VZ4nV"; spf=pass (imf14.hostedemail.com: domain of spasswolf@web.de designates 217.72.192.78 as permitted sender) smtp.mailfrom=spasswolf@web.de; dmarc=pass (policy=quarantine) header.from=web.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723824628; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=tY1u/l18ICxFndL+rMYHYdlbuL5/Ji75gvQvXubIHqs=; b=XcVX5cGgnA4+IurPgYzkbY4K/jSlADuakgkkhEldi6GeW5+feLuHAax4EsnXQ/lespuruD T0fr+bv7oqosyup7c1zzZ9rFTH+bsn7jxlOfo+5LzQ9ryx6Y8MkLg0cQbUgY9Nt3CHaTm6 cu+PS5dr+LcFZCf8Xa4jfnsghXlhmuM= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b="Hm/VZ4nV"; spf=pass (imf14.hostedemail.com: domain of spasswolf@web.de designates 217.72.192.78 as permitted sender) smtp.mailfrom=spasswolf@web.de; dmarc=pass (policy=quarantine) header.from=web.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723824628; a=rsa-sha256; cv=none; b=h0a9Vzj9G8qnzj7ewLKnqwahJTjEzFCp9Se+/zmIuQNuxFekDgIVhUQaZjoXbYLjOah4Um SRPtuzh5RmyHeP8sPUXDIildN7Tcmo8jhvDlsEAfbHccIqEDA0qF5lnUpWLtKTR0T/tV95 B3QFLumZDXkK2xNPbBs3G4BaGWgwoUQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1723824634; x=1724429434; i=spasswolf@web.de; bh=tY1u/l18ICxFndL+rMYHYdlbuL5/Ji75gvQvXubIHqs=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-ID: MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=Hm/VZ4nVyBdr5dDUQNibfqMELTiimJ+x3CK5jWY5wydb/FCVtPBhUdDFKjewTK0L utd1GQ4xBY9YI1hBOuxPV74EEEsS0qfuZg8sZsO8ZpYYtWBPBlSsL4rMxZDzPZSYr R6NZSV55IFL9bNNRUETyp+csYo/R+DUgmuPLp+4XAakp0CRvpA0WvWSZ15soIkBow 7IWy2epJ6itNjOOAI8l9d7dpPsX0fh32xjHfUwitePRCqDJOXLe47Pvym9OJN2r+Y AUBC4yyBw6kx3VLT1vVvoEGqCCNOndOf76c3N4hTWGy46fFrDfvYrGdwzANuNhoRu xeTHQuWvh9Y7fSx0jw== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from localhost.localdomain ([84.119.92.193]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MjgX3-1ru43N0hrf-00dbwl; Fri, 16 Aug 2024 18:10:34 +0200 From: Bert Karwatzki To: "Liam R . Howlett" Cc: Bert Karwatzki , Suren Baghdasaryan , Vlastimil Babka , Lorenzo Stoakes , Matthew Wilcox , sidhartha.kumar@oracle.com, "Paul E . McKenney" , Jiri Olsa , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Kees Cook , Jeff Xu , "Liam R . Howlett" , Lorenzo Stoakes Subject: [PATCH v5.1 19/19] mm/mmap: Move may_expand_vm() check in mmap_region() Date: Fri, 16 Aug 2024 18:10:27 +0200 Message-ID: <20240816161029.2865-1-spasswolf@web.de> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Provags-ID: V03:K1:CpBP6kSbrB8sL8flKl+ECNfHj/Uc35TmetZAHX0QnWtel46XMNb ZiZBIbboeJJ6tVLYcRzceYO1MDLzuDd23AZvXklcX1N5/dSjrmMN5hc8CUFPzWDqomsO6Zj efZvSyAq2qKR3ajjesfQhlBS1V7I1Zo2tl3gcC0mMHjTj4rdN+sSq+wKGpyMPyFIomNU2X5 SkFEdwpRClee9hPkap6sQ== UI-OutboundReport: notjunk:1;M01:P0:TD/RjrwwdTk=;IQOlooLy+sbkfuRILnJuuPi8E05 eTOFKmGrhsyKxUGmTKfbVqPOTXKxrVCFgKtxI9Ge35vs1fZaRTnG2vkZqgesytNioXiXSeaE0 QYgatLk2+Y1P18xgsrv6jvAWBcxTIogQn/00Tu4bXu3m+E39ZSE7jnp/UEmARv/W26cfn/5FP BKWdOQr8bNvp85EcYFMpEAtYyfbPsQmkFqrnV4UNWg5tK1TTxyVMx2LeKleR9oNk011/34Hc2 jW3UOFZiXz97xpsHUoA09NrJPiK+cvSHbRPFOjLq2wnVM8rysqI4AFlkEA8JlaE4C94quRVlZ ERLXkAGDDBwZx2CY0Tnczw57wBX9eOb2qneQGjhUcQkzsUO2abmTwcqiUBvJluIShOCN98rEb qD1q0EGDRWCciyL7HNr/Vaptu7hTsopiXiSHMqxW4pf+LxqDYgFfri+mVltiQsCkdDw4QB4jz C8jSXFeFsYhBR8HKHaaFBfNQlZApxM+x01QXwvPQWafgZc67ePXg4JsiD3TR0spEkmIHZ2dAe IgLhtupZJXjT3EHG9UsbJ6f73QYd9/46PMukJXTgzPkYKZc2JnEMChXZJnrhAYsxq26p+Fwsi txnXkeneShJYs+QayAhRAhdgfOCfJXqLqw18w368jRPdrtOvIpKtDNyz0m46RRC6m5nJlEwR8 794pRsOvVEYGW+PAcQxu0sEHjdN0hNbQmZ+OrMSp4eUaUFEtCWh/KzEK4iIcJVTwfXZHPUzyj TZm9s7eQbXgP1E8bpkmddo0KD4NN8BUkq+/HB5Vqev6YuwJb36knkUDhAuNxveFacQo5YT/Zd eWjaBNunMLJMmts+0dSNhg3Q== X-Rspam-User: X-Stat-Signature: cej3xcwrpzupq8mx5o11mo316tem839r X-Rspamd-Queue-Id: E4CCB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oZvy/izl3TnUb36iwh7mMEmNQJNpBLvun5dFFLbMZAfCtitUqePILq+XIFc6KvP9JgvQuusrAYYxEjEAPlOLDBe5O2Pww+/lttuaTRlY6eqQk/kpY97rPuUiUgNbFQxfZDTmPpCdnSR+wfc8xj4Jn4THyKADPeBczv6J3f4SsrFqII1gKN03nN3sBnnVcNSm+RNpKupjaSyH8ljHTRvqB2z9VuoLYvnNkBmxgZqF1t3OWpPRmV72/3JM5nnVJ1LMbbL+srEldidsMWROFq9gNw6wyUJFDGm6q4AwyaH 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: List-Subscribe: List-Unsubscribe: The may_expand_vm() check requires the count of the pages within the munmap range. Since this is needed for accounting and obtained later, the reodering of may_expand_vm() to later in the call stack, after the vma munmap struct (vms) is initialised and the gather stage is potentially run, will allow for a single loop over the vmas. The gather sage does not commit any work and so everything can be undone in the case of a failure. The MAP_FIXED page count is available after the vms_gather_munmap_vmas() call, so use it instead of looping over the vmas twice. Signed-off-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes --- mm/mmap.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) -- 2.45.2 diff --git a/mm/mmap.c b/mm/mmap.c index d2f47cd66650..e0c321b7c642 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1376,17 +1376,6 @@ unsigned long mmap_region(struct file *file, unsigned long addr, pgoff_t vm_pgoff; int error = -ENOMEM; VMA_ITERATOR(vmi, mm, addr); - unsigned long nr_pages, nr_accounted; - - nr_pages = count_vma_pages_range(mm, addr, end, &nr_accounted); - - /* - * Check against address space limit. - * MAP_FIXED may remove pages of mappings that intersects with requested - * mapping. Account for the pages it would unmap. - */ - if (!may_expand_vm(mm, vm_flags, pglen - nr_pages)) - return -ENOMEM; /* Find the first overlapping VMA */ @@ -1414,6 +1403,10 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma_iter_next_range(&vmi); } + /* Check against address space limit. */ + if (!may_expand_vm(mm, vm_flags, pglen - vms.nr_pages)) + goto abort_munmap; + /* * Private writable mapping: check memory availability */