erofs-utils: mkfs: eliminate redundant FD duplication
PATCH 1/3 erofs-utils: mkfs: eliminate redundant FD duplication
Removing unnecessary FD duplication prevents EMFILE errors.
Signed-off-by Gao Xiang <[email protected]>
--- lib/compress.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
lib/compress.c +1 -3
@@ -1720,7 +1720,6 @@ int erofs_mt_write_compressed_file(struct z_erofs_compress_ictx *ictx) ret =erofs_commit_compressed_file (ictx, bh, pstart - ptotal, ptotal); out: -close (ictx->fd);free (ictx);return ret; }
@@ -1791,13 +1790,12 @@ void *erofs_begin_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)pthread_mutex_unlock (&g_ictx.mutex);#endif ictx = &g_ictx; - ictx->fd = fd; }else { ictx =malloc (sizeof (*ictx));if (!ictx)return ERR_PTR (-ENOMEM ); - ictx->fd =dup (fd); } + ictx->fd = fd; ictx->ccfg = &sbi->zmgr->ccfg[inode->z_algorithmtype[0 ]]; inode->z_algorithmtype[0 ] = ictx->ccfg->algorithmtype;
Signature · 1 line
2.43.5
PATCH 2/3 erofs-utils: mkfs: limit the default asynchronous queue size
Limit the default asynchronous queue size based on the maximum file descriptor limit.
Otherwise, exceeding the FD limit will result in EMFILE errors.
Note that EROFS reuses the file page cache instead of duplicating all file contents into process memory, though this is constrained by the maximum FD limit.
Fixes 9bd592bfed10 ("erofs-utils: mkfs: increase the maximum size of the asynchronous queue")
Signed-off-by Gao Xiang <[email protected]>
--- configure.ac | 2 ++ lib/inode.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-)
configure.ac +2 -0
@@ -211,6 +211,7 @@ AC_CHECK_HEADERS(m4_flatten([ sys/ioctl.h sys/mman.h sys/random.h + sys/resource.h sys/sendfile.h sys/stat.h sys/statfs.h
@@ -265,6 +266,7 @@ AC_CHECK_FUNCS(m4_flatten([ backtrace copy_file_range fallocate + getrlimit gettimeofday lgetxattr llistxattr
lib/inode.c +30 -1
@@ -1888,6 +1888,28 @@ static int __erofs_mkfs_build_tree(struct erofs_mkfs_buildtree_ctx *ctx) }#ifdef EROFS_MT_ENABLED + +#ifdef HAVE_SYS_RESOURCE_H +#include <sys/resource.h> +#endif + +static int erofs_get_fdlimit (void ) +{ +#if defined(HAVE_SYS_RESOURCE_H ) && defined(HAVE_GETRLIMIT ) +struct rlimit rlim; +int err; + + err =getrlimit (RLIMIT_NOFILE , &rlim); +if (err <0 ) +return _POSIX_OPEN_MAX; +if (rlim.rlim_cur ==RLIM_INFINITY ) +return 0 ; +return rlim.rlim_cur; +#else +return _POSIX_OPEN_MAX; +#endif +} +static int erofs_mkfs_build_tree (struct erofs_mkfs_buildtree_ctx *ctx) {struct erofs_mkfs_dfops *q;
@@ -1898,7 +1920,14 @@ static int erofs_mkfs_build_tree(struct erofs_mkfs_buildtree_ctx *ctx)if (!q)return -ENOMEM ; - q->entries = cfg.c_mt_async_queue_limit ?:32768 ; +if (cfg.c_mt_async_queue_limit) { + q->entries = cfg.c_mt_async_queue_limit; + }else { + err =roundup_pow_of_two (erofs_get_fdlimit ()) >>1 ; + q->entries = err && err <32768 ? err :32768 ; + } +erofs_dbg ("Set the asynchronous queue size to %u" , q->entries); + q->queue =malloc (q->entries *sizeof (*q->queue));if (!q->queue) {free (q);
Signature · 1 line
2.43.5
PATCH 3/3 erofs-utils: mkfs: handle early exit of the dfops worker
In reply to Gao XiangOtherwise, the main thread will be stuck due to the full queue.
Signed-off-by Gao Xiang <[email protected]>
--- lib/inode.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
lib/inode.c +19 -4
@@ -1393,6 +1393,7 @@ struct erofs_mkfs_dfops {struct erofs_mkfs_jobitem *queue;unsigned int entries, head, tail;bool idle;/* initialize as false before the dfops worker runs */ +bool exited; };static void erofs_mkfs_flushjobs (struct erofs_sb_info *sbi)
@@ -1443,6 +1444,10 @@ static void *z_erofs_mt_dfops_worker(void *arg) ret =erofs_mkfs_jobfn (item);erofs_mkfs_pop_jobitem (dfops); }while (!ret); + + dfops->exited = true; +if (ret <0 ) +pthread_cond_signal (&dfops->full);pthread_exit ((void *)(uintptr_t )(ret <0 ? ret :0 )); }
@@ -1454,8 +1459,13 @@ static int erofs_mkfs_go(struct erofs_sb_info *sbi,pthread_mutex_lock (&q->lock); -while (q->tail - q->head >= q->entries) +while (q->tail - q->head >= q->entries) { +if (q->exited) { +pthread_mutex_unlock (&q->lock); +return -ECHILD ; + }pthread_cond_wait (&q->full, &q->lock); + } item = q->queue + (q->tail++ & (q->entries -1 )); item->type = type;
@@ -1912,9 +1922,10 @@ static int erofs_get_fdlimit(void)static int erofs_mkfs_build_tree (struct erofs_mkfs_buildtree_ctx *ctx) { +struct erofs_sb_info *sbi = ctx->sbi ? ctx->sbi : ctx->u.root->sbi;struct erofs_mkfs_dfops *q;int err, err2; -struct erofs_sb_info *sbi = ctx->sbi ? ctx->sbi : ctx->u.root->sbi; +void *retval; q =calloc (1 ,sizeof (*q));if (!q)
@@ -1946,9 +1957,13 @@ static int erofs_mkfs_build_tree(struct erofs_mkfs_buildtree_ctx *ctx) err =__erofs_mkfs_build_tree (ctx);erofs_mkfs_go (sbi, ~0 ,NULL ,0 ); - err2 =pthread_join (sbi->dfops_worker,NULL ); -if (!err) + err2 =pthread_join (sbi->dfops_worker, &retval); +DBG_BUGON (!q->exited); +if (!err || err == -ECHILD ) { err = err2; +if (!err) + err = (intptr_t )retval; + } fail:pthread_cond_destroy (&q->empty);
Signature · 1 line
2.43.5