Skip to content

Commit 7c4f75a

Browse files
Sebastian Andrzej SiewiorPeter Zijlstra
authored andcommitted
futex: Allow automatic allocation of process wide futex hash
Allocate a private futex hash with 16 slots if a task forks its first thread. Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 80367ad commit 7c4f75a

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

include/linux/futex.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
8080
int futex_hash_prctl(unsigned long arg2, unsigned long arg3, unsigned long arg4);
8181

8282
#ifdef CONFIG_FUTEX_PRIVATE_HASH
83+
int futex_hash_allocate_default(void);
8384
void futex_hash_free(struct mm_struct *mm);
8485

8586
static inline void futex_mm_init(struct mm_struct *mm)
@@ -88,6 +89,7 @@ static inline void futex_mm_init(struct mm_struct *mm)
8889
}
8990

9091
#else /* !CONFIG_FUTEX_PRIVATE_HASH */
92+
static inline int futex_hash_allocate_default(void) { return 0; }
9193
static inline void futex_hash_free(struct mm_struct *mm) { }
9294
static inline void futex_mm_init(struct mm_struct *mm) { }
9395
#endif /* CONFIG_FUTEX_PRIVATE_HASH */
@@ -107,6 +109,10 @@ static inline int futex_hash_prctl(unsigned long arg2, unsigned long arg3, unsig
107109
{
108110
return -EINVAL;
109111
}
112+
static inline int futex_hash_allocate_default(void)
113+
{
114+
return 0;
115+
}
110116
static inline void futex_hash_free(struct mm_struct *mm) { }
111117
static inline void futex_mm_init(struct mm_struct *mm) { }
112118

kernel/fork.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,6 +2164,13 @@ static void rv_task_fork(struct task_struct *p)
21642164
#define rv_task_fork(p) do {} while (0)
21652165
#endif
21662166

2167+
static bool need_futex_hash_allocate_default(u64 clone_flags)
2168+
{
2169+
if ((clone_flags & (CLONE_THREAD | CLONE_VM)) != (CLONE_THREAD | CLONE_VM))
2170+
return false;
2171+
return true;
2172+
}
2173+
21672174
/*
21682175
* This creates a new process as a copy of the old one,
21692176
* but does not actually start it yet.
@@ -2544,6 +2551,21 @@ __latent_entropy struct task_struct *copy_process(
25442551
if (retval)
25452552
goto bad_fork_cancel_cgroup;
25462553

2554+
/*
2555+
* Allocate a default futex hash for the user process once the first
2556+
* thread spawns.
2557+
*/
2558+
if (need_futex_hash_allocate_default(clone_flags)) {
2559+
retval = futex_hash_allocate_default();
2560+
if (retval)
2561+
goto bad_fork_core_free;
2562+
/*
2563+
* If we fail beyond this point we don't free the allocated
2564+
* futex hash map. We assume that another thread will be created
2565+
* and makes use of it. The hash map will be freed once the main
2566+
* thread terminates.
2567+
*/
2568+
}
25472569
/*
25482570
* From this point on we must avoid any synchronous user-space
25492571
* communication until we take the tasklist-lock. In particular, we do

kernel/futex/core.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,17 @@ static int futex_hash_allocate(unsigned int hash_slots, bool custom)
12941294
return 0;
12951295
}
12961296

1297+
int futex_hash_allocate_default(void)
1298+
{
1299+
if (!current->mm)
1300+
return 0;
1301+
1302+
if (current->mm->futex_phash)
1303+
return 0;
1304+
1305+
return futex_hash_allocate(16, false);
1306+
}
1307+
12971308
static int futex_hash_get_slots(void)
12981309
{
12991310
struct futex_private_hash *fph;

0 commit comments

Comments
 (0)