From patchwork Mon Aug 12 09:05:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760335 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 2B939C52D7C for ; Mon, 12 Aug 2024 09:08:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B2C336B00A0; Mon, 12 Aug 2024 05:08:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ADBD56B00A1; Mon, 12 Aug 2024 05:08:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 92FC26B00A2; Mon, 12 Aug 2024 05:08:17 -0400 (EDT) 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 737DF6B00A0 for ; Mon, 12 Aug 2024 05:08:17 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 1893F121BAE for ; Mon, 12 Aug 2024 09:08:17 +0000 (UTC) X-FDA: 82443017034.19.723BAF6 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf22.hostedemail.com (Postfix) with ESMTP id 18513C0032 for ; Mon, 12 Aug 2024 09:08:14 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=G3r8F5El; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.169 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=1723453640; 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=NACQ7ELOlfYjhnP0zeg6TZ0yzINb3WcQXKg7nf5cJS8=; b=wnpVoEPAuV6IGPpp5K9C3Zcx5E1Eq+ZWZzu15sTZe/wb/wTr8Y7BrWOg4H22QOjWLBmkwg 7t7X5RFSZiclPkVDxkoZl/AvtxEIb+Sb5HAgrRk7VUVMmyfvHWcRdICuBKggO5j35fvAHy uH3hfISy2LvhKCEv+QXdyDrJ020JSMg= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=G3r8F5El; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.169 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=1723453640; a=rsa-sha256; cv=none; b=jQjyLWBoU71LilodzEu8Tj91xdd23VyfQCIdrOmxTjXm91q1/DR6lKahikMcZ+MBVYjYFb XL16xILDkzqJuBCY5d/oHddFxzkN9Wz8Kj3KSxQMDJJiKCf7P3ju6G/FIM1ZFQfy0EDOae GpXuVU/T8dj7INeWyRVkTVW5pDY/e6Q= Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-7a1be7b5d70so2914241a12.0 for ; Mon, 12 Aug 2024 02:08:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723453694; x=1724058494; 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=NACQ7ELOlfYjhnP0zeg6TZ0yzINb3WcQXKg7nf5cJS8=; b=G3r8F5ElhUrE96aHppaiZx+0bCmShidRPZndccWnJbCkfczuDLrWVjTIDoxLbQvxGK HEKe8KleqSOEPQm7MBwanXy+nRP+0SmnI4QTjQbihRy5aPnQA1Llofkh1yZInajZQwJL GP/Avm0aRWnORORQXch05V5CEpKXYhLiTDTqu0PufuYAfhBI+AUjtc0J3gmEXAjWNbPi sF3RM1G90M/cWcUBXM+LkHnvaCH+QwYYtnrYq+YB5cUtRXsghsuoI5Glvn5ZLBS31CRw AjjNIYvGesALvCKq2jAyg/6xd326ogoVJRLmvMv0qb1zCevciP+aeM4WPkcTkaGcKq+H Dutg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723453694; x=1724058494; 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=NACQ7ELOlfYjhnP0zeg6TZ0yzINb3WcQXKg7nf5cJS8=; b=LJhoqAM9hJYhrnZMAszlnF9MTE/NrI/p0LtAKr7eA7ldh7fpncOMYd/EhfXtpItI0z KaV2ZAhb5pWN+V+oW7SXYejcZOyYCEIwrLU9wH0Uty3eywAbBj7I96qtlRNQdLvmKHMP 2Cl0pHyaaMwTwbxcWDyF9W0GT4HY50jYV6FXePBYR69ERyX1j59yytLyaQ9tZc8YjinR sh4JfDtJsLwoynMLF00CtXBtNFo6ON84qkbzHi7fur1YDKJ5AzzyH9YfRadpaQB3VlY8 1CNZt75kRd6jSU9VCCEki2ZB+69Ubx19E46vslfa1VRjyhOKU75D0znu1H+m8m2ozlit oHEQ== X-Forwarded-Encrypted: i=1; AJvYcCVAzbzln0TRGQ9DW9isGik7ueF+bYAcnxARUhd4HDLT5I238Rs1M4kkUCirX+jBTzy+0tuveyCsZfuGepnPh5w+0LM= X-Gm-Message-State: AOJu0YwiHdYSqOJp1rF5278XMZhU6h3ZS75RhraAycenG6lvaKGpTehf aPPCinQWoED6rId1iuzm0LtCt/NYaEbNZdOCMJVSQgYo5FyV+SfJ X-Google-Smtp-Source: AGHT+IGJZX9ZcwD3zVcDl0RfaEongQHdZrDxDKxiWRmiIqWGUKGPjJtJ672FdIkDuHFOVdmglMnrhw== X-Received: by 2002:a17:90a:55c3:b0:2d1:c9f9:e871 with SMTP id 98e67ed59e1d1-2d1e7f07945mr14004654a91.8.1723453693692; Mon, 12 Aug 2024 02:08:13 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d1fce6cfc1sm4450262a91.6.2024.08.12.02.08.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Aug 2024 02:08:13 -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 2/2] mm: allow read-ahead with IOCB_NOWAIT set Date: Mon, 12 Aug 2024 17:05:25 +0800 Message-Id: <20240812090525.80299-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812090525.80299-1-laoar.shao@gmail.com> References: <20240812090525.80299-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: dh36j4m4yq7f4uborh3drzk9ey3q7uak X-Rspam-User: X-Rspamd-Queue-Id: 18513C0032 X-Rspamd-Server: rspam02 X-HE-Tag: 1723453694-888413 X-HE-Meta: U2FsdGVkX1/r8+uuy2MPhP28eqSyoK0eSzqm5GQoyHUT8YX+OZSZu6v7h1z1+2sjVtqh/qM6M+dn8T89H6QFeW1JBMnlqOgm73Zng/X7BYs6gT4J0p2ljXTSwpmZ3LRhPky2segbEDVakVBSFCTuXzXYN0E+cTUdlIl86sV9oBaVA9HoybgbcG/sbaTDG2+oGnSx6FYkDTPzEfGZWPruYsawdUtr8EINkMmbZT7QV1x2yc57X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oty8SAkM4nCPqlaOOZZfYen7wMj96A1ecCRv3SgHEqBXdtGUN5ZaK56chSkLSN+FMJq70rKPud8aGSDbmFOYJuQmUFxONiR00bqi79XeaD8WEUbMFNyuQ7WusQYtl7F/MZACkDJAiMjxifOK5GV3M4ef9FrTcutfQW4+DleNmzd+3HUb5VpmHZqY7+Z2qOd1t35DiFSPeCAjYXy84jsefdXbQHfPgs2TbYG2sv9BWwSVYAnRWcU27Ag72G3mSQzKhfcAq/FNSULvkUl2nRQzXDX5bXiXC2nxNrtbAzoPbQHN7ettfcMLFSQmhgwQojefUbh75eGxkkiPKV7eME6loEn56DNqP028g9JEBW84pHtoL+5YWZhJwug2DA/P46o24DplioeeiYKwmKA= 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 behavior was altered in commit efa8480a8316 ("fs: RWF_NOWAIT should imply IOCB_NOIO") to prevent potential blocking during memory reclamation. To address the blocking issue during memory reclamation, we can use memalloc_nowait_{save,restore} to ensure non-blocking behavior. With this change, we can restore the original functionality, allowing preadv2(IOCB_NOWAIT) to read data directly from the disk if it's not already in the page cache. 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..93c690e1d1fd 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_nowait_save(); page_cache_sync_readahead(mapping, ra, filp, index, last_index - index); + if (iocb->ki_flags & IOCB_NOWAIT) + memalloc_nowait_restore(flags); filemap_get_read_batch(mapping, index, last_index - 1, fbatch); } if (!folio_batch_count(fbatch)) {