Skip to content

Wait for concurrent reapers in homa_sock_destroy - #153

Closed
breakertt wants to merge 1 commit into
PlatformLab:mainfrom
breakertt:fix/destroy-wait-for-reapers
Closed

breakertt wants to merge 1 commit into
PlatformLab:mainfrom
breakertt:fix/destroy-wait-for-reapers

Conversation

@breakertt

Copy link
Copy Markdown
Contributor

homa_sock_destroy waits only until dead_rpcs is empty. But homa_timer or SoftIRQ can still free the last batch that it took off the list. That reaper returns the buffers to the pool and subtracts their frag_bytes from sk_wmem_alloc. Destroy then hits WARN_ON_ONCE(sk_wmem_alloc != 1) and frees the pool under that reaper.

This patch counts the reaps in progress in hsk->reapers. Destroy increments protect_count, so a new reap returns at once, and then waits until reapers is 0.

Test on two xl170 nodes: 16 threads each keep 150 8 KB RPCs in flight, then all close their sockets together. On upstream, destroy found sk_wmem_alloc != 1 in 318 of 128000 closes. In one run, homa_timer then oopsed in homa_pool_free_bufs (NULL buffer_pool) and 2 sockets leaked. With this patch: 0 of 144000. cp_node throughput does not change.

Reproduction, logs and the oops: https://github.com/breakertt/HomaModule/tree/fix-artifact/destroy-wait-for-reapers

@johnousterhout

johnousterhout commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Thanks for pointing out the bug, and for the suggested fix. However, I'm a bit uncomfortable with this PR because it adds yet another synchronization variable; Homa's synchronization is already way too complicated, so I hate to make it even more so.

Is there any other way to fix this problem without introducing a new synchronization variable? Homa already has a bunch of variables related to synchronizing sockets, such as its lock, its reference count, hsk->shutdown, and protect_count. Is there any way that some combination of these could be used to solve the problem?

@breakertt

Copy link
Copy Markdown
Contributor Author

Hi,

Thanks for your reply! To be honest, I am also uncomfortable with that. I will double check and get you back.

Cheers,
Tianyi

@Stuartwastaken

Copy link
Copy Markdown
Contributor

One way to do this with the existing reference count: free the pool in sk->sk_destruct instead of homa_sock_destroy. homa_timer (through the socktab scan) and softirq (through homa_sock_find) both hold a reference on the socket while they reap, and sk_destruct only runs after the last reference is dropped (and an RCU grace period, since Homa sets SOCK_RCU_FREE). So a reap in progress always finishes before the pool is freed. homa_sock_destroy still shuts the pool down and cancels its wakeup work, since that has to sleep.

The sk_wmem_alloc WARN in homa_sock_destroy is racy for the same reason, so I removed it.

I have this on a branch, with a unit test that hits the use-after-free under ASan without the change: Stuartwastaken#14. Tianyi, feel free to fold it into #153 if it works for your repro; I haven't run your xl170 test.

@breakertt

Copy link
Copy Markdown
Contributor Author

One way to do this with the existing reference count: free the pool in sk->sk_destruct instead of homa_sock_destroy. homa_timer (through the socktab scan) and softirq (through homa_sock_find) both hold a reference on the socket while they reap, and sk_destruct only runs after the last reference is dropped (and an RCU grace period, since Homa sets SOCK_RCU_FREE). So a reap in progress always finishes before the pool is freed. homa_sock_destroy still shuts the pool down and cancels its wakeup work, since that has to sleep.

The sk_wmem_alloc WARN in homa_sock_destroy is racy for the same reason, so I removed it.

I have this on a branch, with a unit test that hits the use-after-free under ASan without the change: Stuartwastaken#14. Tianyi, feel free to fold it into #153 if it works for your repro; I haven't run your xl170 test.

Hi Stuart,

Thanks a lot for looking into this and for putting the branch together. Using the socket reference count is a neat idea, and it does keep the pool alive for a reaper in progress. I have two concerns:

  1. homa_sock_destroy still does not wait for a reaper in progress. After ->destroy returns, sock_orphan sets sk->sk_wq to NULL, and the reaper calls homa_sock_wakeup_wmem after it unlocks the socket, which can then dereference NULL. In the version I am testing, the reaper does the wakeup before it unlocks the socket, and destroy waits for the socket lock, so this window is closed.

  2. Since John asked for less complexity, I would like to keep the change as small as possible, and mine ends up quite a bit smaller than moving the pool free to sk_destruct.

I will post a v2 soon. Comments and reviews are very welcome.

Cheers,
Tianyi

* homa_rpc_reap took RPCs off dead_rpcs under the socket lock, but
  released their pool space and sk_wmem_alloc and woke up writers only
  after it unlocked the socket (since d00a93b for sk_wmem_alloc).
  homa_sock_destroy waits only for dead_rpcs to be empty, so it could
  run while another thread (e.g. homa_timer) was still in that window:
  destroy then found sk_wmem_alloc != 1, or freed the pool before the
  reaper released into it (oops in homa_pool_free_bufs).
* homa_rpc_reap now releases the pool space and sk_wmem_alloc of each
  RPC before it takes the RPC off dead_rpcs, and wakes up writers before
  it unlocks the socket. An RPC that is off dead_rpcs holds no socket
  resources.
* homa_sock_destroy takes the socket lock once after its loop, so a
  reaper still in that locked section finishes first.
* homa_rpc_reap wakes up writers only if it reaped an RPC: a reap that
  gets the socket lock after homa_sock_destroy finds no RPCs, and must
  not touch sk_wq, which sk_common_release then sets to NULL.
@breakertt
breakertt force-pushed the fix/destroy-wait-for-reapers branch from 6f20431 to 04b3f68 Compare November 19, 2026 01:37
@breakertt

Copy link
Copy Markdown
Contributor Author

Hi John,

I have force-pushed a new fix to this branch.

Now homa_rpc_reap releases an RPC's pool space and sk_wmem_alloc, and wakes up writers, before it takes the RPC off dead_rpcs and before it unlocks the socket. homa_sock_destroy then takes the socket lock once after its loop, to wait for a reaper that is still in that section. This adds at most 5 pool releases per batch under the socket lock.

I updated the artifact: https://github.com/breakertt/HomaModule/tree/fix-artifact/destroy-wait-for-reapers

node1 module dead_frags_limit socket closes destroy found sk_wmem_alloc != 1
this change 0 3 x 48000 0
07ce63e 100 (default) 2 x 48000 1, 2
07ce63e 0 16000 3, then homa_timer oopsed in homa_pool_free_bufs (NULL pool)

Cheers,
Tianyi

@johnousterhout

Copy link
Copy Markdown
Member

This new version is definitely an improvement. However, it's still a bit special-casey in that it resolves one particular problem but doesn't provide a framework for preventing other similar problems. I had a long discussion about this with Claude and eventually decided to go with something along the lines of what Stuart proposed: Homa now takes advantage of the sock->sk_destruct callback, which is invoked at a time when there cannot be any more concurrent use of the socket. This provides a more general framework for cleaning things up (I had previously thought that homa_sock_destroy was invoked when the socket was quiescent, but that was incorrect).

Also, I removed the sk_wmem_alloc WARN: the logical place to do that would be in homa_sock_destruct, but it turns out that homa_sock_destruct won't be invoked until sk_wmem_alloc becomes zero, so checking it is pointless.

-John-

@breakertt

Copy link
Copy Markdown
Contributor Author

Hi John,

Thank you for taking the time to look into this. I was aware there were
other similar problems; my plan was to send PRs for them one by one so
as not to overwhelm you. It is really good to see them all fixed in one
go, and kudos to @Stuartwastaken as well.

Cheers,
Tianyi

@breakertt
breakertt deleted the fix/destroy-wait-for-reapers branch November 24, 2026 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants