From patchwork Thu Nov 14 15:25:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13875217 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 63992D68B30 for ; Thu, 14 Nov 2024 15:28:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BFB776B009F; Thu, 14 Nov 2024 10:28:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BAD796B009D; Thu, 14 Nov 2024 10:28:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B16A6B009F; Thu, 14 Nov 2024 10:28:16 -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 739826B009C for ; Thu, 14 Nov 2024 10:28:16 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 369761A10AD for ; Thu, 14 Nov 2024 15:28:16 +0000 (UTC) X-FDA: 82785080448.26.A80E6D1 Received: from mail-oo1-f45.google.com (mail-oo1-f45.google.com [209.85.161.45]) by imf15.hostedemail.com (Postfix) with ESMTP id 21CB7A0004 for ; Thu, 14 Nov 2024 15:27:28 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=O21dH7z5; spf=pass (imf15.hostedemail.com: domain of axboe@kernel.dk designates 209.85.161.45 as permitted sender) smtp.mailfrom=axboe@kernel.dk; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731597918; 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=7749KjgMy6eatM2VSy2jowDIab8tvoATfJQmYOPl1pc=; b=TJqjXlxQwhVZ5kypUWlwu7IoI151sx3IJCawAKZN1EgaXVUbvZO+necDNS9xsfGnXJNI5I 51cpMGlQ8sqK6ALK3UiXxKpARCrPq8FE908m/+sCvIn0EAvZ+Qf6YWfqPiKK1lPRV8OIlX jSjAtkZV13e3vBsSQWLJY1kY20tOPXs= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=O21dH7z5; spf=pass (imf15.hostedemail.com: domain of axboe@kernel.dk designates 209.85.161.45 as permitted sender) smtp.mailfrom=axboe@kernel.dk; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731597918; a=rsa-sha256; cv=none; b=19S9DG6c8x38zoIhFCDhIcy19FahEQZd5afRWJRfSzEWF+kfgSLO7NV3Cy16GgVApn5rhL LOxxm89JiaFwJcKN9jy2Oh/ypOaAOnWwT4jvWCAXG01PcUOpgDnwnaQvr1rUb4QCV3jUX9 Mbth4Xq+4sFEefzgBOVuheQRIowtp4E= Received: by mail-oo1-f45.google.com with SMTP id 006d021491bc7-5ebc9bda8c8so358733eaf.0 for ; Thu, 14 Nov 2024 07:28:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1731598093; x=1732202893; 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=7749KjgMy6eatM2VSy2jowDIab8tvoATfJQmYOPl1pc=; b=O21dH7z5HMdG+Te68rp47FoXmAlTwO4YHlGdSi4JJioNLXcQQCF2VFx5tuOH6URr8M UqpC6c0mzbID7qhykskG2RzOvGo+RIv/CQKh58bft4AVZp4a2mHI8BY0ETkJDJp1g9VA XBPkFAbNp5sMfO5VTCI5qa6DsM8x+c3A4nrVhCDNWAJnXDvIlvIYaR77T4KYRa/12nrN 1VX4FhVa/zhJJyUcPTCQBxTFtJcmQa2ua1kbwUtJmVs3kbF6B0LfFj57wVTFf0eQyFkQ SOjPnDLvFvbu3BTYX03EyOyGQANyuIUbFApXvbl4up/uIZ649m5v7SbndA7Bqe99gDU+ A8vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731598093; x=1732202893; 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=7749KjgMy6eatM2VSy2jowDIab8tvoATfJQmYOPl1pc=; b=DWWz1s3d1cWyC4G4BnKgBznZ7UBUGcyQvBZAsaC9gLucacR8WfPnlJajSlECN/XxmP 8atwZ1koExPX7SLQfzvUFZoN3JZs9OE5BChl4SG17GYean6XgemWkTcWHPlMYblPzZmh iAc9jy3q3oztbrD0ck3rliwwc38fUZS3to+9yyZaOCu2uJGIV6FTXkGxX8HkPI64jFA1 BYerS5GrCq9FOtL4aWaMOD7OQ9kaU4hnCqZ2a0/fnHFws4kWdvuryWO10mxWRSFa0U+u QyJ2rlxAgbbWdn3VOFxzkR9qGKV84lAF2qVKfR1dMnk5wrg9kgkwrLz15ku+6hxejJZq X4Hg== X-Gm-Message-State: AOJu0Yy8lgeq26DnjrS5R6qzv/wg3qu2O1DqEitd+kEuOOL9jdMU7+AX f4qJ3/k47WQbLcjJZxDuxI8oE+S/CZFFfQK+yw7xZSIorl5zBaLVC+ZY4ESTmcl0HQFm3weC7Vs /zGI= X-Google-Smtp-Source: AGHT+IHV4oSrYqhQg8SNV67NfRcdIlRE7mzCITp5FLKpYfj2Lpw4MvYi01EsizY0Kl5ORLV8Nbza+Q== X-Received: by 2002:a05:6820:8cc:b0:5eb:6a67:6255 with SMTP id 006d021491bc7-5ee9ec3bae1mr2681521eaf.1.1731598092948; Thu, 14 Nov 2024 07:28:12 -0800 (PST) Received: from localhost.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5eea026eb41sm368250eaf.39.2024.11.14.07.28.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Nov 2024 07:28:12 -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, linux-btrfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, bfoster@redhat.com, Jens Axboe Subject: [PATCH 08/17] mm/filemap: add read support for RWF_UNCACHED Date: Thu, 14 Nov 2024 08:25:12 -0700 Message-ID: <20241114152743.2381672-10-axboe@kernel.dk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241114152743.2381672-2-axboe@kernel.dk> References: <20241114152743.2381672-2-axboe@kernel.dk> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 21CB7A0004 X-Stat-Signature: j6o7gw5h7phu5t85t6uj1jj3fobccagk X-Rspam-User: X-HE-Tag: 1731598048-615612 X-HE-Meta: U2FsdGVkX1/IVte4kWLIOogDdprZauiprl/HtSevVb76kPaW0sa7SATJ4CAtnQh+x3IAHEa6fF9SEjy0o4Fc21XbE86FdRus9Plk2Wj98wP9G5P+dU67/fgim/wBJXnq9fLJ9stqcG7LT61WQqZ27oIrBMVe7KbgMbn8ikt8NYGO14kQZcbcL5x51XQQdnn7Egx+m4IVMy5X30BgNWl+mkG5hOGq0/gKZD2wdUcl/uXt2VfIAWetjsFxYpHNi/GQeHoSXf9arKNryBZF+8Xo/+2tOkrh223LPvXNoeXAJqTdFe+JnuRgrtgoZQcFTJYS7PvNiry+elX2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vack.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 02d9cb585195..3d0614ea5f59 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2474,6 +2474,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 @@ -2519,6 +2521,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; } @@ -2548,6 +2552,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); @@ -2595,6 +2601,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)) + return; + if (folio_test_clear_uncached(folio)) { + folio_lock(folio); + folio_unmap_invalidate(mapping, folio, 0); + folio_unlock(folio); + } +} + /** * filemap_read - Read data from the page cache. * @iocb: The iocb to read. @@ -2706,8 +2726,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 b8e3259ea2c4..542f298d3dcd 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -472,6 +472,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;