linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: huang ying <huang.ying.caritas@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: "Huang, Ying" <ying.huang@intel.com>,
	linux-mm@kvack.org, LKML <linux-kernel@vger.kernel.org>,
	Hugh Dickins <hughd@google.com>,
	"Paul E . McKenney" <paulmck@linux.vnet.ibm.com>,
	Minchan Kim <minchan@kernel.org>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Tim Chen <tim.c.chen@linux.intel.com>,
	Shaohua Li <shli@kernel.org>,
	Mel Gorman <mgorman@techsingularity.net>,
	jglisse@redhat.com, Michal Hocko <mhocko@suse.com>,
	Andrea Arcangeli <aarcange@redhat.com>,
	David Rientjes <rientjes@google.com>,
	Rik van Riel <riel@redhat.com>, Jan Kara <jack@suse.cz>,
	Dave Jiang <dave.jiang@intel.com>, Aaron Lu <aaron.lu@intel.com>
Subject: Re: [PATCH -mm -v5 RESEND] mm, swap: Fix race between swapoff and some swap operations
Date: Wed, 21 Feb 2018 14:28:08 +0800	[thread overview]
Message-ID: <CAC=cRTP1=PLbiL+VCy3xa4DkQj4j5=M_+eNEGqCRtW0hcbekQw@mail.gmail.com> (raw)
In-Reply-To: <20180220153857.dee8f7d5c904cd7219529312@linux-foundation.org>

On Wed, Feb 21, 2018 at 7:38 AM, Andrew Morton
<akpm@linux-foundation.org> wrote:
> On Sun, 18 Feb 2018 09:06:47 +0800 huang ying <huang.ying.caritas@gmail.com> wrote:
>
>> >> >> +struct swap_info_struct *get_swap_device(swp_entry_t entry)
>> >> >> +{
>> >> >> +  struct swap_info_struct *si;
>> >> >> +  unsigned long type, offset;
>> >> >> +
>> >> >> +  if (!entry.val)
>> >> >> +          goto out;
>> >> >> +  type = swp_type(entry);
>> >> >> +  if (type >= nr_swapfiles)
>> >> >> +          goto bad_nofile;
>> >> >> +  si = swap_info[type];
>> >> >> +
>> >> >> +  preempt_disable();
>> >> >
>> >> > This preempt_disable() is later than I'd expect.  If a well-timed race
>> >> > occurs, `si' could now be pointing at a defunct entry.  If that
>> >> > well-timed race include a swapoff AND a swapon, `si' could be pointing
>> >> > at the info for a new device?
>> >>
>> >> struct swap_info_struct pointed to by swap_info[] will never be freed.
>> >> During swapoff, we only free the memory pointed to by the fields of
>> >> struct swap_info_struct.  And when swapon, we will always reuse
>> >> swap_info[type] if it's not NULL.  So it should be safe to dereference
>> >> swap_info[type] with preemption enabled.
>> >
>> > That's my point.  If there's a race window during which there is a
>> > parallel swapoff+swapon, this swap_info_struct may now be in use for a
>> > different device?
>>
>> Yes.  It's possible.  And the caller of get_swap_device() can live
>> with it if the swap_info_struct has been fully initialized.  For
>> example, for the race in the patch description,
>>
>> do_swap_page
>>   swapin_readahead
>>     __read_swap_cache_async
>>       swapcache_prepare
>>         __swap_duplicate
>>
>> in __swap_duplicate(), it's possible that the swap device returned by
>> get_swap_device() is different from the swap device when
>> __swap_duplicate() call get_swap_device().  But the struct_info_struct
>> has been fully initialized, so __swap_duplicate() can reference
>> si->swap_map[] safely.  And we will check si->swap_map[] before any
>> further operation.  Even if the swap entry is swapped out again for
>> the new swap device, we will check the page table again in
>> do_swap_page().  So there is no functionality problem.
>
> That's rather revolting.  Can we tighten this up?  Or at least very
> loudly document it?

TBH, I think my original fix patch which uses a reference count in
swap_info_struct is easier to be understood.  But I understand it has
its own drawbacks too.  Anyway, unless there are some better ideas to
resolve this, I will send out a new version with more document.

Best Regards,
Huang, Ying

      reply	other threads:[~2018-02-21  6:28 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-13  1:42 [PATCH -mm -v5 RESEND] mm, swap: Fix race between swapoff and some swap operations Huang, Ying
2018-02-13 23:41 ` Andrew Morton
2018-02-14  0:38   ` Huang, Ying
2018-02-16 23:38     ` Andrew Morton
2018-02-18  1:06       ` huang ying
2018-02-20 23:38         ` Andrew Morton
2018-02-21  6:28           ` huang ying [this message]

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='CAC=cRTP1=PLbiL+VCy3xa4DkQj4j5=M_+eNEGqCRtW0hcbekQw@mail.gmail.com' \
    --to=huang.ying.caritas@gmail.com \
    --cc=aarcange@redhat.com \
    --cc=aaron.lu@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=dave.jiang@intel.com \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=jack@suse.cz \
    --cc=jglisse@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mgorman@techsingularity.net \
    --cc=mhocko@suse.com \
    --cc=minchan@kernel.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=riel@redhat.com \
    --cc=rientjes@google.com \
    --cc=shli@kernel.org \
    --cc=tim.c.chen@linux.intel.com \
    --cc=ying.huang@intel.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;
as well as URLs for NNTP newsgroup(s).