Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

futex: Allow to make the private hash immutable

My initial testing showed that:

perf bench futex hash

reported less operations/sec with private hash. After using the same
amount of buckets in the private hash as used by the global hash then
the operations/sec were about the same.

This changed once the private hash became resizable. This feature added
an RCU section and reference counting via atomic inc+dec operation into
the hot path.
The reference counting can be avoided if the private hash is made
immutable.
Extend PR_FUTEX_HASH_SET_SLOTS by a fourth argument which denotes if the
private should be made immutable. Once set (to true) the a further
resize is not allowed (same if set to global hash).
Add PR_FUTEX_HASH_GET_IMMUTABLE which returns true if the hash can not
be changed.
Update "perf bench" suite.

For comparison, results of "perf bench futex hash -s":
- Xeon CPU E5-2650, 2 NUMA nodes, total 32 CPUs:
- Before the introducing task local hash
shared Averaged 1.487.148 operations/sec (+- 0,53%), total secs = 10
private Averaged 2.192.405 operations/sec (+- 0,07%), total secs = 10

- With the series
shared Averaged 1.326.342 operations/sec (+- 0,41%), total secs = 10
-b128 Averaged 141.394 operations/sec (+- 1,15%), total secs = 10
-Ib128 Averaged 851.490 operations/sec (+- 0,67%), total secs = 10
-b8192 Averaged 131.321 operations/sec (+- 2,13%), total secs = 10
-Ib8192 Averaged 1.923.077 operations/sec (+- 0,61%), total secs = 10
128 is the default allocation of hash buckets.
8192 was the previous amount of allocated hash buckets.

- Xeon(R) CPU E7-8890 v3, 4 NUMA nodes, total 144 CPUs:
- Before the introducing task local hash
shared Averaged 1.810.936 operations/sec (+- 0,26%), total secs = 20
private Averaged 2.505.801 operations/sec (+- 0,05%), total secs = 20

- With the series
shared Averaged 1.589.002 operations/sec (+- 0,25%), total secs = 20
-b1024 Averaged 42.410 operations/sec (+- 0,20%), total secs = 20
-Ib1024 Averaged 740.638 operations/sec (+- 1,51%), total secs = 20
-b65536 Averaged 48.811 operations/sec (+- 1,35%), total secs = 20
-Ib65536 Averaged 1.963.165 operations/sec (+- 0,18%), total secs = 20
1024 is the default allocation of hash buckets.
65536 was the previous amount of allocated hash buckets.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Shrikanth Hegde <sshegde@linux.ibm.com>
Link: https://lore.kernel.org/r/20250416162921.513656-16-bigeasy@linutronix.de

authored by

Sebastian Andrzej Siewior and committed by
Peter Zijlstra
63e8595c bd54df5e

