[v5,2/3] cachestat: implement cachestat syscall
Commit Message
Implement 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.
NAME
cachestat - query the page cache statistics of a file.
SYNOPSIS
#include <sys/mman.h>
struct cachestat {
__u64 nr_cache;
__u64 nr_dirty;
__u64 nr_writeback;
__u64 nr_evicted;
__u64 nr_recently_evicted;
};
int cachestat(unsigned int fd, off_t off, size_t len,
size_t cstat_size, 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.
`cstat_size` allows users to obtain partial results. The syscall
will copy the first `csstat_size` bytes to the specified userspace
memory. `cstat_size` must be a non-negative value that is no larger
than the current size of the cachestat struct.
The `flags` argument is unused for now, but is included for future
extensibility. User should pass 0 (i.e no flag specified).
RETURN VALUE
On success, cachestat returns 0. On error, -1 is returned, and errno
is set to indicate the error.
ERRORS
EFAULT cstat points to an invalid address.
EINVAL invalid `cstat_size` or `flags`
EBADF invalid file descriptor.
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
arch/alpha/kernel/syscalls/syscall.tbl | 1 +
arch/arm/tools/syscall.tbl | 1 +
arch/ia64/kernel/syscalls/syscall.tbl | 1 +
arch/m68k/kernel/syscalls/syscall.tbl | 1 +
arch/microblaze/kernel/syscalls/syscall.tbl | 1 +
arch/parisc/kernel/syscalls/syscall.tbl | 1 +
arch/powerpc/kernel/syscalls/syscall.tbl | 1 +
arch/s390/kernel/syscalls/syscall.tbl | 1 +
arch/sh/kernel/syscalls/syscall.tbl | 1 +
arch/sparc/kernel/syscalls/syscall.tbl | 1 +
arch/x86/entry/syscalls/syscall_32.tbl | 1 +
arch/x86/entry/syscalls/syscall_64.tbl | 1 +
arch/xtensa/kernel/syscalls/syscall.tbl | 1 +
include/linux/fs.h | 3 +
include/linux/syscalls.h | 3 +
include/uapi/asm-generic/unistd.h | 5 +-
include/uapi/linux/mman.h | 9 ++
init/Kconfig | 10 ++
kernel/sys_ni.c | 1 +
mm/filemap.c | 143 ++++++++++++++++++++
20 files changed, 186 insertions(+), 1 deletion(-)
Comments
On Wed, Jan 04, 2023 at 03:11:26PM -0800, Nhat Pham wrote:
> Implement 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.
>
> NAME
> cachestat - query the page cache statistics of a file.
>
> SYNOPSIS
> #include <sys/mman.h>
>
> struct cachestat {
> __u64 nr_cache;
> __u64 nr_dirty;
> __u64 nr_writeback;
> __u64 nr_evicted;
> __u64 nr_recently_evicted;
> };
>
> int cachestat(unsigned int fd, off_t off, size_t len,
> size_t cstat_size, 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.
>
> `cstat_size` allows users to obtain partial results. The syscall
> will copy the first `csstat_size` bytes to the specified userspace
> memory. `cstat_size` must be a non-negative value that is no larger
> than the current size of the cachestat struct.
>
> The `flags` argument is unused for now, but is included for future
> extensibility. User should pass 0 (i.e no flag specified).
>
> RETURN VALUE
> On success, cachestat returns 0. On error, -1 is returned, and errno
> is set to indicate the error.
>
> ERRORS
> EFAULT cstat points to an invalid address.
>
> EINVAL invalid `cstat_size` or `flags`
>
> EBADF invalid file descriptor.
>
> Signed-off-by: Nhat Pham <nphamcs@gmail.com>
> ---
> arch/alpha/kernel/syscalls/syscall.tbl | 1 +
> arch/arm/tools/syscall.tbl | 1 +
> arch/ia64/kernel/syscalls/syscall.tbl | 1 +
> arch/m68k/kernel/syscalls/syscall.tbl | 1 +
> arch/microblaze/kernel/syscalls/syscall.tbl | 1 +
> arch/parisc/kernel/syscalls/syscall.tbl | 1 +
> arch/powerpc/kernel/syscalls/syscall.tbl | 1 +
> arch/s390/kernel/syscalls/syscall.tbl | 1 +
> arch/sh/kernel/syscalls/syscall.tbl | 1 +
> arch/sparc/kernel/syscalls/syscall.tbl | 1 +
> arch/x86/entry/syscalls/syscall_32.tbl | 1 +
> arch/x86/entry/syscalls/syscall_64.tbl | 1 +
> arch/xtensa/kernel/syscalls/syscall.tbl | 1 +
> include/linux/fs.h | 3 +
> include/linux/syscalls.h | 3 +
> include/uapi/asm-generic/unistd.h | 5 +-
> include/uapi/linux/mman.h | 9 ++
> init/Kconfig | 10 ++
> kernel/sys_ni.c | 1 +
> mm/filemap.c | 143 ++++++++++++++++++++
> 20 files changed, 186 insertions(+), 1 deletion(-)
>
...
> diff --git a/mm/filemap.c b/mm/filemap.c
> index 08341616ae7a..d70d47b20700 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
...
> @@ -3949,3 +3953,142 @@ bool filemap_release_folio(struct folio *folio, gfp_t gfp)
> return try_to_free_buffers(folio);
> }
> EXPORT_SYMBOL(filemap_release_folio);
...
> +#ifdef CONFIG_CACHESTAT_SYSCALL
> +/*
> + * The cachestat(5) system call.
> + *
> + * cachestat() returns the page cache statistics of a file in the
> + * bytes range specified by `off` and `len`: number of cached pages,
> + * number of dirty pages, number of pages marked for writeback,
> + * number of evicted pages, and number of recently evicted pages.
> + *
> + * 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.
> + *
> + * `off` and `len` 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.
> + *
> + * `cstat_size` allows users to obtain partial results. The syscall
> + * will copy the first `csstat_size` bytes to the specified userspace
> + * memory. It also makes the cachestat struct extensible - new fields
> + * can be added in the future without breaking existing usage.
> + * `cstat_size` must be a non-negative value that is no larger than
> + * the current size of the cachestat struct.
> + *
> + * The `flags` argument is unused for now, but is included for future
> + * extensibility. User should pass 0 (i.e no flag specified).
> + *
> + * 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 values:
> + * zero - success
> + * -EFAULT - cstat points to an illegal address
> + * -EINVAL - invalid arguments
> + * -EBADF - invalid file descriptor
> + */
> +SYSCALL_DEFINE6(cachestat, unsigned int, fd, off_t, off, size_t, len,
> + size_t, cstat_size, struct cachestat __user *, cstat,
> + unsigned int, flags)
> +{
> + struct fd f = fdget(fd);
> + struct address_space *mapping;
> + struct cachestat cs;
> + pgoff_t first_index = off >> PAGE_SHIFT;
> + pgoff_t last_index =
> + len == 0 ? ULONG_MAX : (off + len - 1) >> PAGE_SHIFT;
> +
> + if (off < 0 || cstat_size > sizeof(struct cachestat) || flags != 0)
> + return -EINVAL;
> +
> + if (!f.file)
> + return -EBADF;
> +
It looks like we miss an fdput() before returning via the above error
checks.
The only other thing that stands out as a bit odd to me is the
cstat_size check and associated ability to return a partial cachestat
struct. Do other syscalls do anything like that? I'd think we'd want to
ensure we always at least return a fully populated cachestat struct,
even if it happened to be an old/compat version if the size does ever
increase. Hm?
Brian
> + memset(&cs, 0, sizeof(struct cachestat));
> + mapping = f.file->f_mapping;
> + filemap_cachestat(mapping, first_index, last_index, &cs);
> + fdput(f);
> +
> + if (copy_to_user(cstat, &cs, cstat_size))
> + return -EFAULT;
> +
> + return 0;
> +}
> +#endif /* CONFIG_CACHESTAT_SYSCALL */
> --
> 2.30.2
>
On Tue, Jan 10, 2023 at 6:21 AM Brian Foster <bfoster@redhat.com> wrote:
>
> On Wed, Jan 04, 2023 at 03:11:26PM -0800, Nhat Pham wrote:
> > Implement 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.
> >
> > NAME
> > cachestat - query the page cache statistics of a file.
> >
> > SYNOPSIS
> > #include <sys/mman.h>
> >
> > struct cachestat {
> > __u64 nr_cache;
> > __u64 nr_dirty;
> > __u64 nr_writeback;
> > __u64 nr_evicted;
> > __u64 nr_recently_evicted;
> > };
> >
> > int cachestat(unsigned int fd, off_t off, size_t len,
> > size_t cstat_size, 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.
> >
> > `cstat_size` allows users to obtain partial results. The syscall
> > will copy the first `csstat_size` bytes to the specified userspace
> > memory. `cstat_size` must be a non-negative value that is no larger
> > than the current size of the cachestat struct.
> >
> > The `flags` argument is unused for now, but is included for future
> > extensibility. User should pass 0 (i.e no flag specified).
> >
> > RETURN VALUE
> > On success, cachestat returns 0. On error, -1 is returned, and errno
> > is set to indicate the error.
> >
> > ERRORS
> > EFAULT cstat points to an invalid address.
> >
> > EINVAL invalid `cstat_size` or `flags`
> >
> > EBADF invalid file descriptor.
> >
> > Signed-off-by: Nhat Pham <nphamcs@gmail.com>
> > ---
> > arch/alpha/kernel/syscalls/syscall.tbl | 1 +
> > arch/arm/tools/syscall.tbl | 1 +
> > arch/ia64/kernel/syscalls/syscall.tbl | 1 +
> > arch/m68k/kernel/syscalls/syscall.tbl | 1 +
> > arch/microblaze/kernel/syscalls/syscall.tbl | 1 +
> > arch/parisc/kernel/syscalls/syscall.tbl | 1 +
> > arch/powerpc/kernel/syscalls/syscall.tbl | 1 +
> > arch/s390/kernel/syscalls/syscall.tbl | 1 +
> > arch/sh/kernel/syscalls/syscall.tbl | 1 +
> > arch/sparc/kernel/syscalls/syscall.tbl | 1 +
> > arch/x86/entry/syscalls/syscall_32.tbl | 1 +
> > arch/x86/entry/syscalls/syscall_64.tbl | 1 +
> > arch/xtensa/kernel/syscalls/syscall.tbl | 1 +
> > include/linux/fs.h | 3 +
> > include/linux/syscalls.h | 3 +
> > include/uapi/asm-generic/unistd.h | 5 +-
> > include/uapi/linux/mman.h | 9 ++
> > init/Kconfig | 10 ++
> > kernel/sys_ni.c | 1 +
> > mm/filemap.c | 143 ++++++++++++++++++++
> > 20 files changed, 186 insertions(+), 1 deletion(-)
> >
> ...
> > diff --git a/mm/filemap.c b/mm/filemap.c
> > index 08341616ae7a..d70d47b20700 100644
> > --- a/mm/filemap.c
> > +++ b/mm/filemap.c
> ...
> > @@ -3949,3 +3953,142 @@ bool filemap_release_folio(struct folio *folio, gfp_t gfp)
> > return try_to_free_buffers(folio);
> > }
> > EXPORT_SYMBOL(filemap_release_folio);
> ...
> > +#ifdef CONFIG_CACHESTAT_SYSCALL
> > +/*
> > + * The cachestat(5) system call.
> > + *
> > + * cachestat() returns the page cache statistics of a file in the
> > + * bytes range specified by `off` and `len`: number of cached pages,
> > + * number of dirty pages, number of pages marked for writeback,
> > + * number of evicted pages, and number of recently evicted pages.
> > + *
> > + * 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.
> > + *
> > + * `off` and `len` 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.
> > + *
> > + * `cstat_size` allows users to obtain partial results. The syscall
> > + * will copy the first `csstat_size` bytes to the specified userspace
> > + * memory. It also makes the cachestat struct extensible - new fields
> > + * can be added in the future without breaking existing usage.
> > + * `cstat_size` must be a non-negative value that is no larger than
> > + * the current size of the cachestat struct.
> > + *
> > + * The `flags` argument is unused for now, but is included for future
> > + * extensibility. User should pass 0 (i.e no flag specified).
> > + *
> > + * 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 values:
> > + * zero - success
> > + * -EFAULT - cstat points to an illegal address
> > + * -EINVAL - invalid arguments
> > + * -EBADF - invalid file descriptor
> > + */
> > +SYSCALL_DEFINE6(cachestat, unsigned int, fd, off_t, off, size_t, len,
> > + size_t, cstat_size, struct cachestat __user *, cstat,
> > + unsigned int, flags)
> > +{
> > + struct fd f = fdget(fd);
> > + struct address_space *mapping;
> > + struct cachestat cs;
> > + pgoff_t first_index = off >> PAGE_SHIFT;
> > + pgoff_t last_index =
> > + len == 0 ? ULONG_MAX : (off + len - 1) >> PAGE_SHIFT;
> > +
> > + if (off < 0 || cstat_size > sizeof(struct cachestat) || flags != 0)
> > + return -EINVAL;
> > +
> > + if (!f.file)
> > + return -EBADF;
> > +
>
> It looks like we miss an fdput() before returning via the above error
> checks.
Ooops yeah I missed that. I'll fix it.
>
> The only other thing that stands out as a bit odd to me is the
> cstat_size check and associated ability to return a partial cachestat
> struct. Do other syscalls do anything like that? I'd think we'd want to
> ensure we always at least return a fully populated cachestat struct,
> even if it happened to be an old/compat version if the size does ever
> increase. Hm?
Not that I know of, but the idea is that the user might expect a smaller
struct cachestat in their code (and allocate memory accordingly).
With this cstat_size, we can make sure that the expansion of cachestat
struct (with new fields) does not break existing user's code - we only
copy part of the struct.
>
> Brian
>
> > + memset(&cs, 0, sizeof(struct cachestat));
> > + mapping = f.file->f_mapping;
> > + filemap_cachestat(mapping, first_index, last_index, &cs);
> > + fdput(f);
> > +
> > + if (copy_to_user(cstat, &cs, cstat_size))
> > + return -EFAULT;
> > +
> > + return 0;
> > +}
> > +#endif /* CONFIG_CACHESTAT_SYSCALL */
> > --
> > 2.30.2
> >
>
On Tue, Jan 10, 2023 at 01:16:27PM -0800, Nhat Pham wrote:
> On Tue, Jan 10, 2023 at 6:21 AM Brian Foster <bfoster@redhat.com> wrote:
> >
> > On Wed, Jan 04, 2023 at 03:11:26PM -0800, Nhat Pham wrote:
> > > Implement 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.
> > >
> > > NAME
> > > cachestat - query the page cache statistics of a file.
> > >
> > > SYNOPSIS
> > > #include <sys/mman.h>
> > >
> > > struct cachestat {
> > > __u64 nr_cache;
> > > __u64 nr_dirty;
> > > __u64 nr_writeback;
> > > __u64 nr_evicted;
> > > __u64 nr_recently_evicted;
> > > };
> > >
> > > int cachestat(unsigned int fd, off_t off, size_t len,
> > > size_t cstat_size, 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.
> > >
> > > `cstat_size` allows users to obtain partial results. The syscall
> > > will copy the first `csstat_size` bytes to the specified userspace
> > > memory. `cstat_size` must be a non-negative value that is no larger
> > > than the current size of the cachestat struct.
> > >
> > > The `flags` argument is unused for now, but is included for future
> > > extensibility. User should pass 0 (i.e no flag specified).
> > >
> > > RETURN VALUE
> > > On success, cachestat returns 0. On error, -1 is returned, and errno
> > > is set to indicate the error.
> > >
> > > ERRORS
> > > EFAULT cstat points to an invalid address.
> > >
> > > EINVAL invalid `cstat_size` or `flags`
> > >
> > > EBADF invalid file descriptor.
> > >
> > > Signed-off-by: Nhat Pham <nphamcs@gmail.com>
> > > ---
> > > arch/alpha/kernel/syscalls/syscall.tbl | 1 +
> > > arch/arm/tools/syscall.tbl | 1 +
> > > arch/ia64/kernel/syscalls/syscall.tbl | 1 +
> > > arch/m68k/kernel/syscalls/syscall.tbl | 1 +
> > > arch/microblaze/kernel/syscalls/syscall.tbl | 1 +
> > > arch/parisc/kernel/syscalls/syscall.tbl | 1 +
> > > arch/powerpc/kernel/syscalls/syscall.tbl | 1 +
> > > arch/s390/kernel/syscalls/syscall.tbl | 1 +
> > > arch/sh/kernel/syscalls/syscall.tbl | 1 +
> > > arch/sparc/kernel/syscalls/syscall.tbl | 1 +
> > > arch/x86/entry/syscalls/syscall_32.tbl | 1 +
> > > arch/x86/entry/syscalls/syscall_64.tbl | 1 +
> > > arch/xtensa/kernel/syscalls/syscall.tbl | 1 +
> > > include/linux/fs.h | 3 +
> > > include/linux/syscalls.h | 3 +
> > > include/uapi/asm-generic/unistd.h | 5 +-
> > > include/uapi/linux/mman.h | 9 ++
> > > init/Kconfig | 10 ++
> > > kernel/sys_ni.c | 1 +
> > > mm/filemap.c | 143 ++++++++++++++++++++
> > > 20 files changed, 186 insertions(+), 1 deletion(-)
> > >
> > ...
> > > diff --git a/mm/filemap.c b/mm/filemap.c
> > > index 08341616ae7a..d70d47b20700 100644
> > > --- a/mm/filemap.c
> > > +++ b/mm/filemap.c
> > ...
> > > @@ -3949,3 +3953,142 @@ bool filemap_release_folio(struct folio *folio, gfp_t gfp)
> > > return try_to_free_buffers(folio);
> > > }
> > > EXPORT_SYMBOL(filemap_release_folio);
> > ...
> > > +#ifdef CONFIG_CACHESTAT_SYSCALL
> > > +/*
> > > + * The cachestat(5) system call.
> > > + *
> > > + * cachestat() returns the page cache statistics of a file in the
> > > + * bytes range specified by `off` and `len`: number of cached pages,
> > > + * number of dirty pages, number of pages marked for writeback,
> > > + * number of evicted pages, and number of recently evicted pages.
> > > + *
> > > + * 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.
> > > + *
> > > + * `off` and `len` 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.
> > > + *
> > > + * `cstat_size` allows users to obtain partial results. The syscall
> > > + * will copy the first `csstat_size` bytes to the specified userspace
> > > + * memory. It also makes the cachestat struct extensible - new fields
> > > + * can be added in the future without breaking existing usage.
> > > + * `cstat_size` must be a non-negative value that is no larger than
> > > + * the current size of the cachestat struct.
> > > + *
> > > + * The `flags` argument is unused for now, but is included for future
> > > + * extensibility. User should pass 0 (i.e no flag specified).
> > > + *
> > > + * 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 values:
> > > + * zero - success
> > > + * -EFAULT - cstat points to an illegal address
> > > + * -EINVAL - invalid arguments
> > > + * -EBADF - invalid file descriptor
> > > + */
> > > +SYSCALL_DEFINE6(cachestat, unsigned int, fd, off_t, off, size_t, len,
> > > + size_t, cstat_size, struct cachestat __user *, cstat,
> > > + unsigned int, flags)
> > > +{
> > > + struct fd f = fdget(fd);
> > > + struct address_space *mapping;
> > > + struct cachestat cs;
> > > + pgoff_t first_index = off >> PAGE_SHIFT;
> > > + pgoff_t last_index =
> > > + len == 0 ? ULONG_MAX : (off + len - 1) >> PAGE_SHIFT;
> > > +
> > > + if (off < 0 || cstat_size > sizeof(struct cachestat) || flags != 0)
> > > + return -EINVAL;
> > > +
> > > + if (!f.file)
> > > + return -EBADF;
> > > +
> >
> > It looks like we miss an fdput() before returning via the above error
> > checks.
>
> Ooops yeah I missed that. I'll fix it.
>
> >
> > The only other thing that stands out as a bit odd to me is the
> > cstat_size check and associated ability to return a partial cachestat
> > struct. Do other syscalls do anything like that? I'd think we'd want to
> > ensure we always at least return a fully populated cachestat struct,
> > even if it happened to be an old/compat version if the size does ever
> > increase. Hm?
>
> Not that I know of, but the idea is that the user might expect a smaller
> struct cachestat in their code (and allocate memory accordingly).
> With this cstat_size, we can make sure that the expansion of cachestat
> struct (with new fields) does not break existing user's code - we only
> copy part of the struct.
>
Yup, I get the idea of supporting future expansion. The part that
doesn't make sense to me is allowing a partial copy of struct cachestat.
For example, what happens if the user passes a cstat_size of 2? We copy
out two bytes of the first field of the struct and return 0 for
"success?"
The way I've commonly seen this sort of support for future expansion is
by versioning the structure and providing backwards compatibility for
old versions of the structure. I'd guess there are various ways to
implement that, but a simple example that comes to mind for me is
xfs_ioc_fsgeometry(). The caveat there is that it's an ioctl where IIRC
the command essentially changes when the structure size does, which
allows the kernel to determine what version is being used.
I'm not quite sure what the ideal way to do that for a syscall is. I
suppose the kernel could just check that the size param matches some
version of the structure it knows about and assume that. Or since you
have the flags field, you could require a flag to be passed in to return
values for any new fields. Or yet another option could be to pad out the
size of the userspace structure to support future expansion for the
foreseeable future without the need for such a flag.
Perhaps this is something linux-api can help with?
Brian
> >
> > Brian
> >
> > > + memset(&cs, 0, sizeof(struct cachestat));
> > > + mapping = f.file->f_mapping;
> > > + filemap_cachestat(mapping, first_index, last_index, &cs);
> > > + fdput(f);
> > > +
> > > + if (copy_to_user(cstat, &cs, cstat_size))
> > > + return -EFAULT;
> > > +
> > > + return 0;
> > > +}
> > > +#endif /* CONFIG_CACHESTAT_SYSCALL */
> > > --
> > > 2.30.2
> > >
> >
>
On Wed, Jan 11, 2023 at 4:52 AM Brian Foster <bfoster@redhat.com> wrote:
>
> On Tue, Jan 10, 2023 at 01:16:27PM -0800, Nhat Pham wrote:
> > On Tue, Jan 10, 2023 at 6:21 AM Brian Foster <bfoster@redhat.com> wrote:
> > >
> > > On Wed, Jan 04, 2023 at 03:11:26PM -0800, Nhat Pham wrote:
> > > > Implement 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.
> > > >
> > > > NAME
> > > > cachestat - query the page cache statistics of a file.
> > > >
> > > > SYNOPSIS
> > > > #include <sys/mman.h>
> > > >
> > > > struct cachestat {
> > > > __u64 nr_cache;
> > > > __u64 nr_dirty;
> > > > __u64 nr_writeback;
> > > > __u64 nr_evicted;
> > > > __u64 nr_recently_evicted;
> > > > };
> > > >
> > > > int cachestat(unsigned int fd, off_t off, size_t len,
> > > > size_t cstat_size, 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.
> > > >
> > > > `cstat_size` allows users to obtain partial results. The syscall
> > > > will copy the first `csstat_size` bytes to the specified userspace
> > > > memory. `cstat_size` must be a non-negative value that is no larger
> > > > than the current size of the cachestat struct.
> > > >
> > > > The `flags` argument is unused for now, but is included for future
> > > > extensibility. User should pass 0 (i.e no flag specified).
> > > >
> > > > RETURN VALUE
> > > > On success, cachestat returns 0. On error, -1 is returned, and errno
> > > > is set to indicate the error.
> > > >
> > > > ERRORS
> > > > EFAULT cstat points to an invalid address.
> > > >
> > > > EINVAL invalid `cstat_size` or `flags`
> > > >
> > > > EBADF invalid file descriptor.
> > > >
> > > > Signed-off-by: Nhat Pham <nphamcs@gmail.com>
> > > > ---
> > > > arch/alpha/kernel/syscalls/syscall.tbl | 1 +
> > > > arch/arm/tools/syscall.tbl | 1 +
> > > > arch/ia64/kernel/syscalls/syscall.tbl | 1 +
> > > > arch/m68k/kernel/syscalls/syscall.tbl | 1 +
> > > > arch/microblaze/kernel/syscalls/syscall.tbl | 1 +
> > > > arch/parisc/kernel/syscalls/syscall.tbl | 1 +
> > > > arch/powerpc/kernel/syscalls/syscall.tbl | 1 +
> > > > arch/s390/kernel/syscalls/syscall.tbl | 1 +
> > > > arch/sh/kernel/syscalls/syscall.tbl | 1 +
> > > > arch/sparc/kernel/syscalls/syscall.tbl | 1 +
> > > > arch/x86/entry/syscalls/syscall_32.tbl | 1 +
> > > > arch/x86/entry/syscalls/syscall_64.tbl | 1 +
> > > > arch/xtensa/kernel/syscalls/syscall.tbl | 1 +
> > > > include/linux/fs.h | 3 +
> > > > include/linux/syscalls.h | 3 +
> > > > include/uapi/asm-generic/unistd.h | 5 +-
> > > > include/uapi/linux/mman.h | 9 ++
> > > > init/Kconfig | 10 ++
> > > > kernel/sys_ni.c | 1 +
> > > > mm/filemap.c | 143 ++++++++++++++++++++
> > > > 20 files changed, 186 insertions(+), 1 deletion(-)
> > > >
> > > ...
> > > > diff --git a/mm/filemap.c b/mm/filemap.c
> > > > index 08341616ae7a..d70d47b20700 100644
> > > > --- a/mm/filemap.c
> > > > +++ b/mm/filemap.c
> > > ...
> > > > @@ -3949,3 +3953,142 @@ bool filemap_release_folio(struct folio *folio, gfp_t gfp)
> > > > return try_to_free_buffers(folio);
> > > > }
> > > > EXPORT_SYMBOL(filemap_release_folio);
> > > ...
> > > > +#ifdef CONFIG_CACHESTAT_SYSCALL
> > > > +/*
> > > > + * The cachestat(5) system call.
> > > > + *
> > > > + * cachestat() returns the page cache statistics of a file in the
> > > > + * bytes range specified by `off` and `len`: number of cached pages,
> > > > + * number of dirty pages, number of pages marked for writeback,
> > > > + * number of evicted pages, and number of recently evicted pages.
> > > > + *
> > > > + * 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.
> > > > + *
> > > > + * `off` and `len` 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.
> > > > + *
> > > > + * `cstat_size` allows users to obtain partial results. The syscall
> > > > + * will copy the first `csstat_size` bytes to the specified userspace
> > > > + * memory. It also makes the cachestat struct extensible - new fields
> > > > + * can be added in the future without breaking existing usage.
> > > > + * `cstat_size` must be a non-negative value that is no larger than
> > > > + * the current size of the cachestat struct.
> > > > + *
> > > > + * The `flags` argument is unused for now, but is included for future
> > > > + * extensibility. User should pass 0 (i.e no flag specified).
> > > > + *
> > > > + * 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 values:
> > > > + * zero - success
> > > > + * -EFAULT - cstat points to an illegal address
> > > > + * -EINVAL - invalid arguments
> > > > + * -EBADF - invalid file descriptor
> > > > + */
> > > > +SYSCALL_DEFINE6(cachestat, unsigned int, fd, off_t, off, size_t, len,
> > > > + size_t, cstat_size, struct cachestat __user *, cstat,
> > > > + unsigned int, flags)
> > > > +{
> > > > + struct fd f = fdget(fd);
> > > > + struct address_space *mapping;
> > > > + struct cachestat cs;
> > > > + pgoff_t first_index = off >> PAGE_SHIFT;
> > > > + pgoff_t last_index =
> > > > + len == 0 ? ULONG_MAX : (off + len - 1) >> PAGE_SHIFT;
> > > > +
> > > > + if (off < 0 || cstat_size > sizeof(struct cachestat) || flags != 0)
> > > > + return -EINVAL;
> > > > +
> > > > + if (!f.file)
> > > > + return -EBADF;
> > > > +
> > >
> > > It looks like we miss an fdput() before returning via the above error
> > > checks.
> >
> > Ooops yeah I missed that. I'll fix it.
> >
> > >
> > > The only other thing that stands out as a bit odd to me is the
> > > cstat_size check and associated ability to return a partial cachestat
> > > struct. Do other syscalls do anything like that? I'd think we'd want to
> > > ensure we always at least return a fully populated cachestat struct,
> > > even if it happened to be an old/compat version if the size does ever
> > > increase. Hm?
> >
> > Not that I know of, but the idea is that the user might expect a smaller
> > struct cachestat in their code (and allocate memory accordingly).
> > With this cstat_size, we can make sure that the expansion of cachestat
> > struct (with new fields) does not break existing user's code - we only
> > copy part of the struct.
> >
>
I found a couple of syscalls doing something similar:
statfs64
https://github.com/torvalds/linux/blob/master/fs/statfs.c#L168-L190
io_uring_enter:
https://github.com/torvalds/linux/blob/master/io_uring/io_uring.c#L3232-L3243
It looks like these syscalls just error-out if the size argument does not match
with the size of the kernel struct.
> Yup, I get the idea of supporting future expansion. The part that
> doesn't make sense to me is allowing a partial copy of struct cachestat.
> For example, what happens if the user passes a cstat_size of 2? We copy
> out two bytes of the first field of the struct and return 0 for
> "success?"
>
> The way I've commonly seen this sort of support for future expansion is
> by versioning the structure and providing backwards compatibility for
> old versions of the structure. I'd guess there are various ways to
> implement that, but a simple example that comes to mind for me is
> xfs_ioc_fsgeometry(). The caveat there is that it's an ioctl where IIRC
> the command essentially changes when the structure size does, which
> allows the kernel to determine what version is being used.
Hmm xfs_ioc_fsgeometry looks interesting. From my understanding,
it basically has an argument that can take on a couple of values, each of
which delineates a version, right? I can give that a try and see if it looks
better (and maybe send a new version with this updated API).
>
> I'm not quite sure what the ideal way to do that for a syscall is. I
> suppose the kernel could just check that the size param matches some
> version of the structure it knows about and assume that. Or since you
> have the flags field, you could require a flag to be passed in to return
> values for any new fields. Or yet another option could be to pad out the
> size of the userspace structure to support future expansion for the
> foreseeable future without the need for such a flag.
>
> Perhaps this is something linux-api can help with?
I think I should definitely forward my patch series to linux-api. Thanks for
the suggestion, Brian!
>
> Brian
>
> > >
> > > Brian
> > >
> > > > + memset(&cs, 0, sizeof(struct cachestat));
> > > > + mapping = f.file->f_mapping;
> > > > + filemap_cachestat(mapping, first_index, last_index, &cs);
> > > > + fdput(f);
> > > > +
> > > > + if (copy_to_user(cstat, &cs, cstat_size))
> > > > + return -EFAULT;
> > > > +
> > > > + return 0;
> > > > +}
> > > > +#endif /* CONFIG_CACHESTAT_SYSCALL */
> > > > --
> > > > 2.30.2
> > > >
> > >
> >
>
On Wed, Jan 11, 2023 at 12:39:38PM -0800, Nhat Pham wrote:
> On Wed, Jan 11, 2023 at 4:52 AM Brian Foster <bfoster@redhat.com> wrote:
> >
> > On Tue, Jan 10, 2023 at 01:16:27PM -0800, Nhat Pham wrote:
> > > On Tue, Jan 10, 2023 at 6:21 AM Brian Foster <bfoster@redhat.com> wrote:
> > > >
> > > > On Wed, Jan 04, 2023 at 03:11:26PM -0800, Nhat Pham wrote:
> > > > > Implement 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.
> > > > >
> > > > > NAME
> > > > > cachestat - query the page cache statistics of a file.
> > > > >
> > > > > SYNOPSIS
> > > > > #include <sys/mman.h>
> > > > >
> > > > > struct cachestat {
> > > > > __u64 nr_cache;
> > > > > __u64 nr_dirty;
> > > > > __u64 nr_writeback;
> > > > > __u64 nr_evicted;
> > > > > __u64 nr_recently_evicted;
> > > > > };
> > > > >
> > > > > int cachestat(unsigned int fd, off_t off, size_t len,
> > > > > size_t cstat_size, 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.
> > > > >
> > > > > `cstat_size` allows users to obtain partial results. The syscall
> > > > > will copy the first `csstat_size` bytes to the specified userspace
> > > > > memory. `cstat_size` must be a non-negative value that is no larger
> > > > > than the current size of the cachestat struct.
> > > > >
> > > > > The `flags` argument is unused for now, but is included for future
> > > > > extensibility. User should pass 0 (i.e no flag specified).
> > > > >
> > > > > RETURN VALUE
> > > > > On success, cachestat returns 0. On error, -1 is returned, and errno
> > > > > is set to indicate the error.
> > > > >
> > > > > ERRORS
> > > > > EFAULT cstat points to an invalid address.
> > > > >
> > > > > EINVAL invalid `cstat_size` or `flags`
> > > > >
> > > > > EBADF invalid file descriptor.
> > > > >
> > > > > Signed-off-by: Nhat Pham <nphamcs@gmail.com>
> > > > > ---
> > > > > arch/alpha/kernel/syscalls/syscall.tbl | 1 +
> > > > > arch/arm/tools/syscall.tbl | 1 +
> > > > > arch/ia64/kernel/syscalls/syscall.tbl | 1 +
> > > > > arch/m68k/kernel/syscalls/syscall.tbl | 1 +
> > > > > arch/microblaze/kernel/syscalls/syscall.tbl | 1 +
> > > > > arch/parisc/kernel/syscalls/syscall.tbl | 1 +
> > > > > arch/powerpc/kernel/syscalls/syscall.tbl | 1 +
> > > > > arch/s390/kernel/syscalls/syscall.tbl | 1 +
> > > > > arch/sh/kernel/syscalls/syscall.tbl | 1 +
> > > > > arch/sparc/kernel/syscalls/syscall.tbl | 1 +
> > > > > arch/x86/entry/syscalls/syscall_32.tbl | 1 +
> > > > > arch/x86/entry/syscalls/syscall_64.tbl | 1 +
> > > > > arch/xtensa/kernel/syscalls/syscall.tbl | 1 +
> > > > > include/linux/fs.h | 3 +
> > > > > include/linux/syscalls.h | 3 +
> > > > > include/uapi/asm-generic/unistd.h | 5 +-
> > > > > include/uapi/linux/mman.h | 9 ++
> > > > > init/Kconfig | 10 ++
> > > > > kernel/sys_ni.c | 1 +
> > > > > mm/filemap.c | 143 ++++++++++++++++++++
> > > > > 20 files changed, 186 insertions(+), 1 deletion(-)
> > > > >
> > > > ...
> > > > > diff --git a/mm/filemap.c b/mm/filemap.c
> > > > > index 08341616ae7a..d70d47b20700 100644
> > > > > --- a/mm/filemap.c
> > > > > +++ b/mm/filemap.c
> > > > ...
> > > > > @@ -3949,3 +3953,142 @@ bool filemap_release_folio(struct folio *folio, gfp_t gfp)
> > > > > return try_to_free_buffers(folio);
> > > > > }
> > > > > EXPORT_SYMBOL(filemap_release_folio);
> > > > ...
> > > > > +#ifdef CONFIG_CACHESTAT_SYSCALL
> > > > > +/*
> > > > > + * The cachestat(5) system call.
> > > > > + *
> > > > > + * cachestat() returns the page cache statistics of a file in the
> > > > > + * bytes range specified by `off` and `len`: number of cached pages,
> > > > > + * number of dirty pages, number of pages marked for writeback,
> > > > > + * number of evicted pages, and number of recently evicted pages.
> > > > > + *
> > > > > + * 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.
> > > > > + *
> > > > > + * `off` and `len` 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.
> > > > > + *
> > > > > + * `cstat_size` allows users to obtain partial results. The syscall
> > > > > + * will copy the first `csstat_size` bytes to the specified userspace
> > > > > + * memory. It also makes the cachestat struct extensible - new fields
> > > > > + * can be added in the future without breaking existing usage.
> > > > > + * `cstat_size` must be a non-negative value that is no larger than
> > > > > + * the current size of the cachestat struct.
> > > > > + *
> > > > > + * The `flags` argument is unused for now, but is included for future
> > > > > + * extensibility. User should pass 0 (i.e no flag specified).
> > > > > + *
> > > > > + * 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 values:
> > > > > + * zero - success
> > > > > + * -EFAULT - cstat points to an illegal address
> > > > > + * -EINVAL - invalid arguments
> > > > > + * -EBADF - invalid file descriptor
> > > > > + */
> > > > > +SYSCALL_DEFINE6(cachestat, unsigned int, fd, off_t, off, size_t, len,
> > > > > + size_t, cstat_size, struct cachestat __user *, cstat,
> > > > > + unsigned int, flags)
> > > > > +{
> > > > > + struct fd f = fdget(fd);
> > > > > + struct address_space *mapping;
> > > > > + struct cachestat cs;
> > > > > + pgoff_t first_index = off >> PAGE_SHIFT;
> > > > > + pgoff_t last_index =
> > > > > + len == 0 ? ULONG_MAX : (off + len - 1) >> PAGE_SHIFT;
> > > > > +
> > > > > + if (off < 0 || cstat_size > sizeof(struct cachestat) || flags != 0)
> > > > > + return -EINVAL;
> > > > > +
> > > > > + if (!f.file)
> > > > > + return -EBADF;
> > > > > +
> > > >
> > > > It looks like we miss an fdput() before returning via the above error
> > > > checks.
> > >
> > > Ooops yeah I missed that. I'll fix it.
> > >
> > > >
> > > > The only other thing that stands out as a bit odd to me is the
> > > > cstat_size check and associated ability to return a partial cachestat
> > > > struct. Do other syscalls do anything like that? I'd think we'd want to
> > > > ensure we always at least return a fully populated cachestat struct,
> > > > even if it happened to be an old/compat version if the size does ever
> > > > increase. Hm?
> > >
> > > Not that I know of, but the idea is that the user might expect a smaller
> > > struct cachestat in their code (and allocate memory accordingly).
> > > With this cstat_size, we can make sure that the expansion of cachestat
> > > struct (with new fields) does not break existing user's code - we only
> > > copy part of the struct.
> > >
> >
>
> I found a couple of syscalls doing something similar:
>
> statfs64
> https://github.com/torvalds/linux/blob/master/fs/statfs.c#L168-L190
>
> io_uring_enter:
> https://github.com/torvalds/linux/blob/master/io_uring/io_uring.c#L3232-L3243
>
> It looks like these syscalls just error-out if the size argument does not match
> with the size of the kernel struct.
>
> > Yup, I get the idea of supporting future expansion. The part that
> > doesn't make sense to me is allowing a partial copy of struct cachestat.
> > For example, what happens if the user passes a cstat_size of 2? We copy
> > out two bytes of the first field of the struct and return 0 for
> > "success?"
> >
> > The way I've commonly seen this sort of support for future expansion is
> > by versioning the structure and providing backwards compatibility for
> > old versions of the structure. I'd guess there are various ways to
> > implement that, but a simple example that comes to mind for me is
> > xfs_ioc_fsgeometry(). The caveat there is that it's an ioctl where IIRC
> > the command essentially changes when the structure size does, which
> > allows the kernel to determine what version is being used.
>
> Hmm xfs_ioc_fsgeometry looks interesting. From my understanding,
> it basically has an argument that can take on a couple of values, each of
> which delineates a version, right? I can give that a try and see if it looks
> better (and maybe send a new version with this updated API).
>
Yeah, though the difference is that the caller can determine the version
of struct to use based on the different ioctl command. We don't have
that luxury with the syscall, so I'm not sure the same kind of factoring
would make sense here.
From your examples above, I think it probably makes sense to enforce
that the size matches the actual structure. The question is if you want
to plan for future expansion and adding fields to the struct, what's the
accepted way to prepare for that now and design it into the api? If
nobody else can chime in with suggestions in the meantime, hopefully
linux-api can help. :)
Brian
> >
> > I'm not quite sure what the ideal way to do that for a syscall is. I
> > suppose the kernel could just check that the size param matches some
> > version of the structure it knows about and assume that. Or since you
> > have the flags field, you could require a flag to be passed in to return
> > values for any new fields. Or yet another option could be to pad out the
> > size of the userspace structure to support future expansion for the
> > foreseeable future without the need for such a flag.
> >
> > Perhaps this is something linux-api can help with?
>
> I think I should definitely forward my patch series to linux-api. Thanks for
> the suggestion, Brian!
>
> >
> > Brian
> >
> > > >
> > > > Brian
> > > >
> > > > > + memset(&cs, 0, sizeof(struct cachestat));
> > > > > + mapping = f.file->f_mapping;
> > > > > + filemap_cachestat(mapping, first_index, last_index, &cs);
> > > > > + fdput(f);
> > > > > +
> > > > > + if (copy_to_user(cstat, &cs, cstat_size))
> > > > > + return -EFAULT;
> > > > > +
> > > > > + return 0;
> > > > > +}
> > > > > +#endif /* CONFIG_CACHESTAT_SYSCALL */
> > > > > --
> > > > > 2.30.2
> > > > >
> > > >
> > >
> >
>
On Thu, Jan 5, 2023 at 12:13 AM Nhat Pham <nphamcs@gmail.com> wrote:
> Implement 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.
>
> NAME
> cachestat - query the page cache statistics of a file.
>
> SYNOPSIS
> #include <sys/mman.h>
>
> struct cachestat {
> __u64 nr_cache;
> __u64 nr_dirty;
> __u64 nr_writeback;
> __u64 nr_evicted;
> __u64 nr_recently_evicted;
> };
>
> int cachestat(unsigned int fd, off_t off, size_t len,
> size_t cstat_size, 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.
>
> `cstat_size` allows users to obtain partial results. The syscall
> will copy the first `csstat_size` bytes to the specified userspace
> memory. `cstat_size` must be a non-negative value that is no larger
> than the current size of the cachestat struct.
>
> The `flags` argument is unused for now, but is included for future
> extensibility. User should pass 0 (i.e no flag specified).
>
> RETURN VALUE
> On success, cachestat returns 0. On error, -1 is returned, and errno
> is set to indicate the error.
>
> ERRORS
> EFAULT cstat points to an invalid address.
>
> EINVAL invalid `cstat_size` or `flags`
>
> EBADF invalid file descriptor.
>
> Signed-off-by: Nhat Pham <nphamcs@gmail.com>
> arch/m68k/kernel/syscalls/syscall.tbl | 1 +
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org> [m68k]
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
@@ -490,3 +490,4 @@
558 common process_mrelease sys_process_mrelease
559 common futex_waitv sys_futex_waitv
560 common set_mempolicy_home_node sys_ni_syscall
+561 common cachestat sys_cachestat
@@ -464,3 +464,4 @@
448 common process_mrelease sys_process_mrelease
449 common futex_waitv sys_futex_waitv
450 common set_mempolicy_home_node sys_set_mempolicy_home_node
+451 common cachestat sys_cachestat
@@ -371,3 +371,4 @@
448 common process_mrelease sys_process_mrelease
449 common futex_waitv sys_futex_waitv
450 common set_mempolicy_home_node sys_set_mempolicy_home_node
+451 common cachestat sys_cachestat
@@ -450,3 +450,4 @@
448 common process_mrelease sys_process_mrelease
449 common futex_waitv sys_futex_waitv
450 common set_mempolicy_home_node sys_set_mempolicy_home_node
+451 common cachestat sys_cachestat
@@ -456,3 +456,4 @@
448 common process_mrelease sys_process_mrelease
449 common futex_waitv sys_futex_waitv
450 common set_mempolicy_home_node sys_set_mempolicy_home_node
+451 common cachestat sys_cachestat
@@ -448,3 +448,4 @@
448 common process_mrelease sys_process_mrelease
449 common futex_waitv sys_futex_waitv
450 common set_mempolicy_home_node sys_set_mempolicy_home_node
+451 common cachestat sys_cachestat
@@ -530,3 +530,4 @@
448 common process_mrelease sys_process_mrelease
449 common futex_waitv sys_futex_waitv
450 nospu set_mempolicy_home_node sys_set_mempolicy_home_node
+451 common cachestat sys_cachestat
@@ -453,3 +453,4 @@
448 common process_mrelease sys_process_mrelease sys_process_mrelease
449 common futex_waitv sys_futex_waitv sys_futex_waitv
450 common set_mempolicy_home_node sys_set_mempolicy_home_node sys_set_mempolicy_home_node
+451 common cachestat sys_cachestat sys_cachestat
@@ -453,3 +453,4 @@
448 common process_mrelease sys_process_mrelease
449 common futex_waitv sys_futex_waitv
450 common set_mempolicy_home_node sys_set_mempolicy_home_node
+451 common cachestat sys_cachestat
@@ -496,3 +496,4 @@
448 common process_mrelease sys_process_mrelease
449 common futex_waitv sys_futex_waitv
450 common set_mempolicy_home_node sys_set_mempolicy_home_node
+451 common cachestat sys_cachestat
@@ -455,3 +455,4 @@
448 i386 process_mrelease sys_process_mrelease
449 i386 futex_waitv sys_futex_waitv
450 i386 set_mempolicy_home_node sys_set_mempolicy_home_node
+451 i386 cachestat sys_cachestat
@@ -372,6 +372,7 @@
448 common process_mrelease sys_process_mrelease
449 common futex_waitv sys_futex_waitv
450 common set_mempolicy_home_node sys_set_mempolicy_home_node
+451 common cachestat sys_cachestat
#
# Due to a historical design error, certain syscalls are numbered differently
@@ -421,3 +421,4 @@
448 common process_mrelease sys_process_mrelease
449 common futex_waitv sys_futex_waitv
450 common set_mempolicy_home_node sys_set_mempolicy_home_node
+451 common cachestat sys_cachestat
@@ -75,6 +75,7 @@ struct fs_context;
struct fs_parameter_spec;
struct fileattr;
struct iomap_ops;
+struct cachestat;
extern void __init inode_init(void);
extern void __init inode_init_early(void);
@@ -830,6 +831,8 @@ void filemap_invalidate_lock_two(struct address_space *mapping1,
struct address_space *mapping2);
void filemap_invalidate_unlock_two(struct address_space *mapping1,
struct address_space *mapping2);
+void filemap_cachestat(struct address_space *mapping, pgoff_t first_index,
+ pgoff_t last_index, struct cachestat *cs);
/*
@@ -72,6 +72,7 @@ struct open_how;
struct mount_attr;
struct landlock_ruleset_attr;
enum landlock_rule_type;
+struct cachestat;
#include <linux/types.h>
#include <linux/aio_abi.h>
@@ -1056,6 +1057,8 @@ asmlinkage long sys_memfd_secret(unsigned int flags);
asmlinkage long sys_set_mempolicy_home_node(unsigned long start, unsigned long len,
unsigned long home_node,
unsigned long flags);
+asmlinkage long sys_cachestat(unsigned int fd, off_t off, size_t len,
+ size_t cstat_size, struct cachestat __user *cstat, unsigned int flags);
/*
* Architecture-specific system calls
@@ -886,8 +886,11 @@ __SYSCALL(__NR_futex_waitv, sys_futex_waitv)
#define __NR_set_mempolicy_home_node 450
__SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node)
+#define __NR_cachestat 451
+__SYSCALL(__NR_cachestat, sys_cachestat)
+
#undef __NR_syscalls
-#define __NR_syscalls 451
+#define __NR_syscalls 452
/*
* 32 bit systems traditionally used different
@@ -4,6 +4,7 @@
#include <asm/mman.h>
#include <asm-generic/hugetlb_encode.h>
+#include <linux/types.h>
#define MREMAP_MAYMOVE 1
#define MREMAP_FIXED 2
@@ -41,4 +42,12 @@
#define MAP_HUGE_2GB HUGETLB_FLAG_ENCODE_2GB
#define MAP_HUGE_16GB HUGETLB_FLAG_ENCODE_16GB
+struct cachestat {
+ __u64 nr_cache;
+ __u64 nr_dirty;
+ __u64 nr_writeback;
+ __u64 nr_evicted;
+ __u64 nr_recently_evicted;
+};
+
#endif /* _UAPI_LINUX_MMAN_H */
@@ -1798,6 +1798,16 @@ config RSEQ
If unsure, say Y.
+config CACHESTAT_SYSCALL
+ bool "Enable cachestat() system call" if EXPERT
+ default y
+ help
+ Enable the cachestat system call, which queries the page cache
+ statistics of a file (number of cached pages, dirty pages,
+ pages marked for writeback, (recently) evicted pages).
+
+ If unsure say Y here.
+
config DEBUG_RSEQ
default n
bool "Enabled debugging of rseq() system call" if EXPERT
@@ -299,6 +299,7 @@ COND_SYSCALL(set_mempolicy);
COND_SYSCALL(migrate_pages);
COND_SYSCALL(move_pages);
COND_SYSCALL(set_mempolicy_home_node);
+COND_SYSCALL(cachestat);
COND_SYSCALL(perf_event_open);
COND_SYSCALL(accept4);
@@ -22,6 +22,7 @@
#include <linux/mm.h>
#include <linux/swap.h>
#include <linux/swapops.h>
+#include <linux/syscalls.h>
#include <linux/mman.h>
#include <linux/pagemap.h>
#include <linux/file.h>
@@ -55,6 +56,9 @@
#include <linux/buffer_head.h> /* for try_to_free_buffers */
#include <asm/mman.h>
+#include <uapi/linux/mman.h>
+
+#include "swap.h"
/*
* Shared mappings implemented 30.11.1994. It's not fully working yet,
@@ -3949,3 +3953,142 @@ bool filemap_release_folio(struct folio *folio, gfp_t gfp)
return try_to_free_buffers(folio);
}
EXPORT_SYMBOL(filemap_release_folio);
+
+/**
+ * filemap_cachestat() - compute the page cache statistics of a mapping
+ * @mapping: The mapping to compute the statistics for.
+ * @first_index: The starting page cache index.
+ * @last_index: The final page index (inclusive).
+ * @cs: the cachestat struct to write the result to.
+ *
+ * This will query the page cache statistics of a mapping in the
+ * page range of [first_index, last_index] (inclusive). The statistics
+ * queried include: number of dirty pages, number of pages marked for
+ * writeback, and the number of (recently) evicted pages.
+ */
+void filemap_cachestat(struct address_space *mapping, pgoff_t first_index,
+ pgoff_t last_index, struct cachestat *cs)
+{
+ XA_STATE(xas, &mapping->i_pages, first_index);
+ struct folio *folio;
+
+ rcu_read_lock();
+ xas_for_each(&xas, folio, last_index) {
+ unsigned long nr_pages;
+ pgoff_t folio_first_index, folio_last_index;
+
+ if (xas_retry(&xas, folio))
+ continue;
+
+ nr_pages = folio_nr_pages(folio);
+ folio_first_index = folio_pgoff(folio);
+ folio_last_index = folio_first_index + nr_pages - 1;
+
+ /* Folios might straddle the range boundaries, only count covered subpages */
+ if (folio_first_index < first_index)
+ nr_pages -= first_index - folio_first_index;
+
+ if (folio_last_index > last_index)
+ nr_pages -= folio_last_index - last_index;
+
+ if (xa_is_value(folio)) {
+ /* page is evicted */
+ void *shadow = (void *)folio;
+ bool workingset; /* not used */
+
+ cs->nr_evicted += nr_pages;
+
+#ifdef CONFIG_SWAP /* implies CONFIG_MMU */
+ if (shmem_mapping(mapping)) {
+ /* shmem file - in swap cache */
+ swp_entry_t swp = radix_to_swp_entry(folio);
+
+ shadow = get_shadow_from_swap_cache(swp);
+ }
+#endif
+ if (workingset_test_recent(shadow, true, &workingset))
+ cs->nr_recently_evicted += nr_pages;
+
+ continue;
+ }
+
+ /* page is in cache */
+ cs->nr_cache += nr_pages;
+
+ if (folio_test_dirty(folio))
+ cs->nr_dirty += nr_pages;
+
+ if (folio_test_writeback(folio))
+ cs->nr_writeback += nr_pages;
+ }
+ rcu_read_unlock();
+}
+EXPORT_SYMBOL(filemap_cachestat);
+
+#ifdef CONFIG_CACHESTAT_SYSCALL
+/*
+ * The cachestat(5) system call.
+ *
+ * cachestat() returns the page cache statistics of a file in the
+ * bytes range specified by `off` and `len`: number of cached pages,
+ * number of dirty pages, number of pages marked for writeback,
+ * number of evicted pages, and number of recently evicted pages.
+ *
+ * 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.
+ *
+ * `off` and `len` 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.
+ *
+ * `cstat_size` allows users to obtain partial results. The syscall
+ * will copy the first `csstat_size` bytes to the specified userspace
+ * memory. It also makes the cachestat struct extensible - new fields
+ * can be added in the future without breaking existing usage.
+ * `cstat_size` must be a non-negative value that is no larger than
+ * the current size of the cachestat struct.
+ *
+ * The `flags` argument is unused for now, but is included for future
+ * extensibility. User should pass 0 (i.e no flag specified).
+ *
+ * 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 values:
+ * zero - success
+ * -EFAULT - cstat points to an illegal address
+ * -EINVAL - invalid arguments
+ * -EBADF - invalid file descriptor
+ */
+SYSCALL_DEFINE6(cachestat, unsigned int, fd, off_t, off, size_t, len,
+ size_t, cstat_size, struct cachestat __user *, cstat,
+ unsigned int, flags)
+{
+ struct fd f = fdget(fd);
+ struct address_space *mapping;
+ struct cachestat cs;
+ pgoff_t first_index = off >> PAGE_SHIFT;
+ pgoff_t last_index =
+ len == 0 ? ULONG_MAX : (off + len - 1) >> PAGE_SHIFT;
+
+ if (off < 0 || cstat_size > sizeof(struct cachestat) || flags != 0)
+ return -EINVAL;
+
+ if (!f.file)
+ return -EBADF;
+
+ memset(&cs, 0, sizeof(struct cachestat));
+ mapping = f.file->f_mapping;
+ filemap_cachestat(mapping, first_index, last_index, &cs);
+ fdput(f);
+
+ if (copy_to_user(cstat, &cs, cstat_size))
+ return -EFAULT;
+
+ return 0;
+}
+#endif /* CONFIG_CACHESTAT_SYSCALL */