linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Zhaoyang Huang <huangzhaoyang@gmail.com>
To: Matthew Wilcox <willy@infradead.org>
Cc: "黄朝阳 (Zhaoyang Huang)" <zhaoyang.huang@unisoc.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"linux-mm@kvack.org" <linux-mm@kvack.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"康纪滨 (Steve Kang)" <Steve.Kang@unisoc.com>
Subject: Re: summarize all information again at bottom//reply: reply: [PATCH] mm: fix a race scenario in folio_isolate_lru
Date: Fri, 29 Mar 2024 13:49:05 +0800	[thread overview]
Message-ID: <CAGWkznG7oyh9D-ozN7zQrpJz3s+N_ra1P=Yw3Nd3B0X=thCAxg@mail.gmail.com> (raw)
In-Reply-To: <ZgV65ercduTnVWCA@casper.infradead.org>

On Thu, Mar 28, 2024 at 10:12 PM Matthew Wilcox <willy@infradead.org> wrote:
>
> On Thu, Mar 28, 2024 at 12:03:02PM +0800, Zhaoyang Huang wrote:
> > On Thu, Mar 28, 2024 at 11:18 AM Matthew Wilcox <willy@infradead.org> wrote:
> > >
> > > On Thu, Mar 28, 2024 at 09:27:31AM +0800, Zhaoyang Huang wrote:
> > > > ok, I missed the refcnt from alloc_pages. However, I still think it is
> > > > a bug to call readahead_folio in read_pages as the refcnt obtained by
> > > > alloc_pages should be its final guard which is paired to the one which
> > > > checked in shrink_folio_list->__remove_mapping->folio_ref_freeze(2)(this
> > > > 2 represent alloc_pages & page cache). If we removed this one without
> > >
> > > __remove_mapping()  requires that the caller holds the folio locked.
> > > Since the readahead code unlocks the folio, __remove_mapping() cannot
> > > be run because the caller of __remove_mapping() will wait for the folio
> > > lock.
> > repost the whole timing sequence to make it more clear and fix
> > incorrect description of previous feedback
>
> I can't understand what you think the problem is here.  Please try
> again.
>
> > Follow the refcount through.
> >
> > In page_cache_ra_unbounded():
> >
> >                 folio = filemap_alloc_folio(gfp_mask, 0);
> > (folio has refcount 1)
> >                 ret = filemap_add_folio(mapping, folio, index + i, gfp_mask);
> > (folio has refcount 2, PG_lru)
> >
> > Then we call read_pages()
> > First we call ->readahead() which for some reason stops early.
> > Then we call readahead_folio() which calls folio_put()
> > (folio has refcount 1)
> > Then we call folio_get()
> > (folio has refcount 2)
> > Then we call filemap_remove_folio()
> > (folio has refcount 1)
> > Then we call folio_unlock()
> > Then we call folio_put()
> >
> > Amending steps for previous timing sequence below where [1] races with
> > [2] that has nothing to do with __remove_mapping(). IMO, no file_folio
> > should be freed by folio_put as the refcnt obtained by alloc_pages
> > keep it always imbalanced until shrink_folio_list->__remove_mapping,
> > where the folio_ref_freeze(2) implies the refcnt of alloc_pages and
> > isolation should be the last two. release_pages is a special scenario
> > that the refcnt of alloc_pages is freed implicitly in
> > delete_from_page_cache_batch->filemap_free_folio.
> >
> >     folio_put()
> >     {
> >          if(folio_put_test_zero())
> > *** we should NOT be here as the refcnt of alloc_pages should NOT be dropped ***
> >          if (folio_test_lru())
> > ***  preempted here with refcnt == 0 and pass PG_lru check ***
> > [1]
> >          lruvec_del_folio()
> > Then thread_isolate call folio_isolate_lru()
> >       folio_isolate_lru()
> >       {
> >          folio_test_clear_lru()
> >          folio_get()
> > [2]
> >          lruvec_del_folio()
> >       }
> > --------------------------------------------------------------------------------------------
> > shrink_folio_list()
> > {
> >     __remove_mapping()
> >     {
> >         refcount = 1 + folio_nr_pages;
> > *** the refcount = 1 + 1 implies there should be only the refcnt of
> > alloc_pages and previous isolation for a no-busy folio as all PTE has
> > gone***
> >         if (!folio_ref_freeze(refcount))
> >              goto keeplock;
> >      }
> > }
key steps in brief:
Thread_truncate get folio to its local fbatch by find_get_entry in step 2
The refcnt is deducted to 1 which is not as expect as from alloc_pages
but from thread_truncate's local fbatch in step 7
Thread_reclaim succeed to isolate the folio by the wrong refcnt(not
the value but meaning) in step 8
Thread_truncate hit the VM_BUG_ON in step 9

all steps:
Thread_readahead:
0. folio = filemap_alloc_folio(gfp_mask, 0);
       (folio has refcount 1)
1. ret = filemap_add_folio(mapping, folio, index + i, gfp_mask);
       (folio has refcount 2)
2. thread_truncate hold one refcnt and add this folio to fbatch_truncate
       (folio has refcount 3(alloc, page cache, fbatch_truncate), PG_lru)
3. Then we call read_pages()
       First we call ->readahead() which for some reason stops early.
4. Then we call readahead_folio() which calls folio_put()
       (folio has refcount 2)
5. Then we call folio_get()
       (folio has refcount 3)
6. Then we call filemap_remove_folio()
       (folio has refcount 2)
7. Then we call folio_unlock()
       Then we call folio_put()
       (folio has refcount 1(fbatch_truncate))
8. thread_reclaim call shrink_inactive_list->isolate_lru_folios
        shrink_inactive_list
            isolate_lru_folios
               if (!folio_test_lru(folio))
               if (!folio_try_get(folio))
               if (!folio_test_clear_lru(folio))
               list_move(folio, dst)
       (folio has refcount 2)

8.1. thread_reclaim call shrink_folio_list->__remove_mapping
    shrink_folio_list()
        __remove_mapping()
             (refcount = 2)
            if (!folio_ref_freeze(2)) //true
         list_add(folio, free_folios);
       (folio has refcount 0)

9. thread_truncate will hit the refcnt VM_BUG_ON(refcnt == 0) in
folio_put_testzero


  reply	other threads:[~2024-03-29  5:49 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-18  9:32 黄朝阳 (Zhaoyang Huang)
2024-03-18 12:32 ` Matthew Wilcox
2024-03-19  0:48   ` Zhaoyang Huang
2024-03-19  3:01     ` Matthew Wilcox
2024-03-21  8:25       ` Zhaoyang Huang
2024-03-21 12:36         ` Matthew Wilcox
2024-03-22  1:52           ` Zhaoyang Huang
2024-03-22  3:20             ` Matthew Wilcox
2024-03-24 11:14               ` Zhaoyang Huang
2024-03-25  3:22                 ` Matthew Wilcox
2024-03-26  9:06                   ` Zhaoyang Huang
2024-03-26 12:21                     ` Matthew Wilcox
2024-03-27  1:25                       ` Zhaoyang Huang
2024-03-27 12:31                         ` Matthew Wilcox
2024-03-28  1:27                           ` Zhaoyang Huang
2024-03-28  3:18                             ` Matthew Wilcox
2024-03-28  4:03                               ` Zhaoyang Huang
2024-03-28 14:12                                 ` Matthew Wilcox
2024-03-29  5:49                                   ` Zhaoyang Huang [this message]
2024-03-29 12:19                                     ` Matthew Wilcox

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='CAGWkznG7oyh9D-ozN7zQrpJz3s+N_ra1P=Yw3Nd3B0X=thCAxg@mail.gmail.com' \
    --to=huangzhaoyang@gmail.com \
    --cc=Steve.Kang@unisoc.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=willy@infradead.org \
    --cc=zhaoyang.huang@unisoc.com \
    /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