2022-12-05 18:48:57

by Nhat Pham

[permalink] [raw]
Subject: [PATCH v2 3/4] cachestat: implement cachestat syscall

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 status 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);

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

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.

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 `off` or `len` is negative, or invalid `cstat_size`.

EBADF invalid file descriptor.

Signed-off-by: Nhat Pham <[email protected]>
---
MAINTAINERS | 7 ++
arch/x86/entry/syscalls/syscall_32.tbl | 1 +
arch/x86/entry/syscalls/syscall_64.tbl | 1 +
include/linux/syscalls.h | 3 +
include/uapi/asm-generic/unistd.h | 5 +-
include/uapi/linux/mman.h | 8 ++
kernel/sys_ni.c | 1 +
mm/Makefile | 2 +-
mm/cachestat.c | 115 +++++++++++++++++++++++++
9 files changed, 141 insertions(+), 2 deletions(-)
create mode 100644 mm/cachestat.c

diff --git a/MAINTAINERS b/MAINTAINERS
index a198da986146..baa081a1fe52 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4552,6 +4552,13 @@ S: Supported
F: Documentation/filesystems/caching/cachefiles.rst
F: fs/cachefiles/

+CACHESTAT: PAGE CACHE STATS FOR A FILE
+M: Nhat Pham <[email protected]>
+M: Johannes Weiner <[email protected]>
+L: [email protected]
+S: Maintained
+F: mm/cachestat.c
+
CADENCE MIPI-CSI2 BRIDGES
M: Maxime Ripard <[email protected]>
L: [email protected]
diff --git a/arch/x86/entry/syscalls/syscall_32.tbl b/arch/x86/entry/syscalls/syscall_32.tbl
index 320480a8db4f..bc0a3c941b35 100644
--- a/arch/x86/entry/syscalls/syscall_32.tbl
+++ b/arch/x86/entry/syscalls/syscall_32.tbl
@@ -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
diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
index c84d12608cd2..8eed4cdc7965 100644
--- a/arch/x86/entry/syscalls/syscall_64.tbl
+++ b/arch/x86/entry/syscalls/syscall_64.tbl
@@ -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
diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
index a34b0f9a9972..55cea19b8bf6 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -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);

/*
* Architecture-specific system calls
diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h
index 45fa180cc56a..cd639fae9086 100644
--- a/include/uapi/asm-generic/unistd.h
+++ b/include/uapi/asm-generic/unistd.h
@@ -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
diff --git a/include/uapi/linux/mman.h b/include/uapi/linux/mman.h
index f55bc680b5b0..ee2889e0654e 100644
--- a/include/uapi/linux/mman.h
+++ b/include/uapi/linux/mman.h
@@ -41,4 +41,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 */
diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c
index 860b2dcf3ac4..04bfb1e4d377 100644
--- a/kernel/sys_ni.c
+++ b/kernel/sys_ni.c
@@ -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);
diff --git a/mm/Makefile b/mm/Makefile
index 8e105e5b3e29..e71b15743ce6 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -54,7 +54,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
mm_init.o percpu.o slab_common.o \
compaction.o \
interval_tree.o list_lru.o workingset.o \
- debug.o gup.o mmap_lock.o $(mmu-y)
+ debug.o gup.o mmap_lock.o cachestat.o $(mmu-y)