+45 -6
+2
include/uapi/linux/prctl.h
··· 367 367 /* FUTEX hash management */ 368 368 #define PR_FUTEX_HASH 78 369 369 # define PR_FUTEX_HASH_SET_SLOTS 1 370 + # define FH_FLAG_IMMUTABLE (1ULL << 0) 370 371 # define PR_FUTEX_HASH_GET_SLOTS 2 372 + # define PR_FUTEX_HASH_GET_IMMUTABLE 3 371 373 372 374 #endif /* _LINUX_PRCTL_H */
+43 -6
kernel/futex/core.c
··· 63 63 struct rcu_head rcu; 64 64 void *mm; 65 65 bool custom; 66 + bool immutable; 66 67 struct futex_hash_bucket queues[]; 67 68 }; 68 69 ··· 133 132 134 133 bool futex_private_hash_get(struct futex_private_hash *fph) 135 134 { 135 + if (fph->immutable) 136 + return true; 136 137 return rcuref_get(&fph->users); 137 138 } 138 139 139 140 void futex_private_hash_put(struct futex_private_hash *fph) 140 141 { 141 142 /* Ignore return value, last put is verified via rcuref_is_dead() */ 143 + if (fph->immutable) 144 + return; 142 145 if (rcuref_put(&fph->users)) 143 146 wake_up_var(fph->mm); 144 147 } ··· 282 277 if (!fph) 283 278 return NULL; 284 279 280 + if (fph->immutable) 281 + return fph; 285 282 if (rcuref_get(&fph->users)) 286 283 return fph; 287 284 } ··· 1390 1383 spin_lock_init(&fhb->lock); 1391 1384 } 1392 1385 1386 + #define FH_CUSTOM 0x01 1387 + #define FH_IMMUTABLE 0x02 1388 + 1393 1389 #ifdef CONFIG_FUTEX_PRIVATE_HASH 1394 1390 void futex_hash_free(struct mm_struct *mm) 1395 1391 { ··· 1443 1433 return false; /* equal */ 1444 1434 } 1445 1435 1446 - static int futex_hash_allocate(unsigned int hash_slots, bool custom) 1436 + static int futex_hash_allocate(unsigned int hash_slots, unsigned int flags) 1447 1437 { 1448 1438 struct mm_struct *mm = current->mm; 1449 1439 struct futex_private_hash *fph; 1440 + bool custom = flags & FH_CUSTOM; 1450 1441 int i; 1451 1442 1452 1443 if (hash_slots && (hash_slots == 1 || !is_power_of_2(hash_slots))) ··· 1458 1447 */ 1459 1448 scoped_guard(rcu) { 1460 1449 fph = rcu_dereference(mm->futex_phash); 1461 - if (fph && !fph->hash_mask) { 1450 + if (fph && (!fph->hash_mask || fph->immutable)) { 1462 1451 if (custom) 1463 1452 return -EBUSY; 1464 1453 return 0; ··· 1472 1461 rcuref_init(&fph->users, 1); 1473 1462 fph->hash_mask = hash_slots ? hash_slots - 1 : 0; 1474 1463 fph->custom = custom; 1464 + fph->immutable = !!(flags & FH_IMMUTABLE); 1475 1465 fph->mm = mm; 1476 1466 1477 1467 for (i = 0; i < hash_slots; i++) ··· 1565 1553 if (current_buckets >= buckets) 1566 1554 return 0; 1567 1555 1568 - return futex_hash_allocate(buckets, false); 1556 + return futex_hash_allocate(buckets, 0); 1569 1557 } 1570 1558 1571 1559 static int futex_hash_get_slots(void) ··· 1579 1567 return 0; 1580 1568 } 1581 1569 1570 + static int futex_hash_get_immutable(void) 1571 + { 1572 + struct futex_private_hash *fph; 1573 + 1574 + guard(rcu)(); 1575 + fph = rcu_dereference(current->mm->futex_phash); 1576 + if (fph && fph->immutable) 1577 + return 1; 1578 + if (fph && !fph->hash_mask) 1579 + return 1; 1580 + return 0; 1581 + } 1582 + 1582 1583 #else 1583 1584 1584 - static int futex_hash_allocate(unsigned int hash_slots, bool custom) 1585 + static int futex_hash_allocate(unsigned int hash_slots, unsigned int flags) 1585 1586 { 1586 1587 return -EINVAL; 1587 1588 } ··· 1603 1578 { 1604 1579 return 0; 1605 1580 } 1581 + 1582 + static int futex_hash_get_immutable(void) 1583 + { 1584 + return 0; 1585 + } 1606 1586 #endif 1607 1587 1608 1588 int futex_hash_prctl(unsigned long arg2, unsigned long arg3, unsigned long arg4) 1609 1589 { 1590 + unsigned int flags = FH_CUSTOM; 1610 1591 int ret; 1611 1592 1612 1593 switch (arg2) { 1613 1594 case PR_FUTEX_HASH_SET_SLOTS: 1614 - if (arg4 != 0) 1595 + if (arg4 & ~FH_FLAG_IMMUTABLE) 1615 1596 return -EINVAL; 1616 - ret = futex_hash_allocate(arg3, true); 1597 + if (arg4 & FH_FLAG_IMMUTABLE) 1598 + flags |= FH_IMMUTABLE; 1599 + ret = futex_hash_allocate(arg3, flags); 1617 1600 break; 1618 1601 1619 1602 case PR_FUTEX_HASH_GET_SLOTS: 1620 1603 ret = futex_hash_get_slots(); 1604 + break; 1605 + 1606 + case PR_FUTEX_HASH_GET_IMMUTABLE: 1607 + ret = futex_hash_get_immutable(); 1621 1608 break; 1622 1609 1623 1610 default: