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+ sWti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gHm+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) {