net: socket: integrate sockfd_lookup() and sockfd_lookup_light()
Subsystems (3)
PATCH 1/1 net: socket: integrate sockfd_lookup() and sockfd_lookup_light()
As commit 6cb153cab92a("[NET]: use fget_light() in net/socket.c") said, sockfd_lookup_light() is lower load than sockfd_lookup(). So we can remove sockfd_lookup() but keep the name. As the same time, move flags to sockfd_put().
--- include/linux/net.h | 8 +++- net/socket.c | 101 +++++++++++++++++--------------------------- 2 files changed, 46 insertions(+), 63 deletions(-)
include/linux/net.h +7 -1
@@ -238,8 +238,14 @@ 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); -#define sockfd_put (sock) fput(sock->file)int net_ratelimit (void ); +#define sockfd_put (sock) \ +do { \ + struct fd *fd = (struct fd *)&sock->file; \ + \ + if (fd->flags & FDPUT_FPUT) \ + fput(sock->file); \ +} while (0)#define net_ratelimited_function (function, ...) \ do { \
net/socket.c +39 -62
320 lines not on this page · raw message
Signature · 1 line
2.32.0
Jakub Kicinski
maintainerQuoted 4 lines · On Wed, 22 Sep 2021 14:31:06 +0800 Yajun Deng wrote:
As commit 6cb153cab92a("[NET]: use
fget_light()innet/socket.c") said,sockfd_lookup_light()is lower load thansockfd_lookup(). So we can removesockfd_lookup()but keep the name. As the same time, move flags tosockfd_put().
You just assume that each caller of sockfd_lookup() already meets the criteria under which sockfd_lookup_light() can be used? Am I reading this right?
Please extend the commit message clearly walking us thru why this is safe now (and perhaps why it wasn't in the past).
Quoted 15 lines
static
ssize_tsockfs_listxattr(struct dentry *dentry, char *buffer,net/socket.c +2
-2size_t size)@@ -1680,9 +1659,9 @@ int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen) {struct socket *sock;struct sockaddr_storage address; -int err, fput_needed; +int err; - sock =sockfd_lookup_light (fd, &err, &fput_needed); + sock =sockfd_lookup (fd, &err);if (sock) { err =move_addr_to_kernel (umyaddr, addrlen, &address);if (!err) {
net/socket.c int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen) · line 1676
Quoted 5 lines
net/socket.c +1
-1@@ -1694,7 +1673,7 @@ int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen) (struct sockaddr *) &address, addrlen); } -fput_light (sock->file, fput_needed); +sockfd_put (sock);
And we just replace fput_light() with fput() even tho the reference was taken with fdget()? fdget() == __fget_light().
Maybe you missed fget() vs fdget()?
All these changes do not immediately strike me as correct.
Quoted 3 lines
net/socket.c +0
-0}return err; }
Quoted 4 lines · Yajun Deng
As commit 6cb153cab92a("[NET]: use
fget_light()innet/socket.c") said,sockfd_lookup_light()is lower load thansockfd_lookup(). So we can removesockfd_lookup()but keep the name. As the same time, move flags tosockfd_put().
???
Quoted 15 lines
Signed-off-by Yajun Deng <[email protected]>--- include/linux/net.h | 8 +++- net/socket.c | 101 +++++++++++++++++--------------------------- 2 files changed, 46 insertions(+), 63 deletions(-)include/linux/net.h +0
-0@@ -238,8 +238,14 @@ 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);
include/linux/net.h int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags); · line 249
Quoted 10 lines
include/linux/net.h +7
-1-#define sockfd_put (sock) fput(sock->file)int net_ratelimit (void ); +#define sockfd_put (sock) \ +do { \ + struct fd *fd = (struct fd *)&sock->file; \ + \ + if (fd->flags & FDPUT_FPUT) \ + fput(sock->file); \ +} while (0)
Really ?
I wonder how was this tested ?
We can not store FDPUT_FPUT in the sock itself, for obvious reasons. Each thread needs to keep this information private.
Quoted 11 lines · Jakub Kicinski
Quoted 4 lines · On Wed, 22 Sep 2021 14:31:06 +0800 Yajun Deng wrote:
As commit 6cb153cab92a("[NET]: use
fget_light()innet/socket.c") said,sockfd_lookup_light()is lower load thansockfd_lookup(). So we can removesockfd_lookup()but keep the name. As the same time, move flags tosockfd_put().You just assume that each caller of
sockfd_lookup()already meets the criteria under whichsockfd_lookup_light()can be used? Am I reading this right?
Yes, this patch means each caller of sockfd_lookup() can used sockfd_lookup_light() instead.
Quoted 3 lines
Please extend the commit message clearly walking us thru why this is safe now (and perhaps why it wasn't in the past).
The sockfd_lookup() and sockfd_lookup_light() are both safe. The fact that they have been around for so long is the best proof. sockfd_lookup_light() is just lower load than sockfd_lookup(). so we can used the lower load helper function.
Quoted 24 lines
Quoted 15 lines
static
ssize_tsockfs_listxattr(struct dentry *dentry, char *buffer, size_t size)net/socket.c +2
-2@@ -1680,9 +1659,9 @@ int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen) {struct socket *sock;struct sockaddr_storage address; -int err, fput_needed; +int err; - sock =sockfd_lookup_light (fd, &err, &fput_needed); + sock =sockfd_lookup (fd, &err);if (sock) { err =move_addr_to_kernel (umyaddr, addrlen, &address);if (!err) {net/socket.c int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen) · line 1676
Quoted 5 lines
net/socket.c +1
-1@@ -1694,7 +1673,7 @@ int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen) (struct sockaddr *) &address, addrlen); } -fput_light (sock->file, fput_needed); +sockfd_put (sock);And we just replace
fput_light()withfput()even tho the reference was taken withfdget()?fdget()==__fget_light().
Quoted 1 line
Maybe you missed
fget()vsfdget()?
In fact, the sockfd_put() already changed in this patch. Here is the modified:
#define sockfd_put(sock) \
do { \
struct fd *fd = (struct fd *)&sock->file; \
\
if (fd->flags & FDPUT_FPUT) \
fput(sock->file); \
}Quoted 3 lines
All these changes do not immediately strike me as correct.
This is the information of this patch:
include/linux/net.h | 8 +++- net/socket.c | 101 +++++++++++++++++--------------------------- 2 files changed, 46 insertions(+), 63 deletions(-)
Quoted 3 lines
Quoted 3 lines
net/socket.c +0
-0}return err; }
Quoted 35 lines · Eric Dumazet
Quoted 4 lines · Yajun Deng
As commit 6cb153cab92a("[NET]: use
fget_light()innet/socket.c") said,sockfd_lookup_light()is lower load thansockfd_lookup(). So we can removesockfd_lookup()but keep the name. As the same time, move flags tosockfd_put().???
Quoted 13 lines
Signed-off-by Yajun Deng <[email protected]>---
include/linux/net.h| 8 +++-net/socket.c| 101 +++++++++++++++++---------------------------2 files changed, 46 insertions(+), 63 deletions(-)include/linux/net.h +0
-0@@ -238,8 +238,14 @@ 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);include/linux/net.h int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags); · line 248
Quoted 10 lines
include/linux/net.h +7
-1struct socket *sock_from_file (struct file *file); -#define sockfd_put (sock) fput(sock->file)int net_ratelimit (void ); +#define sockfd_put (sock) \ +do { \ + struct fd *fd = (struct fd *)&sock->file; \ + \ + if (fd->flags & FDPUT_FPUT) \ + fput(sock->file); \ +} while (0)Really ?
Quoted 4 lines
I wonder how was this tested ?
We can not store
FDPUT_FPUTin the sock itself, for obvious reasons. Each thread needs to keep this information private.
The sockfd_lookup() already changed in this patch. FDPUT_FPUT stored in fdget(), precisely in __fget_light().
include/linux/net.h int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags); · line 244
Quoted 5 lines · Yajun Deng
include/linux/net.h +3
-1-#define sockfd_put (sock) fput(sock->file)int net_ratelimit (void ); +#define sockfd_put (sock) \ +do { \ + struct fd *fd = (struct fd *)&sock->file; \
Have you even bothered to take a look at struct fd declaration? Or struct socket one, for that matter... What we have there is
...
struct file *file;
struct sock *sk;
...You are taking the address of 'file' field. And treat it as a pointer to a structure consisting of struct file * and unsigned int.
include/linux/net.h int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags); · line 246
Quoted 2 lines
include/linux/net.h +2
-0+ \ +if (fd->flags &FDPUT_FPUT ) \
... so that would take first 4 bytes in the memory occupied by 'sk' field of struct socket and check if bit 0 is set.
And what significance would that bit have, pray tell? On little-endian architectures it's going to be the least significant bit in the first byte in 'sk' field. I.e. there you are testing if the contents of 'sk' (a pointer to struct sock) happens to be odd. It won't be. The same goes for 32bit big-endian - there you will be checking the least significant bit of the 4th byte of 'sk', which again is asking 'is the pointer stored there odd for some reason?'
On 64bit big-endian you are checking if the bit 32 of the address of object sock->sk points to is set. And the answer to that is "hell knows and how could that possibly be relevant to anything?"
Quoted 35 lines · Al Viro
include/linux/net.h int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags); · line 244
Quoted 5 lines · Yajun Deng
-#define
sockfd_put(sock)fput(sock->file)
intnet_ratelimit(void);include/linux/net.h +3
-0+#define sockfd_put (sock) \ +do { \ + struct fd *fd = (struct fd *)&sock->file; \Have you even bothered to take a look at struct fd declaration? Or struct socket one, for that matter... What we have there is
...
struct file *file;
struct sock *sk;
...You are taking the address of 'file' field. And treat it as a pointer to a structure consisting of struct file * and unsigned int.
Quoted 2 lines
+ \
+ if (fd->flags&FDPUT_FPUT) \... so that would take first 4 bytes in the memory occupied by 'sk' field of struct socket and check if bit 0 is set.
And what significance would that bit have, pray tell? On little-endian architectures it's going to be the least significant bit in the first byte in 'sk' field. I.e. there you are testing if the contents of 'sk' (a pointer to struct sock) happens to be odd. It won't be. The same goes for 32bit big-endian - there you will be checking the least significant bit of the 4th byte of 'sk', which again is asking 'is the pointer stored there odd for some reason?'
Quoted 4 lines
On 64bit big-endian you are checking if the bit 32 of the address of object
sock->skpoints to is set. And the answer to that is "hell knows and how could that possibly be relevant to anything?"
Well, the forced conversion is wrong. sorry for that.