Skip to contentSkip to conversation

mm: report all-zero cachestat for DAX mappings

PATCH In review Henry Martin 2 participants 2 messages Nov 25

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

PATCH v2 1/1 mm: report all-zero cachestat for 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 mappings have no page cache, so none of the cachestat fields make sense for them. Skip the xarray walk for them and report all zeros, and document the behaviour in the syscall's kernel-doc.

This issue was discovered by Tencent CodeBuddy Security.

Fixes cf264e1329fb ("cachestat: implement cachestat syscall")

---
Changes in v2:
- Return all zeros for DAX mappings instead of counting DAX entries as cached, per Jan Kara's suggestion (Nhat Pham agreed). This also drops the dirty-accounting gap and the inaccurate PMD-order claim from v1.
- Document the DAX behaviour in the syscall's kernel-doc.

 mm/filemap.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
mm/filemap.c +6 -2
index a1b2ca5b933cb..4f6fd1f4caedb 100644
@@ -4779,7 +4779,8 @@ static inline bool can_do_cachestat(struct file *f)
  * The `flags` argument is unused for now, but is included for future
  * extensibility. User should pass 0 (i.e no flag specified).
  *
- * Currently, hugetlbfs is not supported.
+ * Currently, hugetlbfs is not supported.  DAX mappings have no page
+ * cache, so cachestat() always reports all zeros for them.
  *
  * Because the status of a page can change after cachestat() checks it
  * but before it returns to the application, the returned values may
@@ -4824,7 +4825,10 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd,
 		csr.len == 0 ? ULONG_MAX : (csr.off + csr.len - 1) >> PAGE_SHIFT;
 	memset(&cs, 0, sizeof(struct cachestat));
 	mapping = fd_file(f)->f_mapping;
-	filemap_cachestat(mapping, first_index, last_index, &cs);
+
+	/* DAX mappings have no page cache; report all zeros. */
+	if (!dax_mapping(mapping))
+		filemap_cachestat(mapping, first_index, last_index, &cs);
 
 	if (copy_to_user(cstat, &cs, sizeof(struct cachestat)))
 		return -EFAULT;
Signature · 1 line
2.53.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 17 lines · Henry Martin

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.

Quoted 8 lines

DAX mappings have no page cache, so none of the cachestat fields make sense for them. Skip the xarray walk for them and report all zeros, and document the behaviour in the syscall's kernel-doc.

This issue was discovered by Tencent CodeBuddy Security.

Fixes cf264e1329fb ("cachestat: implement cachestat syscall")

Looks good. Feel free to add:

Honza

Quoted 43 lines

---
Changes in v2:
- Return all zeros for DAX mappings instead of counting DAX entries as cached, per Jan Kara's suggestion (Nhat Pham agreed). This also drops the dirty-accounting gap and the inaccurate PMD-order claim from v1.
- Document the DAX behaviour in the syscall's kernel-doc.

 mm/filemap.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
mm/filemap.c +6 -2
index a1b2ca5b933cb..4f6fd1f4caedb 100644
@@ -4779,7 +4779,8 @@ static inline bool can_do_cachestat(struct file *f)
  * The `flags` argument is unused for now, but is included for future
  * extensibility. User should pass 0 (i.e no flag specified).
  *
- * Currently, hugetlbfs is not supported.
+ * Currently, hugetlbfs is not supported.  DAX mappings have no page
+ * cache, so cachestat() always reports all zeros for them.
  *
  * Because the status of a page can change after cachestat() checks it
  * but before it returns to the application, the returned values may
@@ -4824,7 +4825,10 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd,
 		csr.len == 0 ? ULONG_MAX : (csr.off + csr.len - 1) >> PAGE_SHIFT;
 	memset(&cs, 0, sizeof(struct cachestat));
 	mapping = fd_file(f)->f_mapping;
-	filemap_cachestat(mapping, first_index, last_index, &cs);
+
+	/* DAX mappings have no page cache; report all zeros. */
+	if (!dax_mapping(mapping))
+		filemap_cachestat(mapping, first_index, last_index, &cs);
 
 	if (copy_to_user(cstat, &cs, sizeof(struct cachestat)))
 		return -EFAULT;
Signature · 1 line
2.53.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR