From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752921AbdL1Bx0 (ORCPT ); Wed, 27 Dec 2017 20:53:26 -0500 Received: from mga14.intel.com ([192.55.52.115]:12774 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752062AbdL1BxZ (ORCPT ); Wed, 27 Dec 2017 20:53:25 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,468,1508828400"; d="scan'208";a="17038465" From: "Huang\, Ying" To: Andrew Morton Cc: , , Hugh Dickins , "Paul E . McKenney" , Minchan Kim , "Johannes Weiner" , Tim Chen , Shaohua Li , Mel Gorman , =?utf-8?B?SsOp?= =?utf-8?B?csO0bWU=?= Glisse , Michal Hocko , Andrea Arcangeli , David Rientjes , Rik van Riel , Jan Kara , Dave Jiang , Aaron Lu Subject: Re: [PATCH -V5b -mm] mm, swap: Fix race between swapoff and some swap operations References: <20171228005805.15632-1-ying.huang@intel.com> Date: Thu, 28 Dec 2017 09:53:22 +0800 In-Reply-To: <20171228005805.15632-1-ying.huang@intel.com> (Ying Huang's message of "Thu, 28 Dec 2017 08:58:05 +0800") Message-ID: <87d12zsjn1.fsf@yhuang-dev.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org "Huang, Ying" writes: > From: Huang Ying > > When the 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 > ----- ----- > do_swap_page > swapin_readahead > __read_swap_cache_async > swapoff swapcache_prepare > p->swap_map = NULL __swap_duplicate > p->swap_map[?] /* !!! NULL pointer access */ > > Because swapoff is usually done when system shutdown only, the race > may not hit many people in practice. But it is still a race need to > be fixed. > > To fix the race, get_swap_device() is added to check whether the > specified swap entry is valid in its swap device. If so, it will keep > the swap entry valid via preventing the swap device from being > swapoff, until put_swap_device() is called. > > 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() to put_swap_device(), the preemption is disabled, so > stop_machine() in swapoff() will wait until put_swap_device() is > called. > > In addition to swap_map, cluster_info, etc. data structure in the > struct swap_info_struct, the swap cache radix tree will be freed after > swapoff, so this patch fixes the race between swap cache looking up > and swapoff too. > > Races between some other swap cache usages protected via disabling > preemption and swapoff are fixed too via calling stop_machine() > between clearing PageSwapCache() and freeing swap cache data > structure. > > Alternative implementation could be replacing disable preemption with > rcu_read_lock_sched and stop_machine() with synchronize_sched(). > > Cc: Hugh Dickins > Cc: Paul E. McKenney > Cc: Minchan Kim > Cc: Johannes Weiner > Cc: Tim Chen > Cc: Shaohua Li > Cc: Mel Gorman > Cc: "Jérôme Glisse" > Cc: Michal Hocko > Cc: Andrea Arcangeli > Cc: David Rientjes > Cc: Rik van Riel > Cc: Jan Kara > Cc: Dave Jiang > Cc: Aaron Lu > Signed-off-by: "Huang, Ying" > > Changelog: > > v5: > > - Replace RCU with stop_machine() 2 versions (-V5a and -V5b) have been sent, one is implemented with stop_machine(), the other is implemented with RCU-sched. RCU-sched based version is better for real time users. Both are OK for me. Best Regards, Huang, Ying