Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp433061lqi; Thu, 7 Mar 2024 01:20:46 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWJv5KySCMkSISOAQ4aDPuJmAM2l8QNVS6t0vyOTsPvm1HvjVMb3FASIRaZiPu6wRiT2F5+yNOSAg+lBaID7wymQVJrB0+7zoi1Hn2obA== X-Google-Smtp-Source: AGHT+IGLbIiMz47ksXmVo5GPWS7pJ86Z+I6qZncDBhdPjNM8W75LnCvilb4+euezMb9XeU/r1yXJ X-Received: by 2002:a17:903:2351:b0:1dc:a84b:e83c with SMTP id c17-20020a170903235100b001dca84be83cmr1419723plh.25.1709803246635; Thu, 07 Mar 2024 01:20:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709803246; cv=pass; d=google.com; s=arc-20160816; b=sMb3foWVmI9ztTFn+UaLdVLJP8K6ThHJU5N78cLi+NN2Wx/EENCX6U0SBHcl9fewLM UcCx7Jf+rmgUSq1QldNRoo34d748FC3qC3dD+SlN3YQ5L5rMLq5GyjvF61GFmrfEfdcr 1Yk4XgDsye2vOgQ3OD/iJwEp3r3NXr9T+m4KGcxr7gD4shnUCSkhWuYD3ih0+VXx3QLl 9TOOP2JIWJg1/k4gkTS0SwWRWB6J7S14ueShyiZRXTiFWPPep6MtGV7Y6AhKgDsVRitt rGGGiUC/ACcSXzOx8tHLXVwh6zo/TMJMNiU+1T2FZ1mR+Ozwe+/w9Ma1ScT888skao1T JZbg== 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=HuBU2eobQkyWJZeYadAxz/cAzjUfAmkYWs6jaO2YCNs=; fh=U0Muu+k9uCY72IKiag3xkX8vjZkvdz6f5z7bqq/Oap0=; b=OsJzhcOtFkWgz7f54ILW7hB1jqeYaPQyHyBMvl2DGBwXadEI6pSEeq2WfxxggtzeeV 7k3SQxOqNUsMkNSf0Whskejj5hrJ7b2M5MEtDZEiZj+geFemzNc9voE/Xe1x6ZQNA8If TtxmAVYjZX+yGplT/ms+yPxgFlayJjU61vY4qvrfOB4D/6DawxM7Yd2aEroisRCvC/aV 1UEzZd12wKjKQKkHPA6OlAk4ZeYu0ge9lxprvtoZreSfshg8bZQ3dH0EjUJ8i/gR2f/8 YkWlOzIF4nZYhiKj6q/2XDR/UK7fhL2sHykfPanxMOu+K8jyVKL3J32I8Xl69kXPWVto yiWQ==; 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-95202-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95202-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id u1-20020a170902e80100b001dca6272e21si13610797plg.36.2024.03.07.01.20.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 01:20:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-95202-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; 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-95202-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95202-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 51889281661 for ; Thu, 7 Mar 2024 09:19:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B8ACA83CA1; Thu, 7 Mar 2024 09:19:27 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BB113839F4; Thu, 7 Mar 2024 09:19:24 +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=1709803167; cv=none; b=utqvuQPAH2eb+s/10JSDSjLY2MlNqCSNDQfhHGgGjz3cNwK9LMsSGcxLJxDOvbtxXDNcpHJbbE58p91gKI6Fr4z3AK8FqgxvW+wE1wiqb6hn+SO89itsf9GdY662v2yJvXLzAc2ONcheaxpe4N+SA7glkmcW4+St/a14t1g+m9g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709803167; c=relaxed/simple; bh=2Ovq5uBAOM0L7vDNedetadWSHR9W5vxYB419k+cm1kU=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=o2Z2gP2BDImGNtDflv7fZHJu+SMHZxr1TwM/y1nSOXDTtUyqzjuHsn3GrTI9Cm4ik1SWgbWttWBqPV4k/GDoenIuh87FggjATmPz+B14dfEAKC/eKuDCkNjbko4de6H5Cf3fFpvF6CUTO280gvYIbd84bGSbtNkI3/SjrHZXDk4= 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 03D381FB; Thu, 7 Mar 2024 01:20:01 -0800 (PST) Received: from [10.57.68.241] (unknown [10.57.68.241]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id BC6B63F762; Thu, 7 Mar 2024 01:19:22 -0800 (PST) Message-ID: <29335a89-b14b-4ef3-abf8-0b41e6d0ec67@arm.com> Date: Thu, 7 Mar 2024 09:19:20 +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 v1] mm: swap: Fix race between free_swap_and_cache() and swapoff() Content-Language: en-GB To: "Huang, Ying" Cc: Miaohe Lin , Andrew Morton , David Hildenbrand , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20240305151349.3781428-1-ryan.roberts@arm.com> <875xy0842q.fsf@yhuang6-desk2.ccr.corp.intel.com> <87bk7q7ffp.fsf@yhuang6-desk2.ccr.corp.intel.com> <0925807f-d226-7f08-51d1-ab771b1a6c24@huawei.com> <8734t27awd.fsf@yhuang6-desk2.ccr.corp.intel.com> <92672c62-47d8-44ff-bd05-951c813c95a5@arm.com> <87y1au5smu.fsf@yhuang6-desk2.ccr.corp.intel.com> From: Ryan Roberts In-Reply-To: <87y1au5smu.fsf@yhuang6-desk2.ccr.corp.intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 07/03/2024 08:54, Huang, Ying wrote: > Ryan Roberts writes: > >> On 07/03/2024 07:34, Huang, Ying wrote: >>> Miaohe Lin writes: >>> >>>> On 2024/3/7 13:56, Huang, Ying wrote: >>>>> Miaohe Lin writes: >>>>> >>>>>> On 2024/3/6 17:31, Ryan Roberts wrote: >>>>>>> On 06/03/2024 08:51, Miaohe Lin wrote: >>>>>>>> On 2024/3/6 10:52, Huang, Ying wrote: >>>>>>>>> Ryan Roberts writes: >>>>>>>>> >>>>>>>>>> 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 valid. This wasn't present in get_swap_device() so >>>>>>>>>> I've added it. I couldn't find any existing get_swap_device() call sites >>>>>>>>>> where this extra check would cause any false alarms. >>>>>>>>>> >>>>>>>>>> Details of how to provoke one possible issue (thanks to David Hilenbrand >>>>>>>>>> for deriving this): >>>>>>>>>> >>>>>>>>>> --8<----- >>>>>>>>>> >>>>>>>>>> __swap_entry_free() might be the last user and result in >>>>>>>>>> "count == SWAP_HAS_CACHE". >>>>>>>>>> >>>>>>>>>> swapoff->try_to_unuse() will stop as soon as soon as si->inuse_pages==0. >>>>>>>>>> >>>>>>>>>> So the question is: could someone reclaim the folio and turn >>>>>>>>>> si->inuse_pages==0, before we completed swap_page_trans_huge_swapped(). >>>>>>>>>> >>>>>>>>>> Imagine the following: 2 MiB folio in the swapcache. Only 2 subpages are >>>>>>>>>> still references by swap entries. >>>>>>>>>> >>>>>>>>>> Process 1 still references subpage 0 via swap entry. >>>>>>>>>> Process 2 still references subpage 1 via swap entry. >>>>>>>>>> >>>>>>>>>> Process 1 quits. Calls free_swap_and_cache(). >>>>>>>>>> -> count == SWAP_HAS_CACHE >>>>>>>>>> [then, preempted in the hypervisor etc.] >>>>>>>>>> >>>>>>>>>> Process 2 quits. Calls free_swap_and_cache(). >>>>>>>>>> -> count == SWAP_HAS_CACHE >>>>>>>>>> >>>>>>>>>> Process 2 goes ahead, passes swap_page_trans_huge_swapped(), and calls >>>>>>>>>> __try_to_reclaim_swap(). >>>>>>>>>> >>>>>>>>>> __try_to_reclaim_swap()->folio_free_swap()->delete_from_swap_cache()-> >>>>>>>>>> put_swap_folio()->free_swap_slot()->swapcache_free_entries()-> >>>>>>>>>> swap_entry_free()->swap_range_free()-> >>>>>>>>>> ... >>>>>>>>>> WRITE_ONCE(si->inuse_pages, si->inuse_pages - nr_entries); >>>>>>>>>> >>>>>>>>>> What stops swapoff to succeed after process 2 reclaimed the swap cache >>>>>>>>>> but before process1 finished its call to swap_page_trans_huge_swapped()? >>>>>>>>>> >>>>>>>>>> --8<----- >>>>>>>>> >>>>>>>>> I think that this can be simplified. Even for a 4K folio, this could >>>>>>>>> happen. >>>>>>>>> >>>>>>>>> CPU0 CPU1 >>>>>>>>> ---- ---- >>>>>>>>> >>>>>>>>> zap_pte_range >>>>>>>>> free_swap_and_cache >>>>>>>>> __swap_entry_free >>>>>>>>> /* swap count become 0 */ >>>>>>>>> swapoff >>>>>>>>> try_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 !!! >>>>>>>> >>>>>>>> Sorry for jumping the discussion here. IMHO, free_swap_and_cache is called with pte lock held. >>>>>>> >>>>>>> I don't beleive it has the PTL when called by shmem. >>>>>> >>>>>> In the case of shmem, folio_lock is used to guard against the race. >>>>> >>>>> I don't find folio is lock for shmem. find_lock_entries() will only >>>>> lock the folio if (!xa_is_value()), that is, not swap entry. Can you >>>>> point out where the folio is locked for shmem? >>>> >>>> You're right, folio is locked if not swap entry. That's my mistake. But it seems above race is still nonexistent. >>>> shmem_unuse() will first be called to read all the shared memory data that resides in the swap device back into >>>> memory when doing swapoff. In that case, all the swapped pages are moved to page cache thus there won't be any >>>> xa_is_value(folio) cases when calling shmem_undo_range(). free_swap_and_cache() even won't be called from >>>> shmem_undo_range() after shmem_unuse(). Or am I miss something? >>> >>> I think the following situation is possible. Right? >>> >>> 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 !!! >>> >>> shmem_undo_range can run earlier. >> >> Yes that's the shmem problem I've been trying to convey. Perhaps there are other >> (extremely subtle) mechanisms that make this impossible, I don't know. >> >> Either way, given the length of this discussion, and the subtleties in the >> syncrhonization mechanisms that have so far been identified, I think the safest >> thing to do is just apply the patch. Then we have explicit syncrhonization that >> we can trivially reason about. > > Yes. This is tricky and we can improve it. So I suggest to, > > - Revise the patch description to use shmem race as example except > someone found it's impossible. > > - Revise the comments of get_swap_device() about RCU reader side lock > (including IRQ off, spinlock, etc.) can prevent swapoff via > synchronize_rcu() in swapoff(). > > - Revise the comments of synchronize_rcu() in swapoff(), which can > prevent swapoff in parallel with RCU reader side lock including swap > cache operations, etc. The only problem with this is that Andrew has already put my v2 into mm-*stable* :-| So (1) from that list isn't possible. I could do a patch for (2) and (3), but to be honest, I think you would do a better job of writing it up than I would - any chance you could post the patch? > > -- > Best Regards, > Huang, Ying