Skip to contentSkip to conversation

mm: fix cachestat NULL pgdat dereference on DAX mappings

PATCH New Henry Martin 2 participants 2 messages Nov 23 – Nov 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: fix cachestat NULL pgdat dereference on DAX mappings

filemap_cachestat() treats every xarray value entry as a workingset shadow. On DAX mappings, value entries are DAX exceptional entries:
same value bit, different bit layout (flags in the low bits, pfn above). Decoded as a shadow, a DAX entry yields a bogus node id; NODE_DATA() for it can be NULL, and lru_gen_test_recent() then dereferences it:

  Oops: general protection fault
  RIP: 0010:lru_gen_test_recent+0xde/0x300
  Call Trace:
   workingset_test_recent
   filemap_cachestat
   __x64_sys_cachestat

Any writable file on a DAX mount triggers this once an mmap write fault has populated entries.

DAX entries track present pages, not evicted folios. Count them as cached and skip the workingset decode, mirroring the dax_mapping() split in the invalidation paths in mm/truncate.c. PMD-order entries are counted as one page, as value entries carry no order information.

This issue was discovered by Tencent CodeBuddy Security.

Fixes cf264e1329fb ("cachestat: implement cachestat syscall")
---
 mm/filemap.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
mm/filemap.c +10 -0
index a1b2ca5b933c..caad0adde792 100644
@@ -4694,6 +4694,16 @@ static void filemap_cachestat(struct address_space *mapping,
 			void *shadow = (void *)folio;
 			bool workingset; /* not used */
 
+			/*
+			 * DAX exceptional entries track present pages, not
+			 * evicted folios.  They must not be decoded as
+			 * workingset shadows.
+			 */
+			if (dax_mapping(mapping)) {
+				cs->nr_cache += nr_pages;
+				goto resched;
+			}
+
 			cs->nr_evicted += nr_pages;
 
 #ifdef CONFIG_SWAP /* implies CONFIG_MMU */
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] mm: skipped dirty accounting for DAX entries in filemap_cachestat() - [Low] mm: inaccurate commit…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mm: skipped dirty accounting for DAX entries in filemap_cachestat()
- [Low] mm: inaccurate commit message claim on PMD-order DAX entry count --

commit 00a07291e64203188f9848c4b4c587fb6f5215ca
Author: Henry Martin <[email protected]>

mm: fix cachestat NULL pgdat dereference on DAX mappings

This commit avoids decoding DAX exceptional value entries as workingset shadows in filemap_cachestat(), counting them as cached pages instead.

Quoted 2 lines

PMD-order entries
are counted as one page, as value entries carry no order information.

[Severity: Low]
Is this statement in the commit message accurate?

dax_iomap_pmd_fault() and dax_insert_pfn_mkwrite() insert PMD entries with XA_STATE_ORDER(..., PMD_ORDER), and filemap_cachestat() derives nr_pages from xas_get_order(&xas) before checking xa_is_value(folio), so PMD-order DAX entries are counted across all pages they cover.

Quoted 11 lines
mm/filemap.c +3 -0
index dc0ceaa58a0f7..5d12230de32d9 100644
@@ -4706,6 +4706,16 @@ static void filemap_cachestat(struct address_space *mapping,
 			void *shadow = (void *)folio;
 			bool workingset; /* not used */
 
+			/*
+			 * DAX exceptional entries track present pages, not
+			 * evicted folios.  They must not be decoded as

mm/filemap.c static void filemap_cachestat(struct address_space *mapping, · line 4721

Quoted 10 lines
mm/filemap.c +7 -0
+			 * workingset shadows.
+			 */
+			if (dax_mapping(mapping)) {
+				cs->nr_cache += nr_pages;
+				goto resched;
+			}
+
 			cs->nr_evicted += nr_pages;
 
 #ifdef CONFIG_SWAP /* implies CONFIG_MMU */

[Severity: Medium]
Does jumping to resched here skip cs->nr_dirty accounting for dirty DAX entries?

DAX marks dirty exceptional entries with PAGECACHE_TAG_DIRTY in mapping->i_pages (via dax_insert_entry() and dax_insert_pfn_mkwrite()) until writeback flushes CPU caches. Because the in-cache path below only queries &xas and never dereferences folio, guarding the evicted branch with "if (xa_is_value(folio) && !dax_mapping(mapping))" would avoid duplicating cs->nr_cache += nr_pages and preserve cs->nr_dirty reporting via xas_get_mark(&xas, PAGECACHE_TAG_DIRTY).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1