Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1176271lqe; Mon, 8 Apr 2024 00:23:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX/Txy3abNZKfgJqsnfLOWIIjIUiZe7iqpLftVkUXjuIhExUwLjfLrCOdJrrfO/inDRrtX6uPOBTpKL/+5MWsCXk4Zt1Ji/EAqkLNJC3w== X-Google-Smtp-Source: AGHT+IHGiRpDgmlrnZJxo77R+9LT3dZsZSCCBrMgsy/PlGUcEobH2mkSAmWBWPIeFdL3tJe5uM8o X-Received: by 2002:a17:906:b7ca:b0:a4d:f901:477d with SMTP id fy10-20020a170906b7ca00b00a4df901477dmr5291545ejb.19.1712561028486; Mon, 08 Apr 2024 00:23:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712561028; cv=pass; d=google.com; s=arc-20160816; b=H5kVYp9Wgt3G3l/BPT2x7cVes7TRV/EpEgA/nxnHVcFxNZrqdzceTQACUD80gWhzFG gHHybNceCzmvR8ypeMPZ3g9h9HjGbGwfhSwuFdZZkv1rVD/KM6zhzRyH6l+0x/R132F0 TeqjNHusCGTfvxT/S7vZlMJhY5XMSFRbxhXMAc/CsK31dIMD+S9sLMm1jNrBTU5U5whX n2xeYw22ebOqgAX/+LubIHL27M6nVsGhaESwT4ovSN0N/YF4qvPh+7ERFOTA/ekClube BUn/GxqW7g3iY2nSqyE30x1j+ISiI2ESxhtInShlryR81rus5ml2pZyT0uzO3mcTjUAk lOSA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=bLI3llylebNmQVL3IpHbZ+qCV6kW6Buoji1qmehN80Y=; fh=6960Rv3r5IxjX5ZS+KxfIbRlgjLGxhcQCo2T5EKz9Ps=; b=DLwEodoOE9NTGKf++cBzJ73ZPzNoiw3y+//pC17cVpc2YjgnXBgfOHo2sPCzugCkaF flnkfkNln2ccEsI9G+f97HY5HNVt1Yo0vSYRLWybwwdYInHXS0exMv5B8xCybHsHZAgX CK86WE21Yom3PEcvrVhuTtd/XV0wmNeCvXDh/5m5DyE5Oko1EZDIPTKys2JzfGPA/Wqc GygG2HN6lAuX5ecpUafc5v1RLNKb7M3rL92u3rYwryrcuiiitUB//2kAVcpOq+e10/AC zHLRXrESUTWk6K6iXc3nVMd6uUaWT3ohqYTLVS6188Gt6bxgdYuNN0vM6GQQbXeRwZMi GyRA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-134894-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-134894-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id v21-20020a1709063bd500b00a519ec2e06asi3230870ejf.1000.2024.04.08.00.23.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 00:23:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-134894-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-134894-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-134894-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 3A6A71F229BC for ; Mon, 8 Apr 2024 07:23:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6E5FD2C87A; Mon, 8 Apr 2024 07:23:36 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 766952C19D for ; Mon, 8 Apr 2024 07:23:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712561015; cv=none; b=mtPOD+s5q69tRyHIE03NLFLuNY2337i3J5U4z+HyXvPFH3q6yNbxiTIg7XO6f6lYYG3M3MjLlnmgpQPlDXdrfhIJ61OEl5vK6qG4DBZ0k4wJ5tk/uJXJwfScdw/sf3NCzZ2m+apPzMZIWHDDZXi2n9OjeBQYBJWX2TpTz8vqCa8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712561015; c=relaxed/simple; bh=ZzRAcHxtSjjr3xSt8JnhXEEAZLntlCbosx2pWWo4Ink=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=bs+z1t13e6ysY7PRmu/6K14PoWelMIbdHOi02CClVjKZaSiZ+R3maW6x/V862CGUMO7/WdK3qEFyua+Y1GkR2z5+Hzymu1XoMew4c4xaRe9OCj38kumILC/qQe5OexytbocwFOd/BdvkyYhdnldtfFgFJ8+qwJhDO03ew5ArEcM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C91B51007; Mon, 8 Apr 2024 00:24:02 -0700 (PDT) Received: from [10.57.73.169] (unknown [10.57.73.169]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 29E063F64C; Mon, 8 Apr 2024 00:23:31 -0700 (PDT) Message-ID: <30f09196-5152-45db-85de-12ac89c974b1@arm.com> Date: Mon, 8 Apr 2024 08:23:29 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] mm,swap: add document about RCU read lock and swapoff interaction Content-Language: en-GB To: Huang Ying , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, David Hildenbrand , Miaohe Lin , Hugh Dickins , Minchan Kim References: <20240407065450.498821-1-ying.huang@intel.com> From: Ryan Roberts In-Reply-To: <20240407065450.498821-1-ying.huang@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 07/04/2024 07:54, Huang Ying wrote: > During reviewing a patch to fix the race condition between > free_swap_and_cache() and swapoff() [1], it was found that the > document about how to prevent racing with swapoff isn't clear enough. > Especially RCU read lock can prevent swapoff from freeing data > structures. So, the document is added as comments. > > [1] https://lore.kernel.org/linux-mm/c8fe62d0-78b8-527a-5bef-ee663ccdc37a@huawei.com/ > > Signed-off-by: "Huang, Ying" > Cc: Ryan Roberts > Cc: David Hildenbrand > Cc: Miaohe Lin > Cc: Hugh Dickins > Cc: Minchan Kim LGTM! Reviewed-by: Ryan Roberts > --- > mm/swapfile.c | 26 +++++++++++++------------- > 1 file changed, 13 insertions(+), 13 deletions(-) > > diff --git a/mm/swapfile.c b/mm/swapfile.c > index 4919423cce76..6925462406fa 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -1226,16 +1226,15 @@ static unsigned char __swap_entry_free_locked(struct swap_info_struct *p, > > /* > * When we get a swap entry, if there aren't some other ways to > - * prevent swapoff, such as the folio in swap cache is locked, page > - * table lock is held, etc., the swap entry may become invalid because > - * of swapoff. Then, we need to enclose all swap related functions > - * with get_swap_device() and put_swap_device(), unless the swap > - * functions call get/put_swap_device() by themselves. > + * prevent swapoff, such as the folio in swap cache is locked, RCU > + * reader side is locked, etc., the swap entry may become invalid > + * because of swapoff. Then, we need to enclose all swap related > + * functions with get_swap_device() and put_swap_device(), unless the > + * swap functions call get/put_swap_device() by themselves. > * > - * Note that when only holding the PTL, swapoff might succeed immediately > - * after freeing a swap entry. Therefore, immediately after > - * __swap_entry_free(), the swap info might become stale and should not > - * be touched without a prior get_swap_device(). > + * RCU reader side lock (including any spinlock) is sufficient to > + * prevent swapoff, because synchronize_rcu() is called in swapoff() > + * before freeing data structures. > * > * Check whether swap entry is valid in the swap device. If so, > * return pointer to swap_info_struct, and keep the swap entry valid > @@ -2495,10 +2494,11 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) > > /* > * Wait for swap operations protected by get/put_swap_device() > - * to complete. > - * > - * We need synchronize_rcu() here to protect the accessing to > - * the swap cache data structure. > + * to complete. Because of synchronize_rcu() here, all swap > + * operations protected by RCU reader side lock (including any > + * spinlock) will be waited too. This makes it easy to > + * prevent folio_test_swapcache() and the following swap cache > + * operations from racing with swapoff. > */ > percpu_ref_kill(&p->users); > synchronize_rcu();