Skip to content

Commit

Permalink
net: Remove the err argument from sock_from_file
Browse files Browse the repository at this point in the history
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 <viro@zeniv.linux.org.uk>
Signed-off-by: Florent Revest <revest@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: KP Singh <kpsingh@google.com>
Link: https://lore.kernel.org/bpf/20201204113609.1850150-1-revest@google.com
  • Loading branch information
FlorentRevest authored and borkmann committed Dec 4, 2020
1 parent 5c667dc commit dba4a92
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 33 deletions.
3 changes: 1 addition & 2 deletions fs/eventpoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,11 @@ static inline void ep_set_busy_poll_napi_id(struct epitem *epi)
unsigned int napi_id;
struct socket *sock;
struct sock *sk;
int err;

if (!net_busy_loop_on())
return;

sock = sock_from_file(epi->ffd.file, &err);
sock = sock_from_file(epi->ffd.file);
if (!sock)
return;

Expand Down
16 changes: 8 additions & 8 deletions fs/io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -4356,9 +4356,9 @@ static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
unsigned flags;
int ret;

sock = sock_from_file(req->file, &ret);
sock = sock_from_file(req->file);
if (unlikely(!sock))
return ret;
return -ENOTSOCK;

if (req->async_data) {
kmsg = req->async_data;
Expand Down Expand Up @@ -4405,9 +4405,9 @@ static int io_send(struct io_kiocb *req, bool force_nonblock,
unsigned flags;
int ret;

sock = sock_from_file(req->file, &ret);
sock = sock_from_file(req->file);
if (unlikely(!sock))
return ret;
return -ENOTSOCK;

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

sock = sock_from_file(req->file, &ret);
sock = sock_from_file(req->file);
if (unlikely(!sock))
return ret;
return -ENOTSOCK;

if (req->async_data) {
kmsg = req->async_data;
Expand Down Expand Up @@ -4647,9 +4647,9 @@ static int io_recv(struct io_kiocb *req, bool force_nonblock,
unsigned flags;
int ret, cflags = 0;

sock = sock_from_file(req->file, &ret);
sock = sock_from_file(req->file);
if (unlikely(!sock))
return ret;
return -ENOTSOCK;

if (req->flags & REQ_F_BUFFER_SELECT) {
kbuf = io_recv_buffer_select(req, !force_nonblock);
Expand Down
2 changes: 1 addition & 1 deletion include/linux/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ int sock_sendmsg(struct socket *sock, struct msghdr *msg);
int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);
struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
struct socket *sockfd_lookup(int fd, int *err);
struct socket *sock_from_file(struct file *file, int *err);
struct socket *sock_from_file(struct file *file);
#define sockfd_put(sock) fput(sock->file)
int net_ratelimit(void);

Expand Down
3 changes: 1 addition & 2 deletions net/core/netclassid_cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ struct update_classid_context {

static int update_classid_sock(const void *v, struct file *file, unsigned n)
{
int err;
struct update_classid_context *ctx = (void *)v;
struct socket *sock = sock_from_file(file, &err);
struct socket *sock = sock_from_file(file);

if (sock) {
spin_lock(&cgroup_sk_update_lock);
Expand Down
3 changes: 1 addition & 2 deletions net/core/netprio_cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ static ssize_t write_priomap(struct kernfs_open_file *of,

static int update_netprio(const void *v, struct file *file, unsigned n)
{
int err;
struct socket *sock = sock_from_file(file, &err);
struct socket *sock = sock_from_file(file);
if (sock) {
spin_lock(&cgroup_sk_update_lock);
sock_cgroup_set_prioidx(&sock->sk->sk_cgrp_data,
Expand Down
8 changes: 1 addition & 7 deletions net/core/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -2827,14 +2827,8 @@ EXPORT_SYMBOL(sock_no_mmap);
void __receive_sock(struct file *file)
{
struct socket *sock;
int error;

/*
* The resulting value of "error" is ignored here since we only
* need to take action when the file is a socket and testing
* "sock" for NULL is sufficient.
*/
sock = sock_from_file(file, &error);
sock = sock_from_file(file);
if (sock) {
sock_update_netprioidx(&sock->sk->sk_cgrp_data);
sock_update_classid(&sock->sk->sk_cgrp_data);
Expand Down
27 changes: 16 additions & 11 deletions net/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,15 @@ static int sock_map_fd(struct socket *sock, int flags)
/**
* sock_from_file - Return the &socket bounded to @file.
* @file: file
* @err: pointer to an error code return
*
* On failure returns %NULL and assigns -ENOTSOCK to @err.
* On failure returns %NULL.
*/

struct socket *sock_from_file(struct file *file, int *err)
struct socket *sock_from_file(struct file *file)
{
if (file->f_op == &socket_file_ops)
return file->private_data; /* set in sock_map_fd */

*err = -ENOTSOCK;
return NULL;
}
EXPORT_SYMBOL(sock_from_file);
Expand Down Expand Up @@ -484,9 +482,11 @@ struct socket *sockfd_lookup(int fd, int *err)
return NULL;
}

sock = sock_from_file(file, err);
if (!sock)
sock = sock_from_file(file);
if (!sock) {
*err = -ENOTSOCK;
fput(file);
}
return sock;
}
EXPORT_SYMBOL(sockfd_lookup);
Expand All @@ -498,11 +498,12 @@ static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)

*err = -EBADF;
if (f.file) {
sock = sock_from_file(f.file, err);
sock = sock_from_file(f.file);
if (likely(sock)) {
*fput_needed = f.flags & FDPUT_FPUT;
return sock;
}
*err = -ENOTSOCK;
fdput(f);
}
return NULL;
Expand Down Expand Up @@ -1693,9 +1694,11 @@ int __sys_accept4_file(struct file *file, unsigned file_flags,
if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;

sock = sock_from_file(file, &err);
if (!sock)
sock = sock_from_file(file);
if (!sock) {
err = -ENOTSOCK;
goto out;
}

err = -ENFILE;
newsock = sock_alloc();
Expand Down Expand Up @@ -1818,9 +1821,11 @@ int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
struct socket *sock;
int err;

sock = sock_from_file(file, &err);
if (!sock)
sock = sock_from_file(file);
if (!sock) {
err = -ENOTSOCK;
goto out;
}

err =
security_socket_connect(sock, (struct sockaddr *)address, addrlen);
Expand Down

0 comments on commit dba4a92

Please sign in to comment.