The mutex will be used in subsequent changes to replace the busy looping of a waiter when the futex owner is currently executing the exit cleanup to prevent a potential live lock.
Unified · Split
| @@ -65,6 +65,7 @@ static inline void futex_init_task(struct task_struct *tsk) · 64 unmodified lines | ||
| 65 | 65 | INIT_LIST_HEAD(&tsk->pi_state_list); |
| 66 | 66 | tsk->pi_state_cache = NULL; |
| 67 | 67 | tsk->futex_state = FUTEX_STATE_OK; |
| 68 | + mutex_init(&tsk->futex_exit_mutex); | |
| 68 | 69 | } |
| 69 | 70 | |
| 70 | 71 | void futex_exit_recursive(struct task_struct *tsk); |
| @@ -1053,6 +1053,7 @@ struct task_struct { · 1052 unmodified lines | ||
| 1053 | 1053 | #endif |
| 1054 | 1054 | struct list_head pi_state_list; |
| 1055 | 1055 | struct futex_pi_state *pi_state_cache; |
| 1056 | + struct mutex futex_exit_mutex; | |
| 1056 | 1057 | unsigned int futex_state; |
| 1057 | 1058 | #endif |
| 1058 | 1059 | #ifdef CONFIG_PERF_EVENTS |
| @@ -3698,11 +3698,22 @@ static void futex_cleanup(struct task_struct *tsk) · 3697 unmodified lines | ||
| 3698 | 3698 | */ |
| 3699 | 3699 | void futex_exit_recursive(struct task_struct *tsk) |
| 3700 | 3700 | { |
| 3701 | + /* If the state is FUTEX_STATE_EXITING then futex_exit_mutex is held */ | |
| 3702 | + if (tsk->futex_state == FUTEX_STATE_EXITING) | |
| 3703 | + mutex_unlock(&tsk->futex_exit_mutex); | |
| 3701 | 3704 | tsk->futex_state = FUTEX_STATE_DEAD; |
| 3702 | 3705 | } |
| 3703 | 3706 | |
| 3704 | 3707 | static void futex_cleanup_begin(struct task_struct *tsk) |
| 3705 | 3708 | { |
| 3709 | + /* | |
| 3710 | + * Prevent various race issues against a concurrent incoming waiter | |
| 3711 | + * including live locks by forcing the waiter to block on | |
| 3712 | + * tsk->futex_exit_mutex when it observes FUTEX_STATE_EXITING in | |
| 3713 | + * attach_to_pi_owner(). | |
| 3714 | + */ | |
| 3715 | + mutex_lock(&tsk->futex_exit_mutex); | |
| 3716 | + | |
| 3706 | 3717 | /* |
| 3707 | 3718 | * Switch the state to FUTEX_STATE_EXITING under tsk->pi_lock. |
| 3708 | 3719 | * |
| @@ -3726,6 +3737,11 @@ static void futex_cleanup_end(struct task_struct *tsk, int state) · 17 unmodified lines | ||
| 3726 | 3737 | * take another loop until it becomes visible. |
| 3727 | 3738 | */ |
| 3728 | 3739 | tsk->futex_state = state; |
| 3740 | + /* | |
| 3741 | + * Drop the exit protection. This unblocks waiters which observed | |
| 3742 | + * FUTEX_STATE_EXITING to reevaluate the state. | |
| 3743 | + */ | |
| 3744 | + mutex_unlock(&tsk->futex_exit_mutex); | |
| 3729 | 3745 | } |
| 3730 | 3746 | |
| 3731 | 3747 | void futex_exec_release(struct task_struct *tsk) |