From mboxrd@z Thu Jan 1 00:00:00 1970 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 74A3DC43334 for ; Fri, 10 Jun 2022 18:34:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF0E98D00D8; Fri, 10 Jun 2022 14:34:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DA0388D00CB; Fri, 10 Jun 2022 14:34:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C8F148D00D8; Fri, 10 Jun 2022 14:34:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B5CD38D00CB for ; Fri, 10 Jun 2022 14:34:40 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8C9AA20592 for ; Fri, 10 Jun 2022 18:34:40 +0000 (UTC) X-FDA: 79563177120.10.96A99A9 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf20.hostedemail.com (Postfix) with ESMTP id 91FE01C006E for ; Fri, 10 Jun 2022 18:34:39 +0000 (UTC) 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: ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1654886080; a=rsa-sha256; cv=none; b=8qwtooSwRnnuKPotrFIAZnx4ew59+TSq+CKR8zFeQMJsfyisy5wRbixrJ7RPmh4BuHDkCJ iCXXmp4NGm84Wwz12ExF+LYTN9gfllAOMbmmNMHdkl2IdahtpuQi4uFUzK2U6EUDpsALWu n+wFXJGz0jBJxENrFFSRzUatHoLFiNA= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=J+6nFrtP; spf=none (imf20.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1654886080; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5ds5WQBITtKB6EEQtW4hoMd9Sg3HLa34Vavd3wlK3BM=; b=B4SORtfe4LldVuqpW8EXHUZydSi7gHbcJUjoTvI6uGw9SpxZv/xL6OlYM9tPcI55C+NHDs 4jLmHxtgMbvrBVa5KzbWT3bOmTGW3vds/NArh1Hiz0ejMP2P29LvXbYHrbRBALnKykylE2 l0YgWUcjw8NSp7bu+4DKYSuenm/aTlw= X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 91FE01C006E X-Stat-Signature: uc1e6tim34pnj7osghxeozowejwebjq3 X-Rspam-User: Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=J+6nFrtP; spf=none (imf20.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none X-HE-Tag: 1654886079-110102 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: 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++) {