Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp32959587rwd; Sat, 8 Jul 2023 02:23:45 -0700 (PDT) X-Google-Smtp-Source: APBJJlGuraUcrLpxoI+VHzD4KgW3fK5Q7kSCFy3EzvBO1xsAgn8zK4kjbErj1nOxHxcdoDd0v6mh X-Received: by 2002:a05:6a20:3d82:b0:127:76ab:a6ff with SMTP id s2-20020a056a203d8200b0012776aba6ffmr16650064pzi.22.1688808225659; Sat, 08 Jul 2023 02:23:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688808225; cv=none; d=google.com; s=arc-20160816; b=muYesFFhTDgl942EZq6MLbSco3UWv5ez5iGZQUPTgPZzPA2Imjg9NqwfovpbLVsM6w +iWxYoMh4tww65XP99tT32bhzygCXoER3GhGE6iHibsQyS+DPJ4D9w8OwF4XCW6OZd44 4xdWTMmjrrL0ja1BA63U/WKc7NfBeB20UrCMVbzoDzUCfYULIxAY+9erLZB9yi+eMbFv ECuRYUGlUrYyYUHSA3i1gTMI0OQba/z5U5p3Uf9QctFHSaC9p++x3dwHN1CwaKb6dJPi 5Dl/ksBpQJnMEcgvLjhtF++FrKaWEEzXL4cuZtnDablbwi4uk5XoEpYxmVA+PHcWUq8R VmHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id; bh=utObBmXDrrpjsH6aMhartpPAs8R1HVHpYzcuvl3f0nM=; fh=oX2SwJyHdU1i0uIJdHxYf8a4O6/4qGRRcAhuK+4/U6A=; b=GFoQ7YmyVLN/k8sLH7/8/y8Is0JcAziCpmBMwyWMFJv6v6YI4XwBGyxpRA8M7r5gnM Y326kE2DvLPhRs7ZHH53bj64iylvAOcLrHv3TcpymgJ7tPa08lQjJ/c//Y/B7u/6NeRa ckN3s6C7iZJmrFRPIDIymX34pLUFIuiRYCx39i/DUVxfFOoLHB4Xco8Zu05PayPVo1kj qsY25Om0E8tUBg8R9rUghBJQ5Hp0vKejlZyEd5d60OE9KLil2rFxfWqYb4yFgzhou5ZJ HPYn97ip87HK5LJYc6KNQyrVmnAY9IUXBhsD6jjyJ+2gf3kmAL80FFmJ7jaSbcvD15RU Xq0w== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i18-20020a63e452000000b0055ba60a3301si5418859pgk.295.2023.07.08.02.23.31; Sat, 08 Jul 2023 02:23:45 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229751AbjGHJA1 (ORCPT + 99 others); Sat, 8 Jul 2023 05:00:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229496AbjGHJA0 (ORCPT ); Sat, 8 Jul 2023 05:00:26 -0400 Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E099D1723 for ; Sat, 8 Jul 2023 02:00:20 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R711e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046050;MF=hsiangkao@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0VmrW6mA_1688806814; Received: from 172.20.10.3(mailfrom:hsiangkao@linux.alibaba.com fp:SMTPD_---0VmrW6mA_1688806814) by smtp.aliyun-inc.com; Sat, 08 Jul 2023 17:00:16 +0800 Message-ID: <97875049-8df9-e041-61ca-d90723ba6e82@linux.alibaba.com> Date: Sat, 8 Jul 2023 17:00:09 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: [PATCH] erofs: fix two loop issues when read page beyond EOF To: Chunhai Guo , xiang@kernel.org, chao@kernel.org Cc: huyue2@coolpad.com, jefflexu@linux.alibaba.com, linux-erofs@lists.ozlabs.org, linux-kernel@vger.kernel.org References: <20230708062432.67344-1-guochunhai@vivo.com> From: Gao Xiang In-Reply-To: <20230708062432.67344-1-guochunhai@vivo.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL 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 Hi Chunhai, On 2023/7/8 14:24, Chunhai Guo wrote: > When z_erofs_read_folio() reads a page with an offset far beyond EOF, two > issues may occur: > - z_erofs_pcluster_readmore() may take a long time to loop when the offset > is big enough, which is unnecessary. > - For example, it will loop 4691368 times and take about 27 seconds > with following case. > - offset = 19217289215 > - inode_size = 1442672 > - z_erofs_do_read_page() may loop infinitely due to the inappropriate > truncation in the below statement. Since the offset is 64 bits and > min_t() truncates the result to 32 bits. The solution is to replace > unsigned int with another 64-bit type, such as erofs_off_t. > cur = end - min_t(unsigned int, offset + end - map->m_la, end); > - For example: > - offset = 0x400160000 > - end = 0x370 > - map->m_la = 0x160370 > - offset + end - map->m_la = 0x400000000 > - offset + end - map->m_la = 0x00000000 (truncated as unsigned int) Thanks for the catch! Could you split these two into two patches? how about using: cur = end - min_t(erofs_off_t, offend + end - map->m_la, end) for this? since cur and end are all [0, PAGE_SIZE - 1] for now, and folio_size() later. > - Expected result: > - cur = 0 > - Actual result: > - cur = 0x370 > > Signed-off-by: Chunhai Guo > --- > fs/erofs/zdata.c | 13 ++++++++++--- > 1 file changed, 10 insertions(+), 3 deletions(-) > > diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c > index 5f1890e309c6..6abbd4510076 100644 > --- a/fs/erofs/zdata.c > +++ b/fs/erofs/zdata.c > @@ -972,7 +972,8 @@ static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, > struct erofs_map_blocks *const map = &fe->map; > const loff_t offset = page_offset(page); > bool tight = true, exclusive; > - unsigned int cur, end, spiltted; > + erofs_off_t cur, end; > + unsigned int spiltted; > int err = 0; > > /* register locked file pages as online pages in pack */ > @@ -1035,7 +1036,7 @@ static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, > */ > tight &= (fe->mode > Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE); > > - cur = end - min_t(unsigned int, offset + end - map->m_la, end); > + cur = end - min_t(erofs_off_t, offset + end - map->m_la, end); > if (!(map->m_flags & EROFS_MAP_MAPPED)) { > zero_user_segment(page, cur, end); > goto next_part; > @@ -1841,7 +1842,7 @@ static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f, > } > > cur = map->m_la + map->m_llen - 1; > - while (cur >= end) { > + while ((cur >= end) && (cur < i_size_read(inode))) { > pgoff_t index = cur >> PAGE_SHIFT; > struct page *page; > > @@ -1876,6 +1877,12 @@ static int z_erofs_read_folio(struct file *file, struct folio *folio) > trace_erofs_readpage(page, false); > f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; > > + /* when trying to read beyond EOF, return zero page directly */ > + if (f.headoffset >= i_size_read(inode)) { > + zero_user_segment(page, 0, PAGE_SIZE); > + return 0; > + } Do we really need to optimize this rare case? I guess the follow readmore fix is enough, thoughts? Thanks, Gao Xiang