From nobody Mon Feb 9 11:31:01 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 062D51DE88E for ; Wed, 29 Jan 2025 11:58:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738151902; cv=none; b=RZgyEkVczMn732LzQnQ7wZmyK20bsN88nqbVHAN/W6ca1VXHtaf41Eo+x1yszeVzxDoqoklaGOq3aznJuyc9xPaB+xMsjTGsdtZBKH2u2WpFOHqE5YYU29v1vVr3mHnp2PGB0tus/40/ZCL8xtAx4l3M7zxjs4xHj9SOXFJiE0s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738151902; c=relaxed/simple; bh=sOFTxxiTEpmnz84//zewC99r4PEneol5TOg9SJgosTM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CEsrV2E8MhvU8gqO/YtWFYvaEo8938LjF1ci/zp3lPDfybq2f6Nk00okb6tZv0CUgQcvgwB29IpFI1aVigMxEiuy5x8DZ5oUAhYLBySxOSTJ3g29IciW5oAOqpu3gGvLbZ7JhLFT454qHn3JcP25Wa+NRLoOaJhiTvb1TyiWbiM= 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=g5wWcChF; arc=none smtp.client-ip=170.10.129.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="g5wWcChF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738151898; 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=n2/u4hMaC1Y/nn2Uh1F1xHtHTh10RqLxBEMK6rVGx5s=; b=g5wWcChFzVjhYwUyUY42dz1gmoP03zan1wc7zJ+S8ea92QAOUhhPHoSJseZSnzzBsksfz3 ghksBdMbeCESoaJmT2kt1VuEX2SfOQx+fPMAyaOXCgHplnq02qOe7+IldWJhzLqjivxxb8 Tw+5Nw3Z2QvBs2C4h65mxHTQLmfSuCQ= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-251-jRDbW3MdMFylUf5QW9jf1g-1; Wed, 29 Jan 2025 06:58:17 -0500 X-MC-Unique: jRDbW3MdMFylUf5QW9jf1g-1 X-Mimecast-MFC-AGG-ID: jRDbW3MdMFylUf5QW9jf1g Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-38a2140a400so5197994f8f.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=xUds2mz4voBCmTrsxteASl8dscgEm+GgB3trMJPexsmzs9Ejng24ivKIpoKtIeDRQe tC0WlGM55BqFzd/95Bau2UTc89Mv3lDfc9ggQUDGlAkxxr1tu+0AXbgjmZM6+RZb9ca6 qATsHH7bUZZfq8AvwPyMHXJbjpsFgGUyyQbhg0ZMUoFYr3HRvhQPSxe7fG0X7x0gTByg er5juaHJ/A7niJpJaFidfCO4yO5t/8uEvSmmKvW+nENwDV9DqXFeL/tZ2MsvXYndzYL5 KWC4BFBqGejLOWuohEGkc5ZoLaDIeVh59TR3LlHVfq5Nal3ZZz17NphxLNzstDU2Lq9r sYCA== X-Gm-Message-State: AOJu0YzIqA8CKHV90oWriT1jdHYC3tt9lZ+Fg4cepmOADOsL7sSZsKiS OIv8tkpGwC+Ra0wi8C+2KHIHGWVAnJAmHw+JBHSoIDyVMciOyUeSZ4DZtCNDqa6FXTLPR7YgMHb 0SO/8+Fq7r7BBJIgeYK3z04Lc5soYHu2r0aSfUrAesS7stcwAGt/2P3ZzQTzR+gYiHz7qaQ8Wgq /nfYWx5Qg66LkQ8qfgtpsdIoLjgZzmOiyQSyMzxcRhzJmP X-Gm-Gg: ASbGncsTtjMl8xDql3foJRVg8ZebRexCNPSrwdrAS228DxFmnkG+myqn3TApKBIOoT7 MJudJZU60gsh+moMP4myz8QTTsros7h3ljlfikX5KnLqPxY4eK/8VeYEfSITouaayidSIe6D3zQ lHmkh+DAODfcbBazkqlootMj/XsekVWTBGn8xiuWsw36875eZwGCJxg56uKa9zNW69AEoPGHcGT iV0vzhCLREFw/KWaawH+uQQAdA7WmTC0rT7Aak4O2JQLR6n8s2Yow1FVEVz075CL2tnWjbtJWT+ Nh87SuwZyMiTceaccOcepnoNKo6vGtTSbYKxK/0t+Wm93vsDHbJ/RqGxIT6GjGXGGw== X-Received: by 2002:a05:6000:2c8:b0:38a:88f8:aac8 with SMTP id ffacd0b85a97d-38c52096289mr2025174f8f.54.1738151895938; 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?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 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> 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 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_stru= ct *vma, #endif =20 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 =3D page_folio(page); - pte_t orig_pte; pte_t pte; =20 - orig_pte =3D ptep_get(ptep); + VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); + 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); @@ -756,16 +755,15 @@ static void restore_exclusive_pte(struct vm_area_stru= ct *vma, * Tries to restore an exclusive pte if the page lock can be acquired with= out * 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 =3D pte_to_swp_entry(ptep_get(src_pte)); - struct page *page =3D pfn_swap_entry_to_page(entry); + struct page *page =3D pfn_swap_entry_to_page(pte_to_swp_entry(orig_pte)); + struct folio *folio =3D page_folio(page); =20 - 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; } =20 @@ -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(struc= t vm_fault *vmf) vmf->pte =3D 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); =20 if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl); --=20 2.48.1