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 12201CCA473 for ; Mon, 13 Jun 2022 01:31:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 61CB08D013E; Sun, 12 Jun 2022 21:31:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5CBC68D013C; Sun, 12 Jun 2022 21:31:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 444348D013E; Sun, 12 Jun 2022 21:31:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 320F28D013C for ; Sun, 12 Jun 2022 21:31:19 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id ECCB160C24 for ; Mon, 13 Jun 2022 01:31:18 +0000 (UTC) X-FDA: 79571484636.16.0A769CA Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf24.hostedemail.com (Postfix) with ESMTP id 65E33180082 for ; Mon, 13 Jun 2022 01:31:17 +0000 (UTC) Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4LLvBp6Zk0zjXZc; Mon, 13 Jun 2022 09:30:10 +0800 (CST) Received: from kwepemm600009.china.huawei.com (7.193.23.164) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 13 Jun 2022 09:31:13 +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; Mon, 13 Jun 2022 09:31:12 +0800 Subject: Re: [PATCH -next] mm/filemap: fix that first page is not mark accessed in filemap_read() To: Matthew Wilcox CC: Kent Overstreet , , , , , , References: <20220602082129.2805890-1-yukuai3@huawei.com> From: Yu Kuai Message-ID: <7e9889b7-8eeb-5e97-3f4b-cdc914a032f4@huawei.com> Date: Mon, 13 Jun 2022 09:31:11 +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: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm600009.china.huawei.com (7.193.23.164) X-CFilter-Loop: Reflected ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655083878; a=rsa-sha256; cv=none; b=cNwIOvgj2ar4WWVztMb7B6tIDl8/dO9kFSTdwr/Gzk/50R/w6u3NBjcOL4nLNBIb1SmiMs FKGPo2w2w6TlHUVLKShokPycT0jEFKX+AOQPAWadFmjBT4uqU7MDnCbXLUqKYCJD0m9hZc +SIXKem5SKVGOLMned6QaKEZk80sMhQ= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of yukuai3@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=yukuai3@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655083878; 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=3F8NOkeC4lzinXX+B0N6BTnsPlsJL10wl3I0n++lOSs=; b=L0c3um0XI6Q6VPMB15aQO2McqWtdzWyJGCqn8aCSIOlWRTW8jH12T93aQDIr7pogyhIEia NJAZMVX0al9zrAi0d7ysfR3TGlItfjq/DyKQOP4qaXK3XAH42MDIWEc3Zdk4f2fynNytqs +1zSWxq0Hjqhl/ln45EKi2cJjPQqhqg= X-Stat-Signature: 8ekhz5mtpqssu7uisbwcuyxdp41ygisg X-Rspam-User: X-Rspamd-Queue-Id: 65E33180082 X-Rspamd-Server: rspam07 Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of yukuai3@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=yukuai3@huawei.com X-HE-Tag: 1655083877-151693 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/12 1:42, Matthew Wilcox дµÀ: > On Sat, Jun 11, 2022 at 04:23:42PM +0800, Yu Kuai wrote: >>> 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. > > Did you test it? This is clearly a little subtle ;-) Yes, I confirmed that with this patch, small sequential read will mark page accessed. However, multi-page folio is not tested yet. > >> BTW, I still think the fix should be commit 06c0444290ce ("mm/filemap.c: >> generic_file_buffered_read() now uses find_get_pages_contig"). > > Hmm, yes. That code also has problems, but they're more subtle and > probably don't amount to much. > > - iocb->ki_pos += copied; > - > - /* > - * When a sequential read accesses a page several times, > - * only mark it as accessed the first time. > - */ > - if (iocb->ki_pos >> PAGE_SHIFT != ra->prev_pos >> PAGE_SHIFT) > - mark_page_accessed(page); > - > - ra->prev_pos = iocb->ki_pos; > > This will mark the page accessed when we _exit_ a page. So reading > 512-bytes at a time from offset 0, we'll mark page 0 as accessed on the > first read (because the prev_pos is initialised to -1). Then on the > eighth read, we'll mark page 0 as accessed again (because ki_pos will > now be 4096 and prev_pos is 3584). We'll then read chunks of page 1 > without marking it as accessed, until we're about to step into page 2. You are right, I didn't think of that situation. > > Marking page 0 accessed twice is bad; it'll set the referenced bit the > first time, and then the second time, it'll activate it. So it'll be > thought to be part of the workingset when it's really just been part of > a streaming read. > > And the last page we read will never be marked accessed unless it > happens to finish at the end of a page. > > Before Kent started his refactoring, I think it worked: > > - pgoff_t prev_index; > - unsigned int prev_offset; > ... > - prev_index = ra->prev_pos >> PAGE_SHIFT; > - prev_offset = ra->prev_pos & (PAGE_SIZE-1); > ... > - if (prev_index != index || offset != prev_offset) > - mark_page_accessed(page); > - prev_index = index; > - prev_offset = offset; > ... > - ra->prev_pos = prev_index; > - ra->prev_pos <<= PAGE_SHIFT; > - ra->prev_pos |= prev_offset; > > At least, I don't detect any bugs in this. Sure, thanks for your explanation.