diff mbox series

[RFCv1,20/72] libext2fs: avoid too much memory allocation in case fs_num_threads

Message ID 5ae4498b906ea4adffcca5546e2c9deba39dd05a.1667822611.git.ritesh.list@gmail.com
State Under Review
Delegated to: Theodore Ts'o
Headers show
Series e2fsprogs: Parallel fsck support | expand

Commit Message

Ritesh Harjani (IBM) Nov. 7, 2022, 12:21 p.m. UTC
From: Wang Shilong <wshilong@ddn.com>

e2fsck init memory according to filesystem inodes/dir numbers
recorded in the superblock, this should be aware of filesystem
number of threads, otherwise, oom can happen.

So in case of fs->fs_num_threads, this patch controls the amount of
memory consumed for running multiple threads in e2fsck.

Signed-off-by: Wang Shilong <wshilong@ddn.com>
Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
---
 lib/ext2fs/dblist.c | 2 ++
 lib/ext2fs/icount.c | 4 ++++
 2 files changed, 6 insertions(+)

Comments

Andreas Dilger Nov. 18, 2022, 1:37 p.m. UTC | #1
On Nov 7, 2022, at 06:24, Ritesh Harjani (IBM) <ritesh.list@gmail.com> wrote:
> 
> From: Wang Shilong <wshilong@ddn.com>
> 
> e2fsck init memory according to filesystem inodes/dir numbers
> recorded in the superblock, this should be aware of filesystem
> number of threads, otherwise, oom can happen.
> 
> So in case of fs->fs_num_threads, this patch controls the amount of
> memory consumed for running multiple threads in e2fsck.
> 
> Signed-off-by: Wang Shilong <wshilong@ddn.com>
> Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>

Reviewed-by: Andreas Dilger <adilger@dilger.ca>

> ---
> lib/ext2fs/dblist.c | 2 ++
> lib/ext2fs/icount.c | 4 ++++
> 2 files changed, 6 insertions(+)
> 
> diff --git a/lib/ext2fs/dblist.c b/lib/ext2fs/dblist.c
> index 5568b8ec..c19e17bc 100644
> --- a/lib/ext2fs/dblist.c
> +++ b/lib/ext2fs/dblist.c
> @@ -58,6 +58,8 @@ static errcode_t make_dblist(ext2_filsys fs, ext2_ino_t size,
>        if (retval)
>            goto cleanup;
>        dblist->size = (num_dirs * 2) + 12;
> +        if (fs->fs_num_threads)
> +            dblist->size /= fs->fs_num_threads;
>    }
>    len = (size_t) sizeof(struct ext2_db_entry2) * dblist->size;
>    dblist->count = count;
> diff --git a/lib/ext2fs/icount.c b/lib/ext2fs/icount.c
> index 766eccca..48665c7e 100644
> --- a/lib/ext2fs/icount.c
> +++ b/lib/ext2fs/icount.c
> @@ -237,6 +237,8 @@ errcode_t ext2fs_create_icount_tdb(ext2_filsys fs EXT2FS_NO_TDB_UNUSED,
>     * value.
>     */
>    num_inodes = fs->super->s_inodes_count - fs->super->s_free_inodes_count;
> +    if (fs->fs_num_threads)
> +        num_inodes /= fs->fs_num_threads;
> 
>    icount->tdb = tdb_open(fn, num_inodes, TDB_NOLOCK | TDB_NOSYNC,
>                   O_RDWR | O_CREAT | O_TRUNC, 0600);
> @@ -288,6 +290,8 @@ errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
>        if (retval)
>            goto errout;
>        icount->size += fs->super->s_inodes_count / 50;
> +        if (fs->fs_num_threads)
> +            icount->size /= fs->fs_num_threads;
>    }
> 
>    bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
> -- 
> 2.37.3
>
diff mbox series

Patch

diff --git a/lib/ext2fs/dblist.c b/lib/ext2fs/dblist.c
index 5568b8ec..c19e17bc 100644
--- a/lib/ext2fs/dblist.c
+++ b/lib/ext2fs/dblist.c
@@ -58,6 +58,8 @@  static errcode_t make_dblist(ext2_filsys fs, ext2_ino_t size,
 		if (retval)
 			goto cleanup;
 		dblist->size = (num_dirs * 2) + 12;
+		if (fs->fs_num_threads)
+			dblist->size /= fs->fs_num_threads;
 	}
 	len = (size_t) sizeof(struct ext2_db_entry2) * dblist->size;
 	dblist->count = count;
diff --git a/lib/ext2fs/icount.c b/lib/ext2fs/icount.c
index 766eccca..48665c7e 100644
--- a/lib/ext2fs/icount.c
+++ b/lib/ext2fs/icount.c
@@ -237,6 +237,8 @@  errcode_t ext2fs_create_icount_tdb(ext2_filsys fs EXT2FS_NO_TDB_UNUSED,
 	 * value.
 	 */
 	num_inodes = fs->super->s_inodes_count - fs->super->s_free_inodes_count;
+	if (fs->fs_num_threads)
+		num_inodes /= fs->fs_num_threads;
 
 	icount->tdb = tdb_open(fn, num_inodes, TDB_NOLOCK | TDB_NOSYNC,
 			       O_RDWR | O_CREAT | O_TRUNC, 0600);
@@ -288,6 +290,8 @@  errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
 		if (retval)
 			goto errout;
 		icount->size += fs->super->s_inodes_count / 50;
+		if (fs->fs_num_threads)
+			icount->size /= fs->fs_num_threads;
 	}
 
 	bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));