Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2129861iog; Sun, 19 Jun 2022 08:14:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s4K4qETiTHNlSzyamiUNw3cnsMIgMPx98VJUnXyZzLQBxXossfamuTVjFmJxKv9zuItUoP X-Received: by 2002:a05:6402:4144:b0:431:6ef0:bef7 with SMTP id x4-20020a056402414400b004316ef0bef7mr24711146eda.151.1655651652142; Sun, 19 Jun 2022 08:14:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655651652; cv=none; d=google.com; s=arc-20160816; b=W8sd8t3gFaigGT7H0WOR4WAdKSo/qGK3RmWPeGgjBSaANO2tQ8WjsvY54S1f09++gO RQZfNkrKxUIjGb+QEzEDuKwId89pqHx/w20A17ukGL7IA/NzyjTsJR5gYTkd6BoK0iem HBFcxgSJvElrerkwsUF2Rk4yolYNvqnStiiGQyS3xRA46RUiCXCyks/xg1Fzbpzo9Ycz MjZ/hgViYxVSbfr+WBeIDHyzmwOXtG/2HjGC6wmkuL1J8g7JfX2t13iOccPrw0gGG9OJ QtsGWiySVIIIEL3PTEmAOQaaETKMkroL2c7UuYHPlGPX/ZUjRNcdqRfmIBxnRLWi/GHV lhLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bindIO2lTFDesoOeQ2oViKfEfCZlyJ8TfNVBigsijEU=; b=mcSplNVUZM5BO7CvCD0oII4p8BEmeiV5cYlcYdWP8F6XPVUaYiAVRNw7V+xL8xaDkI OYuJGps+Hk2knpIHWgxMKXw5AV7EiaISUJjGPfqqtJgYzI//sZBHwF2t/HiKhw7hZ0UR 1u5KILjajqcBW1hgHk+HDlQ4+oJJW8IthIPnFu1uKS3+gaw2nlHi3ZlQUxL2kGas+SUE PJjMSIHYwCf6QZ9SntKpmv0RAR6UqLFdqG1HOh3Qx26VPqIPgWQrK5M6I4ptPCuaCuO6 HSnCXhNHQXbFiP4cJmrIPYq3+HMwixSryJVLe0c2fK4QWEl6NnPrlQhv0Ftksy7V9H0j WlqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=LFYmATCu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h21-20020a170906111500b006f39e0cbe8esi6542070eja.227.2022.06.19.08.13.45; Sun, 19 Jun 2022 08:14:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=LFYmATCu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233262AbiFSPME (ORCPT + 99 others); Sun, 19 Jun 2022 11:12:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231438AbiFSPLv (ORCPT ); Sun, 19 Jun 2022 11:11:51 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E86B6AE56; Sun, 19 Jun 2022 08:11:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=bindIO2lTFDesoOeQ2oViKfEfCZlyJ8TfNVBigsijEU=; b=LFYmATCu1fX3l78jDNbK+xhNAo DcgucQf7ONWKGyPqRpVHlCUmhmynjxiGS9VGyA6neq2L8jTOMjkb8NmhEArLoabLJyWsGC/xSVtED xy+pBQ314fj/6ws20wk/YlffE786PNkPua4pe8DEjyJZ9tnsH9+7u7aeI5n/RU5fi3XX14U8m8nm4 vEZTIuOjcuFbBJ2gYgAGkq0Z7nv2OnusSgNwE5KLogbSvK26zxBKPyr7N+Gi29o2dOlu+YI9Wh+da umaqTxE5cd3nfTrioNl1gqhwSBpRuJQ/I1gnYIgkaB6BL0re7iafEHA8ZSMPizsETgpK4pM3L2LJz W5Bd5m+w==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1o2waZ-004QOq-3d; Sun, 19 Jun 2022 15:11:47 +0000 From: "Matthew Wilcox (Oracle)" To: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, Yu Kuai , Kent Overstreet Subject: [PATCH 1/3] filemap: Correct the conditions for marking a folio as accessed Date: Sun, 19 Jun 2022 16:11:41 +0100 Message-Id: <20220619151143.1054746-2-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220619151143.1054746-1-willy@infradead.org> References: <20220619151143.1054746-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We had an off-by-one error which meant that we never marked the first page in a read as accessed. This was visible as a slowdown when re-reading a file as pages were being evicted from cache too soon. In reviewing this code, we noticed a second bug where a multi-page folio would be marked as accessed multiple times when doing reads that were less than the size of the folio. Abstract the comparison of whether two file positions are in the same folio into a new function, fixing both of these bugs. Reported-by: Yu Kuai Reviewed-by: Kent Overstreet Signed-off-by: Matthew Wilcox (Oracle) --- mm/filemap.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index ac3775c1ce4c..577068868449 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2629,6 +2629,13 @@ static int filemap_get_pages(struct kiocb *iocb, struct iov_iter *iter, return err; } +static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio) +{ + unsigned int shift = folio_shift(folio); + + return (pos1 >> shift == pos2 >> shift); +} + /** * filemap_read - Read data from the page cache. * @iocb: The iocb to read. @@ -2700,11 +2707,11 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, writably_mapped = mapping_writably_mapped(mapping); /* - * When a sequential read accesses a page several times, only + * When a read accesses the same folio several times, only * mark it as accessed the first time. */ - if (iocb->ki_pos >> PAGE_SHIFT != - ra->prev_pos >> PAGE_SHIFT) + if (!pos_same_folio(iocb->ki_pos, ra->prev_pos - 1, + fbatch.folios[0])) folio_mark_accessed(fbatch.folios[0]); for (i = 0; i < folio_batch_count(&fbatch); i++) { -- 2.35.1