Page MenuHomeFreeBSD

umtx: use a distribution-fair multiplier for the chain hash
ClosedPublic

Authored by nick_spun.io on Jul 19 2026, 5:47 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Aug 30, 2:05 PM
Unknown Object (File)
Sun, Aug 30, 2:01 PM
Unknown Object (File)
Sun, Aug 30, 2:01 PM
Unknown Object (File)
Sun, Aug 30, 9:10 AM
Unknown Object (File)
Sun, Aug 30, 8:29 AM
Unknown Object (File)
Sun, Aug 30, 6:39 AM
Unknown Object (File)
Sun, Aug 30, 5:18 AM
Unknown Object (File)
Sun, Aug 30, 4:26 AM
Subscribers

Details

Summary
umtxq_hash() multiplies the key by 0x9E370001 and keeps the high bits.  That
constant is 0x9E37 * 2^16 + 1, so it degenerates for keys whose spacing carries
trailing zero bits: at a 64 KiB stride it puts 128 of 512 parked waiters onto a
single chain mutex, and at 16 KiB and up it uses only a handful of the 512
chains.  Base-system consumers never hit this because libthr places its own wait
words 128 bytes apart, but a Linux-ABI runtime waiting on addresses it allocates
itself lands squarely on the floor.  Switch to 0x61C88647, which leaves at most 3
waiters per chain at the same stride; Linux made this exact change in 2016, after
judging the sparse constants "actively bad for hashing".

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

I think it'll be worthwhile putting a comment in here explaining the behavioural differences between the two values. Eg, referencing the linux commit, explanation about sparse alloc'ed arenas and waits, etc.
Maybe like three or four lines tops?

I do not see how this could be an option. Either there is no (bad) impact on the native umtxes, and then we should just change the constant, perhaps adding a comment explaining its origin and reasoning behind. Or there is the impact, but then you could check that the curproc ABI is linux, and use the proposed constant for Linux processes.

In D58337#1344794, @kib wrote:

I do not see how this could be an option. Either there is no (bad) impact on the native umtxes, and then we should just change the constant, perhaps adding a comment explaining its origin and reasoning behind. Or there is the impact, but then you could check that the curproc ABI is linux, and use the proposed constant for Linux processes.

I switched to the ABI-based check and it makes more sense, however after further testing I have only been able to demonstrate measurable impact in synthetic benchmarks that end up doing millions of umtx calls against a single chain under the sparse hash, and I don't think we're likely to ever need to guard against that under real-world conditions

That said, any impact from changing to the fair multiplier for _native_ workloads is minimal enough to blend in with background noise even if the the most common scenario gives slightly less contention under similarly-unlikely conditions under our current sparse multiplier.

Extremely unlikely but significant upside, and a virtually unmeasurable downside. Probably not worth losing sleep over either way.

So again, why not change the multiplier unconditionally?

If there is evidence that we should not change it for native binaries, then I suggest to implement this differently. Add sv_umtx_hash_mult member to struct sysent, and set it accordingly for native vs linuxolators sysents. Then you do not need if() in umtxq_hash(), the p_sysent is de-referenced there anyway.

If there is evidence that we should not change it for native binaries

I agree with @kib - I would change it unconditionally, unless we can demonstrate that doing so somehow pessimizes native binaries.

nick_spun.io retitled this revision from umtx: add FAIR_LINUX_FUTEX_HASHING for a distribution-fair chain hash to umtx: use a distribution-fair multiplier for the chain hash.Sun, Aug 16, 3:37 PM

So hm, yay or nay on this?

Apparently I missed the submit button for one more review note.

sys/kern/kern_umtx.c
117

I do not think the comment is appropriate. History of the code needs to go into the commit message. The comment should explain the current state.

In D58337#1355810, @kib wrote:

So hm, yay or nay on this?

Apparently I missed the submit button for one more review note.

I'll cut it out

This revision is now accepted and ready to land.Sun, Aug 30, 5:26 AM

oh, before I remove it - go update the comments - they need to be a bits horter and there's no longer a kernel config option for it.

Change looks fine to me. Agree w/ Adrian that the commit message can be shortened significantly.