Compare
ffbfd3f98f4393e91dffce3a46ccb7cf5329cf2f..3267512ad1c2cc0c7768c5b014dc2cceb398a801
futex: Fix mm reuse handling for FUT_OFF_MMSHARED
A FUT_OFF_MMSHARED futex is a shared futex that refers to an MM.
It is possible for a process to wait on a shared futex with a different MM
because a FUT_OFF_INODE waiter can be requeued onto a FUT_OFF_MMSHARED
futex by another process.
This can cause FUT_OFF_MMSHARED waiters on a freed MM to consume
wakeups intended for a newly allocated MM at the same address.
Fix it by keying FUT_OFF_MMSHARED using a unique 64-bit per-MM ID.
Leave private futexes as before to avoid influencing the performance of the
hotpath.
(Multi-threaded processes typically implicitly use FUT_OFF_MMSHARED by
setting clear_child_tid such that it points into anonymous memory, which
causes mm_release() in a multi-threaded mm to perform FUTEX_WAKE.)
Fixes: 222993395ed3 ("futex: Remove pointless mmgrap() + mmdrop()")
Closes: https://lore.kernel.org/r/CAG48ez0dLBpc3QtbAhMMVNHwHL94iHh2G+h-=BVFR4dDuzZr1g@mail.gmail.com/
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20260929-futex-mmshared-fix-v1-1-262b1ffeb3d0@google.com
Cc: stable@vger.kernel.org
Jann Horn <jannh@google.com> · 2026-09-29 16:31 UTC · committed by Peter Zijlstra
4 files changed,
+42
−35
modified drivers/gpu/drm/xe/xe_bo.c
| @@ -944,8 +944,6 @@ static int xe_bo_move_notify(struct xe_bo *bo, | |||
| 944 | 944 | * | |
| 945 | 945 | * Transfers pages between shrinkable and purgeable buckets when the BO | |
| 946 | 946 | * purgeable state changes. Called automatically from xe_bo_set_purgeable_state(). | |
| 947 | - | * For pinned BOs, whose pages are not accounted, only the purgeable flag is | |
| 948 | - | * updated, so that the correct bucket is used when the BO is unpinned. | |
| 949 | 947 | */ | |
| 950 | 948 | static void xe_bo_set_purgeable_shrinker(struct xe_bo *bo, | |
| 951 | 949 | enum xe_madv_purgeable_state new_state) | |
| @@ -962,8 +960,7 @@ static void xe_bo_set_purgeable_shrinker(struct xe_bo *bo, | |||
| 962 | 960 | return; | |
| 963 | 961 | ||
| 964 | 962 | xe_tt = container_of(tt, struct xe_ttm_tt, ttm); | |
| 965 | - | /* Pinned bos are not accounted. Only update the purgeable flag. */ | |
| 966 | - | tt_pages = xe_bo_is_pinned(bo) ? 0 : tt->num_pages; | |
| 963 | + | tt_pages = tt->num_pages; | |
| 967 | 964 | ||
| 968 | 965 | if (!xe_tt->purgeable && new_state == XE_MADV_PURGEABLE_DONTNEED) { | |
| 969 | 966 | xe_tt->purgeable = true; | |
modified include/linux/futex.h
| @@ -23,28 +23,32 @@ struct task_struct; | |||
| 23 | 23 | * 01 : Shared futex (PTHREAD_PROCESS_SHARED) | |
| 24 | 24 | * mapped on a file (reference on the underlying inode) | |
| 25 | 25 | * 10 : Shared futex (PTHREAD_PROCESS_SHARED) | |
| 26 | - | * (but private mapping on an mm, and reference taken on it) | |
| 26 | + | * (but private mapping on an mm) | |
| 27 | 27 | */ | |
| 28 | 28 | ||
| 29 | - | #define FUT_OFF_INODE 1 /* We set bit 0 if key has a reference on inode */ | |
| 30 | - | #define FUT_OFF_MMSHARED 2 /* We set bit 1 if key has a reference on mm */ | |
| 29 | + | #define FUT_OFF_INODE 1 /* We set bit 0 if shared key identifies an inode */ | |
| 30 | + | #define FUT_OFF_MMSHARED 2 /* We set bit 1 if shared key identifies an mm */ | |
| 31 | 31 | ||
| 32 | 32 | union futex_key { | |
| 33 | + | /* For FUT_OFF_INODE */ | |
| 33 | 34 | struct { | |
| 34 | 35 | u64 i_seq; | |
| 35 | 36 | unsigned long pgoff; | |
| 36 | 37 | unsigned int offset; | |
| 37 | 38 | /* unsigned int node; */ | |
| 38 | 39 | } shared; | |
| 40 | + | ||
| 41 | + | /* For FUT_OFF_MMSHARED or private */ | |
| 39 | 42 | struct { | |
| 40 | 43 | union { | |
| 41 | - | struct mm_struct *mm; | |
| 42 | - | u64 __tmp; | |
| 44 | + | struct mm_struct *mm; /* for private */ | |
| 45 | + | u64 mm_seq; /* for FUT_OFF_MMSHARED */ | |
| 43 | 46 | }; | |
| 44 | 47 | unsigned long address; | |
| 45 | 48 | unsigned int offset; | |
| 46 | 49 | /* unsigned int node; */ | |
| 47 | 50 | } private; | |
| 51 | + | ||
| 48 | 52 | struct { | |
| 49 | 53 | u64 ptr; | |
| 50 | 54 | unsigned long word; | |
| @@ -152,11 +156,10 @@ static inline void futex_set_vdso_cs_range(struct futex_mm_data *fd, unsigned in | |||
| 152 | 156 | static inline void futex_fixup_robust_unlock(struct pt_regs *regs) { } | |
| 153 | 157 | #endif /* !CONFIG_FUTEX_ROBUST_UNLOCK */ | |
| 154 | 158 | ||
| 155 | - | ||
| 156 | - | #if defined(CONFIG_FUTEX_PRIVATE_HASH) || defined(CONFIG_FUTEX_ROBUST_UNLOCK) | |
| 159 | + | #ifdef CONFIG_FUTEX | |
| 157 | 160 | void futex_mm_init(struct mm_struct *mm); | |
| 158 | 161 | #else | |
| 159 | 162 | static inline void futex_mm_init(struct mm_struct *mm) { } | |
| 160 | - | #endif | |
| 163 | + | #endif /* !CONFIG_FUTEX */ | |
| 161 | 164 | ||
| 162 | 165 | #endif /* _LINUX_FUTEX_H */ | |
modified include/linux/futex_types.h
| @@ -85,10 +85,12 @@ struct futex_unlock_cs_ranges { }; | |||
| 85 | 85 | * struct futex_mm_data - Futex related per MM data | |
| 86 | 86 | * @phash: Futex private hash related data | |
| 87 | 87 | * @unlock: Futex unlock VDSO critical sections | |
| 88 | + | * @unique_id: Unique ID of the MM, for FUT_OFF_MMSHARED futexes | |
| 88 | 89 | */ | |
| 89 | 90 | struct futex_mm_data { | |
| 90 | 91 | struct futex_mm_phash phash; | |
| 91 | 92 | struct futex_unlock_cs_ranges unlock; | |
| 93 | + | atomic64_t unique_id; | |
| 92 | 94 | }; | |
| 93 | 95 | #else /* CONFIG_FUTEX */ | |
| 94 | 96 | struct futex_sched_data { }; | |
modified kernel/futex/core.c
| @@ -439,45 +439,51 @@ struct hrtimer_sleeper *futex_setup_timer(ktime_t *time, struct hrtimer_sleeper | |||
| 439 | 439 | } | |
| 440 | 440 | ||
| 441 | 441 | /* | |
| 442 | - | * Generate a machine wide unique identifier for this inode. | |
| 442 | + | * Generate a machine wide unique identifier for this object (inode or mm). | |
| 443 | 443 | * | |
| 444 | 444 | * This relies on u64 not wrapping in the life-time of the machine; which with | |
| 445 | 445 | * 1ns resolution means almost 585 years. | |
| 446 | - | * | |
| 447 | - | * This further relies on the fact that a well formed program will not unmap | |
| 448 | - | * the file while it has a (shared) futex waiting on it. This mapping will have | |
| 449 | - | * a file reference which pins the mount and inode. | |
| 450 | - | * | |
| 451 | - | * If for some reason an inode gets evicted and read back in again, it will get | |
| 452 | - | * a new sequence number and will _NOT_ match, even though it is the exact same | |
| 453 | - | * file. | |
| 454 | - | * | |
| 455 | - | * It is important that futex_match() will never have a false-positive, esp. | |
| 456 | - | * for PI futexes that can mess up the state. The above argues that false-negatives | |
| 457 | - | * are only possible for malformed programs. | |
| 458 | 446 | */ | |
| 459 | - | static u64 get_inode_sequence_number(struct inode *inode) | |
| 447 | + | static u64 get_object_id(atomic64_t *object_seq) | |
| 460 | 448 | { | |
| 461 | - | static atomic64_t i_seq; | |
| 449 | + | static atomic64_t last_assigned_seq; | |
| 462 | 450 | u64 old; | |
| 463 | 451 | ||
| 464 | - | /* Does the inode already have a sequence number? */ | |
| 465 | - | old = atomic64_read(&inode->i_sequence); | |
| 452 | + | /* Does the object already have a sequence number? */ | |
| 453 | + | old = atomic64_read(object_seq); | |
| 466 | 454 | if (likely(old)) | |
| 467 | 455 | return old; | |
| 468 | 456 | ||
| 469 | 457 | for (;;) { | |
| 470 | - | u64 new = atomic64_inc_return(&i_seq); | |
| 458 | + | u64 new = atomic64_inc_return(&last_assigned_seq); | |
| 471 | 459 | if (WARN_ON_ONCE(!new)) | |
| 472 | 460 | continue; | |
| 473 | 461 | ||
| 474 | 462 | old = 0; | |
| 475 | - | if (!atomic64_try_cmpxchg_relaxed(&inode->i_sequence, &old, new)) | |
| 463 | + | if (!atomic64_try_cmpxchg_relaxed(object_seq, &old, new)) | |
| 476 | 464 | return old; | |
| 477 | 465 | return new; | |
| 478 | 466 | } | |
| 479 | 467 | } | |
| 480 | 468 | ||
| 469 | + | /* | |
| 470 | + | * This relies on the fact that a well formed program will not unmap | |
| 471 | + | * the file while it has a (shared) futex waiting on it. This mapping will have | |
| 472 | + | * a file reference which pins the mount and inode. | |
| 473 | + | * | |
| 474 | + | * If for some reason an inode gets evicted and read back in again, it will get | |
| 475 | + | * a new sequence number and will _NOT_ match, even though it is the exact same | |
| 476 | + | * file. | |
| 477 | + | * | |
| 478 | + | * It is important that futex_match() will never have a false-positive, esp. | |
| 479 | + | * for PI futexes that can mess up the state. The above argues that false-negatives | |
| 480 | + | * are only possible for malformed programs. | |
| 481 | + | */ | |
| 482 | + | static u64 get_inode_sequence_number(struct inode *inode) | |
| 483 | + | { | |
| 484 | + | return get_object_id(&inode->i_sequence); | |
| 485 | + | } | |
| 486 | + | ||
| 481 | 487 | /** | |
| 482 | 488 | * get_futex_key() - Get parameters which are the keys for a futex | |
| 483 | 489 | * @uaddr: virtual address of the futex | |
| @@ -683,8 +689,8 @@ again: | |||
| 683 | 689 | goto out; | |
| 684 | 690 | } | |
| 685 | 691 | ||
| 686 | - | key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */ | |
| 687 | - | key->private.mm = mm; | |
| 692 | + | key->both.offset |= FUT_OFF_MMSHARED; | |
| 693 | + | key->private.mm_seq = get_object_id(&mm->futex.unique_id); | |
| 688 | 694 | key->private.address = address; | |
| 689 | 695 | ||
| 690 | 696 | } else { | |
| @@ -2055,13 +2061,12 @@ static void futex_robust_unlock_init_mm(struct futex_mm_data *fd) | |||
| 2055 | 2061 | static inline void futex_robust_unlock_init_mm(struct futex_mm_data *fd) { } | |
| 2056 | 2062 | #endif /* !CONFIG_FUTEX_ROBUST_UNLOCK */ | |
| 2057 | 2063 | ||
| 2058 | - | #if defined(CONFIG_FUTEX_PRIVATE_HASH) || defined(CONFIG_FUTEX_ROBUST_UNLOCK) | |
| 2059 | 2064 | void futex_mm_init(struct mm_struct *mm) | |
| 2060 | 2065 | { | |
| 2066 | + | atomic64_set(&mm->futex.unique_id, 0); | |
| 2061 | 2067 | futex_hash_init_mm(&mm->futex); | |
| 2062 | 2068 | futex_robust_unlock_init_mm(&mm->futex); | |
| 2063 | 2069 | } | |
| 2064 | - | #endif | |
| 2065 | 2070 | ||
| 2066 | 2071 | int futex_hash_prctl(unsigned long arg2, unsigned long arg3, unsigned long arg4) | |
| 2067 | 2072 | { | |