From patchwork Sat Aug 17 00:18:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13766879 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 A630FC52D7D for ; Sat, 17 Aug 2024 00:18:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8281D6B03D3; Fri, 16 Aug 2024 20:18:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D8006B03D4; Fri, 16 Aug 2024 20:18:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 651A86B03D5; Fri, 16 Aug 2024 20:18:49 -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 461506B03D3 for ; Fri, 16 Aug 2024 20:18:49 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id F1BA61C1CBC for ; Sat, 17 Aug 2024 00:18:48 +0000 (UTC) X-FDA: 82459826736.10.1D4F371 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) by imf17.hostedemail.com (Postfix) with ESMTP id 0BEEC4001C for ; Sat, 17 Aug 2024 00:18:46 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KwbRux3U; spf=pass (imf17.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.221.42 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=1723853913; 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=LrPcZ5dxXI2S7kFYpXooZU8/pyUJQvH2vRgq9oEXpMQ=; b=vH3cy1ywtYF0a9neQNSXiYDljsx9e14FukVuIZFdSqyeXEhzJ7tsXFHNb47eyN+6Y/C31j c573qxY7QxsJm1LbK90ZSVCuE/rlCaZPYGIaUC/wse09+C+/8QMMoWPonTBfr//MJpnugc kSavFR8oXVwJ5nvkZ4MtDKPQEkhmo6o= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KwbRux3U; spf=pass (imf17.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.221.42 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723853913; a=rsa-sha256; cv=none; b=NQC/cyhs7AU7azKk6dxqNzMTdFdMkDe/KY6rHyJ5up/YyagTU1N0bzi7HjcUlQOU8xAW2u E5j3NBQlawKXzBChIfLXVXD3ZoQcTBAo8b21DReaI0AgBJdYoTqD5ReN5hoJkYEbIXsnxh rfXMpYOxm/BpFSAGZOl8mgQaiHj3GLg= Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-37186c2278bso1402491f8f.1 for ; Fri, 16 Aug 2024 17:18:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723853925; x=1724458725; 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=LrPcZ5dxXI2S7kFYpXooZU8/pyUJQvH2vRgq9oEXpMQ=; b=KwbRux3UZWxHF/wraj3B9zgit+O62cS+DiOf+3Tdg3nKYJ2dtiUkh44KjB6yDk0Wom YqljrilxHBTXA4QCGssLMKGPzCR/SBKs6qIsvKPsTZF/wsA1kEiZ2xZ5k3R0YfjsRrTV 9WMiAmA0T5zM0ipAbnf4wHlvI6m7YNPfd+aBpGAgKCgOFegBqOOFq6m0/CAItNKdCKQb jFQpTmWGSwEl8it61dg5q2TXQwwFvyrlsvW5zs5KcXAYfoFTNe2rdXlHIX/D3eyhS6aq Q81Fyp8D6gWZu504AfO+3lxfNVGjFztY7neucVH33aYA4PQ7twZGQNRmIrjDxFkVAkQk F0qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723853925; x=1724458725; 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=LrPcZ5dxXI2S7kFYpXooZU8/pyUJQvH2vRgq9oEXpMQ=; b=G815ACOQ04hNPRBDmYCwrXtF1bQnXNHSSjt3/8Ck/k9FfzkukRp64EmN5Ho4bjx8+p nO8T/LHY9+g6NM7k+a75FOqDpyh43Ctwvrya/ON1vRwvounLwYuT9rTXxUSjZMauKVZT ywGGN6kTwsvGv41TXt/Jy41h1YF1wsWKiH3NwZtIN7mLf6TBgFRYJOuASA0VMcb0PDLk riF6TS0MIW+79AOSUOB0JYva+ErqIKg2L8BiYunYihslyoagacMt6wv5QJy+/F2TpSU4 2p6e7TP7ErLXBxOLrwnEMSXuffbtYCGj0pfiD6Xg3m1LSyEaAhxkBdWA+Zqdf3zuJnVi oosA== X-Gm-Message-State: AOJu0Yyd+OmHfEyJ/ft7RvRvsO/6e2DIwcD7MvhOfWP4x/agQgXvl3Sr f1ilAvwg2lJqAFkQXCgdXahf7j0ep6zBue1QoeWCCMNUtHZXcz6Z X-Google-Smtp-Source: AGHT+IHob2iXeWVcLHrIvT5oHF+c90MvHS34UpZmw02JaWHR+F0oJhRdWeWAe0XuH/19P/AChePKAQ== X-Received: by 2002:a05:6000:2a8:b0:368:377a:e8bb with SMTP id ffacd0b85a97d-37186dda493mr7610866f8f.28.1723853924555; Fri, 16 Aug 2024 17:18:44 -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.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 17:18:44 -0700 (PDT) From: Pedro Falcato Date: Sat, 17 Aug 2024 01:18:32 +0100 Subject: [PATCH v3 5/7] mseal: Replace can_modify_mm_madv with a vma variant MIME-Version: 1.0 Message-Id: <20240817-mseal-depessimize-v3-5-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-Rspam-User: X-Stat-Signature: wxxme75bywh1bjenazqb89ie11wpt8rd X-Rspamd-Queue-Id: 0BEEC4001C X-Rspamd-Server: rspam11 X-HE-Tag: 1723853926-775876 X-HE-Meta: U2FsdGVkX1+oN/e8ek+gCMFydjc5g5R/P6eD4WBbhLsBKfJi1tiDDcy6FA67Me/FeD8U95vkGHRSJFjbsqsA7vtIJV8QQwDBWcSkJtCbBIE/Nl/wLL3y5QlRLmhzCng4gvQjbWOPyr6tEJ919JyM4NkFAv1D8ZBiSciOQTHS3FGDJ3ywEv8hKj7mqctZ4gP0KEnL1wDdlMF6mGAv2Jsa8GH6Tp46wlApXN3qXMYQmTIKy6Vmyp+2IjM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IN5bPGTzX6ZDShyEDKSn72NS4gXot9E3CIRLMwgm9ppLDiYfbLaEqAi4dWvnGK5kSbpLuGKezQCffP31ZZdISkZ7+yhIDdqj3bveQyQ+RuLGA7HBtNIEmvCtf5MCK4Muy7W1k/aLBrPe8Lv7TAgS7e2xJUxzxQRUGuT1rjHGm48x3UHKI3BA= 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: Replace can_modify_mm_madv() with a single vma variant, and associated checks in madvise. While we're at it, also invert the order of checks in: if (unlikely(is_ro_anon(vma) && !can_modify_vma(vma)) Checking if we can modify the vma itself (through vm_flags) is certainly cheaper than is_ro_anon() due to arch_vma_access_permitted() looking at e.g pkeys registers (with extra branches) in some architectures. This patch allows for partial madvise success when finding a sealed VMA, which historically has been allowed in Linux. Signed-off-by: Pedro Falcato Reviewed-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes --- mm/internal.h | 2 -- mm/madvise.c | 13 +++---------- mm/mseal.c | 17 ++++------------- mm/vma.h | 7 +++++++ 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index ca422aede342..1db320650539 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1363,8 +1363,6 @@ static inline int can_do_mseal(unsigned long flags) bool can_modify_mm(struct mm_struct *mm, unsigned long start, unsigned long end); -bool can_modify_mm_madv(struct mm_struct *mm, unsigned long start, - unsigned long end, int behavior); #else static inline int can_do_mseal(unsigned long flags) { diff --git a/mm/madvise.c b/mm/madvise.c index 89089d84f8df..4e64770be16c 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1031,6 +1031,9 @@ static int madvise_vma_behavior(struct vm_area_struct *vma, struct anon_vma_name *anon_name; unsigned long new_flags = vma->vm_flags; + if (unlikely(!can_modify_vma_madv(vma, behavior))) + return -EPERM; + switch (behavior) { case MADV_REMOVE: return madvise_remove(vma, prev, start, end); @@ -1448,15 +1451,6 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh start = untagged_addr_remote(mm, start); end = start + len; - /* - * Check if the address range is sealed for do_madvise(). - * can_modify_mm_madv assumes we have acquired the lock on MM. - */ - if (unlikely(!can_modify_mm_madv(mm, start, end, behavior))) { - error = -EPERM; - goto out; - } - blk_start_plug(&plug); switch (behavior) { case MADV_POPULATE_READ: @@ -1470,7 +1464,6 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh } blk_finish_plug(&plug); -out: if (write) mmap_write_unlock(mm); else diff --git a/mm/mseal.c b/mm/mseal.c index 2170e2139ca0..fdd1666344fa 100644 --- a/mm/mseal.c +++ b/mm/mseal.c @@ -75,24 +75,15 @@ bool can_modify_mm(struct mm_struct *mm, unsigned long start, unsigned long end) } /* - * Check if the vmas of a memory range are allowed to be modified by madvise. - * the memory ranger can have a gap (unallocated memory). - * return true, if it is allowed. + * Check if a vma is allowed to be modified by madvise. */ -bool can_modify_mm_madv(struct mm_struct *mm, unsigned long start, unsigned long end, - int behavior) +bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior) { - struct vm_area_struct *vma; - - VMA_ITERATOR(vmi, mm, start); - if (!is_madv_discard(behavior)) return true; - /* going through each vma to check. */ - for_each_vma_range(vmi, vma, end) - if (unlikely(is_ro_anon(vma) && !can_modify_vma(vma))) - return false; + if (unlikely(!can_modify_vma(vma) && is_ro_anon(vma))) + return false; /* Allow by default. */ return true; diff --git a/mm/vma.h b/mm/vma.h index e979015cc7fc..da31d0f62157 100644 --- a/mm/vma.h +++ b/mm/vma.h @@ -380,6 +380,8 @@ static inline bool can_modify_vma(struct vm_area_struct *vma) return true; } +bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior); + #else static inline bool can_modify_vma(struct vm_area_struct *vma) @@ -387,6 +389,11 @@ static inline bool can_modify_vma(struct vm_area_struct *vma) return true; } +static inline bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior) +{ + return true; +} + #endif #endif /* __MM_VMA_H */