Skip to contentSkip to conversation

rds: free zerocopy cookies when the socket is destroyed

PATCH In review Nguyen Ngoc Thang 3 participants 3 messages Nov 30 – Dec 1

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

PATCH 1/1 rds: free zerocopy cookies when the socket is destroyed

rds_release() frees the zerocopy completion cookies still queued on the socket, but more can be queued after that. A cookie is only queued when the last reference to its message is dropped, and rds_send_drop_to() does not wait for references held elsewhere:

- over loopback, the receiver's queue holds the message itself, so it outlives the sender if the sender closes first;

- rds_send_xmit() holds a reference to the message it is sending, and may be running in a worker or in another socket's sendmsg().

rds_message_purge() then adds the cookie to the closed socket and drops the message's socket reference. Nothing empties the queue again before the socket is freed, so the cookie is leaked:

unreferenced object 0xffff88812a3e1880 (size 64):
backtrace:
__kmalloc_cache_noprof rds_message_copy_from_user rds_sendmsg

Free the cookies in rds_sock_destruct() instead. Each queued message holds a socket reference until its cookie has been queued, so none can be added after that. The purge doesn't sleep, so it is safe in the RCU callback that destroys bound sockets.

Fixes 401910db4cd4 ("rds: deliver zerocopy completion notification with data")
Reported-by [email protected]

---
Testing (QEMU, 4 vCPUs, kmemleak and PROVE_LOCKING, on net e2fb9368a416):

I used my own test program rather than the syzbot reproducer. It sends to a local, non-127/8 address, so the sockets use TCP but the connection uses the loopback transport.

- Sender closes before the receiver reads, 51 messages. This needs no race. Leaked cookies: 51 before, 0 after.
- 8 threads that each open a socket, send to it and close it: one run of 400 sends, then three of 40000. Total leaked cookies, including the 51 above: 2243 before, 0 after, in each of three boots.
- Cookies are still delivered to an open sender: 100 of 100, both before and after the 40000-send runs.

Lockdep reported nothing up to and including the 400-send run. The 40000-send runs then hit "MAX_LOCKDEP_KEYS too low!", with and without this patch, which turns lockdep off. The trace points at workqueues that __rds_conn_create() allocates and destroys, so it is a separate issue.

This does not apply directly to linux-next, where net-next adds code next to the line removed from rds_release(), but a 3-way merge is clean.

 net/rds/af_rds.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/rds/af_rds.c +3 -1
index fb6b8ddb2d111..2e4d7888c9851 100644
@@ -78,7 +78,6 @@ static int rds_release(struct socket *sock)
 	rds_send_drop_to(rs, NULL);
 	rds_rdma_drop_keys(rs);
 	rds_notify_queue_get(rs, NULL);
-	rds_notify_msg_zcopy_purge(&rs->rs_zcookie_queue);
 
 	spin_lock_bh(&rds_sock_lock);
 	list_del_init(&rs->rs_item);
@@ -672,6 +671,9 @@ static void rds_sock_destruct(struct sock *sk)
 
 	WARN_ON((&rs->rs_item != rs->rs_item.next ||
 		 &rs->rs_item != rs->rs_item.prev));
+
+	/* Messages hold a sock ref until they have queued their cookie. */
+	rds_notify_msg_zcopy_purge(&rs->rs_zcookie_queue);
 }
 
 static int __rds_create(struct socket *sock, struct sock *sk, int protocol)

base-commit: e2fb9368a41662bc54624f9be87ea8989b109306

Signature · 1 line
2.43.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 26 lines

rds_release() frees the zerocopy completion cookies still queued on the socket, but more can be queued after that. A cookie is only queued when the last reference to its message is dropped, and rds_send_drop_to() does not wait for references held elsewhere:

- over loopback, the receiver's queue holds the message itself, so it outlives the sender if the sender closes first;

- rds_send_xmit() holds a reference to the message it is sending, and may be running in a worker or in another socket's sendmsg().

