Skip to contentSkip to conversation

net: remove sock_iocb

PATCH In review Christoph Hellwig 5 participants 6 messages Jan 28, 2015 – Feb 4, 2015

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: remove sock_iocb

The sock_iocb structure is allocate on stack for each read/write-like operation on sockets, and contains various fields of which only the embedded msghdr and sometimes a pointer to the scm_cookie is ever used. Get rid of the sock_iocb and put a msghdr directly on the stack and pass the scm_cookie explicitly to netlink_mmap_sendmsg.

---
 include/net/sock.h       | 23 ---------------
 net/netlink/af_netlink.c | 28 +++++++------------
 net/socket.c             | 45 +++--------------------------
 net/unix/af_unix.c       | 73 +++++++++++++++++++-----------------------------
 4 files changed, 43 insertions(+), 126 deletions(-)
include/net/sock.h +0 -23
index 2210fec..1534149 100644
@@ -1374,29 +1374,6 @@ void sk_prot_clear_portaddr_nulls(struct sock *sk, int size);
 #define SOCK_BINDADDR_LOCK	4
 #define SOCK_BINDPORT_LOCK	8
 
-/* sock_iocb: used to kick off async processing of socket ios */
-struct sock_iocb {
-	struct list_head	list;
-
-	int			flags;
-	int			size;
-	struct socket		*sock;
-	struct sock		*sk;
-	struct scm_cookie	*scm;
-	struct msghdr		*msg, async_msg;
-	struct kiocb		*kiocb;
-};
-
-static inline struct sock_iocb *kiocb_to_siocb(struct kiocb *iocb)
-{
-	return (struct sock_iocb *)iocb->private;
-}
-
-static inline struct kiocb *siocb_to_kiocb(struct sock_iocb *si)
-{
-	return si->kiocb;
-}
-
 struct socket_alloc {
 	struct socket socket;
 	struct inode vfs_inode;
net/netlink/af_netlink.c +10 -18
index 02fdde2..efae751 100644
@@ -708,7 +708,7 @@ static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
 
 static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
 				u32 dst_portid, u32 dst_group,
-				struct sock_iocb *siocb)
+				struct scm_cookie *scm)
 {
 	struct netlink_sock *nlk = nlk_sk(sk);
 	struct netlink_ring *ring;
@@ -754,7 +754,7 @@ static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
 
 		NETLINK_CB(skb).portid	  = nlk->portid;
 		NETLINK_CB(skb).dst_group = dst_group;
-		NETLINK_CB(skb).creds	  = siocb->scm->creds;
+		NETLINK_CB(skb).creds	  = scm->creds;
 
 		err = security_netlink_send(sk, skb);
 		if (err) {
@@ -833,7 +833,7 @@ static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb)
 #define netlink_tx_is_mmaped(sk)	false
 #define netlink_mmap			sock_no_mmap
 #define netlink_poll			datagram_poll
-#define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb)	0
+#define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, scm)	0
 #endif /* CONFIG_NETLINK_MMAP */
 
 static void netlink_skb_destructor(struct sk_buff *skb)
@@ -2259,7 +2259,6 @@ static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
 			   struct msghdr *msg, size_t len)
 {
-	struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
 	struct sock *sk = sock->sk;
 	struct netlink_sock *nlk = nlk_sk(sk);
 	DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
@@ -2273,10 +2272,7 @@ static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
 	if (msg->msg_flags&MSG_OOB)
 		return -EOPNOTSUPP;
 
-	if (NULL == siocb->scm)
-		siocb->scm = &scm;
-
-	err = scm_send(sock, msg, siocb->scm, true);
+	err = scm_send(sock, msg, &scm, true);
 	if (err < 0)
 		return err;
 
@@ -2305,7 +2301,7 @@ static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
 	if (netlink_tx_is_mmaped(sk) &&
 	    msg->msg_iter.iov->iov_base == NULL) {
 		err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
-					   siocb);
+					   &scm);
 		goto out;
 	}
 
@@ -2319,7 +2315,7 @@ static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
 
 	NETLINK_CB(skb).portid	= nlk->portid;
 	NETLINK_CB(skb).dst_group = dst_group;
