From patchwork Tue Feb 6 18:25:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13547690 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 B0D22C48297 for ; Tue, 6 Feb 2024 18:28:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3925B6B0075; Tue, 6 Feb 2024 13:28:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3689B6B0078; Tue, 6 Feb 2024 13:28:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2307E6B007D; Tue, 6 Feb 2024 13:28:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 13D5D6B0075 for ; Tue, 6 Feb 2024 13:28:46 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8681A80118 for ; Tue, 6 Feb 2024 18:28:45 +0000 (UTC) X-FDA: 81762215010.23.2D818DB Received: from mail-oo1-f46.google.com (mail-oo1-f46.google.com [209.85.161.46]) by imf30.hostedemail.com (Postfix) with ESMTP id B0F6B80017 for ; Tue, 6 Feb 2024 18:28:42 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hqZmcfYq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.161.46 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=1707244122; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=ri33dEDMSF+uLhQGu0qPBpIz5K4xYUeshxoTcvF1uRc=; b=yXNdY/btHV5Qk82rH5gThdCf9om7TMDpcl/Q6S3mESQlLKU2CLW+goVlAYD3CBJEUYLTTm Kr1IZQSI4mM3jaFK/r/+RxKL+tYKghr1ie/J458m1uSzhaiexdiv2Gz2M18Nd+WHNcTMCj CmBmjxo4d3oArnUNrlesvgLiI93HKbQ= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hqZmcfYq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.161.46 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707244122; a=rsa-sha256; cv=none; b=AsSGyysur+W3KEuf+ONYmf8e039J5NuPF3EEGpdZUOsk0hzfQk1akaRJ58wFyzJfKX6FCM T/tRXLbfnNQ0AoPO+UaKazz3ddBq28F0Q4sRqg8jOXPskSWl9fI172odc6Jp480mfJFNbF 225krTKrHmGrIIlNi9Yb0ORyoCPnAWs= Received: by mail-oo1-f46.google.com with SMTP id 006d021491bc7-59883168a83so3001851eaf.2 for ; Tue, 06 Feb 2024 10:28:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707244120; x=1707848920; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=ri33dEDMSF+uLhQGu0qPBpIz5K4xYUeshxoTcvF1uRc=; b=hqZmcfYqy3HugGvycL9L74+capLNDQB8aZHfRRuDQvJsn4PqAF2avh5mcQQtFN9qBZ qzCK3oTHgzI8yar24i/vhNylD0PsQ5Y7dAGDlPbZo6YUjQm5qjTTn36Z7jeeD0OEURDL NArw9hma75C85TiJINGpewz1Q67t2nssiQm+YlMwJhGxc/zHAY6kI6q7sNpeTACWJRAf zYE7r7Ez1mjrD5HcRz9kMduoTOie3fGWHLD6SA5/R+0q5gqgVJ8nNrcTyQius+zDeGWV uaimJ/3ngWf3fcGA4EpCc8Eb/AW9PEgkXRHu1NRefkWuL2gJyi4fvyT3sC8jZswBR1nU qIaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707244120; x=1707848920; h=content-transfer-encoding:mime-version:reply-to:message-id:date :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ri33dEDMSF+uLhQGu0qPBpIz5K4xYUeshxoTcvF1uRc=; b=KE5FMtEoqbSqohYxw5HL8FGdPWJfc3gmCnA9FqmqLRQXu8b04qw+5yAWd+tUAdPOsd saEYvKwmILojQnkQmKMqRIOCXOb9SUV9sP2Sk68vf5gAqwKXNPL+l1WYn6xSf/p1eybI 5YO3sCbl3v0WmhSrEygT/fItL2EoKJc3V4zqifeB9sYb4XcpfbfmwUGusflG/ALqT5yc 01BbnyAjnEOPMPQ4anQ1DCcYa2zcxhCR24xPbG/tDCTeGXRGCvmm6ZhbJrv8A40aLYwA yi9iKU3RzPFyXORtftRAcrjK71Dar6mKtJrzoEsS0PQMxLnM7lZB/5tnjvnZ3mTo4K01 cEQA== X-Gm-Message-State: AOJu0YyhZQFflsWtUpJ7wEni8cD/1F0qyuGPvrqumdMI//SK6hQxS3A+ BucvvaGrbhaSiQwR3ybPy0+OWeKnWeKZxZYLwP6QTHevz9iC4vKBqJruc47OgX0ikg== X-Google-Smtp-Source: AGHT+IHLfQO1mq7zCZus8p7hf70KIAOyXJuPclrtTGt9F4DIPIbnjtaI+hylLkx1qbCPL4009cfXrQ== X-Received: by 2002:a05:6358:2490:b0:176:56b9:d4e1 with SMTP id m16-20020a056358249000b0017656b9d4e1mr322603rwc.23.1707244120454; Tue, 06 Feb 2024 10:28:40 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVtbiL616DbzpVKTfBYtVTxwY4aYLBihKCF6hKfBFX++CaF/KM9zQ+Dj4p6+/D1Zh+tP8ejZa7KmFkCAJQ6S8IsphXfNGQD2PriTPN9UgdxOqrl2uRrVck6ZJ6Eg4YWkz+lkRvOMlPnNqcD+S+N47qrYQx1ZNC+irwh+Uh6kzOEgfWGvgz/QPAdv34ePV7JuFjHiCsmzYJO8em2DqDq6uRt6s/8PeWLgkjZbdYlm/jzK+0mDxGOfL4z4+bfMQSFLSPDmsUJo3K3FiaycIWsDCOBvbr9Edva8Y10ufQIMKbWjVUrparXKzDG+y6OotQOWVeWYEPisCV/mkKnN/GDUIi2jUzPca1g9ikCydDqK6LaEsHt+NKW/C/yAKcy2LSPuXvhxIghicuYxlk4jDGkO/+odTI2UqHI1iXoCYDSvo8QTpi+miu+7e+qcfUG4i6u9JL0GpLpe5PujBPJFL6gOXhBAh/9 Received: from KASONG-MB2.tencent.com ([1.203.117.232]) by smtp.gmail.com with ESMTPSA id g20-20020a635214000000b005d8b89bbf20sm2446839pgb.63.2024.02.06.10.28.35 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 06 Feb 2024 10:28:39 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , "Huang, Ying" , 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, Kairui Song Subject: [PATCH v2] mm/swap: fix race when skipping swapcache Date: Wed, 7 Feb 2024 02:25:59 +0800 Message-ID: <20240206182559.32264-1-ryncsn@gmail.com> X-Mailer: git-send-email 2.43.0 Reply-To: Kairui Song MIME-Version: 1.0 X-Rspam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WfkhF3Z 3sCfjg6D JyB9pYk2K4K4FOyymiJekm5ewiqJE4Getw/oJm/PHWtrnxbzVoRPF9otEzvP4p/FVWhHtHsyaRIS8QB5fFnt5ioU36YGOf7DAy7ZrwHu/i1ewk/VEtVCtnb84S9y9kh17sNsJiJLQr6T/2WEJJmRM7L/N/g4ANv7hvK76i1rLhwdD2aPYQn+gA9l+J/DsU1ZAgfX4UG7Q+xlZ4aIgOOUQ7xv3hIXTo7USk2J5NuBSq3qkPKg0f/7l5CG9XqNMqCHv1ecZr3byraxuC2oIe6MXpHVhRhKMlsLOgGrSzIVkJ+sxhFeekgKuy/4tjRVDmKBj8LdWOc/CWOfZMGyU4eFn49hadw8SENJlSWMXDEVtuWMYeMvt3J3zKgix9u685dR+XACl0xII1OWuP++pfhZRQvkLwNUdoAYS90kCS7GM0p59V54pAuGuz7NPzLfkAUmgA38+LKeSYn97khfOabPShoroWoShXdV9NucaviG/ocT9hTZ8IZqa6yVtK4gXU3nUrzZ/3a3hJSwhwleoEHW3LEvzGjNwGkON09lMP1tD9Q+cZjqnD2pFaCa33mUoDtgeHhBsFo5KDV8gbrtQh+uL4J37PyYCT9hlgyGQ2vUY7L1djUye8O7SsEmNJmwXxtgvMVOW4s8yHWIAwxoqfrlfErt0RQX7w8rnM2F2dqVB8b97QZQkBLFStIajZr4Zh0FIe77Ho2K+OcvXlT5D4V+ran2DMeeKjfMMDNzOOJz2e5NkQ99HzS2Bu16A+6rfRYl5HxlEJctyjzvkIlWVpduHnELAsQ== 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: 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 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 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. 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 by 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 synchronous 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/swap-stress-race [1] Signed-off-by: Kairui Song Reviewed-by: "Huang, Ying" Acked-by: Yu Zhao Acked-by: David? Reported-by: "Huang, Ying" Signed-off-by: Kairui Song Reviewed-by: "Huang, Ying" Acked-by: Yu Zhao Signed-off-by: Andrew Morton --- 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 Song] - Update comments make it cleaner [Huang, Ying] - Add a function place holder to fix CONFIG_SWAP=n built [SeongJae 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 *vmf) if (!folio) { if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && __swap_count(entry) == 1) { + /* + * Prevent parallel swapin from proceeding with + * the cache flag. Otherwise, another thread may + * finish swapin first, free the entry, and swapout + * reusing the same entry. It's undetectable as + * pte_same() returns true due to entry reuse. + */ + if (swapcache_prepare(entry)) + goto out; + /* skip swapcache */ folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, vmf->address, false); @@ -4116,6 +4126,9 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) unlock: if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl); + /* Clear the swap cache pin for direct swapin after PTL unlock */ + if (folio && !swapcache) + swapcache_clear(si, entry); out: if (si) put_swap_device(si); @@ -4124,6 +4137,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl); out_page: + if (!swapcache) + swapcache_clear(si, entry); folio_unlock(folio); out_release: folio_put(folio); diff --git a/mm/swap.h b/mm/swap.h index 758c46ca671e..fc2f6ade7f80 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -41,6 +41,7 @@ void __delete_from_swap_cache(struct folio *folio, void delete_from_swap_cache(struct folio *folio); void clear_shadow_from_swap_cache(int type, unsigned long begin, unsigned long end); +void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry); struct folio *swap_cache_get_folio(swp_entry_t entry, struct vm_area_struct *vma, unsigned long addr); struct folio *filemap_get_incore_folio(struct address_space *mapping, @@ -97,6 +98,10 @@ static inline int swap_writepage(struct page *p, struct writeback_control *wbc) return 0; } +static inline void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry) +{ +} + static inline struct folio *swap_cache_get_folio(swp_entry_t entry, struct vm_area_struct *vma, unsigned long addr) { diff --git a/mm/swapfile.c b/mm/swapfile.c index 556ff7347d5f..746aa9da5302 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3365,6 +3365,19 @@ int swapcache_prepare(swp_entry_t entry) return __swap_duplicate(entry, SWAP_HAS_CACHE); } +void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry) +{ + struct swap_cluster_info *ci; + unsigned long offset = swp_offset(entry); + unsigned char usage; + + ci = lock_cluster_or_swap_info(si, offset); + usage = __swap_entry_free_locked(si, offset, SWAP_HAS_CACHE); + unlock_cluster_or_swap_info(si, ci); + if (!usage) + free_swap_slot(entry); +} + struct swap_info_struct *swp_swap_info(swp_entry_t entry) { return swap_type_to_swap_info(swp_type(entry));