From patchwork Sat Aug 17 00:18:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13766878 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 CE6E2C3DA4A for ; Sat, 17 Aug 2024 00:18:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 801E96B0159; Fri, 16 Aug 2024 20:18:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B0C16B0161; Fri, 16 Aug 2024 20:18:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 602D56B0169; Fri, 16 Aug 2024 20:18:47 -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 3F3606B0159 for ; Fri, 16 Aug 2024 20:18:47 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E8DD7803EB for ; Sat, 17 Aug 2024 00:18:46 +0000 (UTC) X-FDA: 82459826652.25.1E4985F Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) by imf24.hostedemail.com (Postfix) with ESMTP id 16F2D180012 for ; Sat, 17 Aug 2024 00:18:44 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HR0FvjGE; spf=pass (imf24.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.44 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=1723853911; 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=bAmgLDl1px7jqd76Q1p746SydraVXJ6Ud6jad/RgDJQ=; b=cdYvHIeBxzNhUIv1A2a/0R+biFx+Uy1/VLp/Zz8BE0cNqPW5mxCAyZt9GJPOZYHpB5pLDj zZ8EQG5OnwRk8JAfBLYrW6Wwm+TT4cBPEXpvXK4bcLM5wX6n1/gHr2HfxQm7VPP9YmXzot ayXxkMRsx0hRWXOmF7bNsHVmdg0Xbuw= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HR0FvjGE; spf=pass (imf24.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.44 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=1723853911; a=rsa-sha256; cv=none; b=WUPdCBatfny5zPi13lMvwGoGaVXIRgYrNyvXzP7fQd7MZnqjZcgINZHXNnlJCulSU+I4F3 5H31NAvp50oPmHjErIlrqoChnMxgD8v40la9cENPzB/4WucSYfLcIKX4Yuk97EwVOl5aas OE1APKloZK1zm3HOgmO4S6bW1rbfhDU= Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4281ca54fd3so18133355e9.2 for ; Fri, 16 Aug 2024 17:18:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723853924; x=1724458724; 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=bAmgLDl1px7jqd76Q1p746SydraVXJ6Ud6jad/RgDJQ=; b=HR0FvjGEI2aHxF1IOQbQcdXo6MRd39tyejOQUrFjyvejtxbc4QmStvZuE8njghiVqz BnuA7ASQKVkz+yW8pnUqnDzUuTm1Su5+ndAXeRQI22jcDcJKG571f4XzG1YOuHaxdEXB J8y6Y2UHqhn2i0SkMvLB4jo5uOpoimanpgtG07Ow4W+IUUPdHsbUWt3YerKqWwbricPO RjxchTBnpksf+yK8bo7qNMd6UdJKckFPwhKHi8u7JvoRiD0P4xeQn6pS2d4KRV0rlL4e 2Yd7VcqGmMY1vuoXxEfRg8N+d3m4fow8g/Pc5NNL5oZ3ivAPlRiIcQFSNBQecrRoWAea AmwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723853924; x=1724458724; 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=bAmgLDl1px7jqd76Q1p746SydraVXJ6Ud6jad/RgDJQ=; b=KHaFtPEXhkyx9cUDd91/JkreqdOIMaPBDjAccVGgrEUe/UDhRTharOT2kPqW9gDrZU 0kgAJ+XoXhlmq4+c2twuxdOr3RxQqQT6z4NejuAAe43prwNbBhqqDsnG6ClmujQcXRde FwsEDRXp/gQPtU+DD98SoTy3iaIbcscqruBy8/kyaXCs9Bc56WDcgW1qtSlbJheX6zWZ xmCab8bfLiQT95sqqs5vcvG0UE3ttRc+dUtoZ3c+TDryD/uQerA/jaM6pArYhKiqVprd NIQwRBiPCHezCsmD1jiCIZhzbKXC5rpSXZAoRHwLo6zCbUg18igOC+ByKprPfLmqhAiM Um8w== X-Gm-Message-State: AOJu0YwN4xD4/y5VOwYGxgFgze3hLNQd5DIjZF5kIxtKmkAjUYyXHrRu 84//iLNhpWJ4HGUJQiWNTT+ZykTcupZT/246fkPzUG3vnKlI8iyy X-Google-Smtp-Source: AGHT+IGM0TLfyEre+jfwvYu3EzVkhOnPhcS/imjEho/f+Tms2EZbTyomcs1S5bb2scgoXXDqQlsfmg== X-Received: by 2002:a05:600c:4fc8:b0:426:593c:9361 with SMTP id 5b1f17b1804b1-429ed7d1f39mr29141995e9.26.1723853923095; Fri, 16 Aug 2024 17:18:43 -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.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 17:18:42 -0700 (PDT) From: Pedro Falcato Date: Sat, 17 Aug 2024 01:18:31 +0100 Subject: [PATCH v3 4/7] mm/mremap: Replace can_modify_mm with can_modify_vma MIME-Version: 1.0 Message-Id: <20240817-mseal-depessimize-v3-4-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: wg14d9j1eoy6816jch6obmezbrc61cem X-Rspamd-Queue-Id: 16F2D180012 X-Rspamd-Server: rspam11 X-HE-Tag: 1723853924-833835 X-HE-Meta: U2FsdGVkX1+WEQVaEd1HCadKWGN+KDYuYUkxIdpQM4p0AdF2tdBEwh9nF2aLuSfd8TpZCAVygUexyTJyI0MPiR3pCZeXMaM6HfcR1KOfIhjSoQSCYG1vY8opqv7PnRNsR7Z/IhGuFkQfpDq/dMXfivVTjtM+0+2IuFekFEdCjm6Xcr7VJ8Xa4OETIXvGrS61LtDijhz7aGi2YKp+vMi5e6gaThWlHppLnfJyE2OyrI+prCCEdpbj8xOtcMug63McKaLz1DjuEx5cgiwBu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qOlK+6jt0KOtyvbQtDynf3LbaczecF8a9fm4wdXiLm0jSeydLNcARuJRa+1fOjX6jlBC5bVP1wb1GOh67+fqF2czMo5RexzwbskquOC5vxGerL+ZtKFex5Zdr4AzpxAdC6q97uh6YIHnGpbEfnYJc9MlMoimxAoXRfZ/znYA= 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: Delegate all can_modify checks to the proper places. Unmap checks are done in do_unmap (et al). The source VMA check is done purposefully before unmapping, to keep the original mseal semantics. Signed-off-by: Pedro Falcato Reviewed-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes --- mm/mremap.c | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/mm/mremap.c b/mm/mremap.c index e7ae140fc640..24712f8dbb6b 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -902,19 +902,6 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len, if ((mm->map_count + 2) >= sysctl_max_map_count - 3) return -ENOMEM; - /* - * In mremap_to(). - * Move a VMA to another location, check if src addr is sealed. - * - * Place can_modify_mm here because mremap_to() - * does its own checking for address range, and we only - * check the sealing after passing those checks. - * - * can_modify_mm assumes we have acquired the lock on MM. - */ - if (unlikely(!can_modify_mm(mm, addr, addr + old_len))) - return -EPERM; - if (flags & MREMAP_FIXED) { /* * In mremap_to(). @@ -1052,6 +1039,12 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, goto out; } + /* Don't allow remapping vmas when they have already been sealed */ + if (!can_modify_vma(vma)) { + ret = -EPERM; + goto out; + } + if (is_vm_hugetlb_page(vma)) { struct hstate *h __maybe_unused = hstate_vma(vma); @@ -1079,19 +1072,6 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, goto out; } - /* - * Below is shrink/expand case (not mremap_to()) - * Check if src address is sealed, if so, reject. - * In other words, prevent shrinking or expanding a sealed VMA. - * - * Place can_modify_mm here so we can keep the logic related to - * shrink/expand together. - */ - if (unlikely(!can_modify_mm(mm, addr, addr + old_len))) { - ret = -EPERM; - goto out; - } - /* * Always allow a shrinking remap: that just unmaps * the unnecessary pages..