Skip to content

Commit dba4a92

Browse files
FlorentRevestborkmann
authored andcommitted
net: Remove the err argument from sock_from_file
Currently, the sock_from_file prototype takes an "err" pointer that is either not set or set to -ENOTSOCK IFF the returned socket is NULL. This makes the error redundant and it is ignored by a few callers. This patch simplifies the API by letting callers deduce the error based on whether the returned socket is NULL or not. Suggested-by: Al Viro <[email protected]> Signed-off-by: Florent Revest <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: KP Singh <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5c667dc commit dba4a92

File tree

7 files changed

+29
-33
lines changed

7 files changed

+29
-33
lines changed

fs/eventpoll.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,11 @@ static inline void ep_set_busy_poll_napi_id(struct epitem *epi)
416416
unsigned int napi_id;
417417
struct socket *sock;
418418
struct sock *sk;
419-
int err;
420419

421420
if (!net_busy_loop_on())
422421
return;
423422

424-
sock = sock_from_file(epi->ffd.file, &err);
423+
sock = sock_from_file(epi->ffd.file);
425424
if (!sock)
426425
return;
427426

fs/io_uring.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -4356,9 +4356,9 @@ static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
43564356
unsigned flags;
43574357
int ret;
43584358

4359-
sock = sock_from_file(req->file, &ret);
4359+
sock = sock_from_file(req->file);
43604360
if (unlikely(!sock))
4361-
return ret;
4361+
return -ENOTSOCK;
43624362

