1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Jan Kara <jack@suse.cz>
3 Date: Tue, 20 Dec 2022 12:38:45 +0100
4 Subject: udf: Allocate name buffer in directory iterator on heap
6 commit 0aba4860b0d0216a1a300484ff536171894d49d8 upstream.
8 Currently we allocate name buffer in directory iterators (struct
9 udf_fileident_iter) on stack. These structures are relatively large
10 (some 360 bytes on 64-bit architectures). For udf_rename() which needs
11 to keep three of these structures in parallel the stack usage becomes
12 rather heavy - 1536 bytes in total. Allocate the name buffer in the
13 iterator from heap to avoid excessive stack usage.
15 Link: https://lore.kernel.org/all/202212200558.lK9x1KW0-lkp@intel.com
16 Reported-by: kernel test robot <lkp@intel.com>
17 Signed-off-by: Jan Kara <jack@suse.cz>
18 [Add extra include linux/slab.h]
19 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
21 fs/udf/directory.c | 24 ++++++++++++++++--------
22 fs/udf/udfdecl.h | 2 +-
23 2 files changed, 17 insertions(+), 9 deletions(-)
25 --- a/fs/udf/directory.c
26 +++ b/fs/udf/directory.c
28 #include <linux/bio.h>
29 #include <linux/crc-itu-t.h>
30 #include <linux/iversion.h>
31 +#include <linux/slab.h>
33 static int udf_verify_fi(struct udf_fileident_iter *iter)
35 @@ -248,9 +249,14 @@ int udf_fiiter_init(struct udf_fileident
39 + iter->namebuf = kmalloc(UDF_NAME_LEN_CS0, GFP_KERNEL);
43 - if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
44 - return udf_copy_fi(iter);
45 + if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
46 + err = udf_copy_fi(iter);
50 if (inode_bmap(dir, iter->pos >> dir->i_blkbits, &iter->epos,
51 &iter->eloc, &iter->elen, &iter->loffset) !=
52 @@ -260,17 +266,17 @@ int udf_fiiter_init(struct udf_fileident
54 "position %llu not allocated in directory (ino %lu)\n",
55 (unsigned long long)pos, dir->i_ino);
56 - return -EFSCORRUPTED;
57 + err = -EFSCORRUPTED;
60 err = udf_fiiter_load_bhs(iter);
64 err = udf_copy_fi(iter);
68 udf_fiiter_release(iter);
75 int udf_fiiter_advance(struct udf_fileident_iter *iter)
76 @@ -307,6 +313,8 @@ void udf_fiiter_release(struct udf_filei
79 iter->bh[0] = iter->bh[1] = NULL;
80 + kfree(iter->namebuf);
81 + iter->namebuf = NULL;
84 static void udf_copy_to_bufs(void *buf1, int len1, void *buf2, int len2,
85 --- a/fs/udf/udfdecl.h
86 +++ b/fs/udf/udfdecl.h
87 @@ -99,7 +99,7 @@ struct udf_fileident_iter {
88 struct extent_position epos; /* Position after the above extent */
89 struct fileIdentDesc fi; /* Copied directory entry */
90 uint8_t *name; /* Pointer to entry name */
91 - uint8_t namebuf[UDF_NAME_LEN_CS0]; /* Storage for entry name in case
92 + uint8_t *namebuf; /* Storage for entry name in case
93 * the name is split between two blocks