linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Gabriel Ryan <gabe@cs.columbia.edu>
To: Matthew Wilcox <willy@infradead.org>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Abhishek Shah <abhishek.shah@columbia.edu>
Subject: Re: [PATCH] mm: ksm: fix data-race in __ksm_enter / run_store
Date: Tue, 2 Aug 2022 13:20:07 -0400	[thread overview]
Message-ID: <CALbthtfDwB0pNRw8A-ZP+W5sVtxiyyYbAqnuF-BonrPPJ-mxHw@mail.gmail.com> (raw)
In-Reply-To: <YulGbmJt2faelV6c@casper.infradead.org>

[-- Attachment #1: Type: text/plain, Size: 2140 bytes --]

Hi Matthew,

I don't believe execution of unmerge_and_remove_all_rmap_items() after an
mm is misplaced is guaranteed.

Consider the following interleaving:
Thread A executes *__ksm_enter* with KSM_RUN_MERGE set through the check on
https://elixir.bootlin.com/linux/v5.18-rc5/source/mm/ksm.c#L2501
Thread B executes *run_store* and sets KSM_RUN_UNMERGE and then also
executes unmerge_and_remove_all_rmap_items() on
https://elixir.bootlin.com/linux/v5.18-rc5/source/mm/ksm.c#L2900
Thread A completes *__ksm_enter *and misplaces the mm behind the scanning
cursor since it is still on the KSM_RUN_MERGE path on
https://elixir.bootlin.com/linux/v5.18-rc5/source/mm/ksm.c#L2504

I also noticed through manual inspection another check that appears racy of
the KSM_RUN_UNMERGE flag on
https://elixir.bootlin.com/linux/v5.18-rc5/source/mm/ksm.c#L2563

Best,

Gabe



On Tue, Aug 2, 2022 at 11:45 AM Matthew Wilcox <willy@infradead.org> wrote:

> 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)
>

[-- Attachment #2: Type: text/html, Size: 3228 bytes --]

  reply	other threads:[~2022-08-02 17:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-02 15:15 Kefeng Wang
2022-08-02 15:44 ` Matthew Wilcox
2022-08-02 17:20   ` Gabriel Ryan [this message]
2022-08-11 23:00 ` Andrew Morton
2022-08-19 12:00   ` Abhishek Shah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CALbthtfDwB0pNRw8A-ZP+W5sVtxiyyYbAqnuF-BonrPPJ-mxHw@mail.gmail.com \
    --to=gabe@cs.columbia.edu \
    --cc=abhishek.shah@columbia.edu \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=wangkefeng.wang@huawei.com \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox