From patchwork Thu Oct 17 06:23:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pankaj Raghav (Samsung)" X-Patchwork-Id: 13839488 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 EFA03D2F7EF for ; Thu, 17 Oct 2024 06:23:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 728166B0083; Thu, 17 Oct 2024 02:23:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D7506B0088; Thu, 17 Oct 2024 02:23:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 59FDC6B008A; Thu, 17 Oct 2024 02:23:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 39ADA6B0083 for ; Thu, 17 Oct 2024 02:23:55 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 823FAAC5A1 for ; Thu, 17 Oct 2024 06:23:31 +0000 (UTC) X-FDA: 82682103210.21.F15984D Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by imf21.hostedemail.com (Postfix) with ESMTP id 37B831C0017 for ; Thu, 17 Oct 2024 06:23:31 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=y806h+0N; spf=pass (imf21.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.152 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com; dmarc=pass (policy=quarantine) header.from=pankajraghav.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729146183; 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=uWJq3jm/0K8u47eg5W/WP541CVXGY7oY6qDd69RidKg=; b=kKbUmiuPHFVrC+Swz7LbLot3FWYIkb0VS1n9vMcd15VbsDYSN8vRJl+XDyG/ntdZtx5/SO ob+kJpj5kbXn0gykifr3IySZifuIByzd+wJ8nLNNpSBkqAIhGFEBwhgTOIjmjJj64z6bAG x9yyLQopLixDRT2Y4V6kITVZcjZEsvc= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=y806h+0N; spf=pass (imf21.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.152 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com; dmarc=pass (policy=quarantine) header.from=pankajraghav.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729146183; a=rsa-sha256; cv=none; b=D7jcFhafxWJbMWniio4a0UZ6sJP9CCgA0k5As2YYn2KYxAGL39Rd/QTOtdZtnFRaLoByve c6EpE8jCtkBJQVygHKXpsJZKMRjG5vTaGYyr7zkk/CWF6v6M4G+04zGkxKBLoQS2JDHcmt 5QcDIP9W18fjiycC7QdnQhEEIycO+/U= Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4XTd815SCRz9stW; Thu, 17 Oct 2024 08:23:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1729146225; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=uWJq3jm/0K8u47eg5W/WP541CVXGY7oY6qDd69RidKg=; b=y806h+0NNTLh6Cm/5dlCNQ7YJsjcWcBmDCHdVl35RdxBV4Oa6KHVZ2Fq69MJAHTqyr7wOT QAq1P+lXIXP+dnAacf+wvm0STgLFNdjBjDqX9FL/2OwGNHc/9OM1mdiHgfLkF8Q8pHUKjV LsPqvk3aLvfhE6ub+e7nxzb1awOCk3zOvZwOtmRGjOTgyscb0V4S1w5Rqn9xgPmy50UtSG ipkCwZBnw167O9gZuJR5L4+EAqio+s9djqvCwUFJ90YP6uN5hjNFzntjsNGyhWoaCgOC3q jS6JeC+9v4AEalr93KrumvGjpWq4VOKURv5Tx6rarY/7n9ETZQsQw3gsvAAA+A== From: "Pankaj Raghav (Samsung)" To: akpm@linux-foundation.org Cc: kernel@pankajraghav.com, willy@infradead.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, mcgrof@kernel.org, gost.dev@samsung.com, Pankaj Raghav Subject: [PATCH v2] mm: don't set readahead flag on a folio when lookahead_size > nr_to_read Date: Thu, 17 Oct 2024 08:23:42 +0200 Message-ID: <20241017062342.478973-1-kernel@pankajraghav.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 88ejwid1pmoq4hp5f9ksufri9um3aezw X-Rspamd-Queue-Id: 37B831C0017 X-Rspamd-Server: rspam11 X-HE-Tag: 1729146211-650607 X-HE-Meta: U2FsdGVkX19MG7SOBnTWWL8jtBM5CSVeoAnHjSZKWt2pgA6uHcQNFTA36GfP/zIhtcz8CMpB2Tr00bHfsRgqg72AD+RkWqNIuKeDY68DHVRr5YdhUnY830f6CmWLesyDWYQrNbwf8yKReD2JseIibF7OzEBY5roFV+CWYInPRlRymoyTrPWLvmhSCzpDiBh3hldVuVHezd6FJJDWpye45d0J4EMxkg2Fm2/Gw/sw5HC+YubT7ssinny9j6WF5HogbIKjH2mR4a47FsON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Ig== 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: From: Pankaj Raghav The readahead flag is set on a folio based on the lookahead_size and nr_to_read. For example, when the readahead happens from index to index + nr_to_read, then the readahead `mark` offset from index is set at nr_to_read - lookahead_size. There are some scenarios where the lookahead_size > nr_to_read. For example, readahead window was created, but the file was truncated before the readahead starts. do_page_cache_ra() will clamp the nr_to_read if the readahead window extends beyond EOF after truncation. If this happens, readahead flag should not be set on any folio on the current readahead window. The current calculation for `mark` with mapping_min_order > 0 gives incorrect results when lookahead_size > nr_to_read due to rounding up operation: index = 128 nr_to_read = 16 lookahead_size = 28 mapping_min_order = 4 (16 pages) ra_folio_index = round_up(128 + 16 - 28, 16) = 128; mark = 128 - 128 = 0; # offset from index to set RA flag In the above example, the lookahead_size is actually lying outside the current readahead window. Without this patch, RA flag will be set incorrectly on the folio at index 128. This can lead to marking the readahead flag on the wrong folio, therefore, triggering a readahead when it is not necessary. Explicitly initialize `mark` to be ULONG_MAX and only calculate it when lookahead_size is within the readahead window. Fixes: 26cfdb395eef ("readahead: allocate folios with mapping_min_order in readahead") Signed-off-by: Pankaj Raghav --- mm/readahead.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) base-commit: d61a00525464bfc5fe92c6ad713350988e492b88 diff --git a/mm/readahead.c b/mm/readahead.c index 3dc6c7a128dd..475d2940a1ed 100644 --- a/mm/readahead.c +++ b/mm/readahead.c @@ -206,9 +206,9 @@ void page_cache_ra_unbounded(struct readahead_control *ractl, unsigned long nr_to_read, unsigned long lookahead_size) { struct address_space *mapping = ractl->mapping; - unsigned long ra_folio_index, index = readahead_index(ractl); + unsigned long index = readahead_index(ractl); gfp_t gfp_mask = readahead_gfp_mask(mapping); - unsigned long mark, i = 0; + unsigned long mark = ULONG_MAX, i = 0; unsigned int min_nrpages = mapping_min_folio_nrpages(mapping); /* @@ -232,9 +232,14 @@ void page_cache_ra_unbounded(struct readahead_control *ractl, * index that only has lookahead or "async_region" to set the * readahead flag. */ - ra_folio_index = round_up(readahead_index(ractl) + nr_to_read - lookahead_size, - min_nrpages); - mark = ra_folio_index - index; + if (lookahead_size <= nr_to_read) { + unsigned long ra_folio_index; + + ra_folio_index = round_up(readahead_index(ractl) + + nr_to_read - lookahead_size, + min_nrpages); + mark = ra_folio_index - index; + } nr_to_read += readahead_index(ractl) - index; ractl->_index = index;