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 3915CC48297 for ; Wed, 7 Feb 2024 02:55:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7549F6B0080; Tue, 6 Feb 2024 21:55:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7040E6B0081; Tue, 6 Feb 2024 21:55:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5CB238D0001; Tue, 6 Feb 2024 21:55:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 46BCE6B0080 for ; Tue, 6 Feb 2024 21:55:28 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A541F1205CC for ; Wed, 7 Feb 2024 02:55:27 +0000 (UTC) X-FDA: 81763491894.15.96F6B8D Received: from mail-vs1-f54.google.com (mail-vs1-f54.google.com [209.85.217.54]) by imf16.hostedemail.com (Postfix) with ESMTP id C2F6F18000B for ; Wed, 7 Feb 2024 02:55:25 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=h3NkN2fe; spf=pass (imf16.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.217.54 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707274525; a=rsa-sha256; cv=none; b=HC82ATkpcWOCqukoXtjibkamFFEpybcTS8NDwVacsIqLOtSvGxDOjuUMbc7LQfWPlrJHbA nn1y2JJ57xZQbfgPEYvwCe2RkXtsqNDDAD6gCHhZ4YnoN9mm4usJ2zr4xaOTYwTgQGPVNi JkhaehGeAHVJu/oLo9DakO/Yp6EAgWo= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=h3NkN2fe; spf=pass (imf16.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.217.54 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707274525; 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=9EFEgmSHGF3F21j6OUWnw+MyyN4MHGfmS2TtGC3X6/k=; b=EixivzFtNN46hkrbDTN//kR9TRcvEQF0zZlYQ+dKWEKQUhGiFzqeMKsSrKG0gvXN24GxgT kJVRLtmm/ec3aJYFZpecs4gD72DodvWV9gxMWZwMCWHkstGFL6Hq9QLjCpeB4sToAwqWaU ZiT2x/E8IIC3aHG6Q11TSLZaPfvoOmc= Received: by mail-vs1-f54.google.com with SMTP id ada2fe7eead31-46cf02b9f7eso7342137.3 for ; Tue, 06 Feb 2024 18:55:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707274525; x=1707879325; 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=9EFEgmSHGF3F21j6OUWnw+MyyN4MHGfmS2TtGC3X6/k=; b=h3NkN2feoPphE+psClqjJ1XfWRbEysbNyaj1vPq7abDhBSqOqHg9HTmR61yTIzlkuk d66kMTk5YpyismmW5bcWUdHnc0BL8szBLuxLi0qZRxqVpx52PWRyM0plNE85OCuJQ+Lh qQzsxptdyBWpgc5bssGtZ94fiC++HMzLBO0BIRrU8DuDl7728rNMs2X5Qz6swPfdY4Qh G/wD8yYvKdk0o2RrsMpGxZDJyPyMsgbWymctmndiMSTXb8iiCOImMt9Rx+NAwOzi8Z2h Hg6qVFo1GqgTIm8OCRrrIyxpIhvNaUTb71enQ/R0paQXf1vVuZVDJDmGAJm0ixD8+wlN tCJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707274525; x=1707879325; 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=9EFEgmSHGF3F21j6OUWnw+MyyN4MHGfmS2TtGC3X6/k=; b=lmoaHxVLVF80hu5EJmCVhAXXNfAo5nycwpTciU0lijkVRTb1Z0PHt7/CLF+CCeUTbh wrsY4mhmysp7G7lZAi21Nanbk5rPkyxEHfGhYmFp3ZNFIZ8Vq7dlFACPZYKQy1J5EYzN uyBWzSZPcNHo4my9yexBEaID1NYaCz/P90fjS5RNgcUpcMMwVS1e5WnwCX8DSYCvwZe7 FsyPhO5iuE4C9a6E3pouMefZLhpAnRXKGGCvhOis/MhXQACsBKS3bUuOm8+XPcPMNlc4 ZbpBl55/HIUJdjaliYDQpgfvmKSmgnNflw6t91ZcSiSm6oEwV4drn8Y0OB9h7S7HAJ77 YF2w== X-Gm-Message-State: AOJu0YzoDa5+V+FTY+sIeeBEhJ+ZZsbLaA+/Bq8dT0ULEe55CPCf1w/S KItnKCB12GzQe5qqnuD/pKqQrXOeYjFMh3c/VS0Y34HKvAK3u57n57LzIlIHqu/7e1lN58XyOur TRSyl6vPx5M/jLOzTrcq3IoxT1Lk= X-Google-Smtp-Source: AGHT+IGZo5wTrDunN7ImoDgNJqNYrHP7y+7wXb4MascXiZVpjGTkAJ47dyLuYXZ34D8CaqU2sW97Sq+jWy55VEmvMnQ= X-Received: by 2002:a05:6102:66b:b0:46d:393b:5e56 with SMTP id z11-20020a056102066b00b0046d393b5e56mr1132704vsf.35.1707274523739; Tue, 06 Feb 2024 18:55:23 -0800 (PST) MIME-Version: 1.0 References: <20240206182559.32264-1-ryncsn@gmail.com> In-Reply-To: From: Barry Song <21cnbao@gmail.com> Date: Wed, 7 Feb 2024 09:52:21 +0800 Message-ID: Subject: Re: [PATCH v2] mm/swap: fix race when skipping swapcache To: Kairui Song Cc: Chris Li , 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-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: C2F6F18000B X-Stat-Signature: 6p9h1dpo3tdeehfpq8c7d3rkerztsmqk X-Rspam-User: X-HE-Tag: 1707274525-222453 X-HE-Meta: U2FsdGVkX1/PtLJp1r/fz/ACsozOPUSV2vymOdo9pE2rw4kHzOYmR48xJefZHiFehOjStBMtFYuW2Cb2kTNGnSOwsMgSWZJD3jF0wuuEQtXGWKl171PMASEBAD5M3Rqmwn7b2xzSlCc1RTpey8iz3um1ZcSWG2UMvyVzSD+Hju+ONoVAIFbbHCqF2u0mrw+m4gIvMky6MdcOGUEEB9lG3KjNJ/o2K/LWsH25wXUpV4QxLQRgGCkuM+cCT2etqDrJ8kruw8gQ7mXnUPwfXkh5crt5Y/wrL8GLdGroGvL5CaQVlg9Uz+EbmsfYkwhtb8V5LApJtzy8cOznza1S9kXbd9XzxsVVA8mdCjyxVLP3u6XBt2Pqs8Z9zuN3QcdlEiV4Gx8Wg9yi3aB68w+al9geIvYZncd5tnLQXaFbMAjOANbZGOpwb1HJabQEnxHcnKU20SPpDBNq53aoeRnDwMpJu6FDyLdFy64C8phAo7BcjVzyAx7VcYSjLZKZNNFZQqJDo4DFv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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 10:21=E2=80=AFAM Kairui Song wrot= e: > > On Wed, Feb 7, 2024 at 10:03=E2=80=AFAM Chris Li wrot= e: > > > > On Tue, Feb 6, 2024 at 4:43=E2=80=AFPM Barry Song <21cnbao@gmail.com> w= rote: > > > > > > On Wed, Feb 7, 2024 at 7:18=E2=80=AFAM Chris Li w= rote: > > > > > > > > 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 th= reads > > > > > 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) beca= use > > > > > 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 ent= ry > > > > > > > > > > > > > > > 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 disc= ard > > > > > 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 entr= y using > > > > > 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 g= ood > > > > > idea after some tests, that will cause racers to fall back to use= the > > > > > swap cache again. Parallel swapin using different methods leads t= o > > > > > 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 e= asily: > > > > > $ 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 lo= ss! > > > > > Round 0: Error on 0x395200, expected 32746, got 32743, 3 data l= oss! > > > > > Round 0: Error on 0x3fd000, expected 32746, got 32737, 9 data l= oss! > > > > > Round 0 Failed, 15 data loss! > > > > > > > > > > This reproducer spawns multiple threads sharing the same memory r= egion > > > > > using a small swap device. Every two threads updates mapped pages= one by > > > > > one in opposite direction trying to create a race, with one dedic= ated > > > > > thread keep swapping out the data out using madvise. > > > > > > > > > > The reproducer created a reproduce rate of about once every 5 min= utes, > > > > > so the race should be totally possible in production. > > > > > > > > > > After this patch, I ran the reproducer for over a few hundred rou= nds > > > > > and no data loss observed. > > > > > > > > > > Performance overhead is minimal, microbenchmark swapin 10G from 3= 2G > > > > > 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 sync= hronous device") > > > > > Reported-by: "Huang, Ying" > > > > > Closes: https://lore.kernel.org/lkml/87bk92gqpx.fsf_-_@yhuang6-de= sk2.ccr.corp.intel.com/ > > > > > Link: https://github.com/ryncsn/emm-test-project/tree/master/swap= -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 sw= ap_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=3Dn built [Seong= Jae 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= 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 *v= mf) > > > > > if (!folio) { > > > > > if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && > > > > > __swap_count(entry) =3D=3D 1) { > > > > > + /* > > > > > + * Prevent parallel swapin from proceedin= g with > > > > > + * the cache flag. Otherwise, another thr= ead may > > > > > + * finish swapin first, free the entry, a= nd swapout > > > > > + * reusing the same entry. It's undetecta= ble as > > > > > + * pte_same() returns true due to entry r= euse. > > > > > + */ > > > > > + if (swapcache_prepare(entry)) > > > > > + goto out; > > > > > + > > > > > > > > I am puzzled by this "goto out". If I understand this correctly, yo= u > > > > have two threads CPU1 and CPU2 racing to set the flag SWAP_HAS_CACH= E. > > > > 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 w= ill > > > > return *unhandled*. Even worse, the "-EEXIST" error is not preserve= d, > > > > CPU2 does not even know the page fault is not handled, it will resu= me > > > > from the page fault instruction, possibly generate another page fau= lt > > > > 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 loopin= g > > > > 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 mor= e > > > > 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. > > > > Yes. You are right, it is worse. Thanks for catching that. That is why > > I say this needs more discussion, I haven't fully thought it through > > :-) > > Hi Chris and Barry, > > Thanks for the comments! > Hi Kairui, > 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. I don't understand :-) if it is goto out, userspace may re-execute the instruction. What is going to happen is a totally new page fault. > > 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. I feel this case is different from other returns. other returns probably have held ptl or page lock before checking entry/pte= , and another thread has likely changed the PTE/swap entry before those locks are released. then it is likely there is no following page fault. but our case is different, we are unconditionally having one or more page faults. I think making the count "right" is important as we highly depend o= n it to debug performance issues. > 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. I agree. > > 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. right. Thanks Barry