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 60833C4828D for ; Wed, 7 Feb 2024 02:21:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C182A6B007D; Tue, 6 Feb 2024 21:21:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BC6E16B0081; Tue, 6 Feb 2024 21:21:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A41946B0082; Tue, 6 Feb 2024 21:21:11 -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 91EA16B007D for ; Tue, 6 Feb 2024 21:21:11 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3194180292 for ; Wed, 7 Feb 2024 02:21:11 +0000 (UTC) X-FDA: 81763405542.06.FF87597 Received: from mail-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) by imf06.hostedemail.com (Postfix) with ESMTP id 406B9180008 for ; Wed, 7 Feb 2024 02:21:08 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aJOSpNYq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.179 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=1707272469; 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=YPnnKt6ujZ5heZEbRxkEnShISOdcME2cSrFXG7+ktlc=; b=r4cv250XEIsHQb3A0I57yoKEkMNoWbrKz00L5bOmYinNMa3WJoJS1dAOWBTAiIoWvib1Nu XjrmM+p9IUqtDBeE0gvJOR0BKtaYjCXvvbzdnqd/kgYZYC9Kl4blY5yeZnUsaYpHnreu4o LWtS9pReBrNOysSWb7Dl8BKNoUYljQM= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aJOSpNYq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.179 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707272469; a=rsa-sha256; cv=none; b=u9snJ2gtuQsG3mc282HzqAYP7aes6e1R/7XBkPzxKvvRIHQmO6qKXvdc6aVnTZ9Ht9O5Jx K/CjQ2Fz5UCVqG5xNA2guEdP9RSiZ7cHWNUevNfn2mbxJNSrwbMQxb8Fl2yMwPV6Ig2IEv UTDCh3pED9KSoxW7+g7UzK0abwIINhA= Received: by mail-lj1-f179.google.com with SMTP id 38308e7fff4ca-2cf206e4d56so1716241fa.3 for ; Tue, 06 Feb 2024 18:21:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707272467; x=1707877267; 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=YPnnKt6ujZ5heZEbRxkEnShISOdcME2cSrFXG7+ktlc=; b=aJOSpNYqV24vXuC40Jsi5nP471WrDuymuOEl8zg6NOgY0G9A/1Vg/7uFDUZmcnxe+n KiAcf2dX8nzzjAgqLsaucDWuLDiJP82mjr+j5+DEW8aA8++4E9bGh5fCUvhxwANLyhSk acw20ypSZDyXm14vEiOqXCuCegp5NJPKNTQNrM3/FCGBCE1NOGhyiyyQTL0c1uEnlqQZ xzhPFupZYdfRqMGSgIqHX9VIEVm3Q5/h0nrag9nbakzp4yvbdJnp6MrWvPhbFgvQKVtp h3e97qfAqcDDmq1AcWt1Lq1j9kAXFKoHEVlfIcAB8IGn6NLq9Ndu4qQnt78Pxm1SLlUr +FpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707272467; x=1707877267; 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=YPnnKt6ujZ5heZEbRxkEnShISOdcME2cSrFXG7+ktlc=; b=R4gTXIva1TlL8rvdnTBXZj2+sHCWW+WqYBhOZB739AGn0acJBpkMktFXAfJrF5dkUk TbfPGJ77WMckGyCfcMT+iTJvNrYGaNfwlnuDqROMnvvGT2aUpiaXNzkMlvvkjKVzyl6g kUMCHMlUc3+4htHWRSI6xpMQNUMAP9vHtobmaz10Iuko+DkC2Rcy9/GwTo3rlji/lnT/ PZEMZsHVSjePD6ctbO0eYn1A8YMN8wX6ZbbIqQP+AZWzuO336zkQl8QZIpbWaTYlElcP I7AKMd1KZIgGzPqvOdUWlfvp9aokRatbP5Ahu8lO+jgJ+UlDnbmMDDA91L9jqNx0kPL+ GCyw== X-Forwarded-Encrypted: i=1; AJvYcCVe/v/o5dbtvUcY+62jK3Qto30KGTO73qK4Z0qypU2EhSiEmAWxHHP8BA+ufEJ9CeasE7Zx52tK7nfVg+ES8fPnU04= X-Gm-Message-State: AOJu0Yz0LYQ2p7xNbQC01Bcejwgr1sA6i137XskOLDpJljqlf8fRyZiE 2atxhMk10e9v7oTpEJU6Hk5wh1FmC5cw9rP4uk08Oyvs6PKj33kBSsuMjlZxmMhrGhmt6AvGgve NGx+McqJ0ELyeoTAylwKy9ZWu7Qw= X-Google-Smtp-Source: AGHT+IEojrZJvywmznFMRhLkxIayeF0rtm+cG/Cwbh7r8sjQ5Wa9tMH+rxy6WrF7Bx72k1I28jfluTdVzaFkObGL5I4= X-Received: by 2002:a05:651c:506:b0:2d0:c73b:3b49 with SMTP id o6-20020a05651c050600b002d0c73b3b49mr413746ljp.5.1707272467044; Tue, 06 Feb 2024 18:21:07 -0800 (PST) MIME-Version: 1.0 References: <20240206182559.32264-1-ryncsn@gmail.com> In-Reply-To: From: Kairui Song Date: Wed, 7 Feb 2024 10:20:49 +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-Rspamd-Queue-Id: 406B9180008 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: i9fnhn5idsp6kds1iobcxdxt87ro455x X-HE-Tag: 1707272468-447691 X-HE-Meta: U2FsdGVkX1+y7qCVfPPa116CraacmRL/tTR8KumR3bWp8JixfiHSMliD6EWca5QLiHbEpq/Kyi5Rjf0NOSCzugUGGZFDwiHVAwXcTibktZaqA+ehZfVUSsEmt1ZLGIxXq7I9gsFtvNBvhO1mWgL3J3r2d5rCJKVEfSMbyNy7FXMoSyAr8+zeb7S5GZP+gL13kE5KmJHFU8150an57flIaKPyI6LwotUVw8hrw3f8wK1ImZ9d9SNgN7BTf937wWikKjYaYhjpz/W/5BXAXIXnoUzdiKE/1ETzJxbW3iOTLLTdfpexqTCkUWjZDPGPjBfoeJMn2k2CSfr3GJBr4tcleWQMQE61beEPyblsmsDD08pnIWMzbOPOB5A1hyGBU0YhkU0hlb1tZu6zP34XGtTuwugK3WlN2V/0iciwxbts+fan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xgj8gjBP9dramZ2XjpBjwUka/lN7EaF3jWgunR325P42ZuLQ== 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:03=E2=80=AFAM Chris Li wrote: > > On Tue, Feb 6, 2024 at 4:43=E2=80=AFPM Barry Song <21cnbao@gmail.com> wro= te: > > > > 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 thre= ads > > > > 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) becaus= e > > > > 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 p= age 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 discar= d > > > > 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 pi= n > > > > 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 goo= d > > > > idea after some tests, that will cause racers to fall back to use t= he > > > > 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 eas= ily: > > > > $ 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 los= s! > > > > Round 0: Error on 0x3fd000, expected 32746, got 32737, 9 data los= s! > > > > Round 0 Failed, 15 data loss! > > > > > > > > This reproducer spawns multiple threads sharing the same memory reg= ion > > > > using a small swap device. Every two threads updates mapped pages o= ne by > > > > one in opposite direction trying to create a race, with one dedicat= ed > > > > thread keep swapping out the data out using madvise. > > > > > > > > The reproducer created a reproduce rate of about once every 5 minut= es, > > > > so the race should be totally possible in production. > > > > > > > > After this patch, I ran the reproducer for over a few hundred round= s > > > > 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 synchr= onous device") > > > > Reported-by: "Huang, Ying" > > > > Closes: https://lore.kernel.org/lkml/87bk92gqpx.fsf_-_@yhuang6-desk= 2.ccr.corp.intel.com/ > > > > Link: https://github.com/ryncsn/emm-test-project/tree/master/swap-s= tress-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 So= ng] > > > > - Update comments make it cleaner [Huang, Ying] > > > > - Add a function place holder to fix CONFIG_SWAP=3Dn built [SeongJa= e Park] > > > > - Update the commit message and summary, refer to SWP_SYNCHRONOUS_I= O 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 s= wp) > > > > 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 = with > > > > + * the cache flag. Otherwise, another threa= d may > > > > + * finish swapin first, free the entry, and= swapout > > > > + * reusing the same entry. It's undetectabl= e as > > > > + * pte_same() returns true due to entry reu= se. > > > > + */ > > > > + 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 wil= l > > > 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. > > 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! 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. 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. 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.