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
21#include <linux/seccomp.h>21#include <linux/seccomp.h>
22#include <linux/nodemask.h>22#include <linux/nodemask.h>
23#include <linux/rcupdate.h>23#include <linux/rcupdate.h>
24#include <linux/refcount.h>
24#include <linux/resource.h>25#include <linux/resource.h>
25#include <linux/latencytop.h>26#include <linux/latencytop.h>
26#include <linux/sched/prio.h>27#include <linux/sched/prio.h>
@@ -607,7 +608,7 @@ struct task_struct { · 580 unmodified lines
607 randomized_struct_fields_start608 randomized_struct_fields_start
608 609
609 void *stack;610 void *stack;
610 atomic_t usage;611 refcount_t usage;
611 /* Per task flags (PF_*), defined further below: */612 /* Per task flags (PF_*), defined further below: */
612 unsigned int flags;613 unsigned int flags;
613 unsigned int ptrace;614 unsigned int ptrace;
include/linux/sched/task.h+2−2
@@ -88,13 +88,13 @@ extern void sched_exec(void); · 87 unmodified lines
88#define sched_exec() {}88#define sched_exec() {}
89#endif89#endif
90 90
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)
92 92
93extern void __put_task_struct(struct task_struct *t);93extern void __put_task_struct(struct task_struct *t);
94 94
95static inline void put_task_struct(struct task_struct *t)95static inline void put_task_struct(struct task_struct *t)
96{96{
97 if (atomic_dec_and_test(&t->usage))97 if (refcount_dec_and_test(&t->usage))
98 __put_task_struct(t);98 __put_task_struct(t);
99}99}
100 100

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