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 8D502C48297 for ; Wed, 7 Feb 2024 02:29:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1BC216B0075; Tue, 6 Feb 2024 21:29:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 16BED6B007D; Tue, 6 Feb 2024 21:29:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F26C36B0081; Tue, 6 Feb 2024 21:29:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id DB5F46B0075 for ; Tue, 6 Feb 2024 21:29:21 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A141D1A0320 for ; Wed, 7 Feb 2024 02:29:21 +0000 (UTC) X-FDA: 81763426122.05.1946BD3 Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) by imf19.hostedemail.com (Postfix) with ESMTP id B12BF1A0007 for ; Wed, 7 Feb 2024 02:29:19 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Ff6rjEeV; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.177 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=1707272959; 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=RB1KJ0JdRvMYNf1s/iIEygXVJP41nn64OktYdzwiagE=; b=VuFEhag4obMX6wFH1TNdV/uk3NHQ5CRGDGdrSHmtzd8KB5ovuKSpVzrAqd/mScpV01Gf7E qhlFzRoBwnfKl0v9KOD3wMeRmX8tr6QwE90u4ygr8DxM4dctc5GXcBhyl8tHVbPWBxt+Rk wNTCfBlRxris8PZE5jxSMkvfH0xMsLo= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Ff6rjEeV; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707272959; a=rsa-sha256; cv=none; b=lKo7hQtSS7Nf51Mz7hMuq3TMwRBVNo12NEvZMlSXVxoPOZw5bkaBt08Mgey2WeNwY45FpA A7CwDbtRHsuR5vS0pFDZ/8BpRDuKzAdqQjNVQ9VDWz9/hNA1I4yeD5Hlo0IkJQ0b02nCLo y8LqBGPTEszd7kpcD7x2NLc4k+1bocY= Received: by mail-lj1-f177.google.com with SMTP id 38308e7fff4ca-2d09d90fa11so1572671fa.3 for ; Tue, 06 Feb 2024 18:29:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707272958; x=1707877758; 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=RB1KJ0JdRvMYNf1s/iIEygXVJP41nn64OktYdzwiagE=; b=Ff6rjEeVMYAcBl6WaQYz2Dyf9GKY4x6fxekvbjGuYMOvoTdJh/ubNHMBotPo4aZVV+ RKJaYPPQ4mI63suKKghvPKdaBqIfXBB0txyk2oQ3giTFdR01aIHE12oUaPEtRDJnOs0n irpUuIb2+D9HuJxCn/EGsCE/qftDPpM75iCx9Xag6CsHdyXRYCa3wWV/WTI9GsPBq4uM LyDClFlbJo4iID8aQXqQwxRFJUIkLxWI3LMoeaLUWoehfFUpqw510UsaWIz3+nHb5PiF yS4v0M2e3iXLvwhWbHJmQdyMRA+h/i+4CoVRmo70uwnO5YxuXXp7OGroNC5n3G87yim2 SFmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707272958; x=1707877758; 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=RB1KJ0JdRvMYNf1s/iIEygXVJP41nn64OktYdzwiagE=; b=qqB9Huvb+DjrlB6EjuBNbbvJ70FIxx5GFSPs0mGf8o2pbZ3WUem6HFE1KPEy9jTLBB 6vQOkDFFX8dCgc/UEcxN/Pi2GaqD5e61W+8eoTNN95WBuM1x54kkgtGG43yuRBVzh30E 1llyhQZf0azF+GUx8JDQ+KYz8Hqw/LYQm/7WFEn8SFY3+Lbsf8Pozf0zJ9DXJAOkrLmW jNFdPTGN8H6F8p8JaR6DJEJUH4eEUXUMHTRVPiwG4iDIW8pToM9UV0H7jYwYWMund638 dEHac99lnm8J7hk52lXcI0zWcqfVD/4051vPjDK5+zsVSzRf79z+bvQuyHCdUKYXAKoN FHkw== X-Forwarded-Encrypted: i=1; AJvYcCXABAhWWEDpXkC/D5WlbvMIz9DZva9/9VaBY8xEuaiCkUkjETPQ4QNXNFvhHq+INqB0AJ13gNSL5wn1mATB4RylGgE= X-Gm-Message-State: AOJu0Yy6SH6s2lX0xKmB484DwXEYuSpABH/1RFpteasxlU80xPV1uq7R Yi/FusIsSomVa4IWqcDdsyAerRpmK4Kz17jJFSHBlZfxB0ec7FDbCQo4gSMzwcgSRGLpAUH6OiP WsOMD8+A3nj74eP4JkpPY+MJFoXg= X-Google-Smtp-Source: AGHT+IH61BSwULQr4L1a6aIKcdwGLWuKzrlFSjf0/lzC40o7jjLa/Fc9NG/MkE+GL4cZTTlWNTb82qUUw2Xoi3WxPdk= X-Received: by 2002:a2e:9098:0:b0:2d0:996e:b014 with SMTP id l24-20020a2e9098000000b002d0996eb014mr2785728ljg.32.1707272957649; Tue, 06 Feb 2024 18:29:17 -0800 (PST) MIME-Version: 1.0 References: <20240206182559.32264-1-ryncsn@gmail.com> <87sf25yqj2.fsf@yhuang6-desk2.ccr.corp.intel.com> In-Reply-To: <87sf25yqj2.fsf@yhuang6-desk2.ccr.corp.intel.com> From: Kairui Song Date: Wed, 7 Feb 2024 10:28:59 +0800 Message-ID: Subject: Re: [PATCH v2] mm/swap: fix race when skipping swapcache To: "Huang, Ying" Cc: Barry Song <21cnbao@gmail.com>, Chris Li , linux-mm@kvack.org, Andrew Morton , 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-Rspamd-Queue-Id: B12BF1A0007 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: eefa5b73tpb64j4c8u33ih7nyx9qmzzr X-HE-Tag: 1707272959-303489 X-HE-Meta: U2FsdGVkX18zeV4OvincB6IVBgdcdCT2XGlM+fak3m1Dp6gilloGu3zGumW/7fDdriISNFWnujkI3a7jbiv5YQXw92yvZjk79TEzrcxuE3b1Xp7OW2t2XZZA1QW/HctlJwG5BXWOehWXbfq7U8tk+WZc3aZmqRlF6sInuEKe/yJF0+SXvRaVJYNqgvKNVVNPBWiUw1vxaz/zwxTVL5LFSDEx+P2KCFPjk8zFM2VwrwZ+qKgiijAkvKOCMwl8oIjCN6chGn0gxCAx0NR8Ufxku6DQ9zCQtNtmrLmgxeZCJQece9WFi2axT1ccGE8RklM5TzeuO+XiXynNpO8BiH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yo7W8U7ShhHXn9QgHoxxSeSbrHcuuSSt8fNQafjszNbCCDhsgy2V9HKy/GZfPGgYUwY8xg3os7v8LCrK8ghnPjfCfJgFfZziNFdcUdYr8vdLkXfBYi1pJmaYGDFB6HDGw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=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 10:10=E2=80=AFAM Huang, Ying = wrote: > > Barry Song <21cnbao@gmail.com> writes: > > > On Wed, Feb 7, 2024 at 7:18=E2=80=AFAM Chris Li wro= te: > >> > >> Hi Kairui, > >> > >> Sorry replying to your patch V1 late, I will reply on the V2 thread. > >> > >> 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 threa= ds > >> > 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 pa= ge 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 u= sing > >> > the cache flag, and allow only one thread to pin it. Release the pin > >> > after PT unlocked. Racers will simply busy wait since it's a rare > >> > 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 use th= e > >> > 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 easi= ly: > >> > $ 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 regi= on > >> > using a small swap device. Every two threads updates mapped pages on= e by > >> > one in opposite direction trying to create a race, with one dedicate= d > >> > thread keep swapping out the data out using madvise. > >> > > >> > The reproducer created a reproduce rate of about once every 5 minute= s, > >> > 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 synchro= nous 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/swap-st= ress-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. [Barry Son= g] > >> > - Update comments make it cleaner [Huang, Ying] > >> > - Add a function place holder to fix CONFIG_SWAP=3Dn 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. > >> > > >> > 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 sw= p) > >> > 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 proceeding w= ith > >> > + * 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 reus= e. > >> > + */ > >> > + 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_CACHE. > >> 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 preserved, > >> CPU2 does not even know the page fault is not handled, it will resume > >> from the page fault instruction, possibly generate another page fault > >> at the exact same location. That page fault loop will repeat until > >> CPU1 install the new pte on that faulting virtual address and pick 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 above > >> CPU2 page fault looping situation. I consider the page fault looping > >> 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 more > >> 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 can > > miss B. Thus we will trap into an infinite loop. this is even worse. > > > > is it possible to loop for the success of swapcache_prepare(entry) > > instead? > > This doesn't work too. The swap count can increase to > 1 and be put in > swap cache for long time. > > Another possibility is to move swapcache_prepare() after > vma_alloc_folio() to reduce the race window. Reducing the race window seems like a good way. Or maybe we can just add a cpu_relax() so raced swapins will just slow down, and won't loop too much time and so the side effect (counter or power consumption) should be much smaller?