-	NETLINK_CB(skb).creds	= siocb->scm->creds;
+	NETLINK_CB(skb).creds	= scm.creds;
 	NETLINK_CB(skb).flags	= netlink_skb_flags;
 
 	err = -EFAULT;
@@ -2341,7 +2337,7 @@ static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
 	err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
 
 out:
-	scm_destroy(siocb->scm);
+	scm_destroy(&scm);
 	return err;
 }
 
@@ -2349,7 +2345,6 @@ static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
 			   struct msghdr *msg, size_t len,
 			   int flags)
 {
-	struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
 	struct scm_cookie scm;
 	struct sock *sk = sock->sk;
 	struct netlink_sock *nlk = nlk_sk(sk);
@@ -2412,11 +2407,8 @@ static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
 	if (nlk->flags & NETLINK_RECV_PKTINFO)
 		netlink_cmsg_recv_pktinfo(msg, skb);
 
-	if (NULL == siocb->scm) {
-		memset(&scm, 0, sizeof(scm));
-		siocb->scm = &scm;
-	}
-	siocb->scm->creds = *NETLINK_CREDS(skb);
+	memset(&scm, 0, sizeof(scm));
+	scm.creds = *NETLINK_CREDS(skb);
 	if (flags & MSG_TRUNC)
 		copied = data_skb->len;
 
@@ -2431,7 +2423,7 @@ static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
 		}
 	}
 
-	scm_recv(sock, msg, siocb->scm, flags);
+	scm_recv(sock, msg, &scm, flags);
 out:
 	netlink_rcv_wake(sk);
 	return err ? : copied;
net/socket.c +4 -41

112 lines not on this page · raw message

net/unix/af_unix.c +29 -44

212 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#1 lore raw

From: Christoph Hellwig <[email protected]>
Date: Wed, 28 Jan 2015 18:04:53 +0100

Quoted 7 lines

The sock_iocb structure is allocate on stack for each read/write-like operation on sockets, and contains various fields of which only the embedded msghdr and sometimes a pointer to the scm_cookie is ever used. Get rid of the sock_iocb and put a msghdr directly on the stack and pass the scm_cookie explicitly to netlink_mmap_sendmsg.

Looks good, applied, thanks.

#2 lore raw
Quoted 9 lines · David Miller

From: Christoph Hellwig <[email protected]>
Date: Wed, 28 Jan 2015 18:04:53 +0100

Quoted 6 lines

The sock_iocb structure is allocate on stack for each read/write-like operation on sockets, and contains various fields of which only the embedded msghdr and sometimes a pointer to the scm_cookie is ever used. Get rid of the sock_iocb and put a msghdr directly on the stack and pass the scm_cookie explicitly to netlink_mmap_sendmsg.

Quoted 3 lines
Quoted 1 line

Looks good, applied, thanks.

You know, that's getting _really_ interesting. The thing is, now there's only one ->sendmsg() instance using iocb argument at all, and it's a really weird one. TIPC. Which only compares it with NULL, and that - to tell the normal calls (== done by sock_sendmsg() et.al.) from tipc_{accept,connect}()-generated ones. And the way it's used is

        if (iocb)
                lock_sock(sk);

in tipc_send_stream(). IOW, "tipc_accept() and tipc_connect() would like to use the guts of tipc_send_stream(), but they are already holding the socket locked; let's just pass NULL iocb (which net/socket.c never does) to tell it to leave the fucking lock alone, thank you very much".

And no ->recvmsg() are using iocb at all now. How about we take the guts of tipc_send_stream() into a helper function and have tipc_accept/connect use _that_? Then we could drop iocb argument completely and for ->sendmsg() it would be the difference between 4 and 3 arguments, which has interesting effects on certain register-starved architectures...

While we are at it, size (both for sendmsg and recvmsg) is always equal to iov_iter_count(&msg->msg_iter), so that's not the only redundant argument there...

Comments?

maintainer
#3 lore raw

From: Al Viro <[email protected]>
Date: Thu, 29 Jan 2015 07:57:21 +0000

Quoted 19 lines

