stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Chao Yu <chao@kernel.org>,
	Gao Xiang <hsiangkao@linux.alibaba.com>
Subject: [PATCH 5.10 18/21] erofs: fix unsafe pagevec reuse of hooked pclusters
Date: Fri, 19 Nov 2021 18:37:53 +0100	[thread overview]
Message-ID: <20211119171444.470625833@linuxfoundation.org> (raw)
In-Reply-To: <20211119171443.892729043@linuxfoundation.org>

From: Gao Xiang <hsiangkao@linux.alibaba.com>

commit 86432a6dca9bed79111990851df5756d3eb5f57c upstream.

There are pclusters in runtime marked with Z_EROFS_PCLUSTER_TAIL
before actual I/O submission. Thus, the decompression chain can be
extended if the following pcluster chain hooks 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 (general protection fault) by ro_fsstressing a LZMA image for long
time.

I'm going to actively 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

Link: https://lore.kernel.org/r/20211103182006.4040-1-xiang@kernel.org
Fixes: 3883a79abd02 ("staging: erofs: introduce VLE decompression support")
Cc: <stable@vger.kernel.org> # 4.19+
Reviewed-by: Chao Yu <chao@kernel.org>
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 fs/erofs/zdata.c |   13 +++++++------
 fs/erofs/zpvec.h |   13 ++++++++++---
 2 files changed, 17 insertions(+), 9 deletions(-)

--- a/fs/erofs/zdata.c
+++ b/fs/erofs/zdata.c
@@ -278,8 +278,8 @@ static inline bool z_erofs_try_inplace_i
 
 /* 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;
 
@@ -289,9 +289,9 @@ static int z_erofs_attach_page(struct z_
 	    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;
 }
 
@@ -645,7 +645,8 @@ hitted:
 		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 staging page for pagevec */
 	if (err == -EAGAIN) {
 		struct page *const newpage =
@@ -653,7 +654,7 @@ retry:
 
 		newpage->mapping = Z_EROFS_MAPPING_STAGING;
 		err = z_erofs_attach_page(clt, newpage,
-					  Z_EROFS_PAGE_TYPE_EXCLUSIVE);
+					  Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
 		if (!err)
 			goto retry;
 	}
--- a/fs/erofs/zpvec.h
+++ b/fs/erofs/zpvec.h
@@ -107,11 +107,18 @@ static inline void z_erofs_pagevec_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);



  parent reply	other threads:[~2021-11-19 17:38 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-19 17:37 [PATCH 5.10 00/21] 5.10.81-rc1 review Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 01/21] fortify: Explicitly disable Clang support Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 02/21] block: Add a helper to validate the block size Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 03/21] loop: Use blk_validate_block_size() to validate " Greg Kroah-Hartman
2021-11-19 21:45   ` Pavel Machek
2021-11-20  7:39     ` Greg Kroah-Hartman
2021-11-21 16:59       ` Pavel Machek
2021-11-19 17:37 ` [PATCH 5.10 04/21] bootconfig: init: Fix memblock leak in xbc_make_cmdline() Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 05/21] net: stmmac: add clocks management for gmac driver Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 06/21] net: stmmac: platform: fix build error with !CONFIG_PM_SLEEP Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 07/21] net: stmmac: fix missing unlock on error in stmmac_suspend() Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 08/21] net: stmmac: fix system hang if change mac address after interface ifdown Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 09/21] net: stmmac: fix issue where clk is being unprepared twice Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 10/21] net: stmmac: dwmac-rk: fix unbalanced pm_runtime_enable warnings Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 11/21] arm64: vdso32: suppress error message for make mrproper Greg Kroah-Hartman
2021-11-19 21:47   ` Pavel Machek
2021-11-19 22:24     ` Nick Desaulniers
2021-11-19 22:30       ` Nick Desaulniers
2021-11-21  7:48       ` Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 12/21] x86/iopl: Fake iopl(3) CLI/STI usage Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 13/21] parisc/entry: fix trace test in syscall exit path Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 14/21] PCI/MSI: Destroy sysfs before freeing entries Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 15/21] PCI/MSI: Deal with devices lying about their MSI mask capability Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 16/21] PCI: Add MSI masking quirk for Nvidia ION AHCI Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 17/21] erofs: remove the occupied parameter from z_erofs_pagevec_enqueue() Greg Kroah-Hartman
2021-11-19 17:37 ` Greg Kroah-Hartman [this message]
2021-11-19 17:37 ` [PATCH 5.10 19/21] scripts/lld-version.sh: Rewrite based on upstream ld-version.sh Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 20/21] perf/core: Avoid put_page() when GUP fails Greg Kroah-Hartman
2021-11-19 17:37 ` [PATCH 5.10 21/21] thermal: Fix NULL pointer dereferences in of_thermal_ functions Greg Kroah-Hartman
2021-11-19 18:46 ` [PATCH 5.10 00/21] 5.10.81-rc1 review Florian Fainelli
2021-11-19 19:46 ` Fox Chen
2021-11-20  0:49 ` Shuah Khan
2021-11-20  7:28 ` Pavel Machek
2021-11-20 15:21 ` Sudip Mukherjee
2021-11-20 15:27 ` Daniel Díaz
2021-11-20 16:06   ` Naresh Kamboju
2021-11-21  7:49     ` Greg Kroah-Hartman
2021-11-20 16:52 ` Guenter Roeck
2021-11-22  2:48 ` Samuel Zou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211119171444.470625833@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=chao@kernel.org \
    --cc=hsiangkao@linux.alibaba.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).