History

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

Elena Reshetova committed 7 years ago

Commit ec1d2811 parent c45a7794 files · +7 −6Browse files

Showing the changes to include/linux (2 of 4 files). Show all

Unified · Split

2 files
  1. include/linux/sched.h +2 −1
  2. include/linux/sched/task.h +2 −2
include/linux/sched.h+2−1
@@ -21,6 +21,7 @@ · 20 unmodified lines
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 { · 580 unmodified lines
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+2−2
@@ -88,13 +88,13 @@ extern void sched_exec(void); · 87 unmodified lines
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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t