Received: by 2002:ab2:687:0:b0:1f4:6588:b3a7 with SMTP id s7csp262014lqe; Wed, 10 Apr 2024 00:59:06 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUeCadjUkYlC+XGi0QOzuAJIAPB8A8T6sQImP1zsTWAWmZs66/CEu8Gwo8z/INIUwt30kkzjQc20oJwMJFDeLZ7GGBgsPl/vnQotuF6sg== X-Google-Smtp-Source: AGHT+IE33ZA+Xyg8Yek5X1Dcc12XaP7upTpqSHu2iufTzOfdx+5qZZOnDz8Bas4mJXMmAGQceG8V X-Received: by 2002:a50:d49a:0:b0:56d:faa2:7aca with SMTP id s26-20020a50d49a000000b0056dfaa27acamr1539005edi.17.1712735946238; Wed, 10 Apr 2024 00:59:06 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712735946; cv=pass; d=google.com; s=arc-20160816; b=yDUTwuA1e/cdscpKTU6G7aceyAFod1T3pgkyo8go9Wo8pSDhE2+RdDlR07/VkPoJzZ Oa/hIoFU25Q7oB/PDUsc1/pLzFEmyDxDaHuiqkPFueBWUg2xwQ23S6ilfBuHJnSprnjS wH39C/On3K81ItVRKx0rBE4tH0xOqYMRPfLyJatiOoSQRLr9up1Qu8xvCU6YIIBAoVA3 eh9J5LrF0k+x3wnpPP8zlYuw/kO1qPdXsZsZHwAdbOpDOO9p2P4KxRk7/Xnsojird4q4 9doxx+wn8owtfYv4h5pPah4bj2XTFu3XqrtzzD2i7lsq6fuxcQeGGpO1JBjyioQO80pw Vv/A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:in-reply-to:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:user-agent:date :message-id:from:references:cc:to:subject; bh=NqdZKqZz503BbhXMakFsG5NchY7z0xDPKPg0Jn1AFpo=; fh=gzHMOt2lKpXuk44t/7axbMD8w0FKECZ1hkpvyd2L3BQ=; b=K8gEBhVozI5xyEhfi7vGV0AVbbFuNDzNFH9eShIfNA4iMRoVHtBmxqioU1emJLZCLD +l+2JVHHlSaRu2UNanDcM8ys2NT65xxLuiNcWwNa6A7I0schZOpcuSCA+K37mEOmGTKd yDzu/1lTaiiF+nr2dvu8qUSyxXq7+Wrm95X5p7+lm21hcFWOt8wQlMjcWIvoZb2Wfl5w DBbx/n+FDaMNDUyYKn6yFggSpKKmxZjSPOnQxbukqK9E7XJvZxvJe/yVFvuPbFATb/Ck GionkHI5Jo5C7jJx7tRpBLFQ/fXiq1LN7yjeOzAYoigELOKUKvBQn9X1RLzHPol+xOeA 7WTA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-138124-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-138124-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.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 a9-20020a05640213c900b0056e5174f4f0si3829066edx.181.2024.04.10.00.59.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 00:59:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-138124-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=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-138124-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-138124-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.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 3661D1F2158D for ; Wed, 10 Apr 2024 07:59:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 91B7013D516; Wed, 10 Apr 2024 07:58:37 +0000 (UTC) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 32DF713C901 for ; Wed, 10 Apr 2024 07:58:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712735917; cv=none; b=aLHxyxZvZ9sqXkw5M8DBJuU0jyvHbsQ3q3dbbl/5ARuG/uf1Ebw9Ec5yRPSlLRcMuT0eIcK5MYAuOUdjTwCQ4JkPbjsIezp9EoiI7iyRrCQRTETFG9P8mpA5xELOZLe8RvaOoVuyoprO4ABdftZuay2e7OZYYWVslBopHWtGKEM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712735917; c=relaxed/simple; bh=nKJBodP5bAnf+nL27eO3VqssbnJkiUNxzOo7xkepETQ=; h=Subject:To:CC:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=DdTgNgPzK76qqceM5+zuPPpvZ6Sjrdl2caWD6D8Om/uhsJi/HyD3AZMjsEqNHjls4D/jA/PkjIfMHKD9YZnmzi2U+HA8V905GmwD3U0RcNxwJ0K8Ia83UirQnrmaX+3uZbfvKcbm3lADwXXFdZpZD7BHQw3725yWis9Ior5VxiA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VDw9h2wwPzwRcF; Wed, 10 Apr 2024 15:55:36 +0800 (CST) Received: from canpemm500002.china.huawei.com (unknown [7.192.104.244]) by mail.maildlp.com (Postfix) with ESMTPS id BFAFB140159; Wed, 10 Apr 2024 15:58:31 +0800 (CST) Received: from [10.173.135.154] (10.173.135.154) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 10 Apr 2024 15:58:31 +0800 Subject: Re: [PATCH] mm,swap: add document about RCU read lock and swapoff interaction To: Huang Ying CC: , , Ryan Roberts , David Hildenbrand , Hugh Dickins , Minchan Kim , Andrew Morton References: <20240407065450.498821-1-ying.huang@intel.com> From: Miaohe Lin Message-ID: <62d09dae-39b9-f235-9786-6e12302425a0@huawei.com> Date: Wed, 10 Apr 2024 15:58:30 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <20240407065450.498821-1-ying.huang@intel.com> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To canpemm500002.china.huawei.com (7.192.104.244) On 2024/4/7 14: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 Thanks for your work. Reviewed-by: Miaohe Lin . > --- > 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(); >