erofs: don't bother with s_stack_depth increasing for now

Previously, commit d53cd891f0e4 ("erofs: limit the level of fs stacking
for file-backed mounts") bumped `s_stack_depth` by one to avoid kernel
stack overflow when stacking an unlimited number of EROFS on top of
each other.

This fix breaks composefs mounts, which need EROFS+ovl^2 sometimes
(and such setups are already used in production for quite a long time).

One way to fix this regression is to bump FILESYSTEM_MAX_STACK_DEPTH
from 2 to 3, but proving that this is safe in general is a high bar.

After a long discussion on GitHub issues [1] about possible solutions,
one conclusion is that there is no need to support nesting file-backed
EROFS mounts on stacked filesystems, because there is always the option
to use loopback devices as a fallback.

As a quick fix for the composefs regression for this cycle, instead of
bumping `s_stack_depth` for file backed EROFS mounts, we disallow
nesting file-backed EROFS over EROFS and over filesystems with
`s_stack_depth` > 0.

This works for all known file-backed mount use cases (composefs,
containerd, and Android APEX for some Android vendors), and the fix is
self-contained.

Essentially, we are allowing one extra unaccounted fs stacking level of
EROFS below stacking filesystems, but EROFS can only be used in the read
path (i.e. overlayfs lower layers), which typically has much lower stack
usage than the write path.

We can consider increasing FILESYSTEM_MAX_STACK_DEPTH later, after more
stack usage analysis or using alternative approaches, such as splitting
the `s_stack_depth` limitation according to different combinations of
stacking.

Fixes: d53cd891f0e4 ("erofs: limit the level of fs stacking for file-backed mounts")
Reported-and-tested-by: Dusty Mabe <dusty@dustymabe.com>
Reported-by: Timothée Ravier <tim@siosm.fr>
Closes: https://github.com/coreos/fedora-coreos-tracker/issues/2087 [1]
Reported-by: "Alekséi Naidénov" <an@digitaltide.io>
Closes: https://lore.kernel.org/r/CAFHtUiYv4+=+JP_-JjARWjo6OwcvBj1wtYN=z0QXwCpec9sXtg@mail.gmail.com
Acked-by: Amir Goldstein <amir73il@gmail.com>
Acked-by: Alexander Larsson <alexl@redhat.com>
Reviewed-and-tested-by: Sheng Yong <shengyong1@xiaomi.com>
Reviewed-by: Zhiguo Niu <zhiguo.niu@unisoc.com>
Reviewed-by: Chao Yu <chao@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Miklos Szeredi <mszeredi@redhat.com>
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>

Changed files
+12 -6
fs
erofs
+12 -6
fs/erofs/super.c
··· 644 644 * fs contexts (including its own) due to self-controlled RO 645 645 * accesses/contexts and no side-effect changes that need to 646 646 * context save & restore so it can reuse the current thread 647 - * context. However, it still needs to bump `s_stack_depth` to 648 - * avoid kernel stack overflow from nested filesystems. 647 + * context. 648 + * However, we still need to prevent kernel stack overflow due 649 + * to filesystem nesting: just ensure that s_stack_depth is 0 650 + * to disallow mounting EROFS on stacked filesystems. 651 + * Note: s_stack_depth is not incremented here for now, since 652 + * EROFS is the only fs supporting file-backed mounts for now. 653 + * It MUST change if another fs plans to support them, which 654 + * may also require adjusting FILESYSTEM_MAX_STACK_DEPTH. 649 655 */ 650 656 if (erofs_is_fileio_mode(sbi)) { 651 - sb->s_stack_depth = 652 - file_inode(sbi->dif0.file)->i_sb->s_stack_depth + 1; 653 - if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { 654 - erofs_err(sb, "maximum fs stacking depth exceeded"); 657 + inode = file_inode(sbi->dif0.file); 658 + if ((inode->i_sb->s_op == &erofs_sops && !sb->s_bdev) || 659 + inode->i_sb->s_stack_depth) { 660 + erofs_err(sb, "file-backed mounts cannot be applied to stacked fses"); 655 661 return -ENOTBLK; 656 662 } 657 663 }