From patchwork Fri Nov 8 17:43:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13868801 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 2C5D1D64078 for ; Fri, 8 Nov 2024 17:45:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9AF256B00E0; Fri, 8 Nov 2024 12:45:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 961266B00E3; Fri, 8 Nov 2024 12:45:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 73ABA6B00E4; Fri, 8 Nov 2024 12:45:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 544E76B00E0 for ; Fri, 8 Nov 2024 12:45:28 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0219E160231 for ; Fri, 8 Nov 2024 17:45:27 +0000 (UTC) X-FDA: 82763653056.19.FD10C40 Received: from mail-oi1-f172.google.com (mail-oi1-f172.google.com [209.85.167.172]) by imf17.hostedemail.com (Postfix) with ESMTP id 8293840026 for ; Fri, 8 Nov 2024 17:44:57 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=TsvjZw8E; dmarc=none; spf=pass (imf17.hostedemail.com: domain of axboe@kernel.dk designates 209.85.167.172 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=1731087755; 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=Tq+ywqpEyPaYsCraC6z/Wa/2p0SbC90MEdwC8hVGLhU=; b=6reP76dFMGtQ0rDqvNg3CJ2AAdLjaqZYJEczPK5YSq3dBr6emjlId+vkO3kVrOcmBzsVZL zARL3KzhkgyPD9GWeroMyT3CXG1pYWGbj72jyobDv3cJZPKJpu5baCw8ER18qHMNPwEtWp 5WhRvB4zezy9O4/csxtig+aS8UCyidY= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=TsvjZw8E; dmarc=none; spf=pass (imf17.hostedemail.com: domain of axboe@kernel.dk designates 209.85.167.172 as permitted sender) smtp.mailfrom=axboe@kernel.dk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731087755; a=rsa-sha256; cv=none; b=sB0Bjp9QQn1IIBHFs7bkABFGVlUH9RqNiwwmE9LF4wOF1Vm6W8DZ2r9n0tt9jJXDCRAYGX g0WW0BChsYf8lMeDmOMyVwk6KLVNI70TBeDGYYmnWKl4iTQLWyt6L3LO1ZQTLeh3Jdaft3 AhVLIrNNZcoNFN46Nk4IyrfBdtjO3ec= Received: by mail-oi1-f172.google.com with SMTP id 5614622812f47-3e607556c83so1581624b6e.1 for ; Fri, 08 Nov 2024 09:45:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1731087925; x=1731692725; 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=Tq+ywqpEyPaYsCraC6z/Wa/2p0SbC90MEdwC8hVGLhU=; b=TsvjZw8E/RVPtPCjNuHmBndQjfyOd78dkkfVtsAcQEr8Xvi8Nk0xjUy6M4LZJShJTg +8dfjWFTWU8nTpdMb1stdYKMcAOfFbTsDN1DzMl0S31NGB8Oo5I8tdYnLQ9Bv5oF4h00 zVNUr5Sm63JvgD1LD1THF6a2ZSoGhrX2BYNQ0afCCK4sBrmbklWtiFpO9y9InswymFK2 b5sZcPmteTPv9BkMg+SslJhdHkbM2X4m4mtHcEJvcD0kVx9MQbxk/nRqZfKGuGlznjhv lddjF6Q69pnSlZuev698gKxjuj+FQa75r3v9xP8cyyf/RYHR8E1tF54TBYrJsjprnXsV ea0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731087925; x=1731692725; 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=Tq+ywqpEyPaYsCraC6z/Wa/2p0SbC90MEdwC8hVGLhU=; b=QVUEE0dQwgtToi9nUkS7aRmE2KYplGwEoPiWj36YOLDEa+7pkvpcRAtqVrwmGt9NkS 0hK8u3feJygJbpDA3jVIiQP6EC4j8gb5bakz0LF+oWjcBZ+2SgFvNCxrxre6+bLgBkYN LAaj9zlKfFxqXySPgnVi/vhDiClhJLunF3v/+zyFC/jhs66dP8OgYUBLg79xOt44K9CL VhguY0zj6eCtBs43fRadz1DeaMEmxi7vtwifLIp1ylnhl3V/PD1FJnusEIXvsspb460U nY+ERISO+aXpiHrUQGH13AE4/LeYXPA4ohMohXsniFGuHe6sqAgtPkTP3molIYUttPDA dD+g== X-Gm-Message-State: AOJu0YwDN8hGQsQS8UfTfAZ2Zjq5DxPOo/D4eP3Hp0qk9yT/KTfVf9iD eYd+o7T00giDI+PmObBcOiXo1B9bG34v6WnTzs6vl73IkSQeJbpB84hqKMhYhn1WMUc4T9KHTvQ t4n4= X-Google-Smtp-Source: AGHT+IHYb37IuRL+rD3E4bWkVAGE9ioVB03ZuysIUbDHqe4Odg9KTg4FuyvIzrjyhDZyIdkBKG8ezw== X-Received: by 2002:a05:6808:1819:b0:3e6:3205:1a71 with SMTP id 5614622812f47-3e794699f41mr4284876b6e.15.1731087924649; Fri, 08 Nov 2024 09:45:24 -0800 (PST) Received: from localhost.localdomain ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3e78cd28f80sm780969b6e.39.2024.11.08.09.45.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 09:45:24 -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, Jens Axboe Subject: [PATCH 08/13] fs: add read support for RWF_UNCACHED Date: Fri, 8 Nov 2024 10:43:31 -0700 Message-ID: <20241108174505.1214230-9-axboe@kernel.dk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241108174505.1214230-1-axboe@kernel.dk> References: <20241108174505.1214230-1-axboe@kernel.dk> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 8293840026 X-Stat-Signature: r4sqnbr7rkdswszjwz6ihdajty34ju71 X-Rspam-User: X-HE-Tag: 1731087897-754227 X-HE-Meta: U2FsdGVkX18mEu9LbJQ3RTcpbcLea7Gm6gxOAj59WPYE/nst/h8U7K8wZ8cNjOVVSkWvVstmPgrDEZfIbgrAuSgBcLGz5uS43dnMIjs1S3uoRkmSmG+p/OT869EKayCPutCCSUM2WhvDg/pMdn7+ID6kRS3VMrNU6/l/sSimF6vmuL4it4FQnWz6rzgvemNR1c8inz0clutAdypsV9w89yfHlb3moUKwdc4pNmRJoUFB3Nj3bhek+ZXm5mBAPq2YT1eF+sg+zQBmWvVqp4b119PgY0U4RgcseYgVb8QsxQTg8QfTfQV69UOIZ/CJYAGSaHpBvMwuUFfzOFmsx1p76vv6RqmlhahTYpZymTqp38cIpHKzy2eEP8AaNYe0kMxg4w/k4mcBqGG73z1d/zkfQn/nE39E2mCgykktMKFEIfS6dS5emkdidEF9qTmHdVsEGIFVzCc9XjKYurorr38gBZiCBlSyzsH+xF40SzqGtdrnQUB1uUHIBnksjHxaEPlxgoLjllftl20RHjqe/hQ21qFHXqpBH2TxtUT6RWQAIzBAYGPNrSMRXipHWiQGPc2BlMsPDpAEZ63XDxZREGZ99AAJj8NNe6p5CoHWeN3vz38w5bT7pGZHJGc1Vb8jJr2954b8vyrZNyJKnuIAIzP1ZijpesBi2VqTU0/kQDS6IaHhfAH0GIihV6xFqZoyAfwhTsNS69DRUYHWk9W+H/iR0nqcnnClP7iUcjtfztjW2sDJV6PFDjvuwofvXMt+j0bvz7ZnaTYpyYtIYfMxe4oR2zkoM9Hp7lD8c2xfuM/ptt235yBpfAKM0GmghVoo7LeztaIJq6IxiMqeg10UJ5L1Cu9TvyaC3OQ0D8cD4193YqjDjvXuaGonbJf9MrTz/JpgEGTDMv1VHOWqyLHSc+k4VvHc/T9toIQzj1Iu3W1SJK9kcurTuGfB3HqWzuKWk16097ycWHPSSopSqlTSkux h9lKGnoY Kgl/2+UE7yKkDN0DhAcOC4TETLmPYDdPWEo6HTQrfaQyiOF0DebqbR/R5jwqkz/cuHm9LbV3Tbra7leeANVjBUhNE1h51gnhMFIGiBUxkPQ20UbH0GA3xPeXgiiaWAHy3eqc48iLg2JbXjeW7288Ic6uGDuGpT3rQpj3d8c3OaUphXqJ6E0xnnGx+izsWTkDIF7nPH+AhC6mrod4z0v6GHURahG27l6jULhnRLNnNX834/X+5ipz3qkUAVge3nhb+v85t9KJOIPXaBpp2HrnevczbmwvkNFl8oU6v7qme2Rj1a2luYVBFs9SkNQhqmMH/WDTO2XMS0xN+sPs6+BrTQx/r1d6pgKX4cEDy 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 cousing 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 --- include/linux/fs.h | 4 +++- include/uapi/linux/fs.h | 6 +++++- mm/filemap.c | 18 ++++++++++++++++-- mm/swap.c | 2 ++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index 491eeb73e725..5abc53991cd0 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -320,6 +320,7 @@ struct readahead_control; #define IOCB_NOWAIT (__force int) RWF_NOWAIT #define IOCB_APPEND (__force int) RWF_APPEND #define IOCB_ATOMIC (__force int) RWF_ATOMIC +#define IOCB_UNCACHED (__force int) RWF_UNCACHED /* non-RWF related bits - start at 16 */ #define IOCB_EVENTFD (1 << 16) @@ -354,7 +355,8 @@ struct readahead_control; { IOCB_SYNC, "SYNC" }, \ { IOCB_NOWAIT, "NOWAIT" }, \ { IOCB_APPEND, "APPEND" }, \ - { IOCB_ATOMIC, "ATOMIC"}, \ + { IOCB_ATOMIC, "ATOMIC" }, \ + { IOCB_UNCACHED, "UNCACHED" }, \ { IOCB_EVENTFD, "EVENTFD"}, \ { IOCB_DIRECT, "DIRECT" }, \ { IOCB_WRITE, "WRITE" }, \ diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 753971770733..dc77cd8ae1a3 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -332,9 +332,13 @@ typedef int __bitwise __kernel_rwf_t; /* Atomic Write */ #define RWF_ATOMIC ((__force __kernel_rwf_t)0x00000040) +/* buffered IO that drops the cache after reading or writing data */ +#define RWF_UNCACHED ((__force __kernel_rwf_t)0x00000080) + /* mask of flags supported by the kernel */ #define RWF_SUPPORTED (RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT |\ - RWF_APPEND | RWF_NOAPPEND | RWF_ATOMIC) + RWF_APPEND | RWF_NOAPPEND | RWF_ATOMIC |\ + RWF_UNCACHED) #define PROCFS_IOCTL_MAGIC 'f' diff --git a/mm/filemap.c b/mm/filemap.c index 7f8d13f06c04..6f65025782bb 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2471,6 +2471,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 @@ -2516,6 +2518,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; } @@ -2545,6 +2549,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); @@ -2705,8 +2711,16 @@ 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]; + + if (folio_test_uncached(folio)) { + folio_lock(folio); + invalidate_complete_folio2(mapping, folio, 0); + folio_unlock(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 835bdf324b76..f2457acae383 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;