You know, that's getting _really_ interesting. The thing is, now there's only one ->sendmsg() instance using iocb argument at all, and it's a really weird one. TIPC. Which only compares it with NULL, and that - to tell the normal calls (== done by sock_sendmsg() et.al.) from tipc_{accept,connect}()-generated ones. And the way it's used is

        if (iocb)
                lock_sock(sk);

in tipc_send_stream(). IOW, "tipc_accept() and tipc_connect() would like to use the guts of tipc_send_stream(), but they are already holding the socket locked; let's just pass NULL iocb (which net/socket.c never does) to tell it to leave the fucking lock alone, thank you very much".

And no ->recvmsg() are using iocb at all now. How about we take the guts of tipc_send_stream() into a helper function and have tipc_accept/connect use _that_? Then we could drop iocb argument completely and for ->sendmsg() it would be the difference between 4 and 3 arguments, which has interesting effects on certain register-starved architectures...

Quoted 3 lines

While we are at it, size (both for sendmsg and recvmsg) is always equal to iov_iter_count(&msg->msg_iter), so that's not the only redundant argument there...

No objections from me.

#4 lore raw
Quoted 25 lines

-----Original Message-----
From: [email protected] [mailto:netdev- [email protected]] On Behalf Of David Miller
Sent: January-29-15 3:01 AM
To: [email protected]
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] net: remove sock_iocb

From: Al Viro <[email protected]>
Date: Thu, 29 Jan 2015 07:57:21 +0000

Quoted 6 lines

You know, that's getting _really_ interesting. The thing is, now there's only one ->sendmsg() instance using iocb argument at all, and it's a really weird one. TIPC. Which only compares it with NULL, and that - to tell the normal calls (== done by sock_sendmsg() et.al.) from tipc_{accept,connect}()-generated ones. And the way it's used is

Quoted 6 lines
        if (iocb)
                lock_sock(sk);

in tipc_send_stream(). IOW, "tipc_accept() and tipc_connect() would like to use the guts of tipc_send_stream(), but they are already holding the socket locked; let's just pass NULL iocb (which net/socket.c never does) to tell it to leave the fucking lock alone, thank you

Quoted 5 lines

very much".

Quoted 4 lines

And no ->recvmsg() are using iocb at all now. How about we take the guts of tipc_send_stream() into a helper function and have tipc_accept/connect use _that_?

Sounds reasonable. We'll look into this.

///jon

Then we could drop iocb argument

Quoted 13 lines
Quoted 2 lines

completely and for ->sendmsg() it would be the difference between 4 and 3 arguments, which has interesting effects on certain register-starved

architectures...

Quoted 4 lines

While we are at it, size (both for sendmsg and recvmsg) is always equal to iov_iter_count(&msg->msg_iter), so that's not the only redundant argument there...

No objections from me.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html

#5 lore raw
In reply to Al Viro
Quoted 33 lines · Al Viro
Quoted 9 lines · David Miller

From: Christoph Hellwig <[email protected]>
Date: Wed, 28 Jan 2015 18:04:53 +0100

Quoted 6 lines

The sock_iocb structure is allocate on stack for each read/write-like operation on sockets, and contains various fields of which only the embedded msghdr and sometimes a pointer to the scm_cookie is ever used. Get rid of the sock_iocb and put a msghdr directly on the stack and pass the scm_cookie explicitly to netlink_mmap_sendmsg.

Quoted 3 lines
Quoted 1 line

Looks good, applied, thanks.

You know, that's getting _really_ interesting. The thing is, now there's only one ->sendmsg() instance using iocb argument at all, and it's a really weird one. TIPC. Which only compares it with NULL, and that - to tell the normal calls (== done by sock_sendmsg() et.al.) from tipc_{accept,connect}()-generated ones. And the way it's used is

        if (iocb)
                lock_sock(sk);

in tipc_send_stream(). IOW, "tipc_accept() and tipc_connect() would like to use the guts of tipc_send_stream(), but they are already holding the socket locked; let's just pass NULL iocb (which net/socket.c never does) to tell it to leave the fucking lock alone, thank you very much".

And no ->recvmsg() are using iocb at all now. How about we take the guts of tipc_send_stream() into a helper function and have tipc_accept/connect use _that_? Then we could drop iocb argument completely and for ->sendmsg() it would be the difference between 4 and 3 arguments, which has interesting effects on certain register-starved architectures...

