Skip to content

Commit 63e8595

Browse files
Sebastian Andrzej SiewiorPeter Zijlstra
authored andcommitted
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 <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: Shrikanth Hegde <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent bd54df5 commit 63e8595

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

include/uapi/linux/prctl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ struct prctl_mm_map {
367367
/* FUTEX hash management */
368368
#define PR_FUTEX_HASH 78
369369
# define PR_FUTEX_HASH_SET_SLOTS 1
370+
# define FH_FLAG_IMMUTABLE (1ULL << 0)
370371
# define PR_FUTEX_HASH_GET_SLOTS 2
372+
# define PR_FUTEX_HASH_GET_IMMUTABLE 3
371373

372374
#endif /* _LINUX_PRCTL_H */

kernel/futex/core.c

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct futex_private_hash {
6363
struct rcu_head rcu;
6464
void *mm;
6565
bool custom;
66+
bool immutable;
6667
struct futex_hash_bucket queues[];
6768
};
6869

@@ -132,12 +133,16 @@ static inline bool futex_key_is_private(union futex_key *key)
132133

133134
bool futex_private_hash_get(struct futex_private_hash *fph)
134135
{
136+
if (fph->immutable)
137+
return true;
135138
return rcuref_get(&fph->users);
136139
}
137140

138141
void futex_private_hash_put(struct futex_private_hash *fph)
139142
{
140143
/* Ignore return value, last put is verified via rcuref_is_dead() */
144+
if (fph->immutable)
145+
return;
141146
if (rcuref_put(&fph->users))
142147
wake_up_var(fph->mm);
143148
}
@@ -277,6 +282,8 @@ struct futex_private_hash *futex_private_hash(void)
277282
if (!fph)
278283
return NULL;
279284

285+
if (fph->immutable)
286+
return fph;
280287
if (rcuref_get(&fph->users))
281288
return fph;
282289
}
@@ -1383,6 +1390,9 @@ static void futex_hash_bucket_init(struct futex_hash_bucket *fhb,
13831390
spin_lock_init(&fhb->lock);
13841391
}
13851392

1393+
#define FH_CUSTOM 0x01
1394+
#define FH_IMMUTABLE 0x02
1395+
13861396
#ifdef CONFIG_FUTEX_PRIVATE_HASH
13871397
void futex_hash_free(struct mm_struct *mm)
13881398
{
@@ -1433,10 +1443,11 @@ static bool futex_hash_less(struct futex_private_hash *a,
14331443
return false; /* equal */
14341444
}
14351445

1436-
static int futex_hash_allocate(unsigned int hash_slots, bool custom)
1446+
static int futex_hash_allocate(unsigned int hash_slots, unsigned int flags)
14371447
{
14381448
struct mm_struct *mm = current->mm;
14391449
struct futex_private_hash *fph;
1450+
bool custom = flags & FH_CUSTOM;
14401451
int i;
14411452

14421453
if (hash_slots && (hash_slots == 1 || !is_power_of_2(hash_slots)))
@@ -1447,7 +1458,7 @@ static int futex_hash_allocate(unsigned int hash_slots, bool custom)
14471458
*/
14481459
scoped_guard(rcu) {
14491460
fph = rcu_dereference(mm->futex_phash);
1450-
if (fph && !fph->hash_mask) {
1461+
if (fph && (!fph->hash_mask || fph->immutable)) {
14511462
if (custom)
14521463
return -EBUSY;
14531464
return 0;
@@ -1461,6 +1472,7 @@ static int futex_hash_allocate(unsigned int hash_slots, bool custom)
14611472
rcuref_init(&fph->users, 1);
14621473
fph->hash_mask = hash_slots ? hash_slots - 1 : 0;
14631474
fph->custom = custom;
1475+
fph->immutable = !!(flags & FH_IMMUTABLE);
14641476
fph->mm = mm;
14651477

14661478
for (i = 0; i < hash_slots; i++)
@@ -1553,7 +1565,7 @@ int futex_hash_allocate_default(void)
15531565
if (current_buckets >= buckets)
15541566
return 0;
15551567

1556-
return futex_hash_allocate(buckets, false);
1568+
return futex_hash_allocate(buckets, 0);
15571569
}
15581570

15591571
static int futex_hash_get_slots(void)
@@ -1567,9 +1579,22 @@ static int futex_hash_get_slots(void)
15671579
return 0;
15681580
}
15691581

1582+
static int futex_hash_get_immutable(void)
1583+
{
1584+
struct futex_private_hash *fph;
1585+
1586+
guard(rcu)();
1587+
fph = rcu_dereference(current->mm->futex_phash);
1588+
if (fph && fph->immutable)
1589+
return 1;
1590+
if (fph && !fph->hash_mask)
1591+
return 1;
1592+
return 0;
1593+
}
1594+
15701595
#else
15711596

1572-
static int futex_hash_allocate(unsigned int hash_slots, bool custom)
1597+
static int futex_hash_allocate(unsigned int hash_slots, unsigned int flags)
15731598
{
15741599
return -EINVAL;
15751600
}
@@ -1578,23 +1603,35 @@ static int futex_hash_get_slots(void)
15781603
{
15791604
return 0;
15801605
}
1606+
1607+
static int futex_hash_get_immutable(void)
1608+
{
1609+
return 0;
1610+
}
15811611
#endif
15821612

15831613
int futex_hash_prctl(unsigned long arg2, unsigned long arg3, unsigned long arg4)
15841614
{
1615+
unsigned int flags = FH_CUSTOM;
15851616
int ret;
15861617

15871618
switch (arg2) {
15881619
case PR_FUTEX_HASH_SET_SLOTS:
1589-
if (arg4 != 0)
1620+
if (arg4 & ~FH_FLAG_IMMUTABLE)
15901621
return -EINVAL;
1591-
ret = futex_hash_allocate(arg3, true);
1622+
if (arg4 & FH_FLAG_IMMUTABLE)
1623+
flags |= FH_IMMUTABLE;
1624+
ret = futex_hash_allocate(arg3, flags);
15921625
break;
15931626

15941627
case PR_FUTEX_HASH_GET_SLOTS:
15951628
ret = futex_hash_get_slots();
15961629
break;
15971630

1631+
case PR_FUTEX_HASH_GET_IMMUTABLE:
1632+
ret = futex_hash_get_immutable();
1633+
break;
1634+
15981635
default:
15991636
ret = -EINVAL;
16001637
break;

0 commit comments

Comments
 (0)