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, 22 Mar 2024 09:52:36 +0800	[thread overview]
Message-ID: <CAGWkznFtez1fj2L2CtFnA5k-Tn4WtxmDOw=fjOWPg-ZGJX=VWw@mail.gmail.com> (raw)
In-Reply-To: <ZfwpuRjAZV07_lc3@casper.infradead.org>

On Thu, Mar 21, 2024 at 8:36 PM Matthew Wilcox <willy@infradead.org> wrote:
>
> On Thu, Mar 21, 2024 at 04:25:07PM +0800, Zhaoyang Huang wrote:
> > ok. Could the scenario below be suspicious on leaving an orphan folio
> > in step 7 and introduce the bug in step 8. In the scenario,
> > Thread_filemap behaves as a backdoor for Thread_madv by creating the
> > pte after Thread_truncate finishes cleaning all page tables.
> >
> > 0. Thread_bad gets the folio by folio_get_entry and stores it in its
> > local fbatch_bad and go to sleep
>
> There's no function called folio_get_entry(), but clearly thread_bad
> should have a refcount on it at this point.
>
> > 1. Thread_filemap get the folio via
> > filemap_map_pages->next_uptodate_folio->xas_next_entry and gets
> > preempted
> >     refcnt == 1(page_cache), PG_lru == true
>
> so the refcount should be 2 here.
>
> > 2. Thread_truncate get the folio via
> > truncate_inode_pages_range->find_lock_entries
> >     refcnt == 2(fbatch_trunc, page_cache), PG_lru == true
> >
> > 3. Thread_truncate proceed to truncate_cleanup_folio
> >     refcnt == 2(fbatch_trunc, page_cache), PG_lru == true
> >
> > 4. Thread_truncate proceed to delete_from_page_cache_batch
> >     refcnt == 1(fbatch_trunc), PG_lru == true
> >
> > 5. Thread_filemap schedule back and proceed to setup a pte and have
> > folio->_mapcnt = 0 & folio->refcnt += 1
> >     refcnt == 2(pte, fbatch_temp), PG_lru == true
> >
> > 6. Thread_madv clear folio's PG_lru by
> > madvise_xxx_pte_range->folio_isolate_lru->folio_test_clear_lru
> >     refcnt == 2(pte,fbatch_temp), PG_lru == false
> >
> > 7. Thread_truncate call folio_fbatch_release and failed in freeing
> > folio as refcnt not reach 0
> >     refcnt == 1(pte), PG_lru == false
> > ********folio becomes an orphan here which is not on the page cache
> > but on the task's VM**********
> >
> > 8. Thread_xxx scheduled back from 0 to do release_pages(fbatch_bad)
> > and have the folio introduce the bug.
>
> ... because if these steps happen as 7, 8, 6, you hit the BUG in
> folio_isolate_lru().
Thanks for the comments. fix the typo and update the timing sequence
by amending possible preempt points to have the refcnt make sense.

0. Thread_bad gets the folio by find_get_entry and preempted before
take refcnt(could be the second round scan of
truncate_inode_pages_range)
    refcnt == 1(page_cache), PG_lru == true, PG_lock == false
    find_get_entry
        folio = xas_find
        <preempted>
        folio_try_get_rcu

1. Thread_filemap get the folio via
filemap_map_pages->next_uptodate_folio->xas_next_entry and gets preempted
    refcnt == 1(page_cache), PG_lru == true, PG_lock == false
    filemap_map_pages
        next_uptodate_folio
           xas_next_entry
           <preempted>
           folio_try_get_rcu

2. Thread_truncate get the folio via
truncate_inode_pages_range->find_lock_entries
    refcnt == 2(page_cache, fbatch_truncate), PG_lru == true, PG_lock == true

3. Thread_truncate proceed to truncate_cleanup_folio
    refcnt == 2(page_cache, fbatch_truncate), PG_lru == true, PG_lock == true

4. Thread_truncate proceed to delete_from_page_cache_batch
    refcnt == 1(fbatch_truncate), PG_lru == true, PG_lock == true

4.1 folio_unlock
    refcnt == 1(fbatch_truncate), PG_lru == true, PG_lock == false

5. Thread_filemap schedule back from '1' and proceed to setup a pte
and have folio->_mapcnt = 0 & folio->refcnt += 1
    refcnt == 1->2(+fbatch_filemap)->3->2(pte, fbatch_truncate),
PG_lru == true, PG_lock == true->false

6. Thread_madv clear folio's PG_lru by
madvise_xxx_pte_range->folio_isolate_lru->folio_test_clear_lru
    refcnt == 2(pte,fbatch_truncate), PG_lru == false, PG_lock == false

7. Thread_truncate call folio_fbatch_release and failed in freeing
folio as refcnt not reach 0
    refcnt == 1(pte), PG_lru == false, PG_lock == false
********folio becomes an orphan here which is not on the page cache
but on the task's VM**********

8. Thread_bad scheduled back from '0' to be collected in fbatch_bad
    refcnt == 2(pte, fbatch_bad), PG_lru == false, PG_lock == true

9. Thread_bad clear one refcnt wrongly when doing filemap_remove_folio
as it take this refcnt as the page cache one
    refcnt == 1(fbatch_bad), PG_lru == false, PG_lock == true->false
    truncate_inode_folio
        filemap_remove_folio
             filemap_free_folio
******refcnt decreased wrongly here by being taken as the page cache one ******

10. Thread_bad calls release_pages(fbatch_bad) and has the folio
introduce the bug.
    release_pages
        folio_put_testzero == true
        folio_test_lru == false
        list_add(folio->lru, pages_to_free)


  reply	other threads:[~2024-03-22  1:52 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 [this message]
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
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='CAGWkznFtez1fj2L2CtFnA5k-Tn4WtxmDOw=fjOWPg-ZGJX=VWw@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