Re: [PATCH] mm: ksm: fix data-race in __ksm_enter / run_store

From: Matthew Wilcox
Date: Tue Aug 02 2022 - 11:45:14 EST


On Tue, Aug 02, 2022 at 11:15:50PM +0800, Kefeng Wang wrote:
> The ksm_run is alread protected by ksm_thread_mutex in run_store, we
> could add this lock in __ksm_enter() to avoid the above issue.

I don't think this is a great fix. Why not protect the store with
ksm_mmlist_lock? ie:

mutex_lock(&ksm_thread_mutex);
wait_while_offlining();
if (ksm_run != flags) {
+ spin_lock(&ksm_mmlist_lock);
ksm_run = flags;
+ spin_unlock(&ksm_mmlist_lock);
if (flags & KSM_RUN_UNMERGE) {
set_current_oom_origin();
err = unmerge_and_remove_all_rmap_items();
clear_current_oom_origin();
if (err) {
+ spin_lock(&ksm_mmlist_lock);
ksm_run = KSM_RUN_STOP;
+ spin_unlock(&ksm_mmlist_lock);
...

(I also don't think this is a real bug, because the call to
unmerge_and_remove_all_rmap_items() will "cure" the misplacement of
items in the list, but there's value in shutting up the tools, I suppose)