43634363
if (req->async_data) {
43644364
kmsg = req->async_data;
@@ -4405,9 +4405,9 @@ static int io_send(struct io_kiocb *req, bool force_nonblock,
44054405
unsigned flags;
44064406
int ret;
44074407

4408-
sock = sock_from_file(req->file, &ret);
4408+
sock = sock_from_file(req->file);
44094409
if (unlikely(!sock))
4410-
return ret;
4410+
return -ENOTSOCK;
44114411

44124412
ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
44134413
if (unlikely(ret))
@@ -4584,9 +4584,9 @@ static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
45844584
unsigned flags;
45854585
int ret, cflags = 0;
45864586

4587-
sock = sock_from_file(req->file, &ret);
4587+
sock = sock_from_file(req->file);
45884588
if (unlikely(!sock))
4589-
return ret;
4589+
return -ENOTSOCK;
45904590

45914591
if (req->async_data) {
45924592
kmsg = req->async_data;
@@ -4647,9 +4647,9 @@ static int io_recv(struct io_kiocb *req, bool force_nonblock,
46474647
unsigned flags;
46484648
int ret, cflags = 0;
46494649

4650-
sock = sock_from_file(req->file, &ret);
4650+
sock = sock_from_file(req->file);
46514651
if (unlikely(!sock))
4652-
return ret;
4652+
return -ENOTSOCK;
46534653

46544654
if (req->flags & REQ_F_BUFFER_SELECT) {
46554655
kbuf = io_recv_buffer_select(req, !force_nonblock);

include/linux/net.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int sock_sendmsg(struct socket *sock, struct msghdr *msg);
240240
int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);
241241
struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
242242
struct socket *sockfd_lookup(int fd, int *err);
243-
struct socket *sock_from_file(struct file *file, int *err);
243+
struct socket *sock_from_file(struct file *file);
244244
#define sockfd_put(sock) fput(sock->file)
245245
int net_ratelimit(void);
246246

net/core/netclassid_cgroup.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ struct update_classid_context {
6868

6969
static int update_classid_sock(const void *v, struct file *file, unsigned n)
7070
{
71-
int err;
7271
struct update_classid_context *ctx = (void *)v;
73-
struct socket *sock = sock_from_file(file, &err);
72+
struct socket *sock = sock_from_file(file);
7473

7574
if (sock) {
7675
spin_lock(&cgroup_sk_update_lock);

net/core/netprio_cgroup.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ static ssize_t write_priomap(struct kernfs_open_file *of,
220220

221221
static int update_netprio(const void *v, struct file *file, unsigned n)
222222
{
223-
int err;
224-
struct socket *sock = sock_from_file(file, &err);
223+
struct socket *sock = sock_from_file(file);
225224
if (sock) {
226225
spin_lock(&cgroup_sk_update_lock);
227226
sock_cgroup_set_prioidx(&sock->sk->sk_cgrp_data,

net/core/sock.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -2827,14 +2827,8 @@ EXPORT_SYMBOL(sock_no_mmap);
28272827
void __receive_sock(struct file *file)
28282828
{
28292829
struct socket *sock;
2830-
int error;
28312830

2832-
/*
2833-
* The resulting value of "error" is ignored here since we only
2834-
* need to take action when the file is a socket and testing
2835-
* "sock" for NULL is sufficient.
2836-
*/
2837-
sock = sock_from_file(file, &error);
2831+
sock = sock_from_file(file);
28382832
if (sock) {
28392833
sock_update_netprioidx(&sock->sk->sk_cgrp_data);
28402834
sock_update_classid(&sock->sk->sk_cgrp_data);

net/socket.c

+16-11
Original file line numberDiff line numberDiff line change
@@ -445,17 +445,15 @@ static int sock_map_fd(struct socket *sock, int flags)
445445
/**
446446
* sock_from_file - Return the &socket bounded to @file.
447447
* @file: file
448-
* @err: pointer to an error code return
449448
*
450-
* On failure returns %NULL and assigns -ENOTSOCK to @err.
449+
* On failure returns %NULL.
451450
*/
452451

453-
struct socket *sock_from_file(struct file *file, int *err)
452+
struct socket *sock_from_file(struct file *file)
454453
{
455454
if (file->f_op == &socket_file_ops)
456455
return file->private_data; /* set in sock_map_fd */
457456

458-
*err = -ENOTSOCK;
459457
return NULL;
460458
}
461459
EXPORT_SYMBOL(sock_from_file);
@@ -484,9 +482,11 @@ struct socket *sockfd_lookup(int fd, int *err)
484482
return NULL;
485483
}
486484

487-
sock = sock_from_file(file, err);
488-
if (!sock)
485+
sock = sock_from_file(file);
486+
if (!sock) {
487+
*err = -ENOTSOCK;
489488
fput(file);
489+
}
490490
return sock;
491491
}
492492
EXPORT_SYMBOL(sockfd_lookup);
@@ -498,11 +498,12 @@ static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
498498

499499
*err = -EBADF;
500500
if (f.file) {
501-
sock = sock_from_file(f.file, err);
501+
sock = sock_from_file(f.file);
502502
if (likely(sock)) {
503503
*fput_needed = f.flags & FDPUT_FPUT;
504504
return sock;
505505
}
506+
*err = -ENOTSOCK;
506507
fdput(f);
507508
}
508509
return NULL;
@@ -1693,9 +1694,11 @@ int __sys_accept4_file(struct file *file, unsigned file_flags,
16931694
if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
16941695
flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
16951696

1696-
sock = sock_from_file(file, &err);
1697-
if (!sock)
1697+
sock = sock_from_file(file);
1698+
if (!sock) {
1699+
err = -ENOTSOCK;
16981700
goto out;
1701+
}
16991702

17001703
err = -ENFILE;
17011704
newsock = sock_alloc();
@@ -1818,9 +1821,11 @@ int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
18181821
struct socket *sock;
18191822
int err;
18201823

1821-
sock = sock_from_file(file, &err);
1822-
if (!sock)
1824+
sock = sock_from_file(file);
1825+
if (!sock) {
1826+
err = -ENOTSOCK;
18231827
goto out;
1828+
}
18241829

18251830
err =
18261831
security_socket_connect(sock, (struct sockaddr *)address, addrlen);

0 commit comments

Comments
 (0)