From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 051E2C4828F for ; Wed, 7 Feb 2024 04:06:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5DCEE6B007D; Tue, 6 Feb 2024 23:06:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 58D866B0080; Tue, 6 Feb 2024 23:06:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 454D46B0081; Tue, 6 Feb 2024 23:06:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 349E16B007D for ; Tue, 6 Feb 2024 23:06:37 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E9D8480C6B for ; Wed, 7 Feb 2024 04:06:36 +0000 (UTC) X-FDA: 81763671192.19.18E35B5 Received: from mail-lj1-f175.google.com (mail-lj1-f175.google.com [209.85.208.175]) by imf16.hostedemail.com (Postfix) with ESMTP id 10B06180014 for ; Wed, 7 Feb 2024 04:06:34 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=TYipyJ4C; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.175 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707278795; a=rsa-sha256; cv=none; b=CFHSZGwRWqQi9WJpyHsAiL3EHZw06HSENgyCstuiIDOp79jp2V80qrhpjsK5ff3iPiv1Ey tYbFZyY+sS035bi1/W4HO1Jb2ZORBeFrTK11IU+6/XwcsoPwHMcERbfb+o8mW8WKcqJd07 aSO4ie3894tbXCzwmbG7EuxDIsU3KsY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=TYipyJ4C; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.175 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707278795; h=from:from:sender: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:dkim-signature; bh=7hAFMIwS/BE1sRgqMaTOJ2yGZ8MaAcI4W7dcsziTmnk=; b=4Orb9ejw9VwaP9/pi/DFf33jl+rfp6BnYG3cd02e6RF6vPYy2ExauzO0DJNY+2pqBSlWMb HvnuIr7ZYRyWRZgc8OtrzL+hGw0RjtjRiugP3G8pJg0FhIQXFguq4GVStfEmMaoNZte1sI KzKockiPWKAd5GTZjF0dcIsjEOVXKbY= Received: by mail-lj1-f175.google.com with SMTP id 38308e7fff4ca-2d0b4ea773eso1920021fa.0 for ; Tue, 06 Feb 2024 20:06:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707278793; x=1707883593; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=7hAFMIwS/BE1sRgqMaTOJ2yGZ8MaAcI4W7dcsziTmnk=; b=TYipyJ4CJSyuEBKg+6FtYmd/H4CRW/8REYqkXlYvDnsHw9yXSK+oNrCVw2Ay4LLGeN qDzL1Z7vklijmck6+Kf1EVDBA13NIBU/iZmyC7CgtqudyBY2GrLfyOVpkbkjoJzzcLId rAasoqqLA2fw2SYGeFAQQ8GpHyxUkfW+1w5Supfah019Sxs63ybEcG7NABdKKlUESoEl CGon1pVbij8L/CTkzuT+v5TTXVjbhaFBsN3m+SPGWamiGmd0q9aVTqHI9PKOLNMgLvzj i852Gn5zy5vqqhlaS5f2vgq20ho2+au85ZIk+3ICNKKUIRHP8berNrt9sd/Ng535oppu snXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707278793; x=1707883593; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7hAFMIwS/BE1sRgqMaTOJ2yGZ8MaAcI4W7dcsziTmnk=; b=jZX54aa59Tsn9BxT7PhdGMZsY8NqfEYG7XNaZw8HV7VbbCM8a1DW8rZmyEArvVzQQP 79Ih6hlLNRV8DGn1CyvaWUuj1A6oGM2kWOokWd3j6fgDUvGz6Fn6RHmnn2MmeyDM7aQy 27yVDzBWax6c4RFrqU4JRXBALMwQpwML7VfdJbCBFMNZ5Pj9D/P5TMUKl2V/vaKhUd7/ iBdaZpkQHqncP7VgeVvXTupbYbOShJnRCYNdBe4Sny2srL7HlQfiXeMCfEq3ETrQPlMg Mkaaj76aFVgjMCGhYgvP1mjqC4Aw+AzGjoTaN5fvNZCwru715s/rUdPjeRE9fFFHHhdN bARA== X-Gm-Message-State: AOJu0YwhK4ko1bBBaqmMb5WDk9fVzfMN9Tt8P4RwGi40150u8yZlMz2/ XEQ3S8nNtdIQiIoH9Wxxn+qyynh+CMGiC8D9h9jbkyzAxdPEVUkSZdAXSmP/2n9L4qFIoJcxIcK zxOeyGrJk5IjAj09sy9oAmQ1/M1g= X-Google-Smtp-Source: AGHT+IFu+MfkjcRXrpWvUxo4X9gu1eQea5nu3Q5c2Ibt2IhA8b8kssE1CsgtYy/GQKlzKNDoHaXsIkQG0xQ3p/KngGE= X-Received: by 2002:a2e:b527:0:b0:2d0:82cf:d3d1 with SMTP id z7-20020a2eb527000000b002d082cfd3d1mr2785493ljm.22.1707278792971; Tue, 06 Feb 2024 20:06:32 -0800 (PST) MIME-Version: 1.0 References: <20240206182559.32264-1-ryncsn@gmail.com> In-Reply-To: From: Kairui Song Date: Wed, 7 Feb 2024 12:06:15 +0800 Message-ID: Subject: Re: [PATCH v2] mm/swap: fix race when skipping swapcache To: Chris Li Cc: Barry Song <21cnbao@gmail.com>, linux-mm@kvack.org, Andrew Morton , "Huang, Ying" , Minchan Kim , Yu Zhao , Barry Song , SeongJae Park , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Yosry Ahmed , David Hildenbrand , stable@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 10B06180014 X-Stat-Signature: tudag358eg33p4npdpytyndz3i4eatsi X-HE-Tag: 1707278794-199452 X-HE-Meta: U2FsdGVkX18efHFeHRXoshYpPfGXDj51AeardpJtVhRSyJV/nv/7CCXggD9UadeevVeSp+6P7wHPl0GTCH+NYnUIyieeY3WyTOT5fd8eCgUPAAdAW/YtemXa53kn9mPul8reK2YnKiZezZzsGVZtpLhQx+DFkZNoGYPU8JSY8jl0TR4b6UJMUIMUiTs7J9WF/Zis7eI//jwri4Nn0hxi5Wfk72MGw4UcNLme8melohi2ZULjS9Yz7K4OXpxJ0Cqq8OZCoE3XCwgmjp8+exyCc4zqo67AWuICIGTagsQHkNaWLI05W3XMYx6MmuEb26PzygTfhVEQdkGi5IH6IRKcX/fM5bGChrSrIVKVl17bnIPQMFmt6VNHGVthiLfwZIt0Pup3WbEdpdqB8mTE/IWzbY8UtUXD2Y5D5LfjNuZL0tGVK70tIJs7DPMGYCKWNjefnhOIKWzU2O9bQw6OZB//kUmMM5WfibOyID3wIzK6YoRzXh1VjDNMJjNOFRINyxkq6qSv0Uj0PKEvNPs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=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, Feb 7, 2024 at 12:02=E2=80=AFPM Chris Li wrote: > > On Tue, Feb 6, 2024 at 6:21=E2=80=AFPM Kairui Song wro= te: > > > > On Wed, Feb 7, 2024 at 10:03=E2=80=AFAM Chris Li wr= ote: > > > > > > On Tue, Feb 6, 2024 at 4:43=E2=80=AFPM Barry Song <21cnbao@gmail.com>= wrote: > > > > > > > > On Wed, Feb 7, 2024 at 7:18=E2=80=AFAM Chris Li = wrote: > > > > > > > > > > Hi Kairui, > > > > > > > > > > Sorry replying to your patch V1 late, I will reply on the V2 thre= ad. > > > > > > > > > > On Tue, Feb 6, 2024 at 10:28=E2=80=AFAM 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 page= s (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) be= cause > > > > > > PTE value is unchanged, causing ABA problem. Thread (T0) will > > > > > > install a stalled page (A) into the PTE and cause data corrupti= on. > > > > > > > > > > > > One possible callstack is like this: > > > > > > > > > > > > CPU0 CPU1 > > > > > > ---- ---- > > > > > > do_swap_page() do_swap_page() with same e= ntry > > > > > > > > > > > > > > > > > > swap_read_folio() <- read to page A swap_read_folio() <- read = to page B > > > > > > > > > > > > ... set_pte_at() > > > > > > swap_free() <- entry is fr= ee > > > > > > > > > > > > > > > > > > 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 di= scard > > > > > > the entry content, so even if page (B) is not modified, if > > > > > > swap_read_folio() on CPU0 happens later than swap_free() on CPU= 1, > > > > > > it may also cause data loss. > > > > > > > > > > > > To fix this, reuse swapcache_prepare which will pin the swap en= try using > > > > > > the cache flag, and allow only one thread to pin it. Release th= e pin > > > > > > after PT unlocked. Racers will simply busy wait since it's a ra= re > > > > > > and very short event. > > > > > > > > > > > > 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 u= se 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 constructe= d > > > > > > 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 pag= es one by > > > > > > one in opposite direction trying to create a race, with one ded= icated > > > > > > thread keep swapping out the data out using madvise. > > > > > > > > > > > > The reproducer created a reproduce rate of about once every 5 m= inutes, > > > > > > so the race should be totally possible in production. > > > > > > > > > > > > After this patch, I ran the reproducer for over a few hundred r= ounds > > > > > > 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 sy= nchronous device") > > > > > > Reported-by: "Huang, Ying" > > > > > > Closes: https://lore.kernel.org/lkml/87bk92gqpx.fsf_-_@yhuang6-= desk2.ccr.corp.intel.com/ > > > > > > Link: https://github.com/ryncsn/emm-test-project/tree/master/sw= ap-stress-race [1] > > > > > > Signed-off-by: Kairui Song > > > > > > Reviewed-by: "Huang, Ying" > > > > > > Acked-by: Yu Zhao > > > > > > > > > > > > --- > > > > > > 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. [Barr= y Song] > > > > > > - Update comments make it cleaner [Huang, Ying] > > > > > > - Add a function place holder to fix CONFIG_SWAP=3Dn built [Seo= ngJae Park] > > > > > > - Update the commit message and summary, refer to SWP_SYNCHRONO= US_IO instead of "direct swapin path" [Yu Zhao] > > > > > > - Update commit message. > > > > > > - Collect Review and Acks. > > > > > > > > > > > > include/linux/swap.h | 5 +++++ > > > > > > mm/memory.c | 15 +++++++++++++++ > > > > > > mm/swap.h | 5 +++++ > > > > > > mm/swapfile.c | 13 +++++++++++++ > > > > > > 4 files changed, 38 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..1749c700823d 100644 > > > > > > --- a/mm/memory.c > > > > > > +++ b/mm/memory.c > > > > > > @@ -3867,6 +3867,16 @@ vm_fault_t do_swap_page(struct vm_fault = *vmf) > > > > > > if (!folio) { > > > > > > if (data_race(si->flags & SWP_SYNCHRONOUS_IO) &= & > > > > > > __swap_count(entry) =3D=3D 1) { > > > > > > + /* > > > > > > + * Prevent parallel swapin from proceed= ing with > > > > > > + * the cache flag. Otherwise, another t= hread may > > > > > > + * finish swapin first, free the entry,= and swapout > > > > > > + * reusing the same entry. It's undetec= table as > > > > > > + * pte_same() returns true due to entry= reuse. > > > > > > + */ > > > > > > + if (swapcache_prepare(entry)) > > > > > > + goto out; > > > > > > + > > > > > > > > > > I am puzzled by this "goto out". If I understand this correctly, = you > > > > > have two threads CPU1 and CPU2 racing to set the flag SWAP_HAS_CA= CHE. > > > > > The CPU1 will succeed in adding the flag and the CPU2 will get > > > > > "-EEXIST" from "swapcache_prepare(entry)". Am I understanding it > > > > > correctly so far? > > > > > > > > > > Then the goto out seems wrong to me. For the CPU2, the page fault= will > > > > > return *unhandled*. Even worse, the "-EEXIST" error is not preser= ved, > > > > > CPU2 does not even know the page fault is not handled, it will re= sume > > > > > from the page fault instruction, possibly generate another page f= ault > > > > > at the exact same location. That page fault loop will repeat unti= l > > > > > CPU1 install the new pte on that faulting virtual address and pic= k up > > > > > by CPU2. > > > > > > > > > > Am I missing something obvious there? > > > > > > > > I feel you are right. any concurrent page faults at the same pte > > > > will increase the count of page faults for a couple of times now. > > > > > > > > > > > > > > I just re-read your comment: "Racers will simply busy wait since = it's > > > > > a rare and very short event." That might be referring to the abov= e > > > > > CPU2 page fault looping situation. I consider the page fault loop= ing > > > > > on CPU2 not acceptable. For one it will mess up the page fault > > > > > statistics. > > > > > In my mind, having an explicit loop for CPU2 waiting for the PTE = to > > > > > show up is still better than this page fault loop. You can have m= ore > > > > > CPU power friendly loops. > > > > > > > > I assume you mean something like > > > > > > > > while(!pte_same()) > > > > cpu_relax(); > > > > > > > > then we still have a chance to miss the change of B. > > > > > > > > For example, another thread is changing pte to A->B->A, our loop ca= n > > > > miss B. Thus we will trap into an infinite loop. this is even worse= . > > > > > > Yes. You are right, it is worse. Thanks for catching that. That is wh= y > > > I say this needs more discussion, I haven't fully thought it through > > > :-) > > > > Hi Chris and Barry, > > > > Thanks for the comments! > > > > The worst thing I know of returning in do_swap_page without handling > > the swap, is an increase of some statistic counters, note it will not > > cause major page fault counters to grow, only things like perf counter > > and vma lock statistic are affected. > > > > And actually there are multiple already existing return points in > > do_swap_page that will return without handling it, which may > > re-trigger the page fault. > > Thanks for pointing that out. I take a look at those, which seems > different than the case here. In those cases, it truely can not make > forward progress. > Here we actually have all the data it needs to complete the page > fault. Just a data synchronization issue preventing making forward > progress. > Ideally we can have some clever data structure to solve the > synchronization issue and make forward progress. > > > When do_swap_page is called, many pre-checks have been applied, and > > they could all be invalidated if something raced, simply looping > > inside here could miss a lot of corner cases, so we have to go through > > that again. > > Actually, I think about it. Looping it here seems worse in the sense > that it is already holding some locks. Return and retry the page fault > at least release those locks and let others have a chance to make > progress. > > > > > This patch did increase the chance of false positive increase of some > > counters, maybe something like returning a VM_FAULT_RETRY could make > > it better, but code is more complex and will cause other counters to > > grow. > > This is certainly not ideal. It might upset the feedback loop that > uses the swap fault statistic as input to adjust the swapping > behavior. > > Chris Hi Chris, Thanks for the reply. So I think the thing is, it's getting complex because this patch wanted to make it simple and just reuse the swap cache flags. Then maybe time to introduce a new swap map value (eg. SWAP_MAP_LOCK, not an ideal name, open to any suggestion on this...) then we can just wait for that value to be gone, no risk of loops and repeated page faults, just need to tidy some flag set/retrieving helpers.