Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp1226358rdb; Fri, 16 Feb 2024 08:54:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW/C+VfotXWwMM8VH4cB6JaM/08yls1VzkFKURru39XbqKSICmNy/rSWgcOL6L1e1MvSyl6yRu51xY9x02eaHZi5829cDdE8pNVlUKW9Q== X-Google-Smtp-Source: AGHT+IHyVRh4DDTKuXLFePddWvlLZUzuwrYI0FdY5seyAr4EEGvBjmrQGUNo0UsTEOZ2xw50xsVl X-Received: by 2002:a05:6830:1298:b0:6e4:2b9f:244e with SMTP id z24-20020a056830129800b006e42b9f244emr5192134otp.2.1708102455868; Fri, 16 Feb 2024 08:54:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708102455; cv=pass; d=google.com; s=arc-20160816; b=KH5xI7oAvu6Ga6Lc4XgU/w2Izkzu6phPnq5USaMm4RdXK7pudOEXWzKEXZwffpqWLg Q9G2Jl+48SQ8svkVkLDXyrGEtjDWNeN/YuiNqjwhvLWchn2u4Y/QItazTEvzU8OKMVkD z8APuQqpJrC6nVlgU3BHB3cjrXNj4zFr8vZPbCirpmSTRaOIAmKMS/mFVYOmS6GsY0dw jVRiXWpGdN6pbNouNUSGy7llT2iVoaJbjx4M0z+N2lHy382b1qXOKbxeLvXMwUni9/jp l8VvMrR9ke9/ROdMBZWZi+Yp+QNmQn1z9Eiw4ySxK2IQqlZX5AWOC+8QQmWIFwSHIGAN zchQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:cc:to:content-language:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id :dkim-signature; bh=pqSCd0hgk7x07BO/xgvUIHu8B6Qz2MMzhRXvndmAPFw=; fh=fqt7V0gl1FKsMSpfPane91fLwyanD6tpJE6rVBVH04o=; b=kFRFHywd3kBVi9d6W4ZFKVsGzF0DXovRTsCmDqQpKz5TEZQlmNT9MLYyXvEcCywKnC LpTW2ukz4feGrUGVc0vhqf+SxKqrHEuS6x2sg7rTbDfeqvLa0XRyUnNwT41w0mZV56Tb JD+OUdkZaOV6XXQvxB5tgqlp6v9wDFkspbd+12XpG0QEVlSFwXeYQBK2eoRELN3SnstL nR5hHThgdWTxMzNQfqabNgsniZhhy77HzoE7gMcjAP7Djbfsg2mBfeInyAZwvMAvEgm1 RmJAIjDMrts7+EWMWl6zUDQT1sfuOiG8J4HcB9T2IZt2HAOZ1uuCacK+QrK6DAsRt9yM bhMg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Edf+Jawn; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-68989-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68989-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id s22-20020ac87596000000b0042c44dec59bsi163808qtq.482.2024.02.16.08.54.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 08:54:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-68989-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Edf+Jawn; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-68989-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68989-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 8D16D1C21CD0 for ; Fri, 16 Feb 2024 16:54:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F0ED4130AF7; Fri, 16 Feb 2024 16:53:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Edf+Jawn" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 95827130AF6 for ; Fri, 16 Feb 2024 16:53:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708102414; cv=none; b=Q3y487LJb5u27Ni6ddqp+zTa9gFUfre7WaP+Kb9MVZT/VJE5f4e9WJqfz7XHr6vjFg/Lpg5LckhKUIy+kYZNX3GYPP9WljSri/WLun7j9RjIjVoZCvHTv3J9Wa2UU6EvEzTABRC2KE2XhrzXoHSp1qTivU6M8a7xotSA8xuH9D4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708102414; c=relaxed/simple; bh=FEf2Y/KAfI8ze9u45mC8SvInXV3LWFvYIZqq3fzfF80=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=E6YrF1R6qMu7rKmXEm1GKAnYFBplFddMCjkjHVIUPFsFMiYeQZeA3QxOdjGM3LopaSHbWAzyumOndfX8prct5akwOzoXe0Lk2lr68ILUbb/1LKxDOqiMkefM1gsogAUnY0kyfXEl6YaaTD5Kwm0jWLfTakE3dACtPgr5eBwWJYg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Edf+Jawn; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708102411; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=pqSCd0hgk7x07BO/xgvUIHu8B6Qz2MMzhRXvndmAPFw=; b=Edf+Jawn6gEWhWqOlaNOu2WkX8wCH29HkYBvWyfS3AiKYUxhSwZqJ1eHBLQckbGd0kSyeB BagJOWOeye/N/7NTWGouj8hCAqVYaOl4G0CRlG3hulOYv4lWz2hD1AN3JRG+hoS0BYOQa7 44BWMEb2IrFFZ+fBjNnuN0pSjtsWigU= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-622-aVAoSnNKOUyze70gYEjcEQ-1; Fri, 16 Feb 2024 11:53:29 -0500 X-MC-Unique: aVAoSnNKOUyze70gYEjcEQ-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4121ac8634aso5302225e9.2 for ; Fri, 16 Feb 2024 08:53:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708102408; x=1708707208; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=pqSCd0hgk7x07BO/xgvUIHu8B6Qz2MMzhRXvndmAPFw=; b=bagxF16QPVf7JYpoPGapvZq83XiOUL8+jsIAPgxNEp7EBxrVowrCnbih/EUKtcnzBP ve/vPbtK+m67WmOcrgofdmlStTTmCagAKlUABwYmfscdljQlACXQwM3JLfNxfSZK5sYK c362hHiNaGM/79Qmhxai8oVcc2DT3y2xFAPSMh22fTTKnwqcEbX9DBw6ayN2qEBDzEDa akxHYUHBj4MIHWbIWIPxkZVfHCU+yzImS8A3P0l2EQrAk/TiVijgu2eTWjGAK81THCg1 fiQkE0+4X98zz0/XSd1SQdgQUHdXAphrSkodFEnfsvJ92HsONoeLwKj28eATpd480CDf P1fw== X-Forwarded-Encrypted: i=1; AJvYcCVkQ9hK4XzSxLMoxwPpmODWB1KqEbSOJx7a8ycNaX+k0TakCAXFqqfyFXh3FY0HXKwikHLClkFh2XRJ1tRnNvL1fiqnUgyWC7h+21z7 X-Gm-Message-State: AOJu0YzlPdewupyTvvsUE/68k8/m3ojyD7zG7qSxHv0Hjg5/6J1AIvxM CBWAbe7Ja88XVHZWkOrVg1sQXG+OalqCZQjDiTExmgDbqjHDzIEgL+UsujQUiQO5EV1vhoobBeG WUw0h6WWoIyukzbIn54LYB7XPJetCKt96nFzborBOQIKQXoE1db4jksW0YbLbfWYdsgCsOg== X-Received: by 2002:a05:600c:444e:b0:411:e167:364d with SMTP id v14-20020a05600c444e00b00411e167364dmr4458160wmn.30.1708102407881; Fri, 16 Feb 2024 08:53:27 -0800 (PST) X-Received: by 2002:a05:600c:444e:b0:411:e167:364d with SMTP id v14-20020a05600c444e00b00411e167364dmr4458129wmn.30.1708102407379; Fri, 16 Feb 2024 08:53:27 -0800 (PST) Received: from ?IPV6:2003:d8:2f3c:3f00:7177:eb0c:d3d2:4b0e? (p200300d82f3c3f007177eb0cd3d24b0e.dip0.t-ipconnect.de. [2003:d8:2f3c:3f00:7177:eb0c:d3d2:4b0e]) by smtp.gmail.com with ESMTPSA id az5-20020a05600c600500b0040e4733aecbsm2932847wmb.15.2024.02.16.08.53.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 16 Feb 2024 08:53:26 -0800 (PST) Message-ID: Date: Fri, 16 Feb 2024 17:53:25 +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 v3] mm/swap: fix race when skipping swapcache Content-Language: en-US To: Kairui Song , linux-mm@kvack.org Cc: Andrew Morton , "Huang, Ying" , Chris Li , Minchan Kim , Yu Zhao , Barry Song , SeongJae Park , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Yosry Ahmed , stable@vger.kernel.org, linux-kernel@vger.kernel.org References: <20240216095105.14502-1-ryncsn@gmail.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8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fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <20240216095105.14502-1-ryncsn@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 16.02.24 10:51, Kairui Song wrote: > From: Kairui Song > > When skipping swapcache for SWP_SYNCHRONOUS_IO, if two or more threads > swapin the same entry at the same time, they get different pages (A, B). > Before one thread (T0) finishes the swapin and installs page (A) > to the PTE, another thread (T1) could finish swapin of page (B), > swap_free the entry, then swap out the possibly modified page > reusing the same entry. It breaks the pte_same check in (T0) because > PTE value is unchanged, causing ABA problem. Thread (T0) will > install a stalled page (A) into the PTE and cause data corruption. > > One possible callstack is like this: > > CPU0 CPU1 > ---- ---- > do_swap_page() do_swap_page() with same entry > > > swap_read_folio() <- read to page A swap_read_folio() <- read to page B > > ... set_pte_at() > swap_free() <- entry is free > > > pte_same() <- Check pass, PTE seems > unchanged, but page A > is stalled! > swap_free() <- page B content lost! > set_pte_at() <- staled page A installed! > > And besides, for ZRAM, swap_free() allows the swap device to discard > the entry content, so even if page (B) is not modified, if > swap_read_folio() on CPU0 happens later than swap_free() on CPU1, > it may also cause data loss. > > To fix this, reuse swapcache_prepare which will pin the swap entry using > the cache flag, and allow only one thread to pin it. Release the pin > after PT unlocked. Racers will simply wait since it's a rare and very > short event. A schedule() call is added to avoid wasting too much CPU > or adding too much noise to perf statistics > > Other methods like increasing the swap count don't seem to be a good > idea after some tests, that will cause racers to fall back to use the > swap cache again. Parallel swapin using different methods leads to > a much more complex scenario. > > Reproducer: > > This race issue can be triggered easily using a well constructed > reproducer and patched brd (with a delay in read path) [1]: > > With latest 6.8 mainline, race caused data loss can be observed easily: > $ gcc -g -lpthread test-thread-swap-race.c && ./a.out > Polulating 32MB of memory region... > Keep swapping out... > Starting round 0... > Spawning 65536 workers... > 32746 workers spawned, wait for done... > Round 0: Error on 0x5aa00, expected 32746, got 32743, 3 data loss! > Round 0: Error on 0x395200, expected 32746, got 32743, 3 data loss! > Round 0: Error on 0x3fd000, expected 32746, got 32737, 9 data loss! > Round 0 Failed, 15 data loss! > > This reproducer spawns multiple threads sharing the same memory region > using a small swap device. Every two threads updates mapped pages one by > one in opposite direction trying to create a race, with one dedicated > thread keep swapping out the data out using madvise. > > The reproducer created a reproduce rate of about once every 5 minutes, > so the race should be totally possible in production. > > After this patch, I ran the reproducer for over a few hundred rounds > and no data loss observed. > > Performance overhead is minimal, microbenchmark swapin 10G from 32G > zram: > > Before: 10934698 us > After: 11157121 us > Non-direct: 13155355 us (Dropping SWP_SYNCHRONOUS_IO flag) > > Fixes: 0bcac06f27d7 ("mm, swap: skip swapcache for swapin of synchronous device") > Link: https://github.com/ryncsn/emm-test-project/tree/master/swap-stress-race [1] > Reported-by: "Huang, Ying" > Closes: https://lore.kernel.org/lkml/87bk92gqpx.fsf_-_@yhuang6-desk2.ccr.corp.intel.com/ > Signed-off-by: Kairui Song > Cc: stable@vger.kernel.org > > --- > Update from V2: > - Add a schedule() if raced to prevent repeated page faults wasting CPU > and add noise to perf statistics. > - Use a bool to state the special case instead of reusing existing > variables fixing error handling [Minchan Kim]. > > V2: https://lore.kernel.org/all/20240206182559.32264-1-ryncsn@gmail.com/ > > Update from V1: > - Add some words on ZRAM case, it will discard swap content on swap_free so the race window is a bit different but cure is the same. [Barry Song] > - Update comments make it cleaner [Huang, Ying] > - Add a function place holder to fix CONFIG_SWAP=n built [SeongJae Park] > - Update the commit message and summary, refer to SWP_SYNCHRONOUS_IO instead of "direct swapin path" [Yu Zhao] > - Update commit message. > - Collect Review and Acks. > > V1: https://lore.kernel.org/all/20240205110959.4021-1-ryncsn@gmail.com/ > > include/linux/swap.h | 5 +++++ > mm/memory.c | 20 ++++++++++++++++++++ > mm/swap.h | 5 +++++ > mm/swapfile.c | 13 +++++++++++++ > 4 files changed, 43 insertions(+) > > diff --git a/include/linux/swap.h b/include/linux/swap.h > index 4db00ddad261..8d28f6091a32 100644 > --- a/include/linux/swap.h > +++ b/include/linux/swap.h > @@ -549,6 +549,11 @@ static inline int swap_duplicate(swp_entry_t swp) > return 0; > } > > +static inline int swapcache_prepare(swp_entry_t swp) > +{ > + return 0; > +} > + > static inline void swap_free(swp_entry_t swp) > { > } > diff --git a/mm/memory.c b/mm/memory.c > index 7e1f4849463a..7059230d0a54 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -3799,6 +3799,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > struct page *page; > struct swap_info_struct *si = NULL; > rmap_t rmap_flags = RMAP_NONE; > + bool need_clear_cache = false; > bool exclusive = false; > swp_entry_t entry; > pte_t pte; > @@ -3867,6 +3868,20 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > if (!folio) { > if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && > __swap_count(entry) == 1) { > + /* > + * Prevent parallel swapin from proceeding with > + * the cache flag. Otherwise, another thread may > + * finish swapin first, free the entry, and swapout > + * reusing the same entry. It's undetectable as > + * pte_same() returns true due to entry reuse. > + */ > + if (swapcache_prepare(entry)) { > + /* Relax a bit to prevent rapid repeated page faults */ > + schedule(); > + goto out; > + } > + need_clear_cache = true; > + I took a closer look at __read_swap_cache_async() and it essentially does something similar. Instead of returning, it keeps retrying until it finds that swapcache_prepare() fails for another reason than -EEXISTS (e.g., freed concurrently) or it finds the entry in the swapcache. So if you would succeed here on a freed+reused swap entry, __read_swap_cache_async() would simply retry. It spells that out: /* * We might race against __delete_from_swap_cache(), and * stumble across a swap_map entry whose SWAP_HAS_CACHE * has not yet been cleared. Or race against another * __read_swap_cache_async(), which has set SWAP_HAS_CACHE * in swap_map, but not yet added its folio to swap cache. */ Whereby we could not race against this code here as well where we speculatively set SWAP_HAS_CACHE and might never add something to the swap cache. I'd probably avoid the wrong returns and do something even closer to __read_swap_cache_async(). while (true) { /* * Fake that we are trying to insert a page into the swapcache, to * serialize against concurrent threads wanting to do the same. * [more from your description] */ ret = swapcache_prepare(entry); if (likely(!ret) /* * Move forward with swapin, we'll recheck if the PTE hasn't * changed later. */ break; else if (ret != -EEXIST) goto out; /* * See __read_swap_cache_async(). We might either have raced against * another thread, or the entry could have been freed and reused in the * meantime. Make sure that the PTE did not change, to detect freeing. */ vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, &vmf->ptl); if (!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte)) goto unlock; schedule(); } I was skeptical about the schedule(), but __read_swap_cache_async() does it already because there is no better way to wait for the event to happen. With something like above you would no longer depend on the speed of schedule() to determine how often you would retry the fault, which would likely make sense. I do wonder about the schedule() vs. schedule_timeout_uninterruptible(), though. No expert on that area, do you have any idea? -- Cheers, David / dhildenb