rds_message_purge() then adds the cookie to the closed socket and drops the message's socket reference. Nothing empties the queue again before the socket is freed, so the cookie is leaked:

unreferenced object 0xffff88812a3e1880 (size 64):
backtrace:
__kmalloc_cache_noprof rds_message_copy_from_user rds_sendmsg

Free the cookies in rds_sock_destruct() instead. Each queued message holds a socket reference until its cookie has been queued, so none can be added after that. The purge doesn't sleep, so it is safe in the RCU callback that destroys bound sockets.

Quoted 4 lines
Fixes 401910db4cd4 ("rds: deliver zerocopy completion notification with data")
Reported-by [email protected]

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Nguyen Ngoc Thang
Quoted 26 lines · Nguyen Ngoc Thang

rds_release() frees the zerocopy completion cookies still queued on the socket, but more can be queued after that. A cookie is only queued when the last reference to its message is dropped, and rds_send_drop_to() does not wait for references held elsewhere:

- over loopback, the receiver's queue holds the message itself, so it outlives the sender if the sender closes first;

- rds_send_xmit() holds a reference to the message it is sending, and may be running in a worker or in another socket's sendmsg().

rds_message_purge() then adds the cookie to the closed socket and drops the message's socket reference. Nothing empties the queue again before the socket is freed, so the cookie is leaked:

unreferenced object 0xffff88812a3e1880 (size 64):
backtrace:
__kmalloc_cache_noprof rds_message_copy_from_user rds_sendmsg

Free the cookies in rds_sock_destruct() instead. Each queued message holds a socket reference until its cookie has been queued, so none can be added after that. The purge doesn't sleep, so it is safe in the RCU callback that destroys bound sockets.

Quoted 4 lines
Fixes 401910db4cd4 ("rds: deliver zerocopy completion notification with data")
Reported-by [email protected]

Hi Nguyen,

This looks correct to me. Thanks!

Allison

Quoted 50 lines

---
Testing (QEMU, 4 vCPUs, kmemleak and PROVE_LOCKING, on net e2fb9368a416):

I used my own test program rather than the syzbot reproducer. It sends to a local, non-127/8 address, so the sockets use TCP but the connection uses the loopback transport.

- Sender closes before the receiver reads, 51 messages. This needs no race. Leaked cookies: 51 before, 0 after.
- 8 threads that each open a socket, send to it and close it: one run of 400 sends, then three of 40000. Total leaked cookies, including the 51 above: 2243 before, 0 after, in each of three boots.
- Cookies are still delivered to an open sender: 100 of 100, both before and after the 40000-send runs.

Lockdep reported nothing up to and including the 400-send run. The 40000-send runs then hit "MAX_LOCKDEP_KEYS too low!", with and without this patch, which turns lockdep off. The trace points at workqueues that __rds_conn_create() allocates and destroys, so it is a separate issue.

This does not apply directly to linux-next, where net-next adds code next to the line removed from rds_release(), but a 3-way merge is clean.

 net/rds/af_rds.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/rds/af_rds.c +3 -1
index fb6b8ddb2d111..2e4d7888c9851 100644
@@ -78,7 +78,6 @@ static int rds_release(struct socket *sock)
 	rds_send_drop_to(rs, NULL);
 	rds_rdma_drop_keys(rs);
 	rds_notify_queue_get(rs, NULL);
-	rds_notify_msg_zcopy_purge(&rs->rs_zcookie_queue);
 
 	spin_lock_bh(&rds_sock_lock);
 	list_del_init(&rs->rs_item);
@@ -672,6 +671,9 @@ static void rds_sock_destruct(struct sock *sk)
 
 	WARN_ON((&rs->rs_item != rs->rs_item.next ||
 		 &rs->rs_item != rs->rs_item.prev));
+
+	/* Messages hold a sock ref until they have queued their cookie. */
+	rds_notify_msg_zcopy_purge(&rs->rs_zcookie_queue);
 }
 
 static int __rds_create(struct socket *sock, struct sock *sk, int protocol)

base-commit: e2fb9368a41662bc54624f9be87ea8989b109306