Skip to contentSkip to conversation

net: socket: Use fdget() and fdput()

PATCH In review [email protected] 3 participants 3 messages May 5, 2023 – May 11, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: socket: Use fdget() and fdput()

From: Ye Xingchen <[email protected]>

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.

---
 net/socket.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
net/socket.c +4 -5
index a7b4b37d86df..84daba774432 100644
@@ -528,19 +528,18 @@ EXPORT_SYMBOL(sock_from_file);
 
 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;
 }
Signature · 1 line
2.25.1
#1 lore raw
Quoted 8 lines · On Fri, May 5, 2023 at 11:06 AM <[email protected]> wrote:

From: Ye Xingchen <[email protected]>

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.

net-next is currently closed.

There are good reasons we have sockfd_lookup() and sockfd_lookup_light(), you probably should take a deeper look at the difference between them.

Quoted 35 lines
---
 net/socket.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
net/socket.c +4 -6
index a7b4b37d86df..84daba774432 100644
@@ -528,19 +528,18 @@ EXPORT_SYMBOL(sock_from_file);
 
 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;
 }
--

2.25.1

#2 lore raw
In reply to [email protected]
Quoted 16 lines · On Fri, May 05, 2023 at 05:06:41PM +0800, [email protected] 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)
{

net/socket.c +2 -3
-	struct file *file;
+	struct fd f = fdget(fd);
 	struct socket *sock;
 
-	file = fget(fd);
-	if (!file) {
+	if (!f.file) {
 		*err = -EBADF;
 		return NULL;

net/socket.c EXPORT_SYMBOL(sock_from_file); · line 544

Quoted 10 lines
net/socket.c +2 -2
 	}
 
-	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.