Re: [PATCH] vfio: Use filp instead of fd

Al Viro
2022-09-26T14:56:02Z

On Mon, Sep 26, 2022 at 02:54:07AM -0400, Deming Wang wrote:
> The function of kvm_vfio_group_set_spapr_tce and kvm_vfio_group_del
> use fd indirectly.But,it only be used for fd.file. So,we can directly
> use the struct of file instead.
>
> Signed-off-by: Deming Wang <wangdeming@inspur.com>

NAK. fget() is for the cases when we must keep the reference across the
syscall boundary/pass to another thread/etc.

If fdget() is applicable, it's a better alternative. And I would suggest
you to look at the generated code - it pretty much turns into
struct file *file;
int need_fput;

r = __fdget(fd);
need_fput = r & 3;
r &= ~3;
file = (struct file *)r;

....

if (unlikely(need_fput))
fput(file);

Note that we are *not* actually passing a structure out of a function, etc. -
fdget() is inlined and out-of-line part returns unsigned long. Lower two bits
carry flags, the rest - file pointer. Rearrangement into struct fd is done
in the caller and compiler manages to dissolve that struct into a couple of
local variables.

Incidentally, pretty much the only thing in struct fd besides struct file pointer
is that "have we failed to skip bumping the reference count?" flag. Between
fdget() and fdput() only the file pointer is used - in all users. fdput()
uses the flag to decide whether it needs to bother with refcount at all.