From patchwork Wed Jan 29 11:54:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13953679 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 77894C02193 for ; Wed, 29 Jan 2025 11:54:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 838BA280016; Wed, 29 Jan 2025 06:54:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7BAB0280017; Wed, 29 Jan 2025 06:54:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F261280016; Wed, 29 Jan 2025 06:54:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 3992A280017 for ; Wed, 29 Jan 2025 06:54:46 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7E6521A0C51 for ; Wed, 29 Jan 2025 11:54:33 +0000 (UTC) X-FDA: 83060332026.02.C06FC83 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf12.hostedemail.com (Postfix) with ESMTP id 38B2640007 for ; Wed, 29 Jan 2025 11:54:31 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=M0txsj2m; spf=pass (imf12.hostedemail.com: domain of dhildenb@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhildenb@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738151671; 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=/5TTv0Iu+aL+vrfHcl/JjDhmk1gN271wttPyRyQHADk=; b=JxoZOX/GfaAdubQXYpmMDkpSO2qt7gT2tHt88vLhOm+rNM6SiH/UDzyqYRDuBGNtfDNcsH G1f+q2Ao+X6/wCrfNsFrT5ZDY2WtI83idFSZF/oAMGh7JdS0i5MfHxjygbAM4RkFHypt7m HdRkCuIh0Xx06YQh8OzQyu2ufIE7TO8= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=M0txsj2m; spf=pass (imf12.hostedemail.com: domain of dhildenb@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhildenb@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738151671; a=rsa-sha256; cv=none; b=SYITbftiFIFNd9rX4jyaFDdh9Cpt9jtz6v4iDE80i+8AhXvBUSX/iVQ6eMSQ29Yusqpqf2 Tdz8SWA/CztiIeHiv5Lnhh/DO9gzbwD99wVumwqJUjblK+eICNmCovLXatagHxPJ9nMf1B 38qgz6hi3+hfXiEx4tXWpt/baDy+gdc= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/5TTv0Iu+aL+vrfHcl/JjDhmk1gN271wttPyRyQHADk=; b=M0txsj2mnKZA2JnGX+StEY7X9CFVO6LK7mzDxUKl8pRpwGU9xs8hUuQ8NAO0Cr8CpLGZRI QHQJqQgxSBWG2sOicDFIaHdMhTzbB25q5tSrAbYvC+CQguBpx5qlcdqm6KjcYCYftAtxXu 7kE8AgBNEr8ehKssuQaBdjt3xxksBps= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-549-5w24BLl9N6SAHXFOOunXfA-1; Wed, 29 Jan 2025 06:54:29 -0500 X-MC-Unique: 5w24BLl9N6SAHXFOOunXfA-1 X-Mimecast-MFC-AGG-ID: 5w24BLl9N6SAHXFOOunXfA Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-38a2140a400so5195812f8f.0 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=cwTanqLl5kN7PeR0jPhvyYv3ZG0Vg9a71e3SxF02yzfSD2TMhJxbyJ9M+6XbAbZL3m c5GJbILBK6pdDALyC6comz60udgYH6WSSyj9PuTpxCnMK1Gbx8ZO4mJYAvra2keKuiG5 bbH1xFj+RyHBvV/7BAeo8tsCnVvucAlEuLc3wFofIdQptoFjjlRJ8tkVxi1qqXxc9wa1 tnsRYU3FUpbmbhf6tZoTOOJv4/qpyOXnxNfOE7hAFXfSOe8ZONsRxzo9YM9dhCg2FBIX Qf3h5lOJWpvwmJzotl6AhGWDNlwth9wAuuZ1O8mNpI9g7gLccPEckLQUSZs1Rh6t4uHJ r2Gw== X-Forwarded-Encrypted: i=1; AJvYcCU3TRq0/3wvG+GT19Z/fetHB1NzWuEBc9sXdU6NW/LVuFp8xzI/yrL8tZPHIoDAu5+a2stnm2zbyA==@kvack.org X-Gm-Message-State: AOJu0YyOsS2rP+vCjKgCF36VMQ7sQVQvxDNdg/JEikNLyG8bHS1AnoFF j6sq3osdndGNWR3HJxRDILqjFcFMKP1CyWGDUfKVdHL67QxNJ6FQSzMkWoVCO6d6LoufsAck+b3 4XwOw+KYo7pLUmm+3h+FyTbdN18iT1FyVfFUnvXmrRSW24m/n X-Gm-Gg: ASbGncuPlAxCtfyM1hhdrBwQfPEIsVJsq9qTCOtATAhpRfLcku07itYigMgUF2cRT2J utjyL+VZcC+5XKCK3UoW7Pnednh8no69GG0gMhC7O1NVGBHeg/inmK5Lp41Xszjn73BbFNuMh/T JSlKc8hhwkxA6ewBNKfpwtzTsBs91h0ynsAKnGXFZhDEbJIM4aFmf+M4EDbEqNBfR6aiibXrqpk 85Ts/4tWHDG+5rnrQqhfyWknN1DW0aFEvBrCPDQXpDiakawVIers8tHEHg35LQ2Jvv6j9T1VpsT nj8bJbPsxpkfJBZTStOjifMkHAz9Ttk729jc3W9lVsGZDj5K0dwLoF89qVGHgAv61g== X-Received: by 2002:a05:6000:1fac:b0:386:3329:6a04 with SMTP id ffacd0b85a97d-38c51e8de63mr2549369f8f.39.1738151668337; 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?b?SsOpcsO0bWUgR2xpc3Nl?= , 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> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: GqJtNwJtgYB5ooHkjTThEEviCb-Vyv-jHgG11pzC_GE_1738151668 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 38B2640007 X-Stat-Signature: zkwzf6qgogssdfpefb48gwugiehzt1xp X-Rspam-User: X-HE-Tag: 1738151671-915446 X-HE-Meta: U2FsdGVkX1/1el0MaqAPn+fhL45sQ+q809U/n11xydmDjVC+aWdtFVNDFtX8gfwEjW9Pmr0emGyCPBzUiUCvZsYMxht1PfDouwj7/X61B63yaredhRAV78uA95Jv4Mc4Hra2nroanEvYtLuFSgQnUVjsyMd+zJLUJlbnURD4ANaAijjhOnMnGrPj9B92zWSkNNNmqgYxYHTiR71Y+fEKie1T2OseiCJiG8+/dSUoQ5a0uNhEDNrsINXcvLPH/VoJMyi7TIBrxpLdNuEcTtSgxTF9MkwdzMPM5ebfSYlbRufPspOXESBmwYFfo2bwNNSlW8eLOMQUv/RK0cZNZAuWkkSGWTcD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FYDVKBtXszltgcmQJ+SY9kjrTEUwHNlvuCLw7t2D59I7DiKIH+yRSAbyHUhTU1q86SNnLUJjIv1EIZiQ7lAslK9p5s7A6xly7e59l8/sUX8i7KtZAnw== 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: 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)==true and can_change_pte_writable()==true, 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_struct *vma, struct folio *folio = page_folio(page); pte_t orig_pte; pte_t pte; - swp_entry_t entry; orig_pte = ptep_get(ptep); pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot))); if (pte_swp_soft_dirty(orig_pte)) pte = pte_mksoft_dirty(pte); - entry = pte_to_swp_entry(orig_pte); if (pte_swp_uffd_wp(orig_pte)) pte = pte_mkuffd_wp(pte); - else if (is_writable_device_exclusive_entry(entry)) - pte = 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 = pte_mkdirty(pte); + pte = pte_mkwrite(pte, vma); + } VM_BUG_ON_FOLIO(pte_write(pte) && (!folio_test_anon(folio) && PageAnonExclusive(page)), folio);