From: ZhangPeng zhangpeng362@huawei.com
maillist inclusion category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/I6PKGM
Reference: https://lore.kernel.org/linux-mm/20220824071909.192535-1-wangkefeng.wang@hua...
--------------------------------
The pgdat->kswapd could be accessed concurrently by kswapd_run() and kcompactd(), it don't be protected by any lock, which could leads to data races, adding READ/WRITE_ONCE() to slince it.
Signed-off-by: Kefeng Wang wangkefeng.wang@huawei.com
Conflicts: mm/compaction.c mm/vmscan.c
Signed-off-by: ZhangPeng zhangpeng362@huawei.com Reviewed-by: Kefeng Wang wangkefeng.wang@huawei.com Signed-off-by: Jialin Zhang zhangjialin11@huawei.com --- mm/compaction.c | 4 +++- mm/vmscan.c | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/mm/compaction.c b/mm/compaction.c index 80116210c2e6..509dee7c2f38 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -1926,7 +1926,9 @@ static inline bool is_via_compact_memory(int order)
static bool kswapd_is_running(pg_data_t *pgdat) { - return pgdat->kswapd && (pgdat->kswapd->state == TASK_RUNNING); + struct task_struct *t = READ_ONCE(pgdat->kswapd); + + return t && (t->state == TASK_RUNNING); }
/* diff --git a/mm/vmscan.c b/mm/vmscan.c index 14f606251e49..a8412c5d4eda 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4290,7 +4290,7 @@ int kswapd_run(int nid) int ret = 0; struct task_struct *t;
- if (pgdat->kswapd) + if (READ_ONCE(pgdat->kswapd)) return 0;
t = kthread_run(kswapd, pgdat, "kswapd%d", nid); @@ -4300,7 +4300,7 @@ int kswapd_run(int nid) pr_err("Failed to start kswapd on node %d\n", nid); ret = PTR_ERR(t); } else { - pgdat->kswapd = t; + WRITE_ONCE(pgdat->kswapd, t); } return ret; } @@ -4311,11 +4311,11 @@ int kswapd_run(int nid) */ void kswapd_stop(int nid) { - struct task_struct *kswapd = NODE_DATA(nid)->kswapd; + struct task_struct *kswapd = READ_ONCE(NODE_DATA(nid)->kswapd);
if (kswapd) { kthread_stop(kswapd); - NODE_DATA(nid)->kswapd = NULL; + WRITE_ONCE(NODE_DATA(nid)->kswapd, NULL); } }