Skip to content

Commit da214a4

Browse files
committed
net: add __sys_socket_file()
This works like __sys_socket(), except instead of allocating and returning a socket fd, it just returns the file associated with the socket. No fd is installed into the process file table. This is similar to do_accept(), and allows io_uring to use this without instantiating a file descriptor in the process file table. Signed-off-by: Jens Axboe <[email protected]> Acked-by: David S. Miller <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0200ce6 commit da214a4

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

include/linux/socket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ extern struct file *do_accept(struct file *file, unsigned file_flags,
434434
extern int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
435435
int __user *upeer_addrlen, int flags);
436436
extern int __sys_socket(int family, int type, int protocol);
437+
extern struct file *__sys_socket_file(int family, int type, int protocol);
437438
extern int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen);
438439
extern int __sys_connect_file(struct file *file, struct sockaddr_storage *addr,
439440
int addrlen, int file_flags);

net/socket.c

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ static int sock_map_fd(struct socket *sock, int flags)
504504
struct socket *sock_from_file(struct file *file)
505505
{
506506
if (file->f_op == &socket_file_ops)
507-
return file->private_data; /* set in sock_map_fd */
507+
return file->private_data; /* set in sock_alloc_file */
508508

509509
return NULL;
510510
}
@@ -1538,29 +1538,61 @@ int sock_create_kern(struct net *net, int family, int type, int protocol, struct
15381538
}
15391539
EXPORT_SYMBOL(sock_create_kern);
15401540

1541-
int __sys_socket(int family, int type, int protocol)
1541+
static struct socket *__sys_socket_create(int family, int type, int protocol)
15421542
{
1543-
int retval;
15441543
struct socket *sock;
1545-
int flags;
1544+
int retval;
15461545

15471546
/* Check the SOCK_* constants for consistency. */
15481547
BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
15491548
BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
15501549
BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
15511550
BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
15521551

1553-
flags = type & ~SOCK_TYPE_MASK;
1554-
if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1555-
return -EINVAL;
1552+
if ((type & ~SOCK_TYPE_MASK) & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1553+
return ERR_PTR(-EINVAL);
15561554
type &= SOCK_TYPE_MASK;
15571555

1556+
retval = sock_create(family, type, protocol, &sock);
1557+
if (retval < 0)
1558+
return ERR_PTR(retval);
1559+
1560+
return sock;
1561+
}
1562+
1563+
struct file *__sys_socket_file(int family, int type, int protocol)
1564+
{
1565+
struct socket *sock;
1566+
struct file *file;
1567+
int flags;
1568+
1569+
sock = __sys_socket_create(family, type, protocol);
1570+
if (IS_ERR(sock))
1571+
return ERR_CAST(sock);
1572+
1573+
flags = type & ~SOCK_TYPE_MASK;
15581574
if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
15591575
flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
15601576

1561-
retval = sock_create(family, type, protocol, &sock);
1562-
if (retval < 0)
1563-
return retval;
1577+
file = sock_alloc_file(sock, flags, NULL);
1578+
if (IS_ERR(file))
1579+
sock_release(sock);
1580+
1581+
return file;
1582+
}
1583+
1584+
int __sys_socket(int family, int type, int protocol)
1585+
{
1586+
struct socket *sock;
1587+
int flags;
1588+
1589+
sock = __sys_socket_create(family, type, protocol);
1590+
if (IS_ERR(sock))
1591+
return PTR_ERR(sock);
1592+
1593+
flags = type & ~SOCK_TYPE_MASK;
1594+
if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1595+
flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
15641596

15651597
return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
15661598
}

0 commit comments

Comments
 (0)