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 39364C4828D for ; Wed, 7 Feb 2024 04:24:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 948EA6B0075; Tue, 6 Feb 2024 23:24:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D17F6B0078; Tue, 6 Feb 2024 23:24:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 724826B007D; Tue, 6 Feb 2024 23:24:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5C70F6B0075 for ; Tue, 6 Feb 2024 23:24:40 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D5FCE1C14E5 for ; Wed, 7 Feb 2024 04:24:39 +0000 (UTC) X-FDA: 81763716678.22.C1FD2F1 Received: from mail-vs1-f53.google.com (mail-vs1-f53.google.com [209.85.217.53]) by imf22.hostedemail.com (Postfix) with ESMTP id 11460C0006 for ; Wed, 7 Feb 2024 04:24:37 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GefZtnQq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.217.53 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707279878; 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=P/T7vVoY4dyYhFaTQIcRmFULAzM3zlFgpAUZpk74xD4=; b=W9Drt9piBAMjywo8FUw6Sd9DULfxqEgPnvumNhCLXzPPvrodx25oVfghgbsd30P3S4StLy IUeQZZIhawvidkmJUsO0GHGMDseVfdFmoVINKtMHf8zKqsE0hAg/7GhUTayceTNFhReLil LkacYfaGPIzSQO4aI+C4c9OJQPkP2D4= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GefZtnQq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.217.53 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707279878; a=rsa-sha256; cv=none; b=wp5k/M7AFklWVaektl9fgyZZDErppSN3EAw/a3uTFrqmqCre0ioC0vHpeqEHsVKv+4D7Il gdYX1UjiQxlfD/XkOvnb85cB+is/GW56rgeQ844cR4bEMv7t91sqA5IEpkyyQ5kt/dHoAg GLSG9e7sDw7FqBFY9l++Cs0qMQfYTos= Received: by mail-vs1-f53.google.com with SMTP id ada2fe7eead31-46cf02b9f7eso24640137.3 for ; Tue, 06 Feb 2024 20:24:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707279877; x=1707884677; 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=P/T7vVoY4dyYhFaTQIcRmFULAzM3zlFgpAUZpk74xD4=; b=GefZtnQqgafFDKvWDOmckXTQyWoLAkE0ZI2rVeztFq7xd1hB9T6coUVD/+en2iXqaC ZDtIguBlNiN+wqiwhcB1tzx42InRNVXcYJPhODtTSYhTi83Fb8axLRIDAenKi/8OV4L6 6g1WmXr7p10gNvnZ3DUfHFQORkvSYSEM014qQy3qCaf2tirlqxzI6/k5YTAbsVbrJrqY AQ18QIXFNws58QQI4Cec8ydWWvLx/N4c889GlgRaYwMHBF2DayABEE6IoPhxVJL3eUUD ormJOdHMrDLYpra0/kIyHwvCEz0zxE1aBtn16KcDGpVPuB8imlCWOjnGEY/BXPTqMsgd aX/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707279877; x=1707884677; 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=P/T7vVoY4dyYhFaTQIcRmFULAzM3zlFgpAUZpk74xD4=; b=vZLXIblVasd/Cpo/pw6ekME5EKblTr1dZuFkiWEPelDVVW23Li7xmlPnYv3E5h/juM J9389j6aNRpH5hrCxXgtJtNMHhKLwApzjNuI5XGtdXQI/QCmBdZtpTkewJDSC1/oWuUh C7Y1P1UY7wJYr48Wrsl8xQNLYxQkF6Y4pqPI3M+d5Z5e5PHD0R7xItZt0hcDGtfVL8J8 dPPIubifYixqPcu/FSeleii7x9wDstGplrDUYcVbJ3dnSyE3j+kgwM1r5Y1DAkGwzX9J 4H8iWOA4mGPYUYCZ1yuxJJc/+3BmF3DOY6nzVz+5yfFRiiuIDuzpp6/sMB4t7R7YtPGW MA0Q== X-Forwarded-Encrypted: i=1; AJvYcCUBLLboaarQiPFrqNAxWIBZVyBSFhMxDYfEAecnIiaaWgKDIG/L9rvQARRKwHjGO6CQa4INi0/JN9ihY4lyjkjRZsI= X-Gm-Message-State: AOJu0YyM2a9B4HXRDLvX9ECFmNLX+MWbqEhii0nHSkClpK8bDDsVQhQr agDejgDlnuYG7Z0E/lgQoGc/L52/Bo08rScgCiQlVB+SdG9+fQKnv1dmhxN9KVVUaoSJn/1Av1a ELCkIeiEg7N4ge1lGwP1+F3BpA9OSpsdUds0T8g== X-Google-Smtp-Source: AGHT+IEWdX498cscZTub6vjfa1EzMDhGIzoxxIvqsYbA9ZU/9pBf5f+mh/eas6G4qz2Uzmameoq4wEv3jML5JGHfEW0= X-Received: by 2002:a05:6102:32c2:b0:46c:f612:9515 with SMTP id o2-20020a05610232c200b0046cf6129515mr1925982vss.15.1707279876997; Tue, 06 Feb 2024 20:24:36 -0800 (PST) MIME-Version: 1.0 References: <20240206182559.32264-1-ryncsn@gmail.com> <87sf25yqj2.fsf@yhuang6-desk2.ccr.corp.intel.com> <874jekzz6e.fsf@yhuang6-desk2.ccr.corp.intel.com> In-Reply-To: <874jekzz6e.fsf@yhuang6-desk2.ccr.corp.intel.com> From: Barry Song <21cnbao@gmail.com> Date: Wed, 7 Feb 2024 17:24:25 +1300 Message-ID: Subject: Re: [PATCH v2] mm/swap: fix race when skipping swapcache To: "Huang, Ying" Cc: Kairui Song , 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: 11460C0006 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: cc655kqf3dnap36re85zn37zk58f6rcz X-HE-Tag: 1707279877-553411 X-HE-Meta: U2FsdGVkX1/NVjNDszZwy5JXOk96XTlrz6t2oB6tcZ9rJMsXGX4wfqX7WHPbcO3/BAfGUrrf7+lWOzBBJ/qsXeR1MCTDXra2UUVJW3Etgrbw/nhajVTKt6v413FS1fjwQm+AMKuiCbOBNNbYVtF+QiTdWlo9fBg3MZyr4hBPPfHeZbLBtSW7oYjhTZ9IFWB18jj/xoPVSuWBolU00ycnTFhdDZG1UCYn/smvy/rHxtSLLGrQb5GSTNV02PywV+MfA4nU0AevyDsdFIsaj5ACRzaIpHis6zf61QLuDqLYMD9O6I0d0hI847UfQhuL3HzvIcq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S3VSm7JL306EJkpqKvkjz2YUzr4ymJRWwn1kk3iAHYPmhDJV6lo4f3TXPgzijQzfZclASwM1vaHHvEY/YFFvToUuB7P8Gh2jZkrHWkrVsQCRV5PuRWypYKz3lZAX1ZDZTxzAh392FW7uWCjJnSaj5e3Cw== 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 5:18=E2=80=AFPM Huang, Ying w= rote: > > Barry Song <21cnbao@gmail.com> writes: > > > On Wed, Feb 7, 2024 at 3:29=E2=80=AFPM Kairui Song w= rote: > >> > >> 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 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 c= an > >> > > miss B. Thus we will trap into an infinite loop. this is even wors= e. > >> > > > >> > > 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 pu= t in > >> > swap cache for long time. > >> > > >> > Another possibility is to move swapcache_prepare() after > >> > vma_alloc_folio() to reduce the race window. > > > > what about we make everything go as it is. I mean, we only need to > > record we have failed on swapcache_prepare, but we don't goto out. > > > > bool swapcache_prepare_failed =3D swapcache_prepare(); > > .... // don't change any code > > > > > > but we only change the last code to set pte from the below > > ptl > > if(pte_same) > > set_pte > > > > to > > > > ptl > > if(pte_same && !swapcache_prepare_failed) > > set_pte > > > > as the chance is close to 0%, the increased count should be very minor. > > IIUC, if (!swapcache_prepare_failed), it will always fail. If so, why > bother wasting CPU cycles? If you return directly, and the first thread > runs quickly enough, you can use the installed PTE directly. > you are right. i thought the actual probability of the race this patch is t= rying to fix is 0%. so we fix the final place of setting pte, we stop setting pte= once we detect one race. but i was wrong because the one who fails on swapcache_prepare could be the one who first gets PTL. > -- > Best Regards, > Huang, Ying > > >> > >> 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? > > Thanks Barry