Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp32882026rwd; Sat, 8 Jul 2023 00:31:36 -0700 (PDT) X-Google-Smtp-Source: APBJJlHUVJoRCJEjjfI8CloKDPmt51ou1uOBEw+nPXsQVjQccEccHtMNnOZvGvxuky61Pjxod0hu X-Received: by 2002:a17:902:be0f:b0:1b5:694:b1a9 with SMTP id r15-20020a170902be0f00b001b50694b1a9mr7307682pls.32.1688801495390; Sat, 08 Jul 2023 00:31:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1688801495; cv=pass; d=google.com; s=arc-20160816; b=NYfacwNi7rPJkexn13REQWD1UVUBoM5GbEx2BF318S/nh8NSuRcF3Ysezc0UBcjScR SNIMGWVEhiqAIipoIJV38ki9IKGKArmVcQImxyBgMT9pjNt+IT+bdCU03l2LyyuxYHLj OoWqZJCOaLf+qAAT/I4fopwk25xBo6GzxGtWm6R5s6gNMEj5353fsNsKSsGAUW3Lkrv+ qDkzniunSKR2rZHvK5m92HSnrJci9H6wmio7RtmwFO5q9/XTg/qjyl7BajkWBQVIJAzt Xl+YbxqBLygSMO3Nc06dsrmKpM6z5UmBxhlCMzdBxrDWTkoFM/MeGI1YfRk46DR6/vYf r5hg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :message-id:date:subject:cc:to:from:dkim-signature; bh=D5hyXC2cc1KjGWSui4/Lb6jxFgrEn2mlVSFkJ8lo3uw=; fh=PkewMBRNl0zhdWgkLiSxHLYhy5XLxeqrO2YwORoMSmA=; b=U/94Rdn9jh7UuWInypXG317ZaRLAasNn14/v7F6Fr7OQGpv0VugamywOUqygSiIBcu zGa7Wd5KGk0Vz/gcIQNGpwjmu7JN5JahbF3a9+CVGytWesYr8VNaocepG37A3MQrfoZ1 WFnIyrS7aWb9i7dypD+/ldKyDIbfl6JIhKM39VC1JTDKUWyIFrf5qclu6EZyDhyKBz7u MJ847dypC3iujCqnGIn1HHDLMyRr3JdDXn+437PMhmBpC1xRfs6a6L47ECTsMMg98+U/ 6bHwOtCr/qNUA2IQUSKAGueOxuSHN2B2SveQUkvN/Ea046FYtHQeRwqLEO3jzQ/D013V xr0g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@vivo.com header.s=selector2 header.b=RfnDyjGw; arc=pass (i=1 spf=pass spfdomain=vivo.com dkim=pass dkdomain=vivo.com dmarc=pass fromdomain=vivo.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=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=vivo.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ju4-20020a170903428400b001b8b37f6b46si4880494plb.556.2023.07.08.00.31.18; Sat, 08 Jul 2023 00:31:35 -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; dkim=pass header.i=@vivo.com header.s=selector2 header.b=RfnDyjGw; arc=pass (i=1 spf=pass spfdomain=vivo.com dkim=pass dkdomain=vivo.com dmarc=pass fromdomain=vivo.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=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=vivo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230245AbjGHGZj (ORCPT + 99 others); Sat, 8 Jul 2023 02:25:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229571AbjGHGZi (ORCPT ); Sat, 8 Jul 2023 02:25:38 -0400 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2103.outbound.protection.outlook.com [40.107.117.103]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 118071FE7 for ; Fri, 7 Jul 2023 23:25:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RWTetpgVFFU5Ra2Ha+YqwPAfGMXsgbJUdzTltWelxciyhITa/XJybqkGgFym5Ebg5f6tBH235CoSboESeIV7VEZqGUe62jxp32qTPywNpkABYRWPjThnKCI3803HrdzFHvH1/Olkz5wRRY9TYHx5cSzX6C5V6ONJowQTolRaG4reSUF1WiHU74K4Xq3wAGce74mZ8wHrv1E9sKL4RZ0lkAzpm1GByAF+DrGCFqPIkykQI0dIAsnYk9YmLf3VAEERJtliqUnuaZt6W2h/Yza7cO/an4CuznC33wyukDtbvn8O8DoXmzjDDT4hkYouIVvRbfkCojfLoNOSSgexydW5jQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=D5hyXC2cc1KjGWSui4/Lb6jxFgrEn2mlVSFkJ8lo3uw=; b=nEXs6ktm7YpUz+As5BCC8MOcwX/RMz6lW++1jyrQYuzKt5mhf7rDnNllvIfVs+Fk6u0NfKPbzg4ehVwUAlNpwYUWsDVl9MYuqGgtXRwLcPvqR0l/0ItZYba4XXPd9vJLYEV5zMzRfElJAHXagWbpjXoXe3fT2D71DaVOXbkMBAx6aJh7RgBCDqPwinToVZ9sdwT7bYLjJozGAfzK0XOkHmlrWtaulKvNe0Wdg+lAV7zR6Z8Dah4ei8CQqp5dl99p5Ia570K/bO9DbSW1+riEuKJWIYTqizwXfWy59iFzVteDTgMYanOQkGCBYIs2yyTIECA+FaTvjZ4J/Om5Y3sdzA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com; dkim=pass header.d=vivo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=D5hyXC2cc1KjGWSui4/Lb6jxFgrEn2mlVSFkJ8lo3uw=; b=RfnDyjGwPMk98Otlm+V/A6jvHPh5OZlpj0Bkq+RE1z3EsHX42n/XGBYXYFvksiG8h/7U8tkOJNKvqxHccMecbMOxd8y0KhlWf6Ye05xy9ltHrK+NhQ7cUZYyVrEQBzxmfnDmGxHzaGc04DFRTzK/G066N6yXZtgPxOEJ3wR19IedlpBD+p0AtQSXD3faiK5/UjuYjRCJgk2Qkui7YB8Vlaj5wVNCKt7KmbIYvE3KDRevL6UoleMsWn09xpQd/51AWRFpLYYAlR1ZCMSUiTmcWOgZdsuwtfhE1SWCTGweRULbCZbYDVkb66h05kV3v4j2bqCMTw8TCPirgZy/06yeTg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vivo.com; Received: from TY2PR06MB3342.apcprd06.prod.outlook.com (2603:1096:404:fb::23) by PSAPR06MB4503.apcprd06.prod.outlook.com (2603:1096:301:8c::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6565.26; Sat, 8 Jul 2023 06:25:33 +0000 Received: from TY2PR06MB3342.apcprd06.prod.outlook.com ([fe80::2b85:24ad:2492:c96c]) by TY2PR06MB3342.apcprd06.prod.outlook.com ([fe80::2b85:24ad:2492:c96c%5]) with mapi id 15.20.6565.026; Sat, 8 Jul 2023 06:25:33 +0000 From: Chunhai Guo To: xiang@kernel.org, chao@kernel.org Cc: huyue2@coolpad.com, jefflexu@linux.alibaba.com, linux-erofs@lists.ozlabs.org, linux-kernel@vger.kernel.org, Chunhai Guo Subject: [PATCH] erofs: fix two loop issues when read page beyond EOF Date: Sat, 8 Jul 2023 14:24:32 +0800 Message-Id: <20230708062432.67344-1-guochunhai@vivo.com> X-Mailer: git-send-email 2.39.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SI2PR02CA0023.apcprd02.prod.outlook.com (2603:1096:4:195::11) To TY2PR06MB3342.apcprd06.prod.outlook.com (2603:1096:404:fb::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TY2PR06MB3342:EE_|PSAPR06MB4503:EE_ X-MS-Office365-Filtering-Correlation-Id: bc6bf441-165d-4a18-1938-08db7f7c2208 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 52RwkpjHMX47KBzbp6ThDAmhC/roiw/LiVeyt23pBX3GIy9NMvzPhf86y3o2tueqjy/h5oJvDiGvdMeYEmnTlWxPk5Qh60NHHFyoA77EIW65UOxXqGE8wzc/nIUxJxmDF9LGen9CYqUchLzcbHh5gcjX4SxW+fv+9nBHpqwgFRKuLur4PQCXId4yp+ADC1L05VvUT5Y5TPkb7oAPOme7AhO1AXNsJ3T+8OjdkysDfXLWL9xRDmD+VOB8tLgRS2v0RMDiucZM/3izn/268qVMyvEQtTZbk4p/18mjWgI1T+EoIRExSOJ+ETf2PlqhWqMEpKvpS5zRH3XTL4Kit4Z9JP/hNlrxUqpt3dzKfVqle2YEJ0Aag8uOSoJH+3PWfOREJxY4YwtqTcdilMcy7IDLjRGkFnCvw80VznnVauIQg6+3SzSifUu6Q0CcYO8TbvAMf40MEDmIiwoFQyKyctbn0A9CsELjL4uBYkBSzjP41qUy0mSfyiPAMhJB8UvPW64Oq6y+R2pRaeswLv+MnMy+fiLOiGPlrjOd1Dq3Nq8qyd00F9lEZfiAjzc7XklA0iE9A8MD3ci0HsYL19zTb5B04ZfM+kmY1RM24sun+tT4tRf0iTsVseenGMfF6qHe7YIA X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:TY2PR06MB3342.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(346002)(376002)(366004)(136003)(396003)(39850400004)(451199021)(41300700001)(5660300002)(6486002)(2906002)(316002)(8676002)(8936002)(4326008)(66476007)(66556008)(66946007)(36756003)(52116002)(6512007)(478600001)(107886003)(86362001)(186003)(38100700002)(2616005)(38350700002)(83380400001)(6506007)(1076003)(26005);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?eAGsGzH40NzUXhkzbB12d9DTJ5aI6I03ulawyNt90nMnr+96sc3cBgkxGX+8?= =?us-ascii?Q?XELQQjq6eGCG4l6eiTylwyt8tv/TKWOVEQ73rep4P1nKxKuBcHUCFOBtTzU+?= =?us-ascii?Q?DcNCcpKGERf1pVCKnaUVCkt3YdcX58PlUeu9/iLsgF19XCFJWgpKKLz9+Eo3?= =?us-ascii?Q?tgpd+I0nJ3REyIYMiC5PBHzM4ek4DzFXiiXFOBxK3fy0h/8JbfXkofRH6nRX?= =?us-ascii?Q?EyTS2/5DbvUWTKrZ0qSMVRFkFVK0ZKetIJ4+8qYdZ3wYEjKL34SNvBvpiltG?= =?us-ascii?Q?ZSf2VUR9benPDhlMkJZuCdYds/sbL1aewCjrSKtIDPzY0nHzhLi1iGdsIPTP?= =?us-ascii?Q?7un6vU6QG9JdtBYye1N8Jr5cl/2iAsjCERzY9aNuFnYsK9xgp6Mc5Y0W7dI0?= =?us-ascii?Q?SxlQrD10e9kedM5KuQJqTOnOHT3Bsf650NzYoo4qgqJfPHjSV/XK5ROmB2js?= =?us-ascii?Q?U1Z5PMAZdk2UXR2e9QQVSWT/eGpbS7X2vBFk3WE8Ur35OjwLvYbj3WH5WJy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egtALi+4KNoH2GdS1sM0eqNn?= =?us-ascii?Q?IECapZTOaB1ACLmq5OEEOaXRaiXsqsKYSakfq+oddGqhdJQEKJh/bzvOaCHr?= =?us-ascii?Q?PEyTIuD/1zjb9QN6QJHMKk30AuLBW81LexoNw1Cek9lOmcx/y703J2zfaodh?= =?us-ascii?Q?medgDs3dws3YKQWNEpTEKTUELzApaS+jG65aKdAx?= X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: bc6bf441-165d-4a18-1938-08db7f7c2208 X-MS-Exchange-CrossTenant-AuthSource: TY2PR06MB3342.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jul 2023 06:25:32.9775 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 923e42dc-48d5-4cbe-b582-1a797a6412ed X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: pRb6DHBizRuKwg6+3Jz56Mbm8V/y6awwPVC/UT7WzdUXJQZWjiz9SBdr7JuxFeF1ymeoRxRC3lG/Eo7Iy1yQ4A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PSAPR06MB4503 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED 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 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) - 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; + } + z_erofs_pcluster_readmore(&f, NULL, true); err = z_erofs_do_read_page(&f, page); z_erofs_pcluster_readmore(&f, NULL, false); -- 2.25.1