Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp288706lql; Mon, 11 Mar 2024 02:45:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUu3kmQAGx1S0zX9zVtWJa5vPkw38YXHWLslcdOlN/9lJ/ZSTtYOit4bNsmV6WTuxjnXb3LpMwRwAn9mf7STcSX4WUFKGq39b2+PPsuLg== X-Google-Smtp-Source: AGHT+IHHKHRLiwC88vyxfJlOFdFz04fSDayFQ68MUnWWoN7K9/vWpbjxk2jRuMtC0o9Z/mS4F1+I X-Received: by 2002:a05:6102:2249:b0:473:2c6a:4c65 with SMTP id e9-20020a056102224900b004732c6a4c65mr1924474vsb.5.1710150302084; Mon, 11 Mar 2024 02:45:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710150302; cv=pass; d=google.com; s=arc-20160816; b=kJSpRoDWUbTC3G4Dfcr08tkqRpyl4A3qCRMO/N2zaAkv5GxBgLBMqmyxCZfOfeM/FI gnG7QXXJz51WJfg51SL3pOYVFdGDrrexIam9kbtrAGsE96+Xmc2D2eIYy1Iny6jfYRuR ZMb8rG981nW3W1fPYJS7l69HP8fu/ag2agFNGdjnYa1vu6fhu6zonnCY1aA4XdnsuaV8 yjqxRs71inGZQfq6q2v1CJ6sLRAPDu0lrP7d9RQZKS0sF6lO0rgCYJPaBmCM+zbBfFnk /pKAHNaOSFxDxp0Tq0l1V9rCFhzCN4S2ucoPIYZW0BxdQIEOVOCAxT0t3NvS7dmuvHm8 YLcA== 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=l41GMzG0VZNQeL2Fm/Gvray7kkO6nxLDk61wTJp266w=; fh=e3zYNrVduDzmsfcHbLYV8RS6IcJfI3RYvA+U+MUNCl8=; b=0SH/+mk7IAmf0x3nHRYQv+IcbH63AORuLon3xO8s69qITBmCrv1E47D+VR1BgkdbCU FKHxcdYPYbwBvsuLDn9yD+Ib1vAwvQmxktf+58QdhwzKMyMeXoO2Inr5WjwrgAa4vqOk AIhv9f6PhLrzR/IKRwgWSgpuVJEUxny6E3HPu/N72IG7brUuhGUYa15+Liv1ArbdGsxn 9YeZ+9n2WIGeumqai0fz+v44KSvRAQBSPkJMrI8JWl0t1z8qDYP5RaD3pmMkJ1wa32DF RNntkL7oCpn7IDPyN7BLEHw3H3FjAeyf7Cdx/lJANiRe5UFj9k+UX9/Qsm2WWMvtOYeH pmow==; 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-98643-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-98643-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id jm12-20020ad45ecc000000b0069075fbf9fesi5089595qvb.179.2024.03.11.02.45.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 02:45:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-98643-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; 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-98643-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-98643-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id C8E111C20EB4 for ; Mon, 11 Mar 2024 09:45:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C69921946F; Mon, 11 Mar 2024 09:44:54 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3476D17BD9; Mon, 11 Mar 2024 09:44:51 +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=1710150294; cv=none; b=gCsQWacMQHI+7x7sEYrUZTl29ZRF4guPa8TzGknAesd7K8GhW2HsuJYw/XF0r8Oa73r82uwGniHMbxVDC/OquVOGoEua1SJcchRjeFObFCs02OHI1MQ3J1iKzVIS0WzrXNEpGZwK/BPcVB8C98Oasbg/MbCfti2ypxW5eDBoVqU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710150294; c=relaxed/simple; bh=mxn6v4qsoLRyc07OUq5fk0S7LTDOD/Zmjqwktx+NXBw=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=T5WOEiFO8/noZDypbBIPDZptawDwOAb84kHyvBiB4acv5UH8tzfBW4RXfV23K8zo8tTsPaI5r6Lm+shYWGSh9icA5lsvfcnRLicrO7EZ3wHPfsJfE8lob9w6KCsOBoCPoSnWxbnK+u7AsjZylq7iDd6HgGSBqXxbCFP8Fjgf1zY= 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 DD1CBFEC; Mon, 11 Mar 2024 02:45:27 -0700 (PDT) Received: from [10.57.68.246] (unknown [10.57.68.246]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E3CA03F762; Mon, 11 Mar 2024 02:44:49 -0700 (PDT) Message-ID: <1d27e93b-1c6b-4909-859f-e0756974a640@arm.com> Date: Mon, 11 Mar 2024 09:44:47 +0000 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 -v3] mm: swap: fix race between free_swap_and_cache() and swapoff() Content-Language: en-GB To: Huang Ying , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, David Hildenbrand , stable@vger.kernel.org References: <20240311084426.447164-1-ying.huang@intel.com> From: Ryan Roberts In-Reply-To: <20240311084426.447164-1-ying.huang@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/03/2024 08:44, Huang Ying wrote: > From: Ryan Roberts > > There was previously a theoretical window where swapoff() could run and > teardown a swap_info_struct while a call to free_swap_and_cache() was > running in another thread. This could cause, amongst other bad > possibilities, swap_page_trans_huge_swapped() (called by > free_swap_and_cache()) to access the freed memory for swap_map. > > This is a theoretical problem and I haven't been able to provoke it from a > test case. But there has been agreement based on code review that this is > possible (see link below). > > Fix it by using get_swap_device()/put_swap_device(), which will stall > swapoff(). There was an extra check in _swap_info_get() to confirm that > the swap entry was not free. This isn't present in get_swap_device() > because it doesn't make sense in general due to the race between getting > the reference and swapoff. So I've added an equivalent check directly in > free_swap_and_cache(). > > Details of how to provoke one possible issue: > > --8<----- > > CPU0 CPU1 > ---- ---- > shmem_undo_range > shmem_free_swap > xa_cmpxchg_irq > free_swap_and_cache > __swap_entry_free > /* swap_count() become 0 */ > swapoff > try_to_unuse > shmem_unuse /* cannot find swap entry */ > find_next_to_unuse > filemap_get_folio > folio_free_swap > /* remove swap cache */ > /* free si->swap_map[] */ > swap_page_trans_huge_swapped <-- access freed si->swap_map !!! > > --8<----- > > Link: https://lkml.kernel.org/r/20240306140356.3974886-1-ryan.roberts@arm.com > Closes: https://lore.kernel.org/linux-mm/8734t27awd.fsf@yhuang6-desk2.ccr.corp.intel.com/ > Signed-off-by: Ryan Roberts > Signed-off-by: "Huang, Ying" [patch description] > Cc: David Hildenbrand > Cc: > Signed-off-by: Andrew Morton > --- > Hi, Andrew, > > If it's not too late. Please replace v2 of this patch in mm-stable > with this version. Thanks for sorting this out, Huang, Ying! I saw your note asking if I could do it, and it was on my list, but I've been busy debugging other urgent issues in mm-stable. That should be solved now so unblocks me finishing the testing on my large folios swap-out v4 series. Hopefully that will be incomming in the next couple of days. You did previously suggest you wanted some comments around synchronise_rcu() in swapoff(), but I don't see those here. I don't think that should hold this up though. Thanks, Ryan > > Changes since v2: > > - Remove comments for get_swap_device() because it's not correct. > - Revised patch description about the race condition description. > > Changes since v1: > > - Added comments for get_swap_device() as suggested by David > - Moved check that swap entry is not free from get_swap_device() to > free_swap_and_cache() since there are some paths that legitimately call with > a free offset. > > Best Regards, > Huang, Ying > > mm/swapfile.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > diff --git a/mm/swapfile.c b/mm/swapfile.c > index 2b3a2d85e350..9e0691276f5e 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -1609,13 +1609,19 @@ int free_swap_and_cache(swp_entry_t entry) > if (non_swap_entry(entry)) > return 1; > > - p = _swap_info_get(entry); > + p = get_swap_device(entry); > if (p) { > + if (WARN_ON(data_race(!p->swap_map[swp_offset(entry)]))) { > + put_swap_device(p); > + return 0; > + } > + > count = __swap_entry_free(p, entry); > if (count == SWAP_HAS_CACHE && > !swap_page_trans_huge_swapped(p, entry)) > __try_to_reclaim_swap(p, swp_offset(entry), > TTRS_UNMAPPED | TTRS_FULL); > + put_swap_device(p); > } > return p != NULL; > }