Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp700794pxb; Wed, 3 Nov 2021 10:53:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxh8fjuwEkD17GMLv3s5T5tp2cVYgYhXpKD237yTDZg2Eh8wYE3Z9usFpLV+5jTcjgUVsKN X-Received: by 2002:a17:906:1f0d:: with SMTP id w13mr37131018ejj.161.1635961995832; Wed, 03 Nov 2021 10:53:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635961995; cv=none; d=google.com; s=arc-20160816; b=S8dT2Q3vAIJ3x8ROvbzV3q5yzs5krlHYKKIhDFfsLR+eIxUJ4kPfu8Ccr5lNqmeRRe HlJB0zGcBEE8L7YsgG8m9QggS0Zbtg7AOXfyjGSnCv57R/Sf4QPMUx4zL+64Y0dOa76o 97djRSACLZXpygOWWt8ntsrbWwSh5Vgk6PcAR7J0lSrYF54BwYHkhhiY22U6ikmEPkq8 y8rPMglaQ5T6P97j2ejtYBarkFekmdn40iZd6IbfjJ76sSfH33A7fFq9one1ON9jbLvt q6OSaoRwXP7TPVCwXukqsF3/hsvZouiHirks39mnnwSB2GbBQXjwIa+1Tr8ZfNGHEYRF LsGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=LyWbgMfEg8NDCRaqj9yeGaIhGQ2lKW/Zwv28yKauS6Y=; b=FG+60vI90X8zI+aDrvcozHKQSLB2KpFFe81HJqDhf8LIlUf3PPYGCUbkLMffhYpKUY bACLU/VggN7WJFU5n+ZxAlzBBeUy/I9uE7pIPFit2VykmkZyGD9rZ+peX9Dsv0iPCYxX v7SSh1gc/Mr3CrOVijho3eHorUa1ofHNruuwfwBSBX8TzTslKyUPxMxsN6IDbfyfOOQF t6yns6PQkzK3gi5Dx6zVpG7Jk+FwmxjqBJwSWzBNoX2yoWkBDAONW499TpAz/0CQeQvu 6Xoxg5jz0WgJ0Uby7cY69YIKDfc3LniWyllLpCemHdi4kVbXYpf7x1Yl46vKqKpIr1W5 4GqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uZfS6Ee4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id nc35si5403398ejc.662.2021.11.03.10.52.51; Wed, 03 Nov 2021 10:53:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uZfS6Ee4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231147AbhKCRx3 (ORCPT + 99 others); Wed, 3 Nov 2021 13:53:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:57292 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231380AbhKCRwq (ORCPT ); Wed, 3 Nov 2021 13:52:46 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A7E0660F90; Wed, 3 Nov 2021 17:50:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1635961809; bh=I7T2p9hGK8SvEPslDuk3l6+20Nz99bU1hW+iZqP6kEA=; h=From:To:Cc:Subject:Date:From; b=uZfS6Ee44JNWwy6mguQFZkJqlMEFe1nwGhQ1fEm9v8k3KfWHaYgtdVrUjLFLKngWB QbpMRqnXH+2u9KRHhel5HuP7mWwYmiK38h1twNOLydC1SIir/5vIoRnvt+0auATcfo SClWjWe2Y/muxtGGfZnnzfLtF++3quKFltm1Ebm6m/j6MuX8y/zYie01O5GRTozJFo z6VlotKr2R/Bog6uWCdocXDfMycsP9e85w5tgiY0Iw8FwdIKLNofecCKWQA7WWhDDM 8R0VxU9Mpq4hs01jLUwDiZHhQV6DDoNv+VnPdfWDZp4K/X24oj49Uzp6PWPofGx0xM z2J4VUd24LQBg== From: Gao Xiang To: linux-erofs@lists.ozlabs.org, Chao Yu Cc: LKML , Gao Xiang , stable@vger.kernel.org Subject: [PATCH] erofs: fix unsafe pagevec reuse of hooked pclusters Date: Thu, 4 Nov 2021 01:49:53 +0800 Message-Id: <20211103174953.3209-1-xiang@kernel.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are pclusters in runtime marked with Z_EROFS_PCLUSTER_TAIL before actual I/O submission. Thus, the submission chain can be extended if the following pcluster chain hook such tail pcluster. As the related comment mentioned, if some page is made of a hooked pcluster and another followed pcluster, it can be reused for in-place I/O (since I/O should be submitted anyway): _______________________________________________________________ | tail (partial) page | head (partial) page | |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________| However, it's by no means safe to reuse as pagevec since if such PRIMARY_HOOKED pclusters finally move into bypass chain without I/O submission. It's somewhat hard to reproduce with LZ4 and I just found it by ro_fsstress a LZMA image for long time. I'm going to clean up related code together with multi-page folio adaption in the next few months. Let's address it directly for easier backporting for now. Call trace for reference: z_erofs_decompress_pcluster+0x10a/0x8a0 [erofs] z_erofs_decompress_queue.isra.36+0x3c/0x60 [erofs] z_erofs_runqueue+0x5f3/0x840 [erofs] z_erofs_readahead+0x1e8/0x320 [erofs] read_pages+0x91/0x270 page_cache_ra_unbounded+0x18b/0x240 filemap_get_pages+0x10a/0x5f0 filemap_read+0xa9/0x330 new_sync_read+0x11b/0x1a0 vfs_read+0xf1/0x190 Fixes: 3883a79abd02 ("staging: erofs: introduce VLE decompression support") Cc: # 4.19+ Signed-off-by: Gao Xiang --- fs/erofs/zdata.c | 13 +++++++------ fs/erofs/zpvec.h | 13 ++++++++++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c index 11c7a1aaebad..eb51df4a9f77 100644 --- a/fs/erofs/zdata.c +++ b/fs/erofs/zdata.c @@ -373,8 +373,8 @@ static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt, /* callers must be with collection lock held */ static int z_erofs_attach_page(struct z_erofs_collector *clt, - struct page *page, - enum z_erofs_page_type type) + struct page *page, enum z_erofs_page_type type, + bool pvec_safereuse) { int ret; @@ -384,9 +384,9 @@ static int z_erofs_attach_page(struct z_erofs_collector *clt, z_erofs_try_inplace_io(clt, page)) return 0; - ret = z_erofs_pagevec_enqueue(&clt->vector, page, type); + ret = z_erofs_pagevec_enqueue(&clt->vector, page, type, + pvec_safereuse); clt->cl->vcnt += (unsigned int)ret; - return ret ? 0 : -EAGAIN; } @@ -729,7 +729,8 @@ static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED); retry: - err = z_erofs_attach_page(clt, page, page_type); + err = z_erofs_attach_page(clt, page, page_type, + clt->mode >= COLLECT_PRIMARY_FOLLOWED); /* should allocate an additional short-lived page for pagevec */ if (err == -EAGAIN) { struct page *const newpage = @@ -737,7 +738,7 @@ static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE); err = z_erofs_attach_page(clt, newpage, - Z_EROFS_PAGE_TYPE_EXCLUSIVE); + Z_EROFS_PAGE_TYPE_EXCLUSIVE, true); if (!err) goto retry; } diff --git a/fs/erofs/zpvec.h b/fs/erofs/zpvec.h index dfd7fe0503bb..b05464f4a808 100644 --- a/fs/erofs/zpvec.h +++ b/fs/erofs/zpvec.h @@ -106,11 +106,18 @@ static inline void z_erofs_pagevec_ctor_init(struct z_erofs_pagevec_ctor *ctor, static inline bool z_erofs_pagevec_enqueue(struct z_erofs_pagevec_ctor *ctor, struct page *page, - enum z_erofs_page_type type) + enum z_erofs_page_type type, + bool pvec_safereuse) { - if (!ctor->next && type) - if (ctor->index + 1 == ctor->nr) + if (!ctor->next) { + /* some pages cannot be reused as pvec safely without I/O */ + if (type == Z_EROFS_PAGE_TYPE_EXCLUSIVE && !pvec_safereuse) + type = Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED; + + if (type != Z_EROFS_PAGE_TYPE_EXCLUSIVE && + ctor->index + 1 == ctor->nr) return false; + } if (ctor->index >= ctor->nr) z_erofs_pagevec_ctor_pagedown(ctor, false); -- 2.20.1