From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ABD4C2571BE for ; Mon, 14 Jul 2025 14:37:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752503858; cv=none; b=TtJJU445o1TvqzWPxeOoyVpwfueDuSlmYrkRG4m4CcMS2+F2We2r/1Z2cHbtw5Xy+u/JsWmwmt1G25z3eS/EkZF9VHEUJ1/PEpYLyNdgV2SYSMtFgGiTJmNqVzd3a9KzpHhdVaNIJo/qtkti2avP+cCBCat4OnEoiPj7kX/wvnE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752503858; c=relaxed/simple; bh=RjGx9P5kC6mARBJP6775NozU85TUMgTGi0IZUOQB1j0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=fhZ3mPvEVz0Kpt1BiU9SaHSJVgjj5JMy+k4G/q4YJJXVp3dARTyZVYeJKmd277LDP0cZ8m4ydFMqF4KA3nabgeMS8Qq+7KYuiGrMrfkCjdUan91+2R4+OBl2owdfJaukRwbYB0gn47wkKA8RRZ2vzFzenvGVRj2r2m5kAc6Sqew= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=C7SeytB4; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="C7SeytB4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1752503855; h=from:from: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; bh=5g+pcFOcxgDZoR5yPrN9tuAezacA6xxzq/+I6doDGUU=; b=C7SeytB489MR4vF+Dwf2LWWThSYqBB94uG/S4l3eGznlp41r7Zff1u4EjdVgtBT6mSaMpD yQA3I45fOJIGRgYTEIl14qS6fef0xmh/0p0mYGseb5NVrPmPRPLQe/JDDuTiS1uI9WH+EB k+ZBhbjZcN3GB8onijT7YK6XMRoP/gE= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-516-DI4PuR94PemDBKMZPYT_XQ-1; Mon, 14 Jul 2025 10:37:33 -0400 X-MC-Unique: DI4PuR94PemDBKMZPYT_XQ-1 X-Mimecast-MFC-AGG-ID: DI4PuR94PemDBKMZPYT_XQ_1752503853 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-45359bfe631so27125225e9.0 for ; Mon, 14 Jul 2025 07:37:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752503853; x=1753108653; h=content-transfer-encoding:in-reply-to:organization:content-language :from:references:cc:to:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5g+pcFOcxgDZoR5yPrN9tuAezacA6xxzq/+I6doDGUU=; b=r/yJj/llh2B0ssHq069etPtsb+3JHCx2XEFvC3Fq+9nNIy5qx4kq7LcLcuUYUolAa9 +NtSCq6S2f60QvJF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:c807 with SMTP id 5b1f17b1804b1-4561608ca40mr47116385e9.26.1752503852572; Mon, 14 Jul 2025 07:37:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFTvnEcDMapbCXWP3qDegKpdCdyuaTjkUqgqccpswwEq+6DQh1O4IMK7Ozrf7oErXapdbaEbg== X-Received: by 2002:a05:600c:1c9a:b0:456:1608:c807 with SMTP id 5b1f17b1804b1-4561608ca40mr47116125e9.26.1752503852095; Mon, 14 Jul 2025 07:37:32 -0700 (PDT) Received: from ?IPV6:2003:d8:2f38:ca00:ca3a:83da:653e:234? (p200300d82f38ca00ca3a83da653e0234.dip0.t-ipconnect.de. [2003:d8:2f38:ca00:ca3a:83da:653e:234]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-454d5103c2asm171298475e9.33.2025.07.14.07.37.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Jul 2025 07:37:31 -0700 (PDT) Message-ID: <5e21df9f-7f75-412b-a173-fe6da49952e5@redhat.com> Date: Mon, 14 Jul 2025 16:37:30 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/5] mm/mseal: move madvise() logic to mm/madvise.c To: Lorenzo Stoakes , Andrew Morton Cc: "Liam R . Howlett" , Vlastimil Babka , Jann Horn , Pedro Falcato , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Jeff Xu References: <5d932ec1f9d0ea115aac65067e4cb8241a06e791.1752497324.git.lorenzo.stoakes@oracle.com> From: David Hildenbrand Content-Language: en-US Organization: Red Hat In-Reply-To: <5d932ec1f9d0ea115aac65067e4cb8241a06e791.1752497324.git.lorenzo.stoakes@oracle.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 14.07.25 15:00, Lorenzo Stoakes wrote: > The madvise() logic is inexplicably performed in mm/mseal.c - this ought to > be located in mm/madvise.c. > > Additionally can_modify_vma_madv() is inconsistently named and, in > combination with is_ro_anon(), is very confusing logic. > > Put a static function in mm/madvise.c instead - can_madvise_modify() - that > spells out exactly what's happening. Also explicitly check for an anon VMA. > > Additionally add commentary to explain what's going on. > > No functional change intended. > > Signed-off-by: Lorenzo Stoakes > --- > mm/madvise.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++- > mm/mseal.c | 49 ----------------------------------------- > mm/vma.h | 7 ------ > 3 files changed, 61 insertions(+), 57 deletions(-) > > diff --git a/mm/madvise.c b/mm/madvise.c > index 9de9b7c797c6..75757ba418a8 100644 > --- a/mm/madvise.c > +++ b/mm/madvise.c > @@ -19,6 +19,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -1256,6 +1257,65 @@ static long madvise_guard_remove(struct madvise_behavior *madv_behavior) > &guard_remove_walk_ops, NULL); > } > > +#ifdef CONFIG_64BIT It's consistent with mm/Makefile etc. but having a simple config MSEAL def_bool y if 64BIT or sth like that would surely clean that up further. > +/* Does the madvise operation result in discarding of mapped data? */ > +static bool is_discard(int behavior) > +{ > + switch (behavior) { > + case MADV_FREE: > + case MADV_DONTNEED: > + case MADV_DONTNEED_LOCKED: > + case MADV_REMOVE: > + case MADV_DONTFORK: > + case MADV_WIPEONFORK: > + case MADV_GUARD_INSTALL: > + return true; > + } > + > + return false; > +} > + > +/* > + * We are restricted from madvise()'ing mseal()'d VMAs only in very particular > + * circumstances - discarding of data from read-only anonymous SEALED mappings. > + * > + * This is because users cannot trivally discard data from these VMAs, and may s/trivally/trivially/ > + * only do so via an appropriate madvise() call. > + */ > +static bool can_madvise_modify(struct madvise_behavior *madv_behavior) > +{ > + struct vm_area_struct *vma = madv_behavior->vma; > + > + /* If the operation won't discard, we're good. */ > + if (!is_discard(madv_behavior->behavior)) > + return true; Conceptually, I would do this first and then handle all the discard cases / exceptions. > + > + /* If the VMA isn't sealed we're also good. */ > + if (can_modify_vma(vma)) > + return true; > +> + /* File-backed mappings don't lose data on discard. */ > + if (!vma_is_anonymous(vma)) > + return true; > + > + /* > + * If the VMA is writable and the architecture permits write access, > + * then discarding is fine. > + */ > + if ((vma->vm_flags & VM_WRITE) && > + arch_vma_access_permitted(vma, /* write= */ true, > + /* execute= */ false, /* foreign= */ false)) > + return true; > + > + return false; > +} > +#else > +static bool can_madvise_modify(struct madvise_behavior *madv_behavior) > +{ > + return true; > +} > +#endif > + > /* LGTM -- Cheers, David / dhildenb