Skip to content

Commit ec1d281

Browse files
ereshetovaingomolnar
authored andcommitted
sched/core: Convert task_struct.usage to refcount_t
atomic_t variables are currently used to implement reference counters with the following properties: - counter is initialized to 1 using atomic_set() - a resource is freed upon counter reaching zero - once counter reaches zero, its further increments aren't allowed - counter schema uses basic atomic operations (set, inc, inc_not_zero, dec_and_test, etc.) Such atomic variables should be converted to a newly provided refcount_t type and API that prevents accidental counter overflows and underflows. This is important since overflows and underflows can lead to use-after-free situation and be exploitable. The variable task_struct.usage is used as pure reference counter. Convert it to refcount_t and fix up the operations. ** Important note for maintainers: Some functions from refcount_t API defined in lib/refcount.c have different memory ordering guarantees than their atomic counterparts. The full comparison can be seen in https://lkml.org/lkml/2017/11/15/57 and it is hopefully soon in state to be merged to the documentation tree. Normally the differences should not matter since refcount_t provides enough guarantees to satisfy the refcounting use cases, but in some rare cases it might matter. Please double check that you don't have some undocumented memory guarantees for this variable usage. For the task_struct.usage it might make a difference in following places: - put_task_struct(): decrement in refcount_dec_and_test() only provides RELEASE ordering and control dependency on success vs. fully ordered atomic counterpart Suggested-by: Kees Cook <keescook@chromium.org> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: David Windsor <dwindsor@gmail.com> Reviewed-by: Hans Liljestrand <ishkamiel@gmail.com> Reviewed-by: Andrea Parri <andrea.parri@amarulasolutions.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: akpm@linux-foundation.org Cc: viro@zeniv.linux.org.uk Link: https://lkml.kernel.org/r/1547814450-18902-5-git-send-email-elena.reshetova@intel.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent c45a779 commit ec1d281

4 files changed

Lines changed: 7 additions & 6 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/seccomp.h>
2222
#include <linux/nodemask.h>
2323
#include <linux/rcupdate.h>
24+
#include <linux/refcount.h>
2425
#include <linux/resource.h>
2526
#include <linux/latencytop.h>
2627
#include <linux/sched/prio.h>
@@ -607,7 +608,7 @@ struct task_struct {
607608
randomized_struct_fields_start
608609

609610
void *stack;
610-
atomic_t usage;
611+
refcount_t usage;
611612
/* Per task flags (PF_*), defined further below: */
612613
unsigned int flags;
613614
unsigned int ptrace;

‎include/linux/sched/task.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ extern void sched_exec(void);
8888
#define sched_exec() {}
8989
#endif
9090

91-
#define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
91+
#define get_task_struct(tsk) do { refcount_inc(&(tsk)->usage); } while(0)
9292

9393
extern void __put_task_struct(struct task_struct *t);
9494

9595
static inline void put_task_struct(struct task_struct *t)
9696
{
97-
if (atomic_dec_and_test(&t->usage))
97+
if (refcount_dec_and_test(&t->usage))
9898
__put_task_struct(t);
9999
}
100100

‎init/init_task.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct task_struct init_task
6565
#endif
6666
.state = 0,
6767
.stack = init_stack,
68-
.usage = ATOMIC_INIT(2),
68+
.usage = REFCOUNT_INIT(2),
6969
.flags = PF_KTHREAD,
7070
.prio = MAX_PRIO - 20,
7171
.static_prio = MAX_PRIO - 20,

‎kernel/fork.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ static inline void put_signal_struct(struct signal_struct *sig)
717717
void __put_task_struct(struct task_struct *tsk)
718718
{
719719
WARN_ON(!tsk->exit_state);
720-
WARN_ON(atomic_read(&tsk->usage));
720+
WARN_ON(refcount_read(&tsk->usage));
721721
WARN_ON(tsk == current);
722722

723723
cgroup_free(tsk);
@@ -896,7 +896,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
896896
* One for us, one for whoever does the "release_task()" (usually
897897
* parent)
898898
*/
899-
atomic_set(&tsk->usage, 2);
899+
refcount_set(&tsk->usage, 2);
900900
#ifdef CONFIG_BLK_DEV_IO_TRACE
901901
tsk->btrace_seq = 0;
902902
#endif

0 commit comments

Comments
 (0)