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 2375DC43334 for ; Fri, 10 Jun 2022 17:47:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A33C28D00D2; Fri, 10 Jun 2022 13:47:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E3948D00CB; Fri, 10 Jun 2022 13:47:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 883F28D00D2; Fri, 10 Jun 2022 13:47:05 -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 76AE18D00CB for ; Fri, 10 Jun 2022 13:47:05 -0400 (EDT) Received: from smtpin31.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4F65B4AC for ; Fri, 10 Jun 2022 17:47:05 +0000 (UTC) X-FDA: 79563057210.31.4D12503 Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) by imf13.hostedemail.com (Postfix) with ESMTP id CFAA420063 for ; Fri, 10 Jun 2022 17:47:04 +0000 (UTC) Received: by mail-qk1-f173.google.com with SMTP id c144so17906356qkg.11 for ; Fri, 10 Jun 2022 10:47:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=YAz+bZr/g5TFMIU51PB2AOx+4yCjcc3R/7cvGuIRiyo=; b=hqc887XUGL/83QS0tEfjOQ0/iBXuc7oDz8bl+SwNjlGXZf7i+yLGFlXB89q/8O8dDn fgplske5d/lSDl8EtLJzmfPjLw8iV4RkYxsUJRBwU+CAvmpzkMFFDOlVYIj2Pf7FNCvI OP9kzaZ3x1+T9/V6XaX3pXB8qvsTkE/hsIteiTJky1X5CJxGDnuB5iA7KMmLzTx3CxJ5 EfIRem8pbrvbwtdz+xZ6/avuhXvgyI5jvOJMkjFBuyHPqh6LDMbZVP1EovCbnDhMGm+y uM3GqGPl33oVQVtYr/Im9sFcDxvdPLaDGUX72iq3uiTCI+f01BemCZJ+WQYokFKdi7Wu IatQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=YAz+bZr/g5TFMIU51PB2AOx+4yCjcc3R/7cvGuIRiyo=; b=kLjIAUJX4wuN83Pd2hwIYDejSDWzurCqln1SGDgtQthpLZ0l3UszMNdzAzqcnmP8km JbfuZe0EvvSFesHEwzoAi9Hi6JZJ7DYTa+MuCqj0fOphfc66km3mVAVk/3jF+M2pLe4o AYfvU2f+ACCPf9IJjH+ULMnlUhQiC+KFXcdy/rbz13lNYV73fvx1Q13S92TgXqO2szYM Z5wxNCWRSzdz6vOy12d0cyDrsOFxQV0VgnpSrBeYLnyAX0FHxojVBygalVlOPU7bWMY/ P0om5L9S9gswBnFnGotmpYkV1qj7RBa0+MTdK6bm6X/waYjmmuhaDAVKUNl70e3+wSQl BKLw== X-Gm-Message-State: AOAM533ado4c3sq69/fDPd4goeJRUc9UJpLel2PUSAj7xAmpRC8blyRN mV43FDVnoGIAZSFf/PBJXg== X-Google-Smtp-Source: ABdhPJz7p+kepbcy956nMSgnmWWAmGzWLG9BfQ9DnHVidVR4QUb9A9T7+i9f2PROtx7QrKzhyumYNQ== X-Received: by 2002:a37:a781:0:b0:6a6:a8f5:d111 with SMTP id q123-20020a37a781000000b006a6a8f5d111mr24277439qke.676.1654883223907; Fri, 10 Jun 2022 10:47:03 -0700 (PDT) Received: from [192.168.1.210] (c-73-219-103-14.hsd1.vt.comcast.net. [73.219.103.14]) by smtp.gmail.com with ESMTPSA id bz24-20020a05622a1e9800b0030522a969e0sm61264qtb.60.2022.06.10.10.47.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 Jun 2022 10:47:03 -0700 (PDT) Message-ID: Date: Fri, 10 Jun 2022 13:47:02 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: Re: [PATCH -next] mm/filemap: fix that first page is not mark accessed in filemap_read() Content-Language: en-US To: Matthew Wilcox , Yu Kuai Cc: 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 References: <20220602082129.2805890-1-yukuai3@huawei.com> From: Kent Overstreet In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1654883224; 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:dkim-signature; bh=YAz+bZr/g5TFMIU51PB2AOx+4yCjcc3R/7cvGuIRiyo=; b=eRbMM+bnS5Iz66nlksfrU+ekX9JUX35MS6wzzA5Ueh5E/RpXXi+GTYQjXVMSyk2zcXCneP GtBb/MrD4HZ6c49usFkf3z4p6o3ImzZxmxbwcJhftAaP6K0adzx198flxcc/gzokFhqDNR Vboql2Rpf8HH/N81vU/kUw68sXLg0D4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1654883224; a=rsa-sha256; cv=none; b=Pw8aFE9UByFfZwkE6mhHboE4QurTPPNa+TmUTX/3ynLPWer5uLB2ELbMDe5B5xXwLocVpU /V+2mY85JxmNpbf6nxxe2Iy0Q3imF0N9n1w42Z/NbFYXryVeuTfEd7A29MGln5MBwDZ1sa BxDWjz7GT9ptYD+qZL1TC+RxFlC/Tx4= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=hqc887XU; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of kent.overstreet@gmail.com designates 209.85.222.173 as permitted sender) smtp.mailfrom=kent.overstreet@gmail.com Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=hqc887XU; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of kent.overstreet@gmail.com designates 209.85.222.173 as permitted sender) smtp.mailfrom=kent.overstreet@gmail.com X-Rspamd-Server: rspam08 X-Rspam-User: X-Stat-Signature: 4kumw5oeq6krer8ogum6rfs8cekmner7 X-Rspamd-Queue-Id: CFAA420063 X-HE-Tag: 1654883224-232623 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 6/10/22 10:36, Matthew Wilcox wrote: > On Fri, Jun 10, 2022 at 03:34:11PM +0100, Matthew Wilcox wrote: >> On Mon, Jun 06, 2022 at 09:10:03AM +0800, Yu Kuai wrote: >>> On 2022/06/03 2:30, Matthew Wilcox wrote: >>>> On Thu, Jun 02, 2022 at 04:21:29PM +0800, Yu Kuai wrote: >>>>> In filemap_read(), 'ra->prev_pos' is set to 'iocb->ki_pos + copied', >>>>> while it should be 'iocb->ki_ops'. >>>> >>>> Can you walk me through your reasoning which leads you to believe that >>>> it should be ki_pos instead of ki_pos + copied? As I understand it, >>>> prev_pos is the end of the previous read, not the beginning of the >>>> previous read. >>> >>> Hi, Matthew >>> >>> The main reason is the following judgement in flemap_read(): >>> >>> if (iocb->ki_pos >> PAGE_SHIFT != -> current page >>> ra->prev_pos >> PAGE_SHIFT) -> previous page >>> folio_mark_accessed(fbatch.folios[0]); >>> >>> Which means if current page is the same as previous page, don't mark >>> page accessed. However, prev_pos is set to 'ki_pos + copied' during last >>> read, which will cause 'prev_pos >> PAGE_SHIFT' to be current page >>> instead of previous page. >>> >>> I was thinking that if prev_pos is set to the begining of the previous >>> read, 'prev_pos >> PAGE_SHIFT' will be previous page as expected. Set to >>> the end of previous read is ok, however, I think the caculation of >>> previous page should be '(prev_pos - 1) >> PAGE_SHIFT' instead. >> >> OK, I think Kent broke this in 723ef24b9b37 ("mm/filemap/c: break >> generic_file_buffered_read up into multiple functions"). Before: >> >> - 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); >> >> After: >> + if (iocb->ki_pos >> PAGE_SHIFT != ra->prev_pos >> PAGE_SHIFT) >> + mark_page_accessed(page); >> >> So surely this should have been: >> >> + if (iocb->ki_pos != ra->prev_pos) >> + mark_page_accessed(page); >> >> Kent, do you recall why you changed it the way you did? > > Oh, and if this is the right diagnosis, then this is the fix for the > current tree: > > +++ b/mm/filemap.c > @@ -2673,8 +2673,7 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, > * 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) > + if (iocb->ki_pos != ra->prev_pos) > folio_mark_accessed(fbatch.folios[0]); > > for (i = 0; i < folio_batch_count(&fbatch); i++) { > > 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++) {