From patchwork Tue Dec 3 15:31:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13892625 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 99C5EE69EAF for ; Tue, 3 Dec 2024 15:33:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CABA76B009C; Tue, 3 Dec 2024 10:33:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BBCF56B009B; Tue, 3 Dec 2024 10:33:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A0E006B009C; Tue, 3 Dec 2024 10:33:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7FFC06B009A for ; Tue, 3 Dec 2024 10:33:01 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 04135C0226 for ; Tue, 3 Dec 2024 15:33:00 +0000 (UTC) X-FDA: 82854040542.22.18A6BCF Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) by imf17.hostedemail.com (Postfix) with ESMTP id A887440028 for ; Tue, 3 Dec 2024 15:32:48 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=WD6XNqMG; dmarc=none; spf=pass (imf17.hostedemail.com: domain of axboe@kernel.dk designates 209.85.167.174 as permitted sender) smtp.mailfrom=axboe@kernel.dk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733239966; a=rsa-sha256; cv=none; b=cyZVKO7vVF1Ioo+PuYZ2sN4Wu7oCS6Ejr81QGJM9ZKYmB8LjyMsaBT8Cd7wwFcObrt/gS0 49adj26L26nIkKIyhAj56OkE4WYWTgnM3qM8K/GLq6yKoeNxVTxv07b9XI90ZOp8U5CEJM c2ruvUXju4pVlUyIEaSeA05fM7FtKpc= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=WD6XNqMG; dmarc=none; spf=pass (imf17.hostedemail.com: domain of axboe@kernel.dk designates 209.85.167.174 as permitted sender) smtp.mailfrom=axboe@kernel.dk ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733239966; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=854g8zvMX205jFWe5CwR3kRcBo1TfCPiGvzDJnv2dMk=; b=icK0L79alMIZi0St1bhDvFZTqTbw/FVZF6/yOGOoG30FNGktZmVgKNKtKWwdoMBng5q6Vq /9+4eCwds0AjlbklDzmCAxBLGi4Y1imkVUSggPNv0Q21AIa/a1vYqcMpUBltCDYoH9wZbG ecmDUrzFQoF43uMTXG1/ON2jN+TUcks= Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3ea60f074c3so3240297b6e.1 for ; Tue, 03 Dec 2024 07:32:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1733239977; x=1733844777; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=854g8zvMX205jFWe5CwR3kRcBo1TfCPiGvzDJnv2dMk=; b=WD6XNqMGYdso5u3YPAHDxxwnUjwwB5bYpobh2Zdy/AQ5YvWMJXZqHN/iEuOw071xgp pYE9njA6g+noqsYoD4X/Yu0Zq8o7/z9b3NsGYX2cNpVqTpjKL/Ql2Da1OyrXE5x7E6Yt JXQw2r1r1GgOv1xVtQiEyipxoS6tX5HenAaRThr/P3pKr5q5l4ZRXsJRM8LloFKmTkyt 5nGKRbUUiRnMMF+S+Op4alSiIrcVUQ1XPxqn3ZQcrB6J7PYo4spEICdSymlsNbiqR3WP omxKQ8xdvSmpyuTfCnKoeP2pOLOgUZ7GtZDZW5u6J50RJFRpoYC2GKEZGKX9IEfBq8Fa J3zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733239977; x=1733844777; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=854g8zvMX205jFWe5CwR3kRcBo1TfCPiGvzDJnv2dMk=; b=oNrOPfIbSGZHqjOiIO1O0vQ+Agd+/kGoxkQPpyiuuIYhEcukhHv5bt5W9QRA9sN81d mE/0ifz02GfF8QlsHYDKB/uers2Uv2r1ffViUrNNXJ6mN+PZHU6EPTzE3N6qi3SxggM3 GQAbBtEqS2O9dz71YhWXEVLpEUoXOoj9Kab4EGxhQz/wwCgnk2pMSiQKaNadyeEWNhro st+hsfc63c+lp3q7kFThZ9WyJFjR6djA1zfxZQtfqPnjzAt3NVnHGZLeKtasFE+hVgc8 gut2OBxj5kGvzbNRtrFEadWMKlDSdTAJCE60KQuA0kHL4RTI4XRAEzU5fU7j6Zz8FrWl yWgw== X-Gm-Message-State: AOJu0YxiJabl1TG142fVxD7kqDoakIvtx1hSyTVaFsNv2r6hpnlmBZhq frR/oWjtsAyl+mqWoRmRq+fflfRDQbie4J1bBDcDlucka9L5KKT5D2eVxlSIU1xOUJKemIaZXYQ C X-Gm-Gg: ASbGncsylXH/Oyd9PC9+peMXCnv3iWAWefBTS0dYYI6BYCliRvfmU7qeRYOYufA3guB sqf1YuMBGvNlDQAELNDhli/vfIkx1LPZ5QL/H8SGuxqENeD2xvIq3QdKA5FlgIWdfwfcCUrhPqz 1Dw/SbtZAGg68rUqXcHQvTA8xPTVlr9ucACASZxXnV1yBj7WJV/CEXa/cLVXEIbFI5bNzyDwc+J pA1EKSMODR3KKGBYQP9S9x36P+2mcgqpf1bJg7IHX9mMoO0C2UEtB2RLiI= X-Google-Smtp-Source: AGHT+IEn2a1k9EtjOpfVet1ysm6MWr9jkiUyE2X2SH8cUoAjtJnT3D5sVQSXdc6UVYtxwSGjKa8I4g== X-Received: by 2002:a05:6808:2e4e:b0:3ea:aa8a:c115 with SMTP id 5614622812f47-3eae4f8c2c0mr3705224b6e.21.1733239977325; Tue, 03 Dec 2024 07:32:57 -0800 (PST) Received: from localhost.localdomain ([130.250.255.163]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3ea86036cbbsm2891878b6e.8.2024.12.03.07.32.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Dec 2024 07:32:56 -0800 (PST) From: Jens Axboe To: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Cc: hannes@cmpxchg.org, clm@meta.com, linux-kernel@vger.kernel.org, willy@infradead.org, kirill@shutemov.name, bfoster@redhat.com, Jens Axboe Subject: [PATCH 08/12] mm/filemap: add read support for RWF_UNCACHED Date: Tue, 3 Dec 2024 08:31:44 -0700 Message-ID: <20241203153232.92224-10-axboe@kernel.dk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241203153232.92224-2-axboe@kernel.dk> References: <20241203153232.92224-2-axboe@kernel.dk> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: A887440028 X-Stat-Signature: wxttmk5h6twx1xoosjtrrgiqm6snt5o4 X-Rspam-User: X-HE-Tag: 1733239968-304518 X-HE-Meta: U2FsdGVkX182dA49Z4E0wVIsxx1Fv/n2UQDw0+IZTMaFOA5NZeQm81wBB32/jv09kYmplRDRtzgV+CJh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VrEZpE91mkBuB14AuLag1Z5mACCg37fdHk3fGmgWOgLf9qV6CobrtPTBT+9nLoJ6LaZzCHhJco748FoLmYHewvn4av6yjOIthzFhQFRKU048bjmEOi479ymY2WHL3HhosRDGTaxH8B2rqp0q++igkQUjNvP0drJvcFPniDVevgsH+jLTr 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: Add RWF_UNCACHED as a read operation flag, which means that any data read wil be removed from the page cache upon completion. Uses the page cache to synchronize, and simply prunes folios that were instantiated when the operation completes. While it would be possible to use private pages for this, using the page cache as synchronization is handy for a variety of reasons: 1) No special truncate magic is needed 2) Async buffered reads need some place to serialize, using the page cache is a lot easier than writing extra code for this 3) The pruning cost is pretty reasonable and the code to support this is much simpler as a result. You can think of uncached buffered IO as being the much more attractive cousin of O_DIRECT - it has none of the restrictions of O_DIRECT. Yes, it will copy the data, but unlike regular buffered IO, it doesn't run into the unpredictability of the page cache in terms of reclaim. As an example, on a test box with 32 drives, reading them with buffered IO looks as follows: Reading bs 65536, uncached 0 1s: 145945MB/sec 2s: 158067MB/sec 3s: 157007MB/sec 4s: 148622MB/sec 5s: 118824MB/sec 6s: 70494MB/sec 7s: 41754MB/sec 8s: 90811MB/sec 9s: 92204MB/sec 10s: 95178MB/sec 11s: 95488MB/sec 12s: 95552MB/sec 13s: 96275MB/sec where it's quite easy to see where the page cache filled up, and performance went from good to erratic, and finally settles at a much lower rate. Looking at top while this is ongoing, we see: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 7535 root 20 0 267004 0 0 S 3199 0.0 8:40.65 uncached 3326 root 20 0 0 0 0 R 100.0 0.0 0:16.40 kswapd4 3327 root 20 0 0 0 0 R 100.0 0.0 0:17.22 kswapd5 3328 root 20 0 0 0 0 R 100.0 0.0 0:13.29 kswapd6 3332 root 20 0 0 0 0 R 100.0 0.0 0:11.11 kswapd10 3339 root 20 0 0 0 0 R 100.0 0.0 0:16.25 kswapd17 3348 root 20 0 0 0 0 R 100.0 0.0 0:16.40 kswapd26 3343 root 20 0 0 0 0 R 100.0 0.0 0:16.30 kswapd21 3344 root 20 0 0 0 0 R 100.0 0.0 0:11.92 kswapd22 3349 root 20 0 0 0 0 R 100.0 0.0 0:16.28 kswapd27 3352 root 20 0 0 0 0 R 99.7 0.0 0:11.89 kswapd30 3353 root 20 0 0 0 0 R 96.7 0.0 0:16.04 kswapd31 3329 root 20 0 0 0 0 R 96.4 0.0 0:11.41 kswapd7 3345 root 20 0 0 0 0 R 96.4 0.0 0:13.40 kswapd23 3330 root 20 0 0 0 0 S 91.1 0.0 0:08.28 kswapd8 3350 root 20 0 0 0 0 S 86.8 0.0 0:11.13 kswapd28 3325 root 20 0 0 0 0 S 76.3 0.0 0:07.43 kswapd3 3341 root 20 0 0 0 0 S 74.7 0.0 0:08.85 kswapd19 3334 root 20 0 0 0 0 S 71.7 0.0 0:10.04 kswapd12 3351 root 20 0 0 0 0 R 60.5 0.0 0:09.59 kswapd29 3323 root 20 0 0 0 0 R 57.6 0.0 0:11.50 kswapd1 [...] which is just showing a partial list of the 32 kswapd threads that are running mostly full tilt, burning ~28 full CPU cores. If the same test case is run with RWF_UNCACHED set for the buffered read, the output looks as follows: Reading bs 65536, uncached 0 1s: 153144MB/sec 2s: 156760MB/sec 3s: 158110MB/sec 4s: 158009MB/sec 5s: 158043MB/sec 6s: 157638MB/sec 7s: 157999MB/sec 8s: 158024MB/sec 9s: 157764MB/sec 10s: 157477MB/sec 11s: 157417MB/sec 12s: 157455MB/sec 13s: 157233MB/sec 14s: 156692MB/sec which is just chugging along at ~155GB/sec of read performance. Looking at top, we see: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 7961 root 20 0 267004 0 0 S 3180 0.0 5:37.95 uncached 8024 axboe 20 0 14292 4096 0 R 1.0 0.0 0:00.13 top where just the test app is using CPU, no reclaim is taking place outside of the main thread. Not only is performance 65% better, it's also using half the CPU to do it. Signed-off-by: Jens Axboe --- mm/filemap.c | 28 ++++++++++++++++++++++++++-- mm/swap.c | 2 ++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index dd3042de8038..139d1db79ff8 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2473,6 +2473,8 @@ static int filemap_create_folio(struct kiocb *iocb, folio = filemap_alloc_folio(mapping_gfp_mask(mapping), min_order); if (!folio) return -ENOMEM; + if (iocb->ki_flags & IOCB_UNCACHED) + __folio_set_uncached(folio); /* * Protect against truncate / hole punch. Grabbing invalidate_lock @@ -2518,6 +2520,8 @@ static int filemap_readahead(struct kiocb *iocb, struct file *file, if (iocb->ki_flags & IOCB_NOIO) return -EAGAIN; + if (iocb->ki_flags & IOCB_UNCACHED) + ractl.uncached = 1; page_cache_async_ra(&ractl, folio, last_index - folio->index); return 0; } @@ -2547,6 +2551,8 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count, return -EAGAIN; if (iocb->ki_flags & IOCB_NOWAIT) flags = memalloc_noio_save(); + if (iocb->ki_flags & IOCB_UNCACHED) + ractl.uncached = 1; page_cache_sync_ra(&ractl, last_index - index); if (iocb->ki_flags & IOCB_NOWAIT) memalloc_noio_restore(flags); @@ -2594,6 +2600,20 @@ static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio) return (pos1 >> shift == pos2 >> shift); } +static void filemap_uncached_read(struct address_space *mapping, + struct folio *folio) +{ + if (!folio_test_uncached(folio)) + return; + if (folio_test_writeback(folio) || folio_test_dirty(folio)) + return; + if (folio_trylock(folio)) { + if (folio_test_clear_uncached(folio)) + folio_unmap_invalidate(mapping, folio, 0); + folio_unlock(folio); + } +} + /** * filemap_read - Read data from the page cache. * @iocb: The iocb to read. @@ -2707,8 +2727,12 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, } } put_folios: - for (i = 0; i < folio_batch_count(&fbatch); i++) - folio_put(fbatch.folios[i]); + for (i = 0; i < folio_batch_count(&fbatch); i++) { + struct folio *folio = fbatch.folios[i]; + + filemap_uncached_read(mapping, folio); + folio_put(folio); + } folio_batch_init(&fbatch); } while (iov_iter_count(iter) && iocb->ki_pos < isize && !error); diff --git a/mm/swap.c b/mm/swap.c index 10decd9dffa1..4019ab371759 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -427,6 +427,8 @@ static void folio_inc_refs(struct folio *folio) */ void folio_mark_accessed(struct folio *folio) { + if (folio_test_uncached(folio)) + return; if (lru_gen_enabled()) { folio_inc_refs(folio); return;