From nobody Mon Feb 9 19:31:08 2026 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 9FF441DE2C0 for ; Wed, 29 Jan 2025 11:54:31 +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=1738151673; cv=none; b=c/gdla1rG3I3RnXarU8iKHzDe3DWQBF/EfkMmEUU57OaxQyJwvkn1AUbALnOkeE8KOpBqjS7MrGH80OzW5gvb4R1cgsczdKyxJALe/ZevaU1tOLyp2R+tUjH7NKd7UxSjBzU7k7dgRJNoj6YKjgmxf76OpsbO1aCNb9N1mA/Igk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738151673; c=relaxed/simple; bh=Yra65BGBV8k5XKIYw+5XZASeR9kSFc/ZXoKOAD23Si4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cXh6TRJNE0SnvV0+n2BsScw1Tub/fXyLd67NKN4yIMa9kkwHjRA+qy0Lricv/yGwzkniLT1FNLAs/mZhAoIW3hkxgioFN3MVBTBdlG5Zg7XKXLofWXOn3gUbogFKdP8iNRDog0aQcWXtK8OQDymrJ6gRPSgLp/uMvZXC9GK72l0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=HGytB2dV; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="HGytB2dV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738151670; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/5TTv0Iu+aL+vrfHcl/JjDhmk1gN271wttPyRyQHADk=; b=HGytB2dVkPgiR9ejfwBiWU3gdGB46hzHD07qcVkIvzbpo+vNrbBZfVvuVm5jHegd5icUB5 tqUvegNVk/Es8n+hRQFyg5MRW2KwD0UPjG46XQ+JUZHjqil2x5N4p+bR04OvtRpgMCV45N rvdmNb23GWmotgdCatioCxRvQXBiMq4= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-552-p1gh93FyMu2VG06A4rijvA-1; Wed, 29 Jan 2025 06:54:29 -0500 X-MC-Unique: p1gh93FyMu2VG06A4rijvA-1 X-Mimecast-MFC-AGG-ID: p1gh93FyMu2VG06A4rijvA Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-38a35a65575so4681872f8f.1 for ; Wed, 29 Jan 2025 03:54:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738151668; x=1738756468; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/5TTv0Iu+aL+vrfHcl/JjDhmk1gN271wttPyRyQHADk=; b=vxJ1TsbJ+jxt2oK2CQGznwO+1q47yB+rHEuYXMwFjA0JL9FVJeTJGpemzcLlJ7NMAJ VJm/mBd0Vf6gn+Yq/uBh1k+OB+p4qFaFNckZ18GatDlqW4pTIfPqiWrDoqJrNNwACbU9 hBhpK8JCBMw7r4eTXXvooLW5jxr9WGut6vXXEj+6tS7wsDOqqpUt8XvLYkPMM7z8etnG iYFcX2bycu35mf6laKqNv07ISmF59QPfIOry+WMOOoo8o/vw9hWkNeA6oL4oek8TyItq VdgJSAxxPLTMYjCAE4z9ggCsXK/KivM3AbDhqt9vAVncxbjTtxRn99gmVOZ2VXk8E8aZ spMw== X-Gm-Message-State: AOJu0YxXsW4SgX0TGw10RfIYsOEVCpJvAXJoFITL9BnB3VyGH3MFJ6Mi cdFIfDd4Ghdu2hjFZBxbx3bjnxXwS/e4GUc4pqX5MRPSr6rUCrg2icH/ePBN3tkBT5lcGdfFoPx lOgXXJVyGS3OwbghCd23XTwqEnSVfoYkS7uOGqY9KsPrQX3BhQD7bz6YWY7KKbrIb5LIhdw6F7S KHqqzXAtdUjOwdwG/FGP/WKNb9a3xEMfQjjHb1lBfNxBZq X-Gm-Gg: ASbGncuEeOXe/dVk762j02s4om1ufgUgUcbelDzzTMvlvqmj4VF+QJ3GKN9RgpHMQHf bwVOnJvge+NQoJG3nXivaCDzV6uCWj8JKYrM0R3AfarkYVIE8LpfcFuygntP6SqLjTOGIaDmhCM 6xUja6R9nFr+Zrc0m38o1JRH6+Vq6SVllJAg1NZCsyDHvuV95gg0Kc95c3JLc1pWON8qyBc8jTB 8Y/ItM8wwBmD4BxM4KRA6UPWII6BY2KQYXBgm1Tvv9TqSPysAqm6hmGWvjDDBEpGfhMSopP3eHP FsSJZDBZdE8oVBkRGGO6qxk66mw+80mjC3gDOR2GEmbDW/K8ts1A5NNZc3T+KAF7dQ== X-Received: by 2002:a05:6000:1fac:b0:386:3329:6a04 with SMTP id ffacd0b85a97d-38c51e8de63mr2549394f8f.39.1738151668398; Wed, 29 Jan 2025 03:54:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IH3OskkNotGDGa9hKk/bhinDJ94hVo+TUAo4zN3qwEB+ZEXq9Nn9aY72BgysSoJ/r2gZh7hQQ== X-Received: by 2002:a05:6000:1fac:b0:386:3329:6a04 with SMTP id ffacd0b85a97d-38c51e8de63mr2549347f8f.39.1738151667985; Wed, 29 Jan 2025 03:54:27 -0800 (PST) Received: from localhost (p200300cbc7053b0064b867195794bf13.dip0.t-ipconnect.de. [2003:cb:c705:3b00:64b8:6719:5794:bf13]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38c2a188fe8sm17066981f8f.56.2025.01.29.03.54.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jan 2025 03:54:27 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org, nouveau@lists.freedesktop.org, David Hildenbrand , Andrew Morton , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Jonathan Corbet , Alex Shi , Yanteng Si , Karol Herbst , Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Pasha Tatashin , Peter Xu , Alistair Popple , Jason Gunthorpe Subject: [PATCH v1 05/12] mm/memory: detect writability in restore_exclusive_pte() through can_change_pte_writable() Date: Wed, 29 Jan 2025 12:54:03 +0100 Message-ID: <20250129115411.2077152-6-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250129115411.2077152-1-david@redhat.com> References: <20250129115411.2077152-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's do it just like mprotect write-upgrade or during NUMA-hinting faults on PROT_NONE PTEs: detect if the PTE can be writable by using can_change_pte_writable(). Set the PTE only dirty if the folio is dirty: we might not necessarily have a write access, and setting the PTE writable doesn't require setting the PTE dirty. With this change in place, there is no need to have separate readable and writable device-exclusive entry types, and we'll merge them next separately. Note that, during fork(), we first convert the device-exclusive entries back to ordinary PTEs, and we only ever allow conversion of writable PTEs to device-exclusive -- only mprotect can currently change them to readable-device-exclusive. Consequently, we always expect PageAnonExclusive(page)=3D=3Dtrue and can_change_pte_writable()=3D=3Dtrue, unless we are dealing with soft-dirty tracking or uffd-wp. But reusing can_change_pte_writable() for now is cleaner. Signed-off-by: David Hildenbrand --- mm/memory.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 03efeeef895a..db38d6ae4e74 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -725,18 +725,21 @@ static void restore_exclusive_pte(struct vm_area_stru= ct *vma, struct folio *folio =3D page_folio(page); pte_t orig_pte; pte_t pte; - swp_entry_t entry; =20 orig_pte =3D ptep_get(ptep); pte =3D pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot))); if (pte_swp_soft_dirty(orig_pte)) pte =3D pte_mksoft_dirty(pte); =20 - entry =3D pte_to_swp_entry(orig_pte); if (pte_swp_uffd_wp(orig_pte)) pte =3D pte_mkuffd_wp(pte); - else if (is_writable_device_exclusive_entry(entry)) - pte =3D maybe_mkwrite(pte_mkdirty(pte), vma); + + if ((vma->vm_flags & VM_WRITE) && + can_change_pte_writable(vma, address, pte)) { + if (folio_test_dirty(folio)) + pte =3D pte_mkdirty(pte); + pte =3D pte_mkwrite(pte, vma); + } =20 VM_BUG_ON_FOLIO(pte_write(pte) && (!folio_test_anon(folio) && PageAnonExclusive(page)), folio); --=20 2.48.1