Quoted 5 lines

While we are at it, size (both for sendmsg and recvmsg) is always equal to iov_iter_count(&msg->msg_iter), so that's not the only redundant argument there...

Comments?

Below is a demonstrated patch to not use iocb argument in tipc socket:

Subject: [PATCH] tipc: Don't use iocb argument in socket layer

---
 net/tipc/socket.c |   44 ++++++++++++++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 14 deletions(-)
net/tipc/socket.c +0 -0
index 679a220..8362feb 100644
@@ -116,6 +116,9 @@ static int tipc_sk_withdraw(struct tipc_sock *tsk,

uint scope,
static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid); static int tipc_sk_insert(struct tipc_sock *tsk);
static void tipc_sk_remove(struct tipc_sock *tsk);
+static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
+ size_t dsz);
+static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);

static const struct proto_ops packet_ops;
static const struct proto_ops stream_ops;
@@ -886,6 +889,18 @@ static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
struct msghdr *m, size_t dsz)
{
+ struct sock *sk = sock->sk;
+ int ret;
+
+ lock_sock(sk);
+ ret = __tipc_sendmsg(sock, m, dsz);
+ release_sock(sk);
+
+ return ret;
+}
+
+static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
+{
DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); struct sock *sk = sock->sk;
struct tipc_sock *tsk = tipc_sk(sk);
@@ -909,9 +924,6 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
if (dsz > TIPC_MAX_USER_MSG_SIZE)
return -EMSGSIZE;

- if (iocb)
- lock_sock(sk);
-
if (unlikely(sock->state != SS_READY)) {
if (sock->state == SS_LISTENING) {
rc = -EPIPE;
@@ -990,9 +1002,6 @@ new_mtu:
__skb_queue_purge(&head);
} while (!rc);
exit:
- if (iocb)
- release_sock(sk);
-
return rc;
}

@@ -1042,6 +1051,18 @@ static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
struct msghdr *m, size_t dsz)
{
struct sock *sk = sock->sk;
+ int ret;
+
+ lock_sock(sk);
+ ret = __tipc_send_stream(sock, m, dsz);
+ release_sock(sk);
+
+ return ret;
+}
+
+static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
+{
+ struct sock *sk = sock->sk;
struct net *net = sock_net(sk);
struct tipc_sock *tsk = tipc_sk(sk);
struct tipc_msg *mhdr = &tsk->phdr;
@@ -1055,7 +1076,7 @@ static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,

/* Handle implied connection establishment */
if (unlikely(dest)) {
- rc = tipc_sendmsg(iocb, sock, m, dsz);
+ rc = __tipc_sendmsg(sock, m, dsz);
if (dsz && (dsz == rc))
tsk->sent_unacked = 1;
return rc;
@@ -1063,9 +1084,6 @@ static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
if (dsz > (uint)INT_MAX)
return -EMSGSIZE;

- if (iocb)
- lock_sock(sk);
-
if (unlikely(sock->state != SS_CONNECTED)) {
if (sock->state == SS_DISCONNECTING)
rc = -EPIPE;
@@ -1108,8 +1126,6 @@ next:
__skb_queue_purge(&head);
} while (!rc);
exit:
- if (iocb)
- release_sock(sk);
return sent ? sent : rc;
}

@@ -1874,7 +1890,7 @@ static int tipc_connect(struct socket *sock, struct sockaddr *dest,
if (!timeout)
m.msg_flags = MSG_DONTWAIT;

- res = tipc_sendmsg(NULL, sock, &m, 0);
+ res = __tipc_sendmsg(sock, &m, 0);
if ((res < 0) && (res != -EWOULDBLOCK))
goto exit;

@@ -2030,7 +2046,7 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
struct msghdr m = {NULL,};

tsk_advance_rx_queue(sk);
- tipc_send_packet(NULL, new_sock, &m, 0);
+ __tipc_send_stream(new_sock, &m, 0);
} else {
__skb_dequeue(&sk->sk_receive_queue); __skb_queue_head(&new_sk->sk_receive_queue, buf);

Regards,
Ying

Quoted 6 lines

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html