From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 500CE4189B9 for ; Tue, 21 Jul 2026 09:45:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784627151; cv=none; b=XRTBPSEY0nGvWD6/jy+bUHWamnMZs6SBRV5uF3ULSfLHr13nHoisG7DAWd5VNcDoVowdwBzpG++9Q79vrGbYSviWeMtgVMK5Y7Z0M+2Qjq+6nFzMQ+XDJz/29ACfe0IU8EJ8HH9XaaDPshdibD1HAPw+0PcS+EQW1BwMro1XIGY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784627151; c=relaxed/simple; bh=Sqx9Im1HASTcqkv7NqQM1Fv6UhjQ0/wjErEc7Em/n6c=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=t1VmHx0/f+veJaUpKlh6khFXVnex6P7nCfpAHDkEnIJJp3WfcUn19I5NwdCH7d3T8vNdd9RIjuqN2ikNHg3yUMOhWp4vTyHDKtL2DDUVujhjmXAkzvRRsoZjghSkrz/9TGGURw0I2ogSAV946Cx9dCu0wYAFzxUoU0lUXnxuZiE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VMdEr/Ij; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VMdEr/Ij" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-38e69bdb0fcso1865498a91.1 for ; Tue, 21 Jul 2026 02:45:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784627148; x=1785231948; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=BlAqH8OpzhDNUGaMI/yhdZ5tSuc+jcqv1mstlCTQxyA=; b=VMdEr/Ij/kwnE6Wh6D/U1o3OkPgEasGhl2J3lAcrTtPETKJePUI0+jhILjXDmfsZMh gHInREr6cPicg/b7SQuPl1R1E/RkLgPJq5mvBZjjwPgMcEsQrwQYX8rUOqKi84KgNWT3 JYJaSVbJ1/J3LdTbxrat6Q3vEsmal9aISmSm4zsR8gCMvW7NFDhFuHjVWktmLmVhZ9hL 6cOBg8/TRyJqlGGrHuCoTmSXaJ9JhFCIVTojyx5h369lS8vB75gsJ9oyvELTHV/7Hryp cr+ictcuOcheCXGMhoWXDf4ve7On4nmoQlwW6+SfMpawdSgZ1lyoTS/243jPOx5X0wWw ZjtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784627148; x=1785231948; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=BlAqH8OpzhDNUGaMI/yhdZ5tSuc+jcqv1mstlCTQxyA=; b=Yj52NiBOU1xw+uBTq9B/hflMDzN8dD+ZdTTfOQ2J9Ij0O3KS+B0kJQ5QaEVapgnPCZ mJpVnPdCLs5jLCGq1VE9sB9yKpDZJScJAH/FFWx0vJ5NXya45LMl461x1+0DhIvEGk0v o6KBg4COHgLmlQG130KOKVEn4x32DDhOx3qZuhOMxMb5EwvyIZcqASNZ8yhL98ixRstv B1soBpkyAIFaMYYfbcI4Em/4lGVzd5Ghhn0R7+C442PuCKYF616ETjVU4OjJ2fGHWkGE dG4eX2SFcGqqmhux+baCw7nSj6MQWuBEwxMyuBuK2jb51QMtCsOw66bFC2mW90nEtNrq j7Sg== X-Forwarded-Encrypted: i=1; AHgh+RolOO4P5m47BxISBAzls7qu52Dv+xkj3tmOqR7ZKRi3OJ7C5/aWrZ1EYZdNnIFa/Cki47PItE/d6VtOo6w=@vger.kernel.org X-Gm-Message-State: AOJu0YyoOGmIoxK56cDdsxVOh53ZzYJIhcunXPf3LgmbzN9MhwHldh9V 8VGqWQ0xYyGvwm3plOWKxGmGuIQ8X6k+dAeUpMQAceqFg0tBlftXMrEs X-Gm-Gg: AfdE7cnkRrC215+QbZO0BTrouxAq51fydCbDdHg4azJcGUe/8uew3WRDda2mpJRcs4X YsThyk3tk3lSFThcDxIrsr8Baott8LOoaTfFVbslJJWFUUHlF5b9y1U6IlHzwUzoOHIZEegnf90 HZKpmAtkQkqL5jDBswjm6q42glYcWOFGaZRgi68ilmJDP5eYOAQInmI2GhmWQmcbUzikXNZPeUP 2tpSILubJxj5yAJkwqliQcnL2CoSTDvzjwh7AOZusVKfAyK42ln36f2d+khaVe/42GXrRGDRe4m NAetf4+ztqlSp06OWEWl+YFAxxTaNaiiG821QDUVBxOrqaGiHOpps6mmWRQOnU/lWU+BgV7Trsb 0jqqrGXak6EYAA2IjzUVj273lsp1ZTbZMAu80nCZvtVCtEB0eJg9UWTOeqJbPPu+Lr2sRRZJvtF 7IhPw/XeHQWOM8owvJkGRMkJ7WmZFK2zqqiw== X-Received: by 2002:a05:6a20:e292:b0:3bf:63af:859 with SMTP id adf61e73a8af0-3c3ad973443mr18673082637.45.1784627147515; Tue, 21 Jul 2026 02:45:47 -0700 (PDT) Received: from fedora ([2601:644:937c:6c90:6d4e:7b2d:4a39:fb0c]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3142a1bb8a4sm49625478eec.14.2026.07.21.02.45.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jul 2026 02:45:46 -0700 (PDT) Date: Tue, 21 Jul 2026 02:45:43 -0700 From: Vishal Moola To: "Lorenzo Stoakes (ARM)" Cc: Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , "Mike Rapoport (Microsoft)" , Jason Gunthorpe , Lu Baolu , Andrew Morton , David Hildenbrand , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Kiryl Shutsemau , iommu@lists.linux.dev, Kevin Tian , stable@vger.kernel.org Subject: Re: [PATCH] x86/mm/pat: allocate split page tables as kernel page tables Message-ID: References: <20260720-fix-cpa-kernel-pagetables-v1-1-0766e782cefe@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Tue, Jul 21, 2026 at 08:43:51AM +0100, Lorenzo Stoakes (ARM) wrote: > On Mon, Jul 20, 2026 at 01:03:57PM -0700, Vishal Moola wrote: > > On Mon, Jul 20, 2026 at 01:01:00PM -0700, Vishal Moola wrote: > > > On Mon, Jul 20, 2026 at 10:27:29AM +0100, Lorenzo Stoakes (ARM) wrote: > > > > When splitting a large page in CPA in __split_large_page() we allocate a > > > > PTE directly without going through the standard page table allocation > > > > routines such as pte_alloc_one_kernel(). > > > > > > > > This means the page table constructor is never called nor is the page table > > > > marked as a kernel page table. > > > > > > > > The former results in the folio associated with the page table not being > > > > marked as a page table (__pagetable_ctor() is never called thus neither is > > > > __folio_set_pgtable()) nor are statistics updated to reflect > > > > it (lruvec_stat_add_folio() is never called). > > > > > > > > The latter issue of failing to mark the page table as a kernel page > > > > table (ptdesc_set_kernel() is never called) is far more problematic. > > > > > > > > Since commit 5ba2f0a15564 ("mm: introduce deferred freeing for kernel page > > > > tables") kernel page table freeing has been batched and since the > > > > subsequent commit e37d5a2d60a3 ("iommu/sva: invalidate stale IOTLB entries > > > > for kernel address space") IOTLB cache entries for kernel page tables have > > > > been invalidated upon being freed. > > > > > > > > Since split page tables are freed without this invalidation, the IOTLB can > > > > contain stale entries for them. > > > > > > > > Resolve the issue by using the ordinary PTE allocation API at split time. > > > > > > > > This results in these kernel page tables invoking a page table constructor, > > > > and thus requires a page table destructor. > > > > > > > > Since we cannot assume one is always present (early allocated direct map > > > > page tables are not marked as such), we conditionally call > > > > pagetable_dtor_free() if the PG_table folio flag for the ptdesc is set, > > > > otherwise we free the page table via pagetable_free(). > > > > > > > > Regardless of which path is taken page tables marked as kernel page tables, > > > > which now includes split page tables, take the correct route through > > > > pagetable_free_kernel(). > > > > > > > > There is a user-visible side effect in that split page tables will appear > > > > in nr_page_table_pages in /proc/vmstat (as do other kernel page tables > > > > allocated after early boot), however this is a positive change. > > > > > > > > This issue started being markedly problematic after commit > > > > 5ba2f0a15564 ("mm: introduce deferred freeing for kernel page tables") so > > > > choose this as the Fixes target. > > > > > > > > Fixes: 5ba2f0a15564 ("mm: introduce deferred freeing for kernel page tables") > > > > Cc: stable@vger.kernel.org > > > > Signed-off-by: Lorenzo Stoakes (ARM) > > > > --- > > > > arch/x86/mm/pat/set_memory.c | 21 ++++++++++++--------- > > > > 1 file changed, 12 insertions(+), 9 deletions(-) > > > > > > > > diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c > > > > index 301fb9e77d91..a67ca33b9dd1 100644 > > > > --- a/arch/x86/mm/pat/set_memory.c > > > > +++ b/arch/x86/mm/pat/set_memory.c > > > > @@ -439,7 +439,11 @@ static void __cpa_collapse_large_pages(struct cpa_data *cpa) > > > > > > > > list_for_each_entry_safe(ptdesc, tmp, &pgtables, pt_list) { > > > > list_del(&ptdesc->pt_list); > > > > - pagetable_free(ptdesc); > > > > + > > > > + if (folio_test_pgtable(ptdesc_folio(ptdesc))) > > > > + pagetable_dtor_free(ptdesc); > > > > + else > > > > + pagetable_free(ptdesc); > > > > > > Lets not introduce more folio-ptdesc crossovers, we're trying to get > > > rid of them :) > > > > > > I believe pagetable_dtor_free() should do what you're looking for on its > > > own anyway. > > > > Actually, looking at it closer, maybe not because of the conditional > > portion? But that makes me think it might be better to just replace the > > ptdesc_clear_kernel() with pagetable_dtor() in the free function... > > Well some kernel page tables are still allocated without ctor (early allocated > direct map for isntance), and if you did pagetable_dtor_free() it > unconditionally calls pagetable_dtor(). > > The ptlock_free() and __folio_clear_pgtable() there would be harmelss (no locks > assigned for kernel page table, and if PG_table never set clearing it is a noop) > but the lruvec_stat_sub_folio() would cause an unbalanced decrement of > nr_page_table_pages. Gotcha, thanks for the explanation :) > It sucks, but until everything is updated to call the ctor we have to do it this > way :>) Yeah that makes sense. Although I'd rather see the condition as: if(PageTable(ptdesc_page(...))) We really shouldn't be calling ptdesc_folio() anywhere anymore.