# Give 'page_alloc' its own module-parameter namespace
page-alloc-y := page_alloc.o
diff --git a/mm/cachestat.c b/mm/cachestat.c
new file mode 100644
index 000000000000..59067f26eba3
--- /dev/null
+++ b/mm/cachestat.c
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/*
+ * The cachestat() system call.
+ */
+#include <linux/file.h>
+#include <linux/fs.h>
+#include <linux/syscalls.h>
+#include <linux/shmem_fs.h>
+#include <linux/swap.h>
+#include <linux/swapops.h>
+#include <uapi/linux/mman.h>
+
+#include "swap.h"
+
+/*
+ * The cachestat(4) system call.
+ *
+ * cachestat() returns the page cache status of a file in the
+ * bytes specified by `off` and `len`: number of cached pages,
+ * number of dirty pages, number of pages marked for writeback,
+ * number of (recently) evicted pages.
+ *
+ * `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.
+ *
+ * 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_DEFINE5(cachestat, unsigned int, fd, off_t, off, size_t, len,
+ size_t, cstat_size, struct cachestat __user *, cstat)
+{
+ struct fd f;
+ struct cachestat cs;
+
+ memset(&cs, 0, sizeof(struct cachestat));
+
+ if (off < 0 || len < 0 ||
+ cstat_size > sizeof(struct cachestat) ||
+ cstat_size < 0)
+ return -EINVAL;
+
+ if (!access_ok(cstat, sizeof(struct cachestat)))
+ return -EFAULT;
+
+ f = fdget(fd);
+ if (f.file) {
+ struct address_space *mapping = f.file->f_mapping;
+ pgoff_t first_index = off >> PAGE_SHIFT;
+ pgoff_t last_index =
+ len == 0 ? ULONG_MAX : (off + len - 1) >> PAGE_SHIFT;
+ XA_STATE(xas, &mapping->i_pages, first_index);
+ struct folio *folio;
+
+ rcu_read_lock();
+ xas_for_each(&xas, folio, last_index) {
+ if (xas_retry(&xas, folio) || !folio)
+ continue;
+
+ if (xa_is_value(folio)) {
+ /* page is evicted */
+ void *shadow = (void *)folio;
+ bool workingset; /* not used */
+
+ cs.nr_evicted += 1;
+
+#if defined(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 += 1;
+
+ continue;
+ }
+
+ /* page is in cache */
+ cs.nr_cache += 1;
+
+ if (folio_test_dirty(folio))
+ cs.nr_dirty += 1;
+
+ if (folio_test_writeback(folio))
+ cs.nr_writeback += 1;
+ }
+ rcu_read_unlock();
+ fdput(f);
+
+ if (copy_to_user(cstat, &cs, cstat_size))
+ return -EFAULT;
+
+ return 0;
+ }
+
+ return -EBADF;
+}
--
2.30.2


2022-12-05 23:27:01

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] cachestat: implement cachestat syscall

Hi Nhat,

I love your patch! Perhaps something to improve:

[auto build test WARNING on shuah-kselftest/next]
[also build test WARNING on shuah-kselftest/fixes tip/x86/asm akpm-mm/mm-everything linus/master v6.1-rc8 next-20221205]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Nhat-Pham/cachestat-a-new-syscall-for-page-cache-state-of-files/20221206-015308
base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
patch link: https://lore.kernel.org/r/20221205175140.1543229-4-nphamcs%40gmail.com
patch subject: [PATCH v2 3/4] cachestat: implement cachestat syscall
config: m68k-allyesconfig
compiler: m68k-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/b45ed49f9d4f655a5e95c14f7019b455929f419d
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Nhat-Pham/cachestat-a-new-syscall-for-page-cache-state-of-files/20221206-015308
git checkout b45ed49f9d4f655a5e95c14f7019b455929f419d
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=m68k prepare

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

All warnings (new ones prefixed by >>):

>> <stdin>:1565:2: warning: #warning syscall cachestat not implemented [-Wcpp]
--
>> <stdin>:1565:2: warning: #warning syscall cachestat not implemented [-Wcpp]
--
scripts/genksyms/parse.y: warning: 9 shift/reduce conflicts [-Wconflicts-sr]
scripts/genksyms/parse.y: warning: 5 reduce/reduce conflicts [-Wconflicts-rr]
scripts/genksyms/parse.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
>> <stdin>:1565:2: warning: #warning syscall cachestat not implemented [-Wcpp]

--
0-DAY CI Kernel Test Service
https://01.org/lkp


Attachments:
(No filename) (2.31 kB)
config (288.30 kB)
Download all attachments

2022-12-06 05:25:48

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] cachestat: implement cachestat syscall

Hi Nhat,

I love your patch! Yet something to improve:

[auto build test ERROR on shuah-kselftest/next]
[also build test ERROR on shuah-kselftest/fixes tip/x86/asm akpm-mm/mm-everything linus/master v6.1-rc8 next-20221205]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Nhat-Pham/cachestat-a-new-syscall-for-page-cache-state-of-files/20221206-015308
base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
patch link: https://lore.kernel.org/r/20221205175140.1543229-4-nphamcs%40gmail.com
patch subject: [PATCH v2 3/4] cachestat: implement cachestat syscall
config: x86_64-randconfig-a001-20221205
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/b45ed49f9d4f655a5e95c14f7019b455929f419d
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Nhat-Pham/cachestat-a-new-syscall-for-page-cache-state-of-files/20221206-015308
git checkout b45ed49f9d4f655a5e95c14f7019b455929f419d
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

