From patchwork Sat Aug 17 00:18:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13766877 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 2CC2AC52D7D for ; Sat, 17 Aug 2024 00:18:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 409566B03CF; Fri, 16 Aug 2024 20:18:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3B9A46B03D0; Fri, 16 Aug 2024 20:18:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2348A6B03D2; Fri, 16 Aug 2024 20:18:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id F326F6B03CF for ; Fri, 16 Aug 2024 20:18:43 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A63401403E0 for ; Sat, 17 Aug 2024 00:18:43 +0000 (UTC) X-FDA: 82459826526.09.8025A3A Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) by imf06.hostedemail.com (Postfix) with ESMTP id C135D18000F for ; Sat, 17 Aug 2024 00:18:41 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=O5CIFoKj; spf=pass (imf06.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.208.176 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723853848; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hQdfB2TJjcHTWcrkMiwvMjty3cwhCh0Z/hQjDmUKSz0=; b=o1RAd8y4647sqyS3C3ajY1kyTq+4UOZolcsAs/+bgaqkolCJhjNWqpm8SuQTXfANVGTejg sQioXYujP40A8swypu+kXy/Kg2Uw3sf+htqZOjTjqaWtHFa6Ad0fD51dHwpXTmhSCRueNe nzXEssb57Pkiskcg4uhyrovyW42spqY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723853848; a=rsa-sha256; cv=none; b=TmpdKH/FyQ8zxvcce4eSYHg89QPmIx3ymulBGD20gzMD3aajOL7uwP2h3QCq6IQaCY20mT 5gEZLjOjdustlQaImhzG89hexnqWrJzGiui1WRcriygmJkyPQx3JUfzQtzQsZYfrYciZQO Ia0Bkflwd6XJKFJQuZ9GkQkvCS8Phu4= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=O5CIFoKj; spf=pass (imf06.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.208.176 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-lj1-f176.google.com with SMTP id 38308e7fff4ca-2f15dd0b489so35015591fa.3 for ; Fri, 16 Aug 2024 17:18:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723853920; x=1724458720; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=hQdfB2TJjcHTWcrkMiwvMjty3cwhCh0Z/hQjDmUKSz0=; b=O5CIFoKjHctDAGZXHb3c9zhiC4A0t4p++AWWCEmVVhSQ8q3XdVM8slTZMJnEfRtwsN ddCidFR/C86etZkizeYoKesgw6fkGrDLToZZjt3mfSCNOoThnVfXlK0EBOvPCECcfLL9 hcJiruYq2cF9G176zVULflHJFxBb8Pvc5mVQEFdbFTerOccngyKOd9Q4b/qXF4lYYLF/ olOjJr9sgjvphTB4QOgT9wYnBhiNPbf0re0Racrqh1w3hkGhvNCFG3T5V0Vr+XVTkCeQ kurhRmp1rgWiCyR9dmOWtYdZ4cfTQmAoHQlfHC4weMXJQBlBFdF7VzMNJxpsuZKOpt1o lHfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723853920; x=1724458720; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hQdfB2TJjcHTWcrkMiwvMjty3cwhCh0Z/hQjDmUKSz0=; b=rutJcY+VTjk4AEjcWykd4L4g3BA7CM3GAU11SJM2D0s0VWlCHHMXZ+xf7jVPOj1gL9 itqkEtp81X2S1DktWfO7f5gviyXIjBsPNdnR4838J3/N9tZ8R3pYe5bnKVgDhl2RRZS6 XH0AiA41iATiq9MHW/adhxgLOQvGHZdFLATk+p3cCC9XDYJJnqyemcgLz1MZvVE3VNIi PZIzkvxv08TjUA7ywi5NBOnhTVnRGy+CGbVwviXVk0LzaA2WT6RRnCKS19n6LSOtnsSp T/KXB8lGN1jOO8jJ61TrFueL6UcudZFYGCEytbKi0Jq/RvX8n0JciIG7djsEVKH3zadT I8OQ== X-Gm-Message-State: AOJu0Yw9HlhHZgbsTPgAR2efRz4ha/16G4Ab5rJ0hpCq3TU0Lqp2qtfB JM4GOx1irujuk37lrz9wnTa3M67PBi8z/5FR79yuGopnKr/QbVPA X-Google-Smtp-Source: AGHT+IFZ9b78tVtcZPQGq9NYpF0/YwksiknLfMYODd0Q+Kd8EWcZCWbrFfO0fyGveiS9SrzB4Z6g/g== X-Received: by 2002:a2e:d01:0:b0:2f1:9248:c325 with SMTP id 38308e7fff4ca-2f3be5f8cecmr29055721fa.31.1723853919102; Fri, 16 Aug 2024 17:18:39 -0700 (PDT) Received: from [192.168.1.91] ([2001:818:e92f:6400:96b:aa92:afc0:2d3d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429d781047asm104842055e9.0.2024.08.16.17.18.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 17:18:38 -0700 (PDT) From: Pedro Falcato Date: Sat, 17 Aug 2024 01:18:29 +0100 Subject: [PATCH v3 2/7] mm/munmap: Replace can_modify_mm with can_modify_vma MIME-Version: 1.0 Message-Id: <20240817-mseal-depessimize-v3-2-d8d2e037df30@gmail.com> References: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> In-Reply-To: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> To: Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Shuah Khan Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, jeffxu@chromium.org, oliver.sang@intel.com, torvalds@linux-foundation.org, Michael Ellerman , Kees Cook , Pedro Falcato X-Mailer: b4 0.14.1 X-Rspamd-Queue-Id: C135D18000F X-Stat-Signature: 3bsauy931zf8pm1mmziyhtekeib3kuw9 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1723853921-501071 X-HE-Meta: U2FsdGVkX1/Sr3DfZbpaIKe5dWUrBwVgSITDqA28+xY6oq+VGWt6D1xsr1nLcsC9r+hlJXFC3rrbcfDEqbrDBS7qCGackI8DV8C/l+EnmfW594mbioYC/il5dlechg4voBXrRyMm4pBwEp3Bs0EKOOU/EK2u07TC732GFd4ujgTPxRYxawROvHwlykoEqPrhLwOpKvECs/oxoj4HcrYWWWvYPlqF0o506vUuIw2Xl7oEYP9IYsBMScvIY8mBr1okxFDrU5CGQrhnA9Xcub2DwzeGKS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wg/yBuL5Zbn/L1eEXunogbmMFMcjWitaGUAlnrkSrDPYLTjXGaUPPLWbgmTSrcFRompYci4LdZlOe19JsviJv9ZSOAzItoBGUPytCq2TPJfR5IoQ5pZcZUn3aVR4Zq5wH3bK74RUNtL5s4To/8YcrUceCGARDM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000336, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: We were doing an extra mmap tree traversal just to check if the entire range is modifiable. This can be done when we iterate through the VMAs instead. Signed-off-by: Pedro Falcato Reviewed-by: Liam R. Howlett --- mm/mmap.c | 11 +---------- mm/vma.c | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 3af256bacef3..30ae4cb5cec9 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1740,16 +1740,7 @@ 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; - - /* - * 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); + return do_vmi_align_munmap(vmi, vma, vma->vm_mm, start, end, uf, unlock); } /* diff --git a/mm/vma.c b/mm/vma.c index 84965f2cd580..5850f7c0949b 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -712,6 +712,12 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) goto map_count_exceeded; + /* Don't bother splitting the VMA if we can't unmap it anyway */ + if (!can_modify_vma(vma)) { + error = -EPERM; + goto start_split_failed; + } + error = __split_vma(vmi, vma, start, 1); if (error) goto start_split_failed; @@ -723,6 +729,11 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, */ next = vma; do { + if (!can_modify_vma(next)) { + error = -EPERM; + goto modify_vma_failed; + } + /* Does it split the end? */ if (next->vm_end > end) { error = __split_vma(vmi, next, end, 0); @@ -815,6 +826,7 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, __mt_destroy(&mt_detach); return 0; +modify_vma_failed: clear_tree_failed: userfaultfd_error: munmap_gather_failed: @@ -860,13 +872,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) {