From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 638682C21DD; Thu, 18 Dec 2025 09:49:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766051373; cv=none; b=Lfg9XJ9gdgHcL2zzyFL/i9Osrx4zuBTwbenatTjfSpVfg0FneHoUmrEWin4KcKS6/hrbOBT2mrgPX09+05vMpFmGAR7k6BZgAVqUOVAdy1UMvdCXVZDHqhJm8I3sr05QM7BaSh17ct70W5PAhT1OHfXQ/bnkpg9BZkffhiAdIKA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766051373; c=relaxed/simple; bh=jpct4wgiQvvz63Cwe2t0WKmpu8qE8JWTj+D4fEqN3KU=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=lZGq+AacwAn/X64H5NUy05grkPr+3YVQEU0cgS/mgw+VLgYwSMsxaZZF+Zucze20MeZbpJkG0d6FDzJQRpQAtF6gp9FGslx6GAuPWqz+Wi1LK55Nx0Nzpn8BzPJu38H4Ug+5rP7JKPAfNRsbGn/zx+o7yqEfn18dWlSMfebGywo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=i+zreEJo; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="i+zreEJo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76F25C4CEFB; Thu, 18 Dec 2025 09:49:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1766051372; bh=jpct4wgiQvvz63Cwe2t0WKmpu8qE8JWTj+D4fEqN3KU=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=i+zreEJolTQBByW8JlPjfJ3Z1VMPes2Pd7PeJAb/lYS9S+kjePXS6fxK/1oiCfHLA SApDHO3FssA1sTS4F0w9Fy38cvl6WxkWVC0byQPuPQVMzC/aSU38p7T3YrTzKe/lEq +t3yiGig3MducPpc0NbGREP5v9YB9iZndsqe1W4uJCR6ayBC+wFjG6VFCOs/Ruqnwr /aqNqVbKxX2atQFftfDEC4z1dyMeo2hNdSyY1J2kx4C/dxYP0m2u9xYNOYoJOhOi8v V67YJYBgk8qBspVZuXfk+ga0SVCqlyGMbpJhXfGQGXvZ8Lw12uC/zGimGfkUK+YzIy Z5I+jzlJ5xCgg== Message-ID: Date: Thu, 18 Dec 2025 10:49:26 +0100 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 v3 08/22] mm: Allow page table accessors to be non-idempotent To: Ryan Roberts , Samuel Holland , Palmer Dabbelt , Paul Walmsley , linux-riscv@lists.infradead.org, Andrew Morton , linux-mm@kvack.org Cc: devicetree@vger.kernel.org, Suren Baghdasaryan , linux-kernel@vger.kernel.org, Mike Rapoport , Michal Hocko , Conor Dooley , Lorenzo Stoakes , Krzysztof Kozlowski , Alexandre Ghiti , Emil Renner Berthing , Rob Herring , Vlastimil Babka , "Liam R . Howlett" References: <20251113014656.2605447-1-samuel.holland@sifive.com> <20251113014656.2605447-9-samuel.holland@sifive.com> From: "David Hildenbrand (Red Hat)" Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 12/11/25 14:59, Ryan Roberts wrote: > On 11/12/2025 00:33, Samuel Holland wrote: >> On 2025-11-28 2:47 AM, David Hildenbrand (Red Hat) wrote: >>> On 11/27/25 17:57, Ryan Roberts wrote: >>>> On 13/11/2025 01:45, Samuel Holland wrote: >>>>> Currently, some functions such as pte_offset_map() are passed both >>>>> pointers to hardware page tables, and pointers to previously-read PMD >>>>> entries on the stack. To ensure correctness in the first case, these >>>>> functions must use the page table accessor function (pmdp_get()) to >>>>> dereference the supplied pointer. However, this means pmdp_get() is >>>>> called twice in the second case. This double call must be avoided if >>>>> pmdp_get() applies some non-idempotent transformation to the value. >>>>> >>>>> Avoid the double transformation by calling set_pmd() on the stack >>>>> variables where necessary to keep set_pmd()/pmdp_get() calls balanced. >>>> >>>> I don't think this is a good solution. >>> >>> Agreed, >>> >>>     set_pmd(&pmd, pmd); >>> >>> is rather horrible. >> I agree that this patch is ugly. The only way I see to avoid code like this is >> to refactor (or duplicate) the functions so no function takes pointers to both >> hardware page tables and on-stack page table entries. Is that sort of >> refactoring the right direction to go for v4? > > From a quick look at the code, I think that some cases are solvable by > refactoring to pass the value instead of the pointer, and leave it to the higher > level decide how to read the value from the pointer - it knows if it is pointing > to HW pgtable or if it's a (e.g) stack value. > > But the more I look at the code, the more instances I find where pointers to > stack variables are being passed to arch pgtable helpers as if they are HW > pgtable entry pointers. (Mainly pmd level). > > I wonder if we need to bite the bullet and explicitly separate the types? At > each level, we have: > > 1. page table entry value > 2. pointer to page table entry _value_ (e.g. pointer to pXX_t on stack) > 3. pointer to page table entry in HW pgtable > > Today, 1 is represented by pte_t, pmd_t, etc. 2 and 3 are represented by the > same type; pte_t*, pmd_t*, etc. > > If we create a new type for 3, it will both document and enforce when type 2 or > type 3 is required. > > e.g: > > // pte_t: defined by arch. > typedef unsigned long pte_t; > > // ptep_t: new opaque type that can't be dereferenced. > struct __ptep_t; > typedef struct __ptep_t *ptep_t; This is what I had in mind when we last discussed this topic and I suggested a way forward to not play whack-a-mole with new users that do *ptep showing up. Agreed that we ideally indicate that this is a HW PTE pointer that must be de-referenced through ptep_get() or similar. (maybe we'd find a new name for this set of functions). I talked to Samuel at LPC, pointing him at the way XEN-pv implemented support for changing PFNs in ptes. We might not need all of that rework to move forward with the risc-v change. Having that said, I also agree that this would be a cleanup worth having (which will result in quite a bit of churn :) ). So if someone wants to bump up the patch count, speak now. -- Cheers David