From patchwork Tue Aug 20 02:26:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13769263 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 CADE8C52D6F for ; Tue, 20 Aug 2024 02:26:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4835C6B0083; Mon, 19 Aug 2024 22:26:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 433A16B0085; Mon, 19 Aug 2024 22:26:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FABB6B0088; Mon, 19 Aug 2024 22:26:55 -0400 (EDT) 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 11BEA6B0083 for ; Mon, 19 Aug 2024 22:26:55 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A97E9416DF for ; Tue, 20 Aug 2024 02:26:54 +0000 (UTC) X-FDA: 82471035948.12.37B622B Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf21.hostedemail.com (Postfix) with ESMTP id E242D1C000C for ; Tue, 20 Aug 2024 02:26:51 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="k/JryKOp"; spf=pass (imf21.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724120708; 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:references:dkim-signature; bh=9sFseQwei+CiGkTUDpQHHUk/qFnB9cNwQ5rhI0xvQcg=; b=ssLPTZ9rLX2hVI5LEno7h1d6ZOSkc7/uR4eUDH7m9MlgLmtSn0kHCorT9xZfZveSNl+tcn jv37W/EyKNbF0hb4xc8n2cZ+lrpXuquzJYoUdxjoyXKB3eCJ3j9cqJZ9UTkYCJViXcbWVe sKT4pBHeGOcXelVMrzwyz/GQ3Pri/+I= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="k/JryKOp"; spf=pass (imf21.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724120708; a=rsa-sha256; cv=none; b=xbto1Gm0xNqacEC9s+tWd0Ez8QMYbUbu6NdmjA/g4BMBsOFwogIGJ0OX5KZ0j0bw4WobQL spoCT5pbe4TEVB3WcDQnhWgClCC4jFgx6q3hsNeENyWw/3WUL+HFHLu9uPdRIwv4sF5nQ7 XejmxNbG8jgJb6KAbNY5pp4+GNU3mrU= Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-7c3e1081804so3271589a12.3 for ; Mon, 19 Aug 2024 19:26:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724120810; x=1724725610; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=9sFseQwei+CiGkTUDpQHHUk/qFnB9cNwQ5rhI0xvQcg=; b=k/JryKOptLZBpnF+HifqUA+j/1+9Uv50zZcDayKkElG4d4Yt6XJDmxI+eVFkHbZLAj RMTGD7bOVZniy+RKyLOPoeMky8DdTPhBFttpvZqw1IQF8sY4MR7yybdE1HeJmUUl0bMm HXkKkCFdOPjfnel2L1eMR7FMqJfs+FeuBuyfDRlcO47o43XEmtCxMYpF/S2KWcctizC1 BgvyFc89ey3JhgcJqY6Gb8sbz+YmfJumK8znmQTq9/vDshOWKoL0uz30GHAP09KnZLe6 jinDcbW8T8EXsltI97OaOaWeoV01SYC1eOZdwgZi4MEWlYueXZ4p2bm0hjAlXyHtXJ6M xKxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724120810; x=1724725610; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9sFseQwei+CiGkTUDpQHHUk/qFnB9cNwQ5rhI0xvQcg=; b=dk1JXIFnVFVBi8stxrkbxopzuJyN/T+3bCHPv6lhyXgk31HmE2fdEtJdyWpefDLByZ Fmwz0n6pKUu8PbgmANK8Yf28CEd4Qn6co5OGqzebIhT6zXVW665l7anyGM94oHyn0S2a e2h5PNp8t76V/1ejLw2bQzW251lIkG2G0PqQ2fe6Tarr/i6lUc/+G5g2K5s0mw1zsQNe 4v9lu5pq15VsyIxGSUIzen0DOvc/3DIn6IV2lXh4iZ0zEoPJiB8MFSsuE78D2WSvwUWe 7Uo8kpKObPq6cC/Yf/sHtrNBcPWsGP5dYUQ871r8ig77km2niw/bvCyWYZAqmmhm+XeG LJqw== X-Forwarded-Encrypted: i=1; AJvYcCUnYCcELZhPYtpf4f5j49/W25xeOZWh/uiuPDZwvHLVLmZWJSZ2LfHX6tjIyYW0thHLm0eXzWLNUw==@kvack.org X-Gm-Message-State: AOJu0YyOXJYa8LYMvJoqojIH9PlOLazSi26nRRpEVwdCPPCv8bQyBGgH Z9bUftkeKpmGksiZq4ToDylcwAspXI/LskuGGAhbY+9fLJtbhfIW X-Google-Smtp-Source: AGHT+IFvxhZMY4v69L+gXdvO6693YPauuZyNg9TnvE1QwtbUBHrltGSIl16MuBx9yuIdNVxyKhA7YQ== X-Received: by 2002:a05:6a20:ce48:b0:1c3:ea28:3c0e with SMTP id adf61e73a8af0-1c90502fa13mr13595284637.33.1724120810400; Mon, 19 Aug 2024 19:26:50 -0700 (PDT) Received: from localhost.localdomain ([39.144.244.90]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f0379048sm68094175ad.143.2024.08.19.19.26.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Aug 2024 19:26:49 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, david@fromorbit.com Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Yafang Shao , Jens Axboe , Matthew Wilcox Subject: [PATCH v2] mm: allow read-ahead with IOCB_NOWAIT set Date: Tue, 20 Aug 2024 10:26:39 +0800 Message-Id: <20240820022639.89562-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: E242D1C000C X-Stat-Signature: b31bj34xt4erzcssq8fsnadg9363rgqg X-Rspam-User: X-HE-Tag: 1724120811-618333 X-HE-Meta: U2FsdGVkX19VwegzqYQB0ftXJ5/NROviVmbTZsrMNBmAL8ZhPEWyOoy72+YPv4UBlcmDrM8jkTrHOIIP6/uB5dN9VtTfH7WLv2UMYMdfBJMjXalgSVvNzQc282asIZE629r4g0vZ/9crwq5mgi5WtcnUPyxuJva2e54XR6bPttFfzw+30D6AErcQpOuL9UdLyoi1BfyvN9g5Q9n2ZbDZBVBrTtS6Uts3caaP+W2mclnyxi/7v5PIaUq9pjtt5ULkeTm6qqoEM5ytZsEByGjdozCXwepMzQMQZIkXZsiDve3XQw1z6pUHjyWC6ymdKyB3ERgx9RpOw6NavnoBiDuZ57GCHb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2468MZkDCG+ztCA2HhTbwmPMD52y3MehrbaAEjJsodaxgjwYYAO9ldptC8Ml3oDQjw5he9E+/cEEDtK8daEu2DHEOuCD5RS95uPj9YJ11MgqCzlUbvbS96ME6svm1Aa1gnpv+d6k85cYR+oOWgBeV8iUFtKuJJKHip+T6ngxObuobnHCb4J32fewAZuPMzR9U5Oj63mQihpuY1KUX49OkrgDSR2HE5fdudr1V298uoRsFEePN3tplgG3Vp8q5+QIuWzRRG1umpEiszMtUUJqRCghPeT6k+tl+K9qZBTLzgsB7Xm1mOB7b2YitWQg= 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: Readahead support for IOCB_NOWAIT was introduced in commit 2e85abf053b9 ("mm: allow read-ahead with IOCB_NOWAIT set"). However, this implementation broke the semantics of IOCB_NOWAIT by potentially causing it to wait on I/O during memory reclamation. This behavior was later modified in commit efa8480a8316 ("fs: RWF_NOWAIT should imply IOCB_NOIO"). To resolve the blocking issue during memory reclamation, we can use memalloc_noio_{save,restore} to ensure non-blocking behavior. This change restores the original functionality, allowing preadv2(IOCB_NOWAIT) to trigger readahead if the file content is not present in the page cache. While this process may trigger direct memory reclamation, the __GFP_NORETRY flag is set in the readahead GFP flags, ensuring it won't block. A use case for this change is when we want to trigger readahead in the preadv2(2) syscall if the file cache is absent, but without waiting for certain filesystem locks, like xfs_ilock. A simple example is as follows: retry: if (preadv2(fd, iovec, cnt, offset, RWF_NOWAIT) < 0) { do_other_work(); goto retry; } Link: https://lore.gnuweeb.org/io-uring/20200624164127.GP21350@casper.infradead.org/ Signed-off-by: Yafang Shao Cc: Jens Axboe Cc: Matthew Wilcox Cc: Dave Chinner Cc: Jan Kara Cc: Christian Brauner --- include/linux/fs.h | 1 - mm/filemap.c | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index fd34b5755c0b..ced74b1b350d 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3455,7 +3455,6 @@ static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags, if (flags & RWF_NOWAIT) { if (!(ki->ki_filp->f_mode & FMODE_NOWAIT)) return -EOPNOTSUPP; - kiocb_flags |= IOCB_NOIO; } if (flags & RWF_ATOMIC) { if (rw_type != WRITE) diff --git a/mm/filemap.c b/mm/filemap.c index 657bcd887fdb..fe325c2ea626 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include "internal.h" @@ -2514,6 +2515,7 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count, pgoff_t index = iocb->ki_pos >> PAGE_SHIFT; pgoff_t last_index; struct folio *folio; + unsigned int flags; int err = 0; /* "last_index" is the index of the page beyond the end of the read */ @@ -2526,8 +2528,12 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count, if (!folio_batch_count(fbatch)) { if (iocb->ki_flags & IOCB_NOIO) return -EAGAIN; + if (iocb->ki_flags & IOCB_NOWAIT) + flags = memalloc_noio_save(); page_cache_sync_readahead(mapping, ra, filp, index, last_index - index); + if (iocb->ki_flags & IOCB_NOWAIT) + memalloc_noio_restore(flags); filemap_get_read_batch(mapping, index, last_index - 1, fbatch); } if (!folio_batch_count(fbatch)) {