Commit 8230984c authored by ZhangPeng's avatar ZhangPeng Committed by Jialin Zhang
Browse files

mm: slince possible data races about pgdat->kswapd

maillist inclusion
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I6PKGM

Reference: https://lore.kernel.org/linux-mm/20220824071909.192535-1-wangkefeng.wang@huawei.com/



--------------------------------

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: default avatarKefeng Wang <wangkefeng.wang@huawei.com>

Conflicts:
	mm/compaction.c
	mm/vmscan.c

Signed-off-by: default avatarZhangPeng <zhangpeng362@huawei.com>
Reviewed-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: default avatarJialin Zhang <zhangjialin11@huawei.com>
parent 7a9ea77f
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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);
}

/*
+4 −4
Original line number Diff line number Diff line
@@ -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);
	}
}