mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	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 <axboe@kernel.dk> Acked-by: David S. Miller <davem@davemloft.net> Link: https://lore.kernel.org/r/20220412202240.234207-2-axboe@kernel.dk
This commit is contained in:
		
							parent
							
								
									0200ce6a57
								
							
						
					
					
						commit
						da214a475f
					
				
					 2 changed files with 44 additions and 11 deletions
				
			
		| 
						 | 
				
			
			@ -434,6 +434,7 @@ extern struct file *do_accept(struct file *file, unsigned file_flags,
 | 
			
		|||
extern int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
 | 
			
		||||
			 int __user *upeer_addrlen, int flags);
 | 
			
		||||
extern int __sys_socket(int family, int type, int protocol);
 | 
			
		||||
extern struct file *__sys_socket_file(int family, int type, int protocol);
 | 
			
		||||
extern int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen);
 | 
			
		||||
extern int __sys_connect_file(struct file *file, struct sockaddr_storage *addr,
 | 
			
		||||
			      int addrlen, int file_flags);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										54
									
								
								net/socket.c
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								net/socket.c
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -504,7 +504,7 @@ static int sock_map_fd(struct socket *sock, int flags)
 | 
			
		|||
struct socket *sock_from_file(struct file *file)
 | 
			
		||||
{
 | 
			
		||||
	if (file->f_op == &socket_file_ops)
 | 
			
		||||
		return file->private_data;	/* set in sock_map_fd */
 | 
			
		||||
		return file->private_data;	/* set in sock_alloc_file */
 | 
			
		||||
 | 
			
		||||
	return NULL;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1538,11 +1538,10 @@ int sock_create_kern(struct net *net, int family, int type, int protocol, struct
 | 
			
		|||
}
 | 
			
		||||
EXPORT_SYMBOL(sock_create_kern);
 | 
			
		||||
 | 
			
		||||
int __sys_socket(int family, int type, int protocol)
 | 
			
		||||
static struct socket *__sys_socket_create(int family, int type, int protocol)
 | 
			
		||||
{
 | 
			
		||||
	int retval;
 | 
			
		||||
	struct socket *sock;
 | 
			
		||||
	int flags;
 | 
			
		||||
	int retval;
 | 
			
		||||
 | 
			
		||||
	/* Check the SOCK_* constants for consistency.  */
 | 
			
		||||
	BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
 | 
			
		||||
| 
						 | 
				
			
			@ -1550,17 +1549,50 @@ int __sys_socket(int family, int type, int protocol)
 | 
			
		|||
	BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
 | 
			
		||||
	BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
 | 
			
		||||
 | 
			
		||||
	flags = type & ~SOCK_TYPE_MASK;
 | 
			
		||||
	if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	if ((type & ~SOCK_TYPE_MASK) & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
 | 
			
		||||
		return ERR_PTR(-EINVAL);
 | 
			
		||||
	type &= SOCK_TYPE_MASK;
 | 
			
		||||
 | 
			
		||||
	if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
 | 
			
		||||
		flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
 | 
			
		||||
 | 
			
		||||
	retval = sock_create(family, type, protocol, &sock);
 | 
			
		||||
	if (retval < 0)
 | 
			
		||||
		return retval;
 | 
			
		||||
		return ERR_PTR(retval);
 | 
			
		||||
 | 
			
		||||
	return sock;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct file *__sys_socket_file(int family, int type, int protocol)
 | 
			
		||||
{
 | 
			
		||||
	struct socket *sock;
 | 
			
		||||
	struct file *file;
 | 
			
		||||
	int flags;
 | 
			
		||||
 | 
			
		||||
	sock = __sys_socket_create(family, type, protocol);
 | 
			
		||||
	if (IS_ERR(sock))
 | 
			
		||||
		return ERR_CAST(sock);
 | 
			
		||||
 | 
			
		||||
	flags = type & ~SOCK_TYPE_MASK;
 | 
			
		||||
	if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
 | 
			
		||||
		flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
 | 
			
		||||
 | 
			
		||||
	file = sock_alloc_file(sock, flags, NULL);
 | 
			
		||||
	if (IS_ERR(file))
 | 
			
		||||
		sock_release(sock);
 | 
			
		||||
 | 
			
		||||
	return file;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int __sys_socket(int family, int type, int protocol)
 | 
			
		||||
{
 | 
			
		||||
	struct socket *sock;
 | 
			
		||||
	int flags;
 | 
			
		||||
 | 
			
		||||
	sock = __sys_socket_create(family, type, protocol);
 | 
			
		||||
	if (IS_ERR(sock))
 | 
			
		||||
		return PTR_ERR(sock);
 | 
			
		||||
 | 
			
		||||
	flags = type & ~SOCK_TYPE_MASK;
 | 
			
		||||
	if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
 | 
			
		||||
		flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
 | 
			
		||||
 | 
			
		||||
	return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue