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 050C2C4829E for ; Sun, 18 Feb 2024 09:20:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6FF038D0002; Sun, 18 Feb 2024 04:20:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6AF4A8D0001; Sun, 18 Feb 2024 04:20:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 576E28D0002; Sun, 18 Feb 2024 04:20:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4071E8D0001 for ; Sun, 18 Feb 2024 04:20:47 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 05FDB1C0051 for ; Sun, 18 Feb 2024 09:20:47 +0000 (UTC) X-FDA: 81804379734.14.351D871 Received: from mail-lj1-f175.google.com (mail-lj1-f175.google.com [209.85.208.175]) by imf29.hostedemail.com (Postfix) with ESMTP id 2231A120009 for ; Sun, 18 Feb 2024 09:20:44 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CGNupdid; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.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=1708248045; 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=dWKsutbRGzYVGE/Ng+6vm7PeOui1iP0vfYSbJEtQMco=; b=qJr0tsLSU4v3a8usBt8W/0k8vy3Z7GzDIHCaq7WgtZ8hG1+y01i7uadIauqV7rROlie6Ji VdqHSUavlkfjNRrZXG8ngbg+PQ8yuKq0tRbJjtDbiT6URwlR9WiUBEzkLjcmWbP3GkGqOV 6SqR4nw8UUBrwcY8LMTCjp5vpdqeMo0= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CGNupdid; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.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=1708248045; a=rsa-sha256; cv=none; b=VavDblr87SUsG0ALuuZxxivfJ/BLpT8usJWz9yL4VOQoJJUh2tR6OcCNAwaoL9iIZ7gwNH t/A+p4D3/EROUMFv+xH6q9L/DtjnyxYHz/CEYbrk9j1zihMn3EShAMh/ae+fZX1Z4Xw5ma JacCOgpwHXd3gVRPcojnlQMad1+9nGQ= Received: by mail-lj1-f175.google.com with SMTP id 38308e7fff4ca-2d220ad10b3so18510331fa.1 for ; Sun, 18 Feb 2024 01:20:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708248043; x=1708852843; 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=dWKsutbRGzYVGE/Ng+6vm7PeOui1iP0vfYSbJEtQMco=; b=CGNupdidlGRbLvrNKmgVkRfq+SEKuGnP53Aiq9IEROt1ddLdTyAZF2giPgtqzYvzla u3/eA+4/wo6dZPvVyJ9oDqAlAsYa29f+2uvvwC75diQ+Jvc7QgF1hehx3ck1a+gtWwKM j19bZ0c+ce9uWJBUJiV9toIEQPqlMWDJ7T0DI3PxdMEerzB/Nms8D1pMVrjGFiD79vJJ bSqZgZkyWtn2EbxmldlL8CcCeobK/KSNgUuUogrdJXugRyxHitDt+gqPnxelH6nczqF+ 3PqAT9DSnWxzsOOM8cYVJOy/0gLyptJku8lZup0C4h5WY2ZgJ3m3Sy6LkFFps3J2gE/7 Ar7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708248043; x=1708852843; 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=dWKsutbRGzYVGE/Ng+6vm7PeOui1iP0vfYSbJEtQMco=; b=WR8SjKPyM7ennekMbvrxne9HY5iP+ff/jZqikXVZeAW4LLx4tb5cPnDzyXDzMn4IUL l9tecGkpFfliGqn6lcjEeJg3QOATLbT7Mlzn3okScsK22R1zabBmQ8OwC1uM3ssImGSH nbyLDdxRYHvTOC+MAgNjUA82GvollsSOkTBI9zu+tolLPE5vGaLpgIAgpwi5zLSV9kdm P02R+SxYZk4CSa10SbN6+ytLSooyUol2x8/VGAIHu97ZAXpzBdqkie85zxpp+w0biQIa lJz7N2uyQEGDgTKk9K/t+tLuNlwaqXRrC/0cO4Bjn6J4gU8WojbCCiaiT+q8gELQGsaU 148w== X-Gm-Message-State: AOJu0Yyd0GY+QdlSxfF1r4bYBcsJUckpHmpUopI5b8Mnh1HQbgj6R403 UrCUuBHvV5cVNqm/JSI2bJAbrCcnFJ2/q7r+7Poj7JcOQ4k/6spbblNjEZooZUib2A8OhH4CxYP otgbX8folkDNzkpN+OUJECQspntc= X-Google-Smtp-Source: AGHT+IGRlGEVWsMOZY57fD0eUIGiF047sjI+EeuDIPkLDq4iRf/tNWMpgoWUnVhtIRCxF6GS9jnfMj+VRtKmnM3lnLM= X-Received: by 2002:a2e:9bcd:0:b0:2d2:3767:ece6 with SMTP id w13-20020a2e9bcd000000b002d23767ece6mr449001ljj.14.1708248043042; Sun, 18 Feb 2024 01:20:43 -0800 (PST) MIME-Version: 1.0 References: <20240216095105.14502-1-ryncsn@gmail.com> <87wmr2rx4a.fsf@yhuang6-desk2.ccr.corp.intel.com> In-Reply-To: <87wmr2rx4a.fsf@yhuang6-desk2.ccr.corp.intel.com> From: Kairui Song Date: Sun, 18 Feb 2024 17:20:25 +0800 Message-ID: Subject: Re: [PATCH v3] mm/swap: fix race when skipping swapcache To: "Huang, Ying" Cc: linux-mm@kvack.org, Andrew Morton , Chris Li , 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: 2231A120009 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: zxn1aieni9mrpbo6uiwy1mk1hms7wtzp X-HE-Tag: 1708248044-197374 X-HE-Meta: U2FsdGVkX19Yeata/mCle+AaMm6d1owaVaI1duD3rFNO+QJ+lrFSnUDBdtEywQqixqbts2Z3cE18sU6ZLtqdCyxJW8CDhOTTy9o3GgiT6VZx9CKTMVI3Dz5jPG3ERBnr3ODMhCBLR2gWhXXTPUUH4xvATzfb1m0BpS3NAuF2k0i23OCdN44UIPgxLg1/M59P1eb4sRByrgADHPQhkDVvFSkjT76a0/F0eyns+4MbW5uqfCNvWf2//HA0n7sMw5BKO2nLXjiuBJPnoU64hPmuXVAxsQIguBTOpIFlmdb4FLNlB0KdT54U4tC9xgk9Bwhr+aA6E5Z408QhYx+V1DLzHidgnyKT2qwZrZqngoVevOn0D8XjHcE9Pm4GnxR1dENhAV1O7BFQwXpP+VRbfJ4wOCISVQhTtmY/FVUtIzZgjNRsANecPJV+o5GaXkgzPGPyCW/DK7w8lY0BFi8Npk30RuSoNSFDGIP4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tNjrkz+Hh 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 Sun, Feb 18, 2024 at 4:34=E2=80=AFPM Huang, Ying = wrote: > > Kairui Song writes: > > > 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 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 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 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 usin= g > > the cache flag, and allow only one thread to pin it. Release the pin > > after PT unlocked. Racers will simply wait since it's a rare and very > > short event. A schedule() call is added to avoid wasting too much CPU > > or adding too much noise to perf statistics > > > > 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 the > > swap cache again. Parallel swapin using different methods leads to > > a much more complex scenario. > > The swap entry may be put in swap cache by some parallel code path > anyway. So, we always need to consider that when reasoning the code. > > > 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 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 pages one b= y > > one in opposite direction trying to create a race, with one dedicated > > thread keep swapping out the data out using madvise. > > > > The reproducer created a reproduce rate of about once every 5 minutes, > > 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 synchronou= s device") > > Link: https://github.com/ryncsn/emm-test-project/tree/master/swap-stres= s-race [1] > > Reported-by: "Huang, Ying" > > Closes: https://lore.kernel.org/lkml/87bk92gqpx.fsf_-_@yhuang6-desk2.cc= r.corp.intel.com/ > > Signed-off-by: Kairui Song > > Cc: stable@vger.kernel.org > > > > --- > > Update from V2: > > - Add a schedule() if raced to prevent repeated page faults wasting CPU > > and add noise to perf statistics. > > - Use a bool to state the special case instead of reusing existing > > variables fixing error handling [Minchan Kim]. > > > > V2: https://lore.kernel.org/all/20240206182559.32264-1-ryncsn@gmail.com= / > > > > Update from V1: > > - Add some words on ZRAM case, it will discard swap content on swap_fre= e 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 [SeongJae Pa= rk] > > - Update the commit message and summary, refer to SWP_SYNCHRONOUS_IO in= stead of "direct swapin path" [Yu Zhao] > > - Update commit message. > > - Collect Review and Acks. > > > > V1: https://lore.kernel.org/all/20240205110959.4021-1-ryncsn@gmail.com/ > > > > include/linux/swap.h | 5 +++++ > > mm/memory.c | 20 ++++++++++++++++++++ > > mm/swap.h | 5 +++++ > > mm/swapfile.c | 13 +++++++++++++ > > 4 files changed, 43 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..7059230d0a54 100644 > > --- a/mm/memory.c > > +++ b/mm/memory.c > > @@ -3799,6 +3799,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > > struct page *page; > > struct swap_info_struct *si =3D NULL; > > rmap_t rmap_flags =3D RMAP_NONE; > > + bool need_clear_cache =3D false; > > bool exclusive =3D false; > > swp_entry_t entry; > > pte_t pte; > > @@ -3867,6 +3868,20 @@ 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 thread may > > + * finish swapin first, free the entry, and swapo= ut > > + * reusing the same entry. It's undetectable as > > + * pte_same() returns true due to entry reuse. > > + */ > > + if (swapcache_prepare(entry)) { > > + /* Relax a bit to prevent rapid repeated = page faults */ > > + schedule(); > > The current task may be chosen in schedule(). So, I think that we > should use cond_resched() here. > I think if we are worried about current task got chosen again we can use schedule_timeout_uninterruptible(1) here. Isn't cond_resched still __schedule() and and it can even get omitted, so it should be "weaker" IIUC.