In file included from <built-in>:1:
>> ./usr/include/linux/mman.h:45:2: error: unknown type name '__u64'
__u64 nr_cache;
^
./usr/include/linux/mman.h:46:2: error: unknown type name '__u64'
__u64 nr_dirty;
^
./usr/include/linux/mman.h:47:2: error: unknown type name '__u64'
__u64 nr_writeback;
^
./usr/include/linux/mman.h:48:2: error: unknown type name '__u64'
__u64 nr_evicted;
^
./usr/include/linux/mman.h:49:2: error: unknown type name '__u64'
__u64 nr_recently_evicted;
^
5 errors generated.

--
0-DAY CI Kernel Test Service
https://01.org/lkp


Attachments:
(No filename) (2.51 kB)
config (136.88 kB)
Download all attachments

2022-12-06 05:38:50

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] cachestat: implement cachestat syscall

Hi Nhat,

I love your patch! Perhaps something to improve:

[auto build test WARNING on shuah-kselftest/next]
[also build test WARNING on shuah-kselftest/fixes tip/x86/asm akpm-mm/mm-everything linus/master v6.1-rc8 next-20221205]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Nhat-Pham/cachestat-a-new-syscall-for-page-cache-state-of-files/20221206-015308
base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
patch link: https://lore.kernel.org/r/20221205175140.1543229-4-nphamcs%40gmail.com
patch subject: [PATCH v2 3/4] cachestat: implement cachestat syscall
config: arm-lpc32xx_defconfig
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 6e4cea55f0d1104408b26ac574566a0e4de48036)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/intel-lab-lkp/linux/commit/b45ed49f9d4f655a5e95c14f7019b455929f419d
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Nhat-Pham/cachestat-a-new-syscall-for-page-cache-state-of-files/20221206-015308
git checkout b45ed49f9d4f655a5e95c14f7019b455929f419d
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm prepare

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

All warnings (new ones prefixed by >>):

>> <stdin>:1565:2: warning: syscall cachestat not implemented [-W#warnings]
#warning syscall cachestat not implemented
^
1 warning generated.
--
>> <stdin>:1565:2: warning: syscall cachestat not implemented [-W#warnings]
#warning syscall cachestat not implemented
^
1 warning generated.
--
>> <stdin>:1565:2: warning: syscall cachestat not implemented [-W#warnings]
#warning syscall cachestat not implemented
^
1 warning generated.

--
0-DAY CI Kernel Test Service
https://01.org/lkp


Attachments:
(No filename) (2.45 kB)
config (125.78 kB)
Download all attachments

2022-12-07 02:04:34

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] cachestat: implement cachestat syscall

Hi Nhat,

I love your patch! Perhaps something to improve:

[auto build test WARNING on shuah-kselftest/next]
[also build test WARNING on shuah-kselftest/fixes tip/x86/asm akpm-mm/mm-everything linus/master v6.1-rc8 next-20221206]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Nhat-Pham/cachestat-a-new-syscall-for-page-cache-state-of-files/20221206-015308
base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
patch link: https://lore.kernel.org/r/20221205175140.1543229-4-nphamcs%40gmail.com
patch subject: [PATCH v2 3/4] cachestat: implement cachestat syscall
config: i386-randconfig-m021-20221205
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

smatch warnings:
mm/cachestat.c:53 __do_sys_cachestat() warn: unsigned 'len' is never less than zero.
mm/cachestat.c:55 __do_sys_cachestat() warn: unsigned 'cstat_size' is never less than zero.
mm/cachestat.c:53 __do_sys_cachestat() warn: unsigned 'len' is never less than zero.
mm/cachestat.c:55 __do_sys_cachestat() warn: unsigned 'cstat_size' is never less than zero.

vim +/len +53 mm/cachestat.c

15
16 /*
17 * The cachestat(4) system call.
18 *
19 * cachestat() returns the page cache status of a file in the
20 * bytes specified by `off` and `len`: number of cached pages,
21 * number of dirty pages, number of pages marked for writeback,
22 * number of (recently) evicted pages.
23 *
24 * `off` and `len` must be non-negative integers. If `len` > 0,
25 * the queried range is [`off`, `off` + `len`]. If `len` == 0,
26 * we will query in the range from `off` to the end of the file.
27 *
28 * `cstat_size` allows users to obtain partial results. The syscall
29 * will copy the first `csstat_size` bytes to the specified userspace
30 * memory. It also makes the cachestat struct extensible - new fields
31 * can be added in the future without breaking existing usage.
32 * `cstat_size` must be a non-negative value that is no larger than
33 * the current size of the cachestat struct.
34 *
35 * Because the status of a page can change after cachestat() checks it
36 * but before it returns to the application, the returned values may
37 * contain stale information.
38 *
39 * return values:
40 * zero - success
41 * -EFAULT - cstat points to an illegal address
42 * -EINVAL - invalid arguments
43 * -EBADF - invalid file descriptor
44 */
45 SYSCALL_DEFINE5(cachestat, unsigned int, fd, off_t, off, size_t, len,
46 size_t, cstat_size, struct cachestat __user *, cstat)
47 {
48 struct fd f;
49 struct cachestat cs;
50
51 memset(&cs, 0, sizeof(struct cachestat));
52
> 53 if (off < 0 || len < 0 ||
54 cstat_size > sizeof(struct cachestat) ||
> 55 cstat_size < 0)
56 return -EINVAL;
57
58 if (!access_ok(cstat, sizeof(struct cachestat)))
59 return -EFAULT;
60
61 f = fdget(fd);
62 if (f.file) {
63 struct address_space *mapping = f.file->f_mapping;
64 pgoff_t first_index = off >> PAGE_SHIFT;
65 pgoff_t last_index =
66 len == 0 ? ULONG_MAX : (off + len - 1) >> PAGE_SHIFT;
67 XA_STATE(xas, &mapping->i_pages, first_index);
68 struct folio *folio;
69
70 rcu_read_lock();
71 xas_for_each(&xas, folio, last_index) {
72 if (xas_retry(&xas, folio) || !folio)
73 continue;
74
75 if (xa_is_value(folio)) {
76 /* page is evicted */
77 void *shadow = (void *)folio;
78 bool workingset; /* not used */
79
80 cs.nr_evicted += 1;
81

--
0-DAY CI Kernel Test Service
https://01.org/lkp


Attachments:
(No filename) (4.06 kB)
config (142.92 kB)
Download all attachments

2022-12-11 01:17:14

by Al Viro

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] cachestat: implement cachestat syscall

On Mon, Dec 05, 2022 at 09:51:39AM -0800, Nhat Pham wrote:

> + if (!access_ok(cstat, sizeof(struct cachestat)))
> + return -EFAULT;

What for? You are using copy_to_user() later, right?

> + f = fdget(fd);
> + if (f.file) {

It would be easier to read if you inverted the condition here.

2022-12-12 14:19:37

by Brian Foster

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] cachestat: implement cachestat syscall

On Sat, Dec 10, 2022 at 11:51:32PM +0000, Al Viro wrote:
> On Mon, Dec 05, 2022 at 09:51:39AM -0800, Nhat Pham wrote:
>
> > + if (!access_ok(cstat, sizeof(struct cachestat)))
> > + return -EFAULT;
>
> What for? You are using copy_to_user() later, right?
>
> > + f = fdget(fd);
> > + if (f.file) {
>
> It would be easier to read if you inverted the condition here.
>

Seconded.. I mentioned the same the last time I looked at this. On
looking again, perhaps it might even make sense to create a
filemap_cachestat() to split up the syscall bits from the associated map
walking bits..? That subsequently raises the question of whether a new
.c file is really necessary..

Brian

2022-12-12 16:36:17

by Matthew Wilcox

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] cachestat: implement cachestat syscall

On Mon, Dec 12, 2022 at 08:23:31AM -0800, Nhat Pham wrote:
> > It would be easier to read if you inverted the condition here.
>
> Oh I think I tried
>
> if (!f.file)
> return -EBADF;
>
> here, but there are some mixing-code-with-decl warnings.
> If I recall correctly, the problem is with this line:
>
> XA_STATE(xas, &mapping->i_pages, first_index);
>
> which is expanded into a declaration:
>
> #define XA_STATE(name, array, index) \
> struct xa_state name = __XA_STATE(array, index, 0, 0)
>
> It requires a valid mapping though, which is
> obtained from f.file:
>
> struct address_space *mapping = f.file->f_mapping;
>
> so it cannot be moved above the if(!f.file) check either...

Perhaps you're trying to do too much in a single function?

2022-12-12 17:04:32

by Nhat Pham

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] cachestat: implement cachestat syscall

On Sat, Dec 10, 2022 at 3:51 PM Al Viro <[email protected]> wrote:
>
> On Mon, Dec 05, 2022 at 09:51:39AM -0800, Nhat Pham wrote:
>
> > + if (!access_ok(cstat, sizeof(struct cachestat)))
> > + return -EFAULT;
>
> What for? You are using copy_to_user() later, right?

Oh I didn't realize copy_to_user() would suffice - mincore also
has this check:

if (!access_ok(vec, pages))
return -EFAULT;

(even though it also has a similar check with copy_to_user),
so I just erred on the side of safety and included it. If this is
redundant, I'll just remove it from the next version.

>
> > + f = fdget(fd);
> > + if (f.file) {
>
> It would be easier to read if you inverted the condition here.

Oh I think I tried

if (!f.file)
return -EBADF;

here, but there are some mixing-code-with-decl warnings.
If I recall correctly, the problem is with this line:

XA_STATE(xas, &mapping->i_pages, first_index);

which is expanded into a declaration:

#define XA_STATE(name, array, index) \
struct xa_state name = __XA_STATE(array, index, 0, 0)

It requires a valid mapping though, which is
obtained from f.file:

struct address_space *mapping = f.file->f_mapping;

so it cannot be moved above the if(!f.file) check either...

2022-12-12 17:07:20

by Nhat Pham

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] cachestat: implement cachestat syscall

On Mon, Dec 12, 2022 at 8:24 AM Matthew Wilcox <[email protected]> wrote:
>
> On Mon, Dec 12, 2022 at 08:23:31AM -0800, Nhat Pham wrote:
> > > It would be easier to read if you inverted the condition here.
> >
> > Oh I think I tried
> >
> > if (!f.file)
> > return -EBADF;
> >
> > here, but there are some mixing-code-with-decl warnings.
> > If I recall correctly, the problem is with this line:
> >
> > XA_STATE(xas, &mapping->i_pages, first_index);
> >
> > which is expanded into a declaration:
> >
> > #define XA_STATE(name, array, index) \
> > struct xa_state name = __XA_STATE(array, index, 0, 0)
> >
> > It requires a valid mapping though, which is
> > obtained from f.file:
> >
> > struct address_space *mapping = f.file->f_mapping;
> >
> > so it cannot be moved above the if(!f.file) check either...
>
> Perhaps you're trying to do too much in a single function?

That's fair. In an ancient version of this, I did try to separate
the folio-walking logic into its own function, but then decided
against it because it seemed unnecessary. That was before
the complexity of that piece of logic blew up to the current
situation though, so perhaps it is time to revisit this decision
and refactor it.

2022-12-12 17:31:32

by Nhat Pham

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] cachestat: implement cachestat syscall

On Mon, Dec 12, 2022 at 5:22 AM Brian Foster <[email protected]> wrote:
>
> On Sat, Dec 10, 2022 at 11:51:32PM +0000, Al Viro wrote:
> > On Mon, Dec 05, 2022 at 09:51:39AM -0800, Nhat Pham wrote:
> >
> > > + if (!access_ok(cstat, sizeof(struct cachestat)))
> > > + return -EFAULT;
> >
> > What for? You are using copy_to_user() later, right?
> >
> > > + f = fdget(fd);
> > > + if (f.file) {
> >
> > It would be easier to read if you inverted the condition here.
> >
>
> Seconded.. I mentioned the same the last time I looked at this. On
> looking again, perhaps it might even make sense to create a
> filemap_cachestat() to split up the syscall bits from the associated map
> walking bits..? That subsequently raises the question of whether a new
> .c file is really necessary..
>
> Brian
>

Personally, I think it's better to add a new .c file for the new syscall.
But I'm impartial with respect to the refactoring otherwise. I'll just give it a
shot and see if it looks cleaner.

(BTW - I just realized this is the v2 thread. I have sent out a v3 - feel free
to take a look at that as well! These comments are still valid for the new
version though).