From patchwork Tue Oct 15 16:41:06 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: 13836703 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 CECE6D1D88B for ; Tue, 15 Oct 2024 16:41:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5E2456B0082; Tue, 15 Oct 2024 12:41:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 591BB6B0083; Tue, 15 Oct 2024 12:41:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 459856B0085; Tue, 15 Oct 2024 12:41: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 267686B0082 for ; Tue, 15 Oct 2024 12:41:17 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B1FE2C0BA3 for ; Tue, 15 Oct 2024 16:41:06 +0000 (UTC) X-FDA: 82676401584.16.4A0C7EB Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by imf16.hostedemail.com (Postfix) with ESMTP id E9E08180016 for ; Tue, 15 Oct 2024 16:41:06 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=UPJz3miN; dmarc=pass (policy=quarantine) header.from=pankajraghav.com; spf=pass (imf16.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.152 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729010317; 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=MocHuP0EhrLPN/nAkv+wZUpjwH3uim1hitLn0pXPYFA=; b=aK+YIx4n/EsV9qeClBOaOnZlDbmnyvDN44KZMdyvm+9SfubuJWvBGgnCX8wGk7T4YpSZhh XGac0s75SqNFzpPBjxnalsCdg0ZsK4N5AIWA05AKZGWLkB0iBiAUReE0l/2PehxuLmtvGU ZBRKDkkDXKV7s7U+k4AeoINShA00sZI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729010317; a=rsa-sha256; cv=none; b=G5O1owAfRKNxaxZgLNfcCx3pzdGqlnIMwMDfsLOfaB0bRpgqO8FeQaThZT1yOQhqnGxQjx wChX/xOJERhSziff1rQL3OzlxBwjK2tYi7V3lOvVWmlidwoaWgSAEkJ44nx79EwTyvVJ9J PkFnNPxyqnWJnm4XwmycOArCs28yRBM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=UPJz3miN; dmarc=pass (policy=quarantine) header.from=pankajraghav.com; spf=pass (imf16.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.152 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com Received: from smtp2.mailbox.org (smtp2.mailbox.org [10.196.197.2]) (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 4XSfxK5960z9sv3; Tue, 15 Oct 2024 18:41:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1729010469; 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=MocHuP0EhrLPN/nAkv+wZUpjwH3uim1hitLn0pXPYFA=; b=UPJz3miNQ52YWS/6Q66WKBZk160F0liquQwrCTWyelkPTDcQTvR3eEa4GrN8SbGCOiCU+a 4RNhyDnreaHPFkaTC1VrR51TcPkU3dVCZ7Duyaua2YLbAeIw1XfGK0nK5frBqNBdLFkPGI C1tYeUHuGRM90SLpNmFADNZ+gTfzXbFyCzn+/OAGOkrn/BQLy5THUZZ4aA8mJfUvlbvruG vznqpa0w1e5z0vsniL9u/gcT7gohOtk6N6YvPySSaLEyClJLZeFehlj6/nebWAfNVJO4WM TGiUfQh9jYfBhMQt2LDTQ+lue+VAoYpQ2OMmDqdfbI31Nfp2Hx0VY2faKAlbRQ== From: "Pankaj Raghav (Samsung)" To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, willy@infradead.org, linux-fsdevel@vger.kernel.org, mcgrof@kernel.org, gost.dev@samsung.com, kernel@pankajraghav.com, Pankaj Raghav Subject: [PATCH v14] mm: don't set readahead flag on a folio when lookahead_size > nr_to_read Date: Tue, 15 Oct 2024 18:41:06 +0200 Message-ID: <20241015164106.465253-1-kernel@pankajraghav.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E9E08180016 X-Stat-Signature: d6tk3nicmitdca5tbc1pohrub5we1pg7 X-Rspam-User: X-HE-Tag: 1729010466-532063 X-HE-Meta: U2FsdGVkX1/woJ8wfloJUxbzekJDZF8+AgOGsSWHt+OVZ3Ej5+PUSnLHKLpmxZqNzVszHbBUlgc01MI6JxySIt8Fh5YsmTxr0nAFVjhZhVwqNfoYJZ2TkRs5UFvDYttNxAz7sCXgTR6KJhirIqtHDYGIDelQAl8LAZ/MN/RrEpcIStRPJY9NOlL1kOikXqN49zCWcqJTfkLqlq1907edXJVRX9pVdbjTkObs4h0QZBm/gJHxlcoUClb8rwwfSD9qtBo934iJfXwK8O3I41UoUsGK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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. If this happens, readahead flag is not set on any folio on the current readahead window. There are two problems at the moment in the way `mark` is calculated when lookahead_size > nr_to_read: - unsigned long `mark` will be assigned a negative value which can lead to unexpected results in extreme cases due to wrap around. - The current calculation for `mark` with mapping_min_order > 0 gives incorrect results when lookahead_size > nr_to_read due to rounding up operation. 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;