Re: [PATCH] net: socket: Use fdget() and fdput()

From: Al Viro
Date: Thu May 11 2023 - 01:25:16 EST


On Fri, May 05, 2023 at 05:06:41PM +0800, ye.xingchen@xxxxxxxxxx wrote:
> By using the fdget function, the socket object, can be quickly obtained
> from the process's file descriptor table without the need to obtain the
> file descriptor first before passing it as a parameter to the fget
> function.

> struct socket *sockfd_lookup(int fd, int *err)
> {
> - struct file *file;
> + struct fd f = fdget(fd);
> struct socket *sock;
>
> - file = fget(fd);
> - if (!file) {
> + if (!f.file) {
> *err = -EBADF;
> return NULL;
> }
>
> - sock = sock_from_file(file);
> + sock = sock_from_file(f.file);
> if (!sock) {
> *err = -ENOTSOCK;
> - fput(file);
> + fdput(f);
> }
> return sock;

Suppose you've got that far. If descriptor table had been shared, you've
bumped the refcount of struct file. If it hadn't been, that refcount
had remained unchanged. And there is no way for the caller of this
function to tell one outcome from another.

That can't work.