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 5EA40C48286 for ; Tue, 6 Feb 2024 07:39:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 79DB16B006E; Tue, 6 Feb 2024 02:39:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 74D966B0071; Tue, 6 Feb 2024 02:39:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 615036B0072; Tue, 6 Feb 2024 02:39:15 -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 4CA756B006E for ; Tue, 6 Feb 2024 02:39:15 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1F5A51C1509 for ; Tue, 6 Feb 2024 07:39:15 +0000 (UTC) X-FDA: 81760578270.16.5CA0905 Received: from mail-lj1-f172.google.com (mail-lj1-f172.google.com [209.85.208.172]) by imf22.hostedemail.com (Postfix) with ESMTP id 46D3BC000A for ; Tue, 6 Feb 2024 07:39:11 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Yegguu1Q; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.172 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=1707205152; 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=FEm7mOJ011S0VyrqJbaw++p65z5dZn5VqwG07p5sWdU=; b=F22DCtt0NKfyhK5ayl/yfSlDZM+pztFX9kJR3sH1pAdiq/tvxL3Pm5TFQYLKjc1M0a6Lk6 C6x/CppRcxGMnPJz0b8SmI04cBSgrVes+XV5Ok4g/VTdOgbSF+vlKLNTOl8S72kN4Yvk5R ntojmkHYpI1Y2RxlmfgjPe5kzB6EXno= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Yegguu1Q; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.208.172 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707205152; a=rsa-sha256; cv=none; b=7IPxdMoBUw4s6ji4PC+MUa/GfRvf4d96J40iOIDVnrYVNzh7IaiS85b96Dj3ArFefpxAe/ h3K+UANb2Z3gwDmfdqiNdysMTocJ70FfMk2cJroYAuAL1XSyxQqTdcY+fdoYciryQ53mcN GS2sSqcbTtmWgFp5EfDN1oi7NG2UrWQ= Received: by mail-lj1-f172.google.com with SMTP id 38308e7fff4ca-2d0512f6e32so62769521fa.1 for ; Mon, 05 Feb 2024 23:39:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707205150; x=1707809950; 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=FEm7mOJ011S0VyrqJbaw++p65z5dZn5VqwG07p5sWdU=; b=Yegguu1Q1o925UBkFfZaqrzhYyX2KXansPHvkXqHya/8o3bw/K0jMrlFFIBflhvxUG lqSHxW2tV/nT8L3yCMTXIQzFRY9Q5MIwhZKyJloXB8/e7DtoLiN8DwwR0Hs3afdKWJ06 NAwzUE0csAfhEWpK+vNhRmpPsR5OU8KeYRuCGdPsgcmZU2j+0Z9xBJI/Y7aunkuVuT8U BAWQKi+SUw0scZ4O7Rf6jJb+QDTOelnPSdkNco3Ck2nMlax2P7AZTQBzg4RPPn86CkrV ieMtDCuAthU49GdFfaJ0c9n3qCD+Sr2inpsQJswmDR1w2Dn3vPKtABpUX6C25KYVCUt9 SpKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707205150; x=1707809950; 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=FEm7mOJ011S0VyrqJbaw++p65z5dZn5VqwG07p5sWdU=; b=f64Gm0rlKHGh3wVH5Vngsyp8LejvUuWlyiYHk5BOnE7qtMnL8kaeCjOWc8orS3hnh9 mThUDWTTRJzFnY5Vr2uigjv4d44Vo+Bq3xjliaa6KSNiXGz3ih2JoQQPp/7kiyBpUt0w XVbTCOJWJllqUbRY+oapZZ0kIUu/NcrzZaAAgKMYng4gZxdAltYN6RxblB/U2tUFmtYR fNbvJXClnlCMEWSj70MSmbWtpm1skV7HEyk1peUu3+IQIN7y/SenjyrFsO/CFLoY05K5 JAFHwa2/lHCb08DCEUCA7lK9zu7/Nc+TUbruHZywbfrBBMqMCW+GmX+BvPY7OV4HaFWq Fl+A== X-Gm-Message-State: AOJu0YzkdPsB4D6kmt/j1hfEiDPsA94aCv4KBrP14JcXxEBDMvhDgOck hMU/f+495gsn9utEzlQGWn3w8GIhdiyuD4V9Kh6F1JeVrzjBMJrnake/GARLMYKuMJmy9QzBtT+ 2AFRGIi746dma9Fq0fb72/r8TxHw= X-Google-Smtp-Source: AGHT+IHzLBDl5xvhebPGq0XGAgn7QlfFsf14RlJNyjGTura3QNWPW72aHbwGFBgkdYfHOouaEaBHb4FASTpHpHSaKUY= X-Received: by 2002:a2e:be23:0:b0:2d0:bf7f:a964 with SMTP id z35-20020a2ebe23000000b002d0bf7fa964mr503930ljq.32.1707205150041; Mon, 05 Feb 2024 23:39:10 -0800 (PST) MIME-Version: 1.0 References: <20240205110959.4021-1-ryncsn@gmail.com> In-Reply-To: From: Kairui Song Date: Tue, 6 Feb 2024 15:38:51 +0800 Message-ID: Subject: Re: [PATCH] mm/swap: fix race condition in direct swapin path To: Yu Zhao Cc: linux-mm@kvack.org, Andrew Morton , "Huang, Ying" , Chris Li , Minchan Kim , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Yosry Ahmed , David Hildenbrand , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 46D3BC000A X-Stat-Signature: fpazhpjjifoghynf9j98ipzbf5yfa5uj X-HE-Tag: 1707205151-186864 X-HE-Meta: U2FsdGVkX18iEutwtAZal35bKZYtYW59uPmdHrmfbXgs5Ou8PwO7YcclfY6VVWVZxMnwM/UdhAWjEjiVPXrSy34wvsqk3B1gX1TiTM9O7AwuPfLCOZ6gZgJHGOBXM0BVbrEG0Fl1gHgZSjT2k632+rHyAL5vjrgn+kYOqG4M4Ai1+rhLkFVfRvdSgNhpgLsp/3TAN3LmghUPJhyXsL+pS83+XubowbR7LP6vanT9pBY2HLHju0E6uMXQZ2brREgZ4xQDLFXOQjfDd8m2FwALz5N7Zu5ayzS3YsQH5QbmkJdeGDUE3OY4QsRQMgTfkZ2/smKPCTR/fZbr/iD/oPt4C5yL7xcfkm5yp1ite2IwqovmB7bezlS6kiTtcS7BunOnBuZJ4BH1vIsyY6ZF22ZurrqzcAV8al8OjdHxRR5t2ZQ+m/5lUbywsRDfDDejyBbF74SHE2Nfyk58EczdXKZO27d6cp3NQQjAuWPxXiBu6QDDh4m3yo/LqbcCSfRlhcEs36HvEWqbpbqb198orgQozX3w4NjupN/re9SqAmZxFFivti3suQgUiVKj+XBpsZe4towRepujukvlWN1ax4LnGlqUtCE8rq1x/SNGVUdRcbrWgEMYsT4UKFOSaWpiDzb0bNRjpsh+m2GcaoG9xgMw/er/WOkYUKIqstnYqQCdJG/7uxVMMjG+tws59j67txsPl5TVb5SYqBKvnFmLOuF3B4SqwXeHzQGAg2jMYY9R9qWrTW4AFqXZhMzFPYbl5XW59xb4lxJMlqNOfP8wgn7FD4sDePlTu+gLwKxLVNe9cqpyV0kJmqKHcmfdHnQAAlFYDJcOI5nH6HuQH5+o9PjQFyEAgNSfFK9lMhuXs72CmzIHXpeec9Zy+YBjxMxsq9Bp0F7JbndMouU+q8k10pOizDfvUbvlwtp7X0auHP0EtH7QmvoxL+/9kWYgfGPlwgMJYSS7GFa4YaoGTqom+Kr /jbw/qvK gwyq9twInr5rUZiRVrpnwiWSRlMzowRDQQbvnR5lV0t+mbmyOVWyURONQALvKupej/GiRyf6kdwXlAffrjTtffcUohvhrQk1i/yLgsEEwKU1HkE5szewL2jEDYnvPbk886bQcwConV0sp3I/m9H/5GcMu9bfZ9S+kQQV8zOPex9/zQcAuoWFWaoxqJddaGmy74gZVJjj1oWYQJS1nIACEojKJEIEoPDvVCrIyruDozLRQUsiQXdVy+SlDcc1hFYDYniVl3qHVKfwox+ncQxzQUUcVy/WoiglSC4wlqhgc7xZKf4hADOZwDH1bKBTsEY7Ll2RHu/pzJJQbm7HZciLOHf3ivPVH0m8h7cjTr4teJgx4vOhjLBKdGb/+bRGGxvhOo7UB531VnnWRN4GGNr9EneBIaU3WdwamxFgW5ZE41wsHJ0zQwNBurigh+MEkxd+LbnT0c1ZoodUtob3gI0mGmr+5n5KUm8lsYacRI1LrIG784qvrS7DP7l0hNV2cr6OBufkhtgaQYt3j7X/MyRbRCfN328sqpI5FgpHr 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 Tue, Feb 6, 2024 at 2:03=E2=80=AFPM Yu Zhao wrote: > > On Mon, Feb 5, 2024 at 4:10=E2=80=AFAM Kairui Song wro= te: > > > > From: Kairui Song > > > > In the direct swapin path, when two or more threads swapin the same ent= ry > > There is no other places referring to that path as "direct" swapin. > > I'd rephrase it as: "When skipping swapcache for SWP_SYNCHRONOUS_IO, > ...", and similarly for the subject: "mm: fix race when skipping > swapcache". Good suggestion. > > > at the same time, they get different pages (A, B) because swap cache is > > skipped. 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 modify and swap-out the page again, using the > > same entry. It break the pte_same check because PTE value is unchanged, > > causing ABA problem. Then thread (T0) will then install the stalled pag= e > > (A) into the PTE so new data in page (B) is lost, one possible callstac= k > > is like this: > > > > CPU0 CPU1 > > ---- ---- > > do_swap_page() do_swap_page() with same entry > > > > > > swap_readpage() <- read to page A swap_readpage() <- read to page B > > > > ... set_pte_at() > > swap_free() <- Now the entry is fre= ed. > > > > > > 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! > > > > 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 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 the > > cached swapin path, two swapin path being used at the same time > > 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 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] > > Signed-off-by: Kairui Song > > Cc: stable@vger.kernel.org > > Acked-by: Yu Zhao Thanks!