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 45EA4C433EF for ; Sat, 11 Jun 2022 08:23:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 906898D010D; Sat, 11 Jun 2022 04:23:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8B4CD8D00F7; Sat, 11 Jun 2022 04:23:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77C628D010D; Sat, 11 Jun 2022 04:23:49 -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 68C648D00F7 for ; Sat, 11 Jun 2022 04:23:49 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 310A860756 for ; Sat, 11 Jun 2022 08:23:49 +0000 (UTC) X-FDA: 79565266578.26.3CF92D8 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf27.hostedemail.com (Postfix) with ESMTP id A873B40080 for ; Sat, 11 Jun 2022 08:23:47 +0000 (UTC) Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4LKrQk5pw7zgYF0; Sat, 11 Jun 2022 16:21:50 +0800 (CST) Received: from kwepemm600009.china.huawei.com (7.193.23.164) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sat, 11 Jun 2022 16:23:43 +0800 Received: from [10.174.176.73] (10.174.176.73) by kwepemm600009.china.huawei.com (7.193.23.164) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sat, 11 Jun 2022 16:23:42 +0800 Subject: Re: [PATCH -next] mm/filemap: fix that first page is not mark accessed in filemap_read() To: Matthew Wilcox , Kent Overstreet CC: , , , , , References: <20220602082129.2805890-1-yukuai3@huawei.com> From: Yu Kuai Message-ID: Date: Sat, 11 Jun 2022 16:23:42 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="gbk"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.176.73] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemm600009.china.huawei.com (7.193.23.164) X-CFilter-Loop: Reflected ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1654935828; a=rsa-sha256; cv=none; b=kc5Tr0hswFXNINXdhja2xPn8fdqujAYsFQl6AdjKyXBp8Fka95x2KSGmYDWNYN3mOucswz XBDCjfIo+JgNMrW4S032OPUdLSGnZ7TaKn+k2ASZolgJj11gMUZdoq3muVO9xZ6zkuYwrL 0eUIcbnDI71Y8uMW5ZlAo1yh00+61Uc= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of yukuai3@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=yukuai3@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1654935828; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tbqU10A9w4v6LKvIvkUXCzNlHr06nYhoUrbD9wOtbTk=; b=63PRe4W1NFECdoF0DhnSdHrYz3JzZx3pzfnMUIFVyLFFs3EU+aEBlHsUd9nKTKjIR5jJWx L9W27cGruJxedw9IiEDosxHK7comVu/F0O1ZpmcslRx1bVkGU7Cs6ag1oRaGqDvGGTczOv 3uSyIKjeIMkE5V8Q/n4cBF9kakz+u/w= X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: A873B40080 X-Rspam-User: Authentication-Results: imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of yukuai3@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=yukuai3@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Stat-Signature: apiddzs4na13ro7rhtchux86wgurdgnf X-HE-Tag: 1654935827-688649 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: ÔÚ 2022/06/11 2:34, Matthew Wilcox дµÀ: > 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? > Hi, Matthew Thanks for the patch, it looks good to me. BTW, I still think the fix should be commit 06c0444290ce ("mm/filemap.c: generic_file_buffered_read() now uses find_get_pages_contig"). > > 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++) { > > . >