From patchwork Sun Nov 10 15:28:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13895565 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 90141E7716C for ; Thu, 5 Dec 2024 15:27:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 00E1C6B00AA; Thu, 5 Dec 2024 10:19:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 27AB16B0109; Thu, 5 Dec 2024 10:19:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 928986B00AA; Thu, 5 Dec 2024 10:19:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 23D2B6B0085 for ; Sun, 10 Nov 2024 10:29:29 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C75F91A06E8 for ; Sun, 10 Nov 2024 15:29:28 +0000 (UTC) X-FDA: 82770568398.10.FE7576B Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf29.hostedemail.com (Postfix) with ESMTP id C007D12001F for ; Sun, 10 Nov 2024 15:28:33 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=ZLXk9exX; dmarc=none; spf=pass (imf29.hostedemail.com: domain of axboe@kernel.dk designates 209.85.216.52 as permitted sender) smtp.mailfrom=axboe@kernel.dk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731252505; a=rsa-sha256; cv=none; b=vbegxPa9foqtAMdVHfSh4JRlfZnJQ6aNpbXkEANPAPoGTFf6oKT2t4wZFGqNouIkbA8PR6 z2IKxVGQhuIS+FKQo0DS0bJhn1jETiB9ZJmMnoAbnUNNOpQ7ESHzNOCLRtj/RA/BL5y3cA Q01KLD1Rwnki/qv8vywHEQQI0g196Oo= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=ZLXk9exX; dmarc=none; spf=pass (imf29.hostedemail.com: domain of axboe@kernel.dk designates 209.85.216.52 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=1731252505; 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=f2GrDSrtQqt8cMSioXW5eWMYaGSDX9LD8WbU7yXVjqU=; b=hCHdoTbWY63ETc328RIOfouKncDLf6Nv3+Rh0FNzR4FMoJaHAeHDYFGksWJKeq/OBzCn2u 5Z8DJpETiZMQze0AoK2TdwdOOiM3s05w+oOSO0//TpSXjuP6MFG1xwkRtgiNmD+mJcBq6T DEjjWd6aZsGk2mNoWoDiXrcSo2aIzZM= Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2e59746062fso2984492a91.2 for ; Sun, 10 Nov 2024 07:29:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1731252565; x=1731857365; 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=f2GrDSrtQqt8cMSioXW5eWMYaGSDX9LD8WbU7yXVjqU=; b=ZLXk9exXXTaigwmgR/d5x0uuaXdeE0/xX8UcxoYvk5FESyDaStihytseAaD0yMtLrq Wp7BpoHUntgU4doN4/aFjCF0OHkWLRMD2R18d4SszuVTGylxUzWF9Id8D7DY5u2kp4Sn 1Dr4Da/eDA5gpamZSJtv8/JTPlA6fItyXQfgoXPVuDovPnXfGc72q5ms3l7rBULNm9WL 26YlPuf1p9w6tBDoZVfVeeAyErUQetahLfL8UyGCp4hARWUcWklSat1k2eos2pGrD2L9 dv4GmDbLHdGyw2b7sGjb6WEjL3ZfUUJlOoZqQm4xRFXqFKzk7Dqsot+yQHh+qH920hYb i4Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731252565; x=1731857365; 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=f2GrDSrtQqt8cMSioXW5eWMYaGSDX9LD8WbU7yXVjqU=; b=v8zCvoImr5mC9+q6KuceGip/BcaGej/SN8eEOnoY4EDFrtoEfvHeSmE9C5H5Z9AwsD sS9f6O5rYlDu9Umzrb7FVT1cY2a0fnbiKXxxOunzLiSg4TJPKjv2Qoc67+SaeYGSjLt3 20DFEG2ZmqOBenJjaCJn9iWM7ec3cJ3RHNqnQo2AIL0WIO2Jpl3L0zStbP4bVAsWfkfv ljU79qEMxck10YKw63R+UNkuoypuqbTXpR7BZCxmAzwSkMegWQqfr0Kc8I2i+HB+Fpm4 4QMQn/L+fiCg+qEzRK7W5WdnbycmXc4t1CTQhDkA0yxKmg4DcJQrPxAcA9BT49HBGlNN lpyg== X-Gm-Message-State: AOJu0YwpVZLpUJlynowtAeRIEx4qRwqSNPXjgxpf7AhLvOxi2V0Ppufq 1othY5oBZH1zyZgg2cvhsY6o0jcDPAg04m5tSD/XOaOflo5XklCQW6wNkioOA5FVBtfn//jvHav lfN0= X-Google-Smtp-Source: AGHT+IHD5ZOaM1/ydWmKI6E2td5s5ih4bl0qHFridFSAcX9UJ78BlPPQ+BWJS6tide+7RYqVyEL5yA== X-Received: by 2002:a17:90b:2748:b0:2d8:840b:9654 with SMTP id 98e67ed59e1d1-2e9b1754affmr13371709a91.34.1731252565293; Sun, 10 Nov 2024 07:29:25 -0800 (PST) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e99a5f935dsm9940973a91.35.2024.11.10.07.29.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 07:29: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, willy@infradead.org, Jens Axboe Subject: [PATCH 08/15] mm/filemap: add read support for RWF_UNCACHED Date: Sun, 10 Nov 2024 08:28:00 -0700 Message-ID: <20241110152906.1747545-9-axboe@kernel.dk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241110152906.1747545-1-axboe@kernel.dk> References: <20241110152906.1747545-1-axboe@kernel.dk> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: C007D12001F X-Rspamd-Server: rspam11 X-Stat-Signature: 818bniustpqoz1tubzaz6gan38yskmno X-HE-Tag: 1731252513-946366 X-HE-Meta: U2FsdGVkX18C53Z6Z8Uob8QK6x3x+87Yotf2DO1anl9TfDwyAYs43+KV8bh7pFXgUD5CbeRmHQU/AfCMXyGQ/DBBo5zynea8kXjAIAICODtPpfjTIXpbumVkj01TpcgnDWirZNy8iO0YGPzdlqleh7byK1XtBIlqlLzHvbi2bYY8AE/mfBDYUjAe2zp4YEOWM/qFelJct0ftoZkUqWLjNlEIqyFrki+uJPOZoDkdS5IVvbRsxtqu7t084TetLBZIj7XE9l4rtAMqiMeO451G/zFpNnumR35yDfGHQVSdUFu+adGwkGkFuwIrjHsRtPlKh1EaMUD3/vqoi5/ekOCQWGLGrl5A1+M8cCnJUsX5vifLD6Lwf+apNF/dI1ltAU0DO3KJXZM7s6JFdCGZaEUvaUC8pkJ4lPuL28t1a4BGmpHepQvXkSsP1XGzYztpJqujCxXQkYxG1JeDraBmReVCY0e//aplTGWXlN65vkPS1v1ZFw0Cw6ujCDc4VdJSx5L04hi5WHQeUpjrt0aPH/Wa8+IDIfJywuIJcINELlOVlSFVwSk06rghbX4HyUJ5hmd5TjHPFrPvn9licj3g0utdHuvlkzReDzv/jHeErihq/PEFrzo2kk4kRvNBVAeEN/oWON2ahZkV22lxv49XcyvnJy3SivKlWPYFSN4sJyeUfsggBVHwfP16s2BAsGLz8GVv2C+4wyE2lLo9DpYWI366YJgWMfHuaAkSHZSIr1RRhUaEm5Mg+o+frhC+VAFe1EcFIa1N68yaOu1yfM8bxGuNtWF1YORHVplR/PQCQyR/7aKrrG+/SyPM5QMLOdtqkQPXBPdsi9gNfOxvZyceHJ48lMAPO36SnqXr0KvbiEfa2rewQNM15dRmeNdcZ1PwSTGyk/dgyw98PLerzeD2TD8dpd3hlZF7CYNaqPzSIvnv6QVl7rmireG9i/C6JkVHbhUjGHBy3j5d8qcdOBTRFKs sIWv4c// xnvta7d8R8hp7QMuuaJXPnff75BSy3zQs9S76O9A8rJ1autuCtGB4EYBjLv6svtsX+2E9wm1dLYcBqcS7u8vU3EtJLCo79zmETr0aDRgTfJvRCo1aTqAeK5IoX87dC0DNETT1XoxXNluncM5BfQivM11Ws51qGJPsib2Q4B2hWFGJJTAcMEIA1IvezPuXxPuCs+Sh/2nfiSy+wM9cA9qpvvAbs2NLg8ts76o8lgWX5wsflUQ2IUAZOD1IC9/QlyFMU7MkftleRsH0SuWHzI8oANdSUMHDoqFfuge3PZDARAUIQDIZ6PYec+zvToPPjq4T0lW/IczkKKd/eI6PBU2FcFRaAu/GpZd0t5em 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 --- mm/filemap.c | 18 ++++++++++++++++-- mm/swap.c | 2 ++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 38dc94b761b7..bd698340ef24 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); @@ -2706,8 +2712,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;