Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-15 Thread Michal Hocko
On Fri 15-02-19 15:08:36, Huang, Ying wrote: > Michal Hocko writes: > > > On Mon 11-02-19 16:38:46, Huang, Ying wrote: > >> From: Huang Ying > >> > >> When swapin is performed, after getting the swap entry information from > >> the page table, system will swap in the swap entry, without any loc

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-14 Thread Huang, Ying
Andrea Arcangeli writes: > On Thu, Feb 14, 2019 at 04:07:37PM +0800, Huang, Ying wrote: >> Before, we choose to use stop_machine() to reduce the overhead of hot >> path (page fault handler) as much as possible. But now, I found >> rcu_read_lock_sched() is just a wrapper of preempt_disable(). So

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-14 Thread Andrea Arcangeli
On Thu, Feb 14, 2019 at 04:07:37PM +0800, Huang, Ying wrote: > Before, we choose to use stop_machine() to reduce the overhead of hot > path (page fault handler) as much as possible. But now, I found > rcu_read_lock_sched() is just a wrapper of preempt_disable(). So maybe > we can switch to RCU ve

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-14 Thread Andrea Arcangeli
Hello, On Thu, Feb 14, 2019 at 12:30:02PM -0800, Andrew Morton wrote: > This was discussed to death and I think the changelog explains the > conclusions adequately. swapoff is super-rare so a stop_machine() in > that path is appropriate if its use permits more efficiency in the > regular swap cod

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-14 Thread Andrew Morton
On Thu, 14 Feb 2019 15:33:18 +0100 Michal Hocko wrote: > > Because swapoff() is very rare code path, to make the normal path runs as > > fast as possible, disabling preemption + stop_machine() instead of > > reference count is used to implement get/put_swap_device(). From > > get_swap_device() t

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-14 Thread Michal Hocko
On Mon 11-02-19 16:38:46, Huang, Ying wrote: > From: Huang Ying > > When swapin is performed, after getting the swap entry information from > the page table, system will swap in the swap entry, without any lock held > to prevent the swap device from being swapoff. This may cause the race > like

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-13 Thread Andrea Arcangeli
Hello everyone, On Mon, Feb 11, 2019 at 04:38:46PM +0800, Huang, Ying wrote: > @@ -2386,7 +2463,17 @@ static void enable_swap_info(struct swap_info_struct > *p, int prio, > frontswap_init(p->type, frontswap_map); > spin_lock(&swap_lock); > spin_lock(&p->lock); > - _enable_s

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-12 Thread Huang, Ying
Tim Chen writes: > On 2/11/19 10:47 PM, Huang, Ying wrote: >> Andrea Parri writes: >> > + if (!si) > + goto bad_nofile; > + > + preempt_disable(); > + if (!(si->flags & SWP_VALID)) > + goto unlock_out; After Hugh alluded to barriers, it seems th

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-12 Thread Daniel Jordan
On Tue, Feb 12, 2019 at 04:21:21AM +0100, Andrea Parri wrote: > > > + if (!si) > > > + goto bad_nofile; > > > + > > > + preempt_disable(); > > > + if (!(si->flags & SWP_VALID)) > > > + goto unlock_out; > > > > After Hugh alluded to barriers, it seems the read of SWP_VALID could be

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-12 Thread Tim Chen
On 2/11/19 10:47 PM, Huang, Ying wrote: > Andrea Parri writes: > + if (!si) + goto bad_nofile; + + preempt_disable(); + if (!(si->flags & SWP_VALID)) + goto unlock_out; >>> >>> After Hugh alluded to barriers, it seems the read of SWP_VALID could

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-12 Thread Andrea Parri
> Alternative implementation could be replacing disable preemption with > rcu_read_lock_sched and stop_machine() with synchronize_sched(). JFYI, starting with v4.20-rc1, synchronize_rcu{,expedited}() also wait for preempt-disable sections (the intent seems to retire the RCU-sched update-side API),

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-11 Thread Huang, Ying
Daniel Jordan writes: > On Mon, Feb 11, 2019 at 04:38:46PM +0800, Huang, Ying 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

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-11 Thread Andrea Parri
> > + if (!si) > > + goto bad_nofile; > > + > > + preempt_disable(); > > + if (!(si->flags & SWP_VALID)) > > + goto unlock_out; > > After Hugh alluded to barriers, it seems the read of SWP_VALID could be > reordered with the write in preempt_disable at runtime. Without s

Re: [PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-11 Thread Daniel Jordan
On Mon, Feb 11, 2019 at 04:38:46PM +0800, Huang, Ying 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); > + si

[PATCH -mm -V7] mm, swap: fix race between swapoff and some swap operations

2019-02-11 Thread Huang, Ying
From: Huang Ying When swapin is performed, after getting the swap entry information from the page table, system will swap in the swap entry, without any lock held to prevent the swap device from being swapoff. This may cause the race like below, CPU 1 CPU 2 -