From patchwork Wed Jan 29 11:58:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13953691 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 7676CC0218D for ; Wed, 29 Jan 2025 11:58:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C09C02801C7; Wed, 29 Jan 2025 06:58:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B69C328005A; Wed, 29 Jan 2025 06:58:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96D702801C7; Wed, 29 Jan 2025 06:58:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 78E2328005A for ; Wed, 29 Jan 2025 06:58:38 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DEB37C0C71 for ; Wed, 29 Jan 2025 11:58:22 +0000 (UTC) X-FDA: 83060341644.13.264708F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf15.hostedemail.com (Postfix) with ESMTP id AADB6A0004 for ; Wed, 29 Jan 2025 11:58:20 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Ucc5PXez; spf=pass (imf15.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=1738151900; 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=n2/u4hMaC1Y/nn2Uh1F1xHtHTh10RqLxBEMK6rVGx5s=; b=2hmqLyEeJie3jgmRlbIe/rVtLa3I2uoGS47Op4IQ7CG08GU3s6meJuGmTNJbdDg+zPV/Io sVN2Yn+TugB13X4FxhyGSgD5gcAmqHgBVgzFdzPUXPNrVSziY2cmbiPwX+gw+xjsFmG1P0 LWwo1h8SEpHr7xuByANNkufWrn4nfZY= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Ucc5PXez; spf=pass (imf15.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=1738151900; a=rsa-sha256; cv=none; b=e39pp91H9veVJDDMarO9d12dPpKrcqWz5r8uoKsNv7hBycwduULtXNDrmnltF6y5DW6ZkB JbyACuRTt8t2AiLYizgZZaI+Yj8fDW8vWjeDr7u8DmMQSXZ7YyKTRUmw4y2zSkbeNubgXC YH4Y9DK6MXNYRB77QLu4ZQK5T2Dz3MI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738151900; 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=n2/u4hMaC1Y/nn2Uh1F1xHtHTh10RqLxBEMK6rVGx5s=; b=Ucc5PXezVRK+eV6OYkwjjCTJY9lK2sZR7LWYq5WOuoDdXC74pfncMME+fDTqYWWlWPVQ5Q 9ZRa0jtyHCTOpQEebW9mLGFfDcKiBBbeARZtYaxOgODQ40SKT+mpgVmU73wQnIJAVbhae/ B5Z06MHjXfXL2czCPUk+dl9hzoFGxb0= 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-251-yK-g8O0fO5qVcTqs0PzoAQ-1; Wed, 29 Jan 2025 06:58:17 -0500 X-MC-Unique: yK-g8O0fO5qVcTqs0PzoAQ-1 X-Mimecast-MFC-AGG-ID: yK-g8O0fO5qVcTqs0PzoAQ Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-38a2140a400so5197988f8f.0 for ; Wed, 29 Jan 2025 03:58:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738151896; x=1738756696; 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=n2/u4hMaC1Y/nn2Uh1F1xHtHTh10RqLxBEMK6rVGx5s=; b=FRfkcw5wVVpT85xfMGFh6PkGE5ISWYMb1nj+SX/sSrOJ4YLI2s2eaiPLfvHuhYM0ZC ZHwiXhTFXnVRqiMQsqoiXNzt4Oq+Osaul5P/XPelC6tkxj4cdulKAj8OKvIJ5ZJujOwZ sY+Cd/k5r1wz6g3UOkKJ61nIxFX0RosTYjYWq68ofjmdrnlB2JS4v1gB2q4vp/x1DUdP 5x+XlVL+UqzklZR9pBJWdBJNsX/AtcQMCyyYMmEE0HHrvc+G/JsA9L1iKI8/4sb5Qyz8 txj4ZQE8hZMlg4EA6plNN3iNU4y9a7WrA9DmxAZnN8Wb+r9ibHo34ffh361lRn3dOwm3 1g0Q== X-Forwarded-Encrypted: i=1; AJvYcCU3RTc84wIAq/AyPO8E6ZkByiK5Qnr5v7kPreYa9SV5tXRvZGHwqblYo6I0edjmo8IbErpdUQDKHQ==@kvack.org X-Gm-Message-State: AOJu0YyBumxlvLP0qmjlGECBH/Mtv3PMYjzDjFI1Zu8nDPrEo0a1GB7m fRm1whSCqB2+s/VhkLHGgqxeooHaYJ82WPp+KDA5U5BW5BNwIB0Seo/1tndA8MAORFUg4H05e7k MDDY2Mvzphn8LLDGnsl2X8Ob0f0eh3Jbz6CoFibrt8d2X7Oxb X-Gm-Gg: ASbGnctD0+t5neQJFBIMsrokMD3qpomug/CxJxFpScAhBFT0g7ZlAHRnczTNCYueZe2 mmZ7Iu7iQpV9aKEaPsc93/ESpAu0NTcm2t13obQYJ0O0MwD0gMHV1v+qEMJ0oLptmOEiiRpyy11 Hm9kBNOBw8DmQOyDEyAdjVn2WBqKWz8soDkQqQcZ1jhpeETA49NIbs7zQ5RN5hr7/wR6z6palKE jXb+c5xzYqeoWVeFtfqDgyjcCVmupfETOY578XeXP3aJlbzSUuc0DDlUmqe64BklsOj1SsPnqXG o1Sp5tv+PWKkvXXov2DjABPsyqwQjt06+MG/BeUq7wCdbqCNfeFFY8yBdcwvuxkYNg== X-Received: by 2002:a05:6000:2c8:b0:38a:88f8:aac8 with SMTP id ffacd0b85a97d-38c52096289mr2025157f8f.54.1738151895828; Wed, 29 Jan 2025 03:58:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IEON5WkoaxoRH3qtkNX0vn2oqksdlJtc4nZs+NotAXeIJk4vQvUCQu3rl+CihtRV9gH+gpH8w== X-Received: by 2002:a05:6000:2c8:b0:38a:88f8:aac8 with SMTP id ffacd0b85a97d-38c52096289mr2025126f8f.54.1738151895421; Wed, 29 Jan 2025 03:58:15 -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-38c2a1764e9sm17070487f8f.17.2025.01.29.03.58.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jan 2025 03:58:14 -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 3/4] mm/memory: pass folio and pte to restore_exclusive_pte() Date: Wed, 29 Jan 2025 12:58:01 +0100 Message-ID: <20250129115803.2084769-4-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250129115803.2084769-1-david@redhat.com> References: <20250129115803.2084769-1-david@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: FC5Sp220I1QZnvtbwY82LAgtCDlPCLzMNdsTKI8TuLk_1738151896 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: AADB6A0004 X-Stat-Signature: f8fbbgn8y7jb9qz9riar7ici699f87zo X-Rspam-User: X-HE-Tag: 1738151900-995022 X-HE-Meta: U2FsdGVkX1/zm3+bePmd92PiB31rfMQJO9lZifVpQMiAQgbNv05NxtoMebUz1pGKQf/GwfZ6K4WgYnEzKj8U0pC2WDs0CDq53ux8G7ZXy8MMMd0Q32IckfpMZMzg4iH86+4bOHjcO047PShVCRYoBckIfGJfTz0ibqN7N7iwC8qFMV7aq9BGBvx09pvw/mLZtMJkZ9KURzurIFo7/ZpgBGuW+HljS+XaYmOj93Ww0m50/AiTV/8OxiehKDadWbOPqRKqCHMtU1ZGECpd/Aa76YxKxC6ACPGDICb5q2C5GMA0AY1uYog9ASDLMqzIjOQVAe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8QmyaQSJg1cWeXgawb8/Xlrc4xz5uYEvB0pLRRos9jaUhWN3UL8T+H6SBujJZXq4Fskgw9TRRVIYK0Wk2QGi9ENr4Nlh6bU71eHp4aJYm1NBF1Z672/DwnycrTagBLlXg== 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 pass the folio and the pte to restore_exclusive_pte(), so we can avoid repeated page_folio() and ptep_get(). To do that, pass the pte to try_restore_exclusive_pte() and use a folio in there already. While at it, just avoid the "swp_entry_t entry" variable in try_restore_exclusive_pte() and add a folio-locked check to restore_exclusive_pte(). Signed-off-by: David Hildenbrand Reviewed-by: Alistair Popple --- mm/memory.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index cd689cd8a7c8..46956994aaff 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -719,14 +719,13 @@ struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, #endif static void restore_exclusive_pte(struct vm_area_struct *vma, - struct page *page, unsigned long address, - pte_t *ptep) + struct folio *folio, struct page *page, unsigned long address, + pte_t *ptep, pte_t orig_pte) { - struct folio *folio = page_folio(page); - pte_t orig_pte; pte_t pte; - orig_pte = ptep_get(ptep); + VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); + pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot))); if (pte_swp_soft_dirty(orig_pte)) pte = pte_mksoft_dirty(pte); @@ -756,16 +755,15 @@ static void restore_exclusive_pte(struct vm_area_struct *vma, * Tries to restore an exclusive pte if the page lock can be acquired without * sleeping. */ -static int -try_restore_exclusive_pte(pte_t *src_pte, struct vm_area_struct *vma, - unsigned long addr) +static int try_restore_exclusive_pte(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, pte_t orig_pte) { - swp_entry_t entry = pte_to_swp_entry(ptep_get(src_pte)); - struct page *page = pfn_swap_entry_to_page(entry); + struct page *page = pfn_swap_entry_to_page(pte_to_swp_entry(orig_pte)); + struct folio *folio = page_folio(page); - if (trylock_page(page)) { - restore_exclusive_pte(vma, page, addr, src_pte); - unlock_page(page); + if (folio_trylock(folio)) { + restore_exclusive_pte(vma, folio, page, addr, ptep, orig_pte); + folio_unlock(folio); return 0; } @@ -871,7 +869,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, * (ie. COW) mappings. */ VM_BUG_ON(!is_cow_mapping(src_vma->vm_flags)); - if (try_restore_exclusive_pte(src_pte, src_vma, addr)) + if (try_restore_exclusive_pte(src_vma, addr, src_pte, orig_pte)) return -EBUSY; return -ENOENT; } else if (is_pte_marker_entry(entry)) { @@ -3979,7 +3977,8 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, &vmf->ptl); if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) - restore_exclusive_pte(vma, vmf->page, vmf->address, vmf->pte); + restore_exclusive_pte(vma, folio, vmf->page, vmf->address, + vmf->pte, vmf->orig_pte); if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl);