Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cachestat: implement cachestat syscall

There is currently no good way to query the page cache state of large file
sets and directory trees. There is mincore(), but it scales poorly: the
kernel writes out a lot of bitmap data that userspace has to aggregate,
when the user really doesn not care about per-page information in that
case. The user also needs to mmap and unmap each file as it goes along,
which can be quite slow as well.

Some use cases where this information could come in handy:
* Allowing database to decide whether to perform an index scan or
direct table queries based on the in-memory cache state of the
index.
* Visibility into the writeback algorithm, for performance issues
diagnostic.
* Workload-aware writeback pacing: estimating IO fulfilled by page
cache (and IO to be done) within a range of a file, allowing for
more frequent syncing when and where there is IO capacity, and
batching when there is not.
* Computing memory usage of large files/directory trees, analogous to
the du tool for disk usage.

More information about these use cases could be found in the following
thread:

https://lore.kernel.org/lkml/20230315170934.GA97793@cmpxchg.org/

This patch implements a new syscall that queries cache state of a file and
summarizes the number of cached pages, number of dirty pages, number of
pages marked for writeback, number of (recently) evicted pages, etc. in a
given range. Currently, the syscall is only wired in for x86
architecture.

NAME
cachestat - query the page cache statistics of a file.

SYNOPSIS
#include <sys/mman.h>

struct cachestat_range {
__u64 off;
__u64 len;
};

struct cachestat {
__u64 nr_cache;
__u64 nr_dirty;
__u64 nr_writeback;
__u64 nr_evicted;
__u64 nr_recently_evicted;
};

int cachestat(unsigned int fd, struct cachestat_range *cstat_range,
struct cachestat *cstat, unsigned int flags);

DESCRIPTION
cachestat() queries the number of cached pages, number of dirty
pages, number of pages marked for writeback, number of evicted
pages, number of recently evicted pages, in the bytes range given by
`off` and `len`.

An evicted page is a page that is previously in the page cache but
has been evicted since. A page is recently evicted if its last
eviction was recent enough that its reentry to the cache would
indicate that it is actively being used by the system, and that
there is memory pressure on the system.

These values are returned in a cachestat struct, whose address is
given by the `cstat` argument.

The `off` and `len` arguments must be non-negative integers. If
`len` > 0, the queried range is [`off`, `off` + `len`]. If `len` ==
0, we will query in the range from `off` to the end of the file.

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.

Because the status of a page can change after cachestat() checks it
but before it returns to the application, the returned values may
contain stale information.

RETURN VALUE
On success, cachestat returns 0. On error, -1 is returned, and errno
is set to indicate the error.

ERRORS
EFAULT cstat or cstat_args points to an invalid address.

EINVAL invalid flags.

EBADF invalid file descriptor.

EOPNOTSUPP file descriptor is of a hugetlbfs file

[nphamcs@gmail.com: replace rounddown logic with the existing helper]
Link: https://lkml.kernel.org/r/20230504022044.3675469-1-nphamcs@gmail.com
Link: https://lkml.kernel.org/r/20230503013608.2431726-3-nphamcs@gmail.com
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Brian Foster <bfoster@redhat.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Nhat Pham and committed by
Andrew Morton
cf264e13 ffcb5f52

+207 -1
+1
arch/x86/entry/syscalls/syscall_32.tbl
··· 455 455 448 i386 process_mrelease sys_process_mrelease 456 456 449 i386 futex_waitv sys_futex_waitv 457 457 450 i386 set_mempolicy_home_node sys_set_mempolicy_home_node 458 + 451 i386 cachestat sys_cachestat
+1
arch/x86/entry/syscalls/syscall_64.tbl
··· 372 372 448 common process_mrelease sys_process_mrelease 373 373 449 common futex_waitv sys_futex_waitv 374 374 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 375 + 451 common cachestat sys_cachestat 375 376 376 377 # 377 378 # Due to a historical design error, certain syscalls are numbered differently
+5
include/linux/syscalls.h
··· 72 72 struct mount_attr; 73 73 struct landlock_ruleset_attr; 74 74 enum landlock_rule_type; 75 + struct cachestat_range; 76 + struct cachestat; 75 77 76 78 #include <linux/types.h> 77 79 #include <linux/aio_abi.h> ··· 1060 1058 asmlinkage long sys_set_mempolicy_home_node(unsigned long start, unsigned long len, 1061 1059 unsigned long home_node, 1062 1060 unsigned long flags); 1061 + asmlinkage long sys_cachestat(unsigned int fd, 1062 + struct cachestat_range __user *cstat_range, 1063 + struct cachestat __user *cstat, unsigned int flags); 1063 1064 1064 1065 /* 1065 1066 * Architecture-specific system calls
+4 -1
include/uapi/asm-generic/unistd.h
··· 886 886 #define __NR_set_mempolicy_home_node 450 887 887 __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node) 888 888 889 + #define __NR_cachestat 451 890 + __SYSCALL(__NR_cachestat, sys_cachestat) 891 + 889 892 #undef __NR_syscalls 890 - #define __NR_syscalls 451 893 + #define __NR_syscalls 452 891 894 892 895 /* 893 896 * 32 bit systems traditionally used different
+14
include/uapi/linux/mman.h
··· 4 4 5 5 #include <asm/mman.h> 6 6 #include <asm-generic/hugetlb_encode.h> 7 + #include <linux/types.h> 7 8 8 9 #define MREMAP_MAYMOVE 1 9 10 #define MREMAP_FIXED 2 ··· 41 40 #define MAP_HUGE_1GB HUGETLB_FLAG_ENCODE_1GB 42 41 #define MAP_HUGE_2GB HUGETLB_FLAG_ENCODE_2GB 43 42 #define MAP_HUGE_16GB HUGETLB_FLAG_ENCODE_16GB 43 + 44 + struct cachestat_range { 45 + __u64 off; 46 + __u64 len; 47 + }; 48 + 49 + struct cachestat { 50 + __u64 nr_cache; 51 + __u64 nr_dirty; 52 + __u64 nr_writeback; 53 + __u64 nr_evicted; 54 + __u64 nr_recently_evicted; 55 + }; 44 56 45 57 #endif /* _UAPI_LINUX_MMAN_H */
+10
init/Kconfig
··· 1771 1771 1772 1772 If unsure, say Y. 1773 1773 1774 + config CACHESTAT_SYSCALL 1775 + bool "Enable cachestat() system call" if EXPERT 1776 + default y 1777 + help 1778 + Enable the cachestat system call, which queries the page cache 1779 + statistics of a file (number of cached pages, dirty pages, 1780 + pages marked for writeback, (recently) evicted pages). 1781 + 1782 + If unsure say Y here. 1783 + 1774 1784 config DEBUG_RSEQ 1775 1785 default n 1776 1786 bool "Enabled debugging of rseq() system call" if EXPERT
+1
kernel/sys_ni.c
··· 299 299 COND_SYSCALL(migrate_pages); 300 300 COND_SYSCALL(move_pages); 301 301 COND_SYSCALL(set_mempolicy_home_node); 302 + COND_SYSCALL(cachestat); 302 303 303 304 COND_SYSCALL(perf_event_open); 304 305 COND_SYSCALL(accept4);
+171
mm/filemap.c
··· 22 22 #include <linux/mm.h> 23 23 #include <linux/swap.h> 24 24 #include <linux/swapops.h> 25 + #include <linux/syscalls.h> 25 26 #include <linux/mman.h> 26 27 #include <linux/pagemap.h> 27 28 #include <linux/file.h> ··· 58 57 #include <linux/buffer_head.h> /* for try_to_free_buffers */ 59 58 60 59 #include <asm/mman.h> 60 + 61 + #include "swap.h" 61 62 62 63 /* 63 64 * Shared mappings implemented 30.11.1994. It's not fully working yet, ··· 4122 4119 return try_to_free_buffers(folio); 4123 4120 } 4124 4121 EXPORT_SYMBOL(filemap_release_folio); 4122 + 4123 + #ifdef CONFIG_CACHESTAT_SYSCALL 4124 + /** 4125 + * filemap_cachestat() - compute the page cache statistics of a mapping 4126 + * @mapping: The mapping to compute the statistics for. 4127 + * @first_index: The starting page cache index. 4128 + * @last_index: The final page index (inclusive). 4129 + * @cs: the cachestat struct to write the result to. 4130 + * 4131 + * This will query the page cache statistics of a mapping in the 4132 + * page range of [first_index, last_index] (inclusive). The statistics 4133 + * queried include: number of dirty pages, number of pages marked for 4134 + * writeback, and the number of (recently) evicted pages. 4135 + */ 4136 + static void filemap_cachestat(struct address_space *mapping, 4137 + pgoff_t first_index, pgoff_t last_index, struct cachestat *cs) 4138 + { 4139 + XA_STATE(xas, &mapping->i_pages, first_index); 4140 + struct folio *folio; 4141 + 4142 + rcu_read_lock(); 4143 + xas_for_each(&xas, folio, last_index) { 4144 + unsigned long nr_pages; 4145 + pgoff_t folio_first_index, folio_last_index; 4146 + 4147 + if (xas_retry(&xas, folio)) 4148 + continue; 4149 + 4150 + if (xa_is_value(folio)) { 4151 + /* page is evicted */ 4152 + void *shadow = (void *)folio; 4153 + bool workingset; /* not used */ 4154 + int order = xa_get_order(xas.xa, xas.xa_index); 4155 + 4156 + nr_pages = 1 << order; 4157 + folio_first_index = round_down(xas.xa_index, 1 << order); 4158 + folio_last_index = folio_first_index + nr_pages - 1; 4159 + 4160 + /* Folios might straddle the range boundaries, only count covered pages */ 4161 + if (folio_first_index < first_index) 4162 + nr_pages -= first_index - folio_first_index; 4163 + 4164 + if (folio_last_index > last_index) 4165 + nr_pages -= folio_last_index - last_index; 4166 + 4167 + cs->nr_evicted += nr_pages; 4168 + 4169 + #ifdef CONFIG_SWAP /* implies CONFIG_MMU */ 4170 + if (shmem_mapping(mapping)) { 4171 + /* shmem file - in swap cache */ 4172 + swp_entry_t swp = radix_to_swp_entry(folio); 4173 + 4174 + shadow = get_shadow_from_swap_cache(swp); 4175 + } 4176 + #endif 4177 + if (workingset_test_recent(shadow, true, &workingset)) 4178 + cs->nr_recently_evicted += nr_pages; 4179 + 4180 + goto resched; 4181 + } 4182 + 4183 + nr_pages = folio_nr_pages(folio); 4184 + folio_first_index = folio_pgoff(folio); 4185 + folio_last_index = folio_first_index + nr_pages - 1; 4186 + 4187 + /* Folios might straddle the range boundaries, only count covered pages */ 4188 + if (folio_first_index < first_index) 4189 + nr_pages -= first_index - folio_first_index; 4190 + 4191 + if (folio_last_index > last_index) 4192 + nr_pages -= folio_last_index - last_index; 4193 + 4194 + /* page is in cache */ 4195 + cs->nr_cache += nr_pages; 4196 + 4197 + if (folio_test_dirty(folio)) 4198 + cs->nr_dirty += nr_pages; 4199 + 4200 + if (folio_test_writeback(folio)) 4201 + cs->nr_writeback += nr_pages; 4202 + 4203 + resched: 4204 + if (need_resched()) { 4205 + xas_pause(&xas); 4206 + cond_resched_rcu(); 4207 + } 4208 + } 4209 + rcu_read_unlock(); 4210 + } 4211 + 4212 + /* 4213 + * The cachestat(2) system call. 4214 + * 4215 + * cachestat() returns the page cache statistics of a file in the 4216 + * bytes range specified by `off` and `len`: number of cached pages, 4217 + * number of dirty pages, number of pages marked for writeback, 4218 + * number of evicted pages, and number of recently evicted pages. 4219 + * 4220 + * An evicted page is a page that is previously in the page cache 4221 + * but has been evicted since. A page is recently evicted if its last 4222 + * eviction was recent enough that its reentry to the cache would 4223 + * indicate that it is actively being used by the system, and that 4224 + * there is memory pressure on the system. 4225 + * 4226 + * `off` and `len` must be non-negative integers. If `len` > 0, 4227 + * the queried range is [`off`, `off` + `len`]. If `len` == 0, 4228 + * we will query in the range from `off` to the end of the file. 4229 + * 4230 + * The `flags` argument is unused for now, but is included for future 4231 + * extensibility. User should pass 0 (i.e no flag specified). 4232 + * 4233 + * Currently, hugetlbfs is not supported. 4234 + * 4235 + * Because the status of a page can change after cachestat() checks it 4236 + * but before it returns to the application, the returned values may 4237 + * contain stale information. 4238 + * 4239 + * return values: 4240 + * zero - success 4241 + * -EFAULT - cstat or cstat_range points to an illegal address 4242 + * -EINVAL - invalid flags 4243 + * -EBADF - invalid file descriptor 4244 + * -EOPNOTSUPP - file descriptor is of a hugetlbfs file 4245 + */ 4246 + SYSCALL_DEFINE4(cachestat, unsigned int, fd, 4247 + struct cachestat_range __user *, cstat_range, 4248 + struct cachestat __user *, cstat, unsigned int, flags) 4249 + { 4250 + struct fd f = fdget(fd); 4251 + struct address_space *mapping; 4252 + struct cachestat_range csr; 4253 + struct cachestat cs; 4254 + pgoff_t first_index, last_index; 4255 + 4256 + if (!f.file) 4257 + return -EBADF; 4258 + 4259 + if (copy_from_user(&csr, cstat_range, 4260 + sizeof(struct cachestat_range))) { 4261 + fdput(f); 4262 + return -EFAULT; 4263 + } 4264 + 4265 + /* hugetlbfs is not supported */ 4266 + if (is_file_hugepages(f.file)) { 4267 + fdput(f); 4268 + return -EOPNOTSUPP; 4269 + } 4270 + 4271 + if (flags != 0) { 4272 + fdput(f); 4273 + return -EINVAL; 4274 + } 4275 + 4276 + first_index = csr.off >> PAGE_SHIFT; 4277 + last_index = 4278 + csr.len == 0 ? ULONG_MAX : (csr.off + csr.len - 1) >> PAGE_SHIFT; 4279 + memset(&cs, 0, sizeof(struct cachestat)); 4280 + mapping = f.file->f_mapping; 4281 + filemap_cachestat(mapping, first_index, last_index, &cs); 4282 + fdput(f); 4283 + 4284 + if (copy_to_user(cstat, &cs, sizeof(struct cachestat))) 4285 + return -EFAULT; 4286 + 4287 + return 0; 4288 + } 4289 + #endif /* CONFIG_CACHESTAT_SYSCALL */