Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp1650873iol; Fri, 10 Jun 2022 11:49:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJylu13znxrioxBvPYLhnVAko2aqPZh48BZTE39tuF1V+MhNZU6KxFLPGWZ4ATzob4bKIr4I X-Received: by 2002:a05:6402:4386:b0:431:9cc4:1214 with SMTP id o6-20020a056402438600b004319cc41214mr19433898edc.45.1654886949456; Fri, 10 Jun 2022 11:49:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654886949; cv=none; d=google.com; s=arc-20160816; b=vf+IhQdGu8mnTK6wOT+UfiNIjEIKZeFuT2BlCfeoNSO2xeFi6xzueWtyOva3vkR8aS 13RqEa5owAvNrmvwyysAfP/J5NqSJAUk3gj5tfSk+oYrlq1cQtmeHpk0RPU03LxpPhTL X7ujT7BhdHUKgs1UgwnDTLD+hM6UYnzjAldJcAYus+KGn5VNre7eYecqozJLJPlAfSCk vuobFyHA204fT8GhmcG9dkBXTqCG6IH6Qdqtw/7QPKzknwzf/v9qsapL4Fckh3FaGDSH 2a6ZPM/SJAtZsveXjnAONUpBqoc8jFPYjHfoyL438stK9PcNXVFO/Ii3ayM9Bkx7fKav p27g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=5ds5WQBITtKB6EEQtW4hoMd9Sg3HLa34Vavd3wlK3BM=; b=Z0yIk9L1XFbcu0lXUHAK/sOAOCc1JXsHfQuxKsnMLM4MRqbcNe7lxgmnTgG+qV7e3z K5ECn5wzbvLoycytczuLffR6aLRkjRfSYWM0Tl/7DdyGGw9oDiR7p0tviXOC6Byq7oHp U5ZP/nnJX5rP0B5EvrAue/Leb54Oz61sRf02uRys7inNBmaRYJFfM2owce6ZoI0bZqJK +9nBXqu7UGnuLPGIfxhVbzTsYu7IOx33+43TeZcAgGOFOrJO2midJQ6AWijZICZZQWf7 buo2wF2pOK4Lg56uhWBZuowCylXVTCp/Qslc68AcQARwCS2IJn3EvWVFBizvmYDkbHHg l/Rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=J+6nFrtP; 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 hr38-20020a1709073fa600b00711dbfcc0aesi8609640ejc.486.2022.06.10.11.48.43; Fri, 10 Jun 2022 11:49:09 -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=J+6nFrtP; 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 S1350152AbiFJSfG (ORCPT + 99 others); Fri, 10 Jun 2022 14:35:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349792AbiFJSeh (ORCPT ); Fri, 10 Jun 2022 14:34:37 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 888193A197; Fri, 10 Jun 2022 11:34:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=5ds5WQBITtKB6EEQtW4hoMd9Sg3HLa34Vavd3wlK3BM=; b=J+6nFrtPQRWqS0sBmw87Kkv/bJ 8Sz2jKPCtpG6GIG2+Q5erX23oNN8K5YTXCOJplN+4Gw3drhbTgmHoggZD70XobYEyXeYZXDdZtLO2 ptTD6vKuCHL1onHZ4Th6xYQFS8TTlm9Bj7KdOR3IgxxwJ4uSy5H8MixcxKagAGH/CIVO/9zzKGQB+ NkSvxpsU03UsXoy9Ss5kzlqXJyCD2Z8PsUT8tnfsH+OWPjsCpj1S6IURai8dA5G297qktZUxwQhNe VrIpR+r50cJtBUcG3vXt1CdXJdl2Jf98gSOFCTlLIfeSFqljmMOM9OkEFElID0duHr42Y0ioKS4Ng IsBHaA6Q==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1nzjSi-00EeyM-DD; Fri, 10 Jun 2022 18:34:24 +0000 Date: Fri, 10 Jun 2022 19:34:24 +0100 From: Matthew Wilcox To: Kent Overstreet Cc: Yu Kuai , akpm@linux-foundation.org, axboe@kernel.dk, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, yi.zhang@huawei.com Subject: Re: [PATCH -next] mm/filemap: fix that first page is not mark accessed in filemap_read() Message-ID: References: <20220602082129.2805890-1-yukuai3@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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,URIBL_BLOCKED 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 On Fri, Jun 10, 2022 at 01:47:02PM -0400, Kent Overstreet wrote: > I think this is the fix we want - I think Yu basically had the right idea > and had the off by one fix, this should be clearer though: > > Yu, can you confirm the fix? > > -- >8 -- > Subject: [PATCH] filemap: Fix off by one error when marking folios accessed > > In filemap_read() we mark pages accessed as we read them - but we don't > want to do so redundantly, if the previous read already did so. > > But there was an off by one error: we want to check if the current page > was the same as the last page we read from, but the last page we read > from was (ra->prev_pos - 1) >> PAGE_SHIFT. > > Reported-by: Yu Kuai > Signed-off-by: Kent Overstreet > > diff --git a/mm/filemap.c b/mm/filemap.c > index 9daeaab360..8d5c8043cb 100644 > --- a/mm/filemap.c > +++ b/mm/filemap.c > @@ -2704,7 +2704,7 @@ ssize_t filemap_read(struct kiocb *iocb, struct > iov_iter *iter, > * mark it as accessed the first time. > */ > if (iocb->ki_pos >> PAGE_SHIFT != > - ra->prev_pos >> PAGE_SHIFT) > + (ra->prev_pos - 1) >> PAGE_SHIFT) > folio_mark_accessed(fbatch.folios[0]); > > for (i = 0; i < folio_batch_count(&fbatch); i++) { > This is going to mark the folio as accessed multiple times if it's a multi-page folio. How about this one? diff --git a/mm/filemap.c b/mm/filemap.c index 5f227b5420d7..a30587f2e598 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2599,6 +2599,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. @@ -2670,11 +2677,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++) {