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

From: Nhat Pham
Date: Mon Dec 12 2022 - 11:24:15 EST


On Sat, Dec 10, 2022 at 3:51 PM Al Viro <viro@xxxxxxxxxxxxxxxxxx> 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...