lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAHbLzkrmTaqBRmHVdE2kyW57Uoghqd_E+jAXC9cB5ofkhL-uvw@mail.gmail.com>
Date:   Fri, 15 Sep 2023 18:35:04 -0700
From:   Yang Shi <shy828301@...il.com>
To:     Hugh Dickins <hughd@...gle.com>
Cc:     Suren Baghdasaryan <surenb@...gle.com>,
        Matthew Wilcox <willy@...radead.org>,
        Michal Hocko <mhocko@...e.com>,
        Vlastimil Babka <vbabka@...e.cz>,
        syzbot <syzbot+b591856e0f0139f83023@...kaller.appspotmail.com>,
        akpm@...ux-foundation.org, linux-kernel@...r.kernel.org,
        linux-mm@...ck.org, syzkaller-bugs@...glegroups.com
Subject: Re: [syzbot] [mm?] kernel BUG in vma_replace_policy

On Thu, Sep 14, 2023 at 9:26 PM Hugh Dickins <hughd@...gle.com> wrote:
>
> On Thu, 14 Sep 2023, Suren Baghdasaryan wrote:
> > On Thu, Sep 14, 2023 at 9:24 PM Matthew Wilcox <willy@...radead.org> wrote:
> > > On Thu, Sep 14, 2023 at 08:53:59PM +0000, Suren Baghdasaryan wrote:
> > > > On Thu, Sep 14, 2023 at 8:00 PM Suren Baghdasaryan <surenb@...gle.com> wrote:
> > > > > On Thu, Sep 14, 2023 at 7:09 PM Matthew Wilcox <willy@...radead.org> wrote:
> > > > > >
> > > > > > On Thu, Sep 14, 2023 at 06:20:56PM +0000, Suren Baghdasaryan wrote:
> > > > > > > I think I found the problem and the explanation is much simpler. While
> > > > > > > walking the page range, queue_folios_pte_range() encounters an
> > > > > > > unmovable page and queue_folios_pte_range() returns 1. That causes a
> > > > > > > break from the loop inside walk_page_range() and no more VMAs get
> > > > > > > locked. After that the loop calling mbind_range() walks over all VMAs,
> > > > > > > even the ones which were skipped by queue_folios_pte_range() and that
> > > > > > > causes this BUG assertion.
> > > > > > >
> > > > > > > Thinking what's the right way to handle this situation (what's the
> > > > > > > expected behavior here)...
> > > > > > > I think the safest way would be to modify walk_page_range() and make
> > > > > > > it continue calling process_vma_walk_lock() for all VMAs in the range
> > > > > > > even when __walk_page_range() returns a positive err. Any objection or
> > > > > > > alternative suggestions?
> > > > > >
> > > > > > So we only return 1 here if MPOL_MF_MOVE* & MPOL_MF_STRICT were
> > > > > > specified.  That means we're going to return an error, no matter what,
> > > > > > and there's no point in calling mbind_range().  Right?
> > > > > >
> > > > > > +++ b/mm/mempolicy.c
> > > > > > @@ -1334,6 +1334,8 @@ static long do_mbind(unsigned long start, unsigned long len,
> > > > > >         ret = queue_pages_range(mm, start, end, nmask,
> > > > > >                           flags | MPOL_MF_INVERT, &pagelist, true);
> > > > > >
> > > > > > +       if (ret == 1)
> > > > > > +               ret = -EIO;
> > > > > >         if (ret < 0) {
> > > > > >                 err = ret;
> > > > > >                 goto up_out;
> > > > > >
> > > > > > (I don't really understand this code, so it can't be this simple, can
> > > > > > it?  Why don't we just return -EIO from queue_folios_pte_range() if
> > > > > > this is the right answer?)
> > > > >
> > > > > Yeah, I'm trying to understand the expected behavior of this function
> > > > > to make sure we are not missing anything. I tried a simple fix that I
> > > > > suggested in my previous email and it works but I want to understand a
> > > > > bit more about this function's logic before posting the fix.
> > > >
> > > > So, current functionality is that after queue_pages_range() encounters
> > > > an unmovable page, terminates the loop and returns 1, mbind_range()
> > > > will still be called for the whole range
> > > > (https://elixir.bootlin.com/linux/latest/source/mm/mempolicy.c#L1345),
> > > > all pages in the pagelist will be migrated
> > > > (https://elixir.bootlin.com/linux/latest/source/mm/mempolicy.c#L1355)
> > > > and only after that the -EIO code will be returned
> > > > (https://elixir.bootlin.com/linux/latest/source/mm/mempolicy.c#L1362).
> > > > So, if we follow Matthew's suggestion we will be altering the current
> > > > behavior which I assume is not what we want to do.
> > >
> > > Right, I'm intentionally changing the behaviour.  My thinking is
> > > that mbind(MPOL_MF_MOVE | MPOL_MF_STRICT) is going to fail.  Should
> > > such a failure actually move the movable pages before reporting that
> > > it failed?  I don't know.
> > >
> > > > The simple fix I was thinking about that would not alter this behavior
> > > > is smth like this:
> > >
> > > I don't like it, but can we run it past syzbot to be sure it solves the
> > > issue and we're not chasing a ghost here?
> >
> > Yes, I just finished running the reproducer on both upstream and
> > linux-next builds listed in
> > https://syzkaller.appspot.com/bug?extid=b591856e0f0139f83023 and the
> > problem does not happen anymore.
> > I'm fine with your suggestion too, just wanted to point out it would
> > introduce change in the behavior. Let me know how you want to proceed.
>
> Well done, identifying the mysterious cause of this problem:
> I'm glad to hear that you've now verified that hypothesis.
>
> You're right, it would be a regression to follow Matthew's suggestion.
>
> Traditionally, modulo bugs and inconsistencies, the queue_pages_range()
> phase of do_mbind() has done the best it can, gathering all the pages it
> can that need migration, even if some were missed; and proceeds to do the
> mbind_range() phase if there was nothing "seriously" wrong (a gap causing
> -EFAULT).  Then at the end, if MPOL_MF_STRICT was set, and not all the
> pages could be migrated (or MOVE was not specified and not all pages
> were well placed), it returns -EIO rather than 0 to inform the caller
> that not all could be done.
>
> There have been numerous tweaks, but I think most importantly
> 5.3's d883544515aa ("mm: mempolicy: make the behavior consistent when
> MPOL_MF_MOVE* and MPOL_MF_STRICT were specified") added those "return 1"s
> which stop the pagewalk early.  In my opinion, not an improvement - makes
> it harder to get mbind() to do the best job it can (or is it justified as
> what you're asking for if you say STRICT?).

Hi Suren and Hugh,

Thanks for figuring this out. The mbind behavior is a little bit messy
and hard to follow. I tried my best to recall all the changes.

IIUC, mbind did break the vma iteration early in the first place, then
commit 6f4576e3687b ("mempolicy: apply page table walker on
queue_pages_range()") changed the behavior (didn't break vma iteration
early for some cases anymore), but it messed up the return value and
caused some test cases failure, also violated the manual. The return
value issue was fixed by commit a7f40cfe3b7a ("mm: mempolicy: make
mbind() return -EIO when MPOL_MF_STRICT is specified"), this commit
also restored the oldest behavior (break loop early). But it also
breaks the loop early when MPOL_MF_MOVE|MOVEALL is set, kernel should
actually continue the loop to try to migrate all existing pages per
the manual. It sounds like a regression. I will take a look at it.

So the logic should conceptually look like:

if (MPOL_MF_MOVE|MOVEALL)
    continue;
if (MPOL_MF_STRICT)
    break;

So it is still possible that some VMAs are not locked if only
MPOL_MF_STRICT is set.

>
> But whatever, it would be a further regression for mbind() not to have
> done the mbind_range(), even though it goes on to return -EIO.
>
> I had a bad first reaction to your walk_page_range() patch (was expecting
> to see vma_start_write()s in mbind_range()), but perhaps your patch is
> exactly what process_mm_walk_lock() does now demand.
>
> [Why is Hugh responding on this?  Because I have some long-standing
> mm/mempolicy.c patches to submit next week, but in reviewing what I
> could or could not afford to get into at this time, had decided I'd
> better stay out of queue_pages_range() for now - beyond the trivial
> preferring an MPOL_MF_WRLOCK flag to your bool lock_vma.]
>
> Hugh

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