erofs-utils: lib: replace bool locked with erofs_mutex_t for MT safety
PATCH 1/2 erofs-utils: lib: replace bool locked with erofs_mutex_t for MT safety
Replace the bool locked field in erofs_diskbufstrm with erofs_mutex_t lock to provide proper mutual exclusion for multi-threaded disk buffer operations. This addresses the TODO comment 'need a real lock for MT' by using the erofs mutex API (erofs_mutex_lock/erofs_mutex_unlock/erofs_mutex_init) instead of a simple boolean flag that provided no actual synchronization.
--- lib/diskbuf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
lib/diskbuf.c +5 -3
@@ -3,6 +3,7 @@#include "erofs/internal.h" #include "erofs/print.h" #include <stdio.h> +#include "erofs/lock.h" #include <errno.h> #include <sys/stat.h> #include <unistd.h>
@@ -14,7 +15,7 @@ static struct erofs_diskbufstrm {u64 tailoffset, devpos;int fd;unsigned int alignsize; -bool locked; +erofs_mutex_t lock; } *dbufstrm;int erofs_diskbuf_getfd (struct erofs_diskbuf *db,u64 *fpos)
@@ -34,6 +35,7 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off) {struct erofs_diskbufstrm *strm = dbufstrm + sid; +erofs_mutex_lock (&strm->lock);if (strm->tailoffset & (strm->alignsize -1 )) { strm->tailoffset =round_up (strm->tailoffset, strm->alignsize); }
@@ -42,7 +44,6 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off) *off = db->offset + strm->devpos; db->sp = strm; (void )erofs_atomic_inc_return (&strm->count); - strm->locked = true;/* TODO: need a real lock for MT */ return strm->fd; }
@@ -51,9 +52,9 @@ void erofs_diskbuf_commit(struct erofs_diskbuf *db, u64 len)struct erofs_diskbufstrm *strm = db->sp;DBG_BUGON (!strm); -DBG_BUGON (!strm->locked);DBG_BUGON (strm->tailoffset != db->offset); strm->tailoffset += len; +erofs_mutex_unlock (&strm->lock); }void erofs_diskbuf_close (struct erofs_diskbuf *db)
@@ -115,6 +116,7 @@ int erofs_diskbuf_init(unsigned int nstrms) setupone: strm->tailoffset =0 ;erofs_atomic_set (&strm->count,1 ); +erofs_mutex_init (&strm->lock);if (fstat (strm->fd, &st))return -errno; strm->alignsize =max_t (u32, st.st_blksize,getpagesize ());
Signature · 1 line
2.51.0.windows.1
PATCH 2/2 erofs-utils: lib: fix meta_blkaddr handling for 48-bit layout
Fix the FIXME in metabox.c by properly handling meta_blkaddr for 48-bit layouts. In erofs_writesb(), set meta_blkaddr to 0 on-disk when 48-bit layout is enabled since meta_blkaddr is encoded differently in that mode. Remove the now-resolved FIXME comment in metabox.c as the issue is addressed in super.c.
--- lib/metabox.c | 1 - lib/super.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-)
lib/metabox.c +0 -1
@@ -62,7 +62,6 @@ int erofs_metadata_init(struct erofs_sb_info *sbi)if (ret) goto err_free; sbi->m2gr = m2gr; -/* FIXME: sbi->meta_blkaddr should be 0 for 48-bit layouts */ sbi->meta_blkaddr =EROFS_META_NEW_ADDR ; }
lib/super.c +1 -1
@@ -209,7 +209,7 @@ int erofs_writesb(struct erofs_sb_info *sbi) .epoch = cpu_to_le64(sbi->epoch), .build_time = cpu_to_le64(sbi->build_time), .fixed_nsec = cpu_to_le32(sbi->fixed_nsec), - .meta_blkaddr = cpu_to_le32(sbi->meta_blkaddr), + .meta_blkaddr = cpu_to_le32(erofs_sb_has_48bit(sbi) ? 0 : sbi->meta_blkaddr), .xattr_blkaddr = cpu_to_le32(sbi->xattr_blkaddr), .xattr_prefix_count = sbi->xattr_prefix_count, .xattr_prefix_start = cpu_to_le32(sbi->xattr_prefix_start),
Signature · 1 line
2.51.0.windows.1
Re: [PATCH 2/2] erofs-utils: lib: fix meta_blkaddr handling for 48-bit layout
Quoted 13 lines · Ajay Rajera
Fix the FIXME in
metabox.cby properly handlingmeta_blkaddrfor 48-bit layouts. Inerofs_writesb(), setmeta_blkaddrto 0 on-disk when 48-bit layout is enabled sincemeta_blkaddris encoded differently in that mode. Remove the now-resolved FIXME comment inmetabox.cas the issue is addressed insuper.c.Signed-off-by Ajay Rajera <[email protected]>--- lib/metabox.c | 1 - lib/super.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-)lib/metabox.c +0
-0
lib/metabox.c int erofs_metadata_init(struct erofs_sb_info *sbi) · line 65
Quoted 4 lines
lib/metabox.c +0
-1@@ -62,7 +62,6 @@ int erofs_metadata_init(struct erofs_sb_info *sbi)if (ret) goto err_free; sbi->m2gr = m2gr; -/* FIXME: sbi->meta_blkaddr should be 0 for 48-bit layouts */
I don't think it's a valid patch, I need to find more clue why it was a FIXME.
Quoted 16 lines
lib/metabox.c +0
-0sbi->meta_blkaddr =EROFS_META_NEW_ADDR ; }lib/super.c +1
-1@@ -209,7 +209,7 @@ int erofs_writesb(struct erofs_sb_info *sbi) .epoch = cpu_to_le64(sbi->epoch), .build_time = cpu_to_le64(sbi->build_time), .fixed_nsec = cpu_to_le32(sbi->fixed_nsec), - .meta_blkaddr = cpu_to_le32(sbi->meta_blkaddr), + .meta_blkaddr = cpu_to_le32(erofs_sb_has_48bit(sbi) ? 0 : sbi->meta_blkaddr), .xattr_blkaddr = cpu_to_le32(sbi->xattr_blkaddr), .xattr_prefix_count = sbi->xattr_prefix_count, .xattr_prefix_start = cpu_to_le32(sbi->xattr_prefix_start),
Re: [PATCH 2/2] erofs-utils: lib: fix meta_blkaddr handling for 48-bit layout
Hi, I looked into the history of this FIXME (which was introduced in your commit 6eae70b1 "erofs-utils: mkfs: enable directory data in the metadata zone"). The reason sbi->meta_blkaddr must not be eagerly set to 0 in lib/metabox.c for 48-bit layouts is due to how the relative NID offset math works in memory before erofs_metazone_flush() resolves the absolute block address. In erofs_fixup_meta_blkaddr(), meta_offset is set to -bsz to avoid NID 0. To compensate, sbi->meta_blkaddr must remain EROFS_META_NEW_ADDR (which equates to -1 block). Later, in erofs_metazone_flush(), when the absolute metazone start block is allocated, it does: sbi->meta_blkaddr += meta_blkaddr; Since sbi->meta_blkaddr is -1 block, -1 + meta_blkaddr equals meta_blkaddr - 1. This correctly offsets the -bsz byte offset so that NID math (addr = meta_blkaddr * bsz + (nid << 5)) calculates the correct actual block address without wasting space!
If we were to eagerly set sbi->meta_blkaddr = 0 in metabox.c, the flush would set it to exactly meta_blkaddr, breaking the -bsz tracking and causing the DBG_BUGON(sbi->meta_blkaddr != -1) assertion in erofs_fixup_meta_blkaddr() to trip.
Since EROFS 48-bit layouts don't use the legacy meta_blkaddr field anyway, leaving the in-memory variable as EROFS_META_NEW_ADDR is mathematically required for the mkfs flush logic, while the on-disk superblock value is correctly hardcoded to 0 in lib/super.c. Because of this, the FIXME comment in metabox.c is actually outdated, which is why the patch just removes the comment itself.
Let me know if this makes sense to you.
Thanks, Ajay Rajera.
Quoted 42 lines · Gao Xiang
Quoted 13 lines · Ajay Rajera
Fix the FIXME in
metabox.cby properly handlingmeta_blkaddrfor 48-bit layouts. Inerofs_writesb(), setmeta_blkaddrto 0 on-disk when 48-bit layout is enabled sincemeta_blkaddris encoded differently in that mode. Remove the now-resolved FIXME comment inmetabox.cas the issue is addressed insuper.c.Signed-off-by Ajay Rajera <[email protected]>--- lib/metabox.c | 1 - lib/super.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-)lib/metabox.c +0
-0lib/metabox.c int erofs_metadata_init(struct erofs_sb_info *sbi) · line 65
Quoted 4 lines
lib/metabox.c +0
-1@@ -62,7 +62,6 @@ int erofs_metadata_init(struct erofs_sb_info *sbi)if (ret) goto err_free; sbi->m2gr = m2gr; -/* FIXME: sbi->meta_blkaddr should be 0 for 48-bit layouts */ I don't think it's a valid patch, I need to find more clue why it was a FIXME.
Quoted 16 lines
lib/metabox.c +0
-0sbi->meta_blkaddr =EROFS_META_NEW_ADDR ; }lib/super.c +1
-1@@ -209,7 +209,7 @@ int erofs_writesb(struct erofs_sb_info *sbi) .epoch = cpu_to_le64(sbi->epoch), .build_time = cpu_to_le64(sbi->build_time), .fixed_nsec = cpu_to_le32(sbi->fixed_nsec), - .meta_blkaddr = cpu_to_le32(sbi->meta_blkaddr), + .meta_blkaddr = cpu_to_le32(erofs_sb_has_48bit(sbi) ? 0 : sbi->meta_blkaddr), .xattr_blkaddr = cpu_to_le32(sbi->xattr_blkaddr), .xattr_prefix_count = sbi->xattr_prefix_count, .xattr_prefix_start = cpu_to_le32(sbi->xattr_prefix_start),
Hi Ajay,
Quoted 1 line · Ajay Rajera
Replace the bool locked field in
erofs_diskbufstrmwitherofs_mutex_tlock to provide proper mutual exclusion for multi-threaded disk buffer operations. This addresses the TODO comment 'need a real lock for MT' by using the erofs mutex API (erofs_mutex_lock/erofs_mutex_unlock/erofs_mutex_init) instead of a simple boolean flag that provided no actual synchronization.
The commit message should be 72-char at maximum each line.
Otherwise it seems a good improvement.
Quoted 62 lines
Signed-off-by Ajay Rajera <[email protected]>--- lib/diskbuf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)lib/diskbuf.c +5
-3@@ -3,6 +3,7 @@#include "erofs/internal.h" #include "erofs/print.h" #include <stdio.h> +#include "erofs/lock.h" #include <errno.h> #include <sys/stat.h> #include <unistd.h> @@ -14,7 +15,7 @@ static struct erofs_diskbufstrm {u64 tailoffset, devpos;int fd;unsigned int alignsize; -bool locked; +erofs_mutex_t lock; } *dbufstrm;int erofs_diskbuf_getfd (struct erofs_diskbuf *db,u64 *fpos)@@ -34,6 +35,7 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off) {struct erofs_diskbufstrm *strm = dbufstrm + sid; +erofs_mutex_lock (&strm->lock);if (strm->tailoffset & (strm->alignsize -1 )) { strm->tailoffset =round_up (strm->tailoffset, strm->alignsize); }@@ -42,7 +44,6 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off) *off = db->offset + strm->devpos; db->sp = strm; (void )erofs_atomic_inc_return (&strm->count); - strm->locked = true;/* TODO: need a real lock for MT */ return strm->fd; }@@ -51,9 +52,9 @@ void erofs_diskbuf_commit(struct erofs_diskbuf *db, u64 len)struct erofs_diskbufstrm *strm = db->sp;DBG_BUGON (!strm); -DBG_BUGON (!strm->locked);DBG_BUGON (strm->tailoffset != db->offset); strm->tailoffset += len; +erofs_mutex_unlock (&strm->lock); }void erofs_diskbuf_close (struct erofs_diskbuf *db)@@ -115,6 +116,7 @@ int erofs_diskbuf_init(unsigned int nstrms) setupone: strm->tailoffset =0 ;erofs_atomic_set (&strm->count,1 ); +erofs_mutex_init (&strm->lock);if (fstat (strm->fd, &st))return -errno; strm->alignsize =max_t (u32, st.st_blksize,getpagesize ());
👍
Ajay reacted via Gmail
<https://www.google.com/gmail/about/?utm_source=gmail-in-product&utm_medium=et&utm_campaign=emojireactionemail#app>
Quoted 71 lines · Gao Xiang
Hi Ajay,
Quoted 1 line · Ajay Rajera
Replace the bool locked field in
erofs_diskbufstrmwitherofs_mutex_tlock to provide proper mutual exclusion for multi-threaded disk buffer operations. This addresses the TODO comment 'need a real lock for MT' by using the erofs mutex API (erofs_mutex_lock/erofs_mutex_unlock/erofs_mutex_init) instead of a simple boolean flag that provided no actual synchronization.
The commit message should be 72-char at maximum each line.
Otherwise it seems a good improvement.
lib/diskbuf.c int erofs_diskbuf_reserve(struct erofs_diskbuf *db,
Quoted 28 lines
Signed-off-by Ajay Rajera <[email protected]>--- lib/diskbuf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)lib/diskbuf.c +2
-1@@ -3,6 +3,7 @@#include "erofs/internal.h" #include "erofs/print.h" #include <stdio.h> +#include "erofs/lock.h" #include <errno.h> #include <sys/stat.h> #include <unistd.h> @@ -14,7 +15,7 @@ static struct erofs_diskbufstrm {u64 tailoffset, devpos;int fd;unsigned int alignsize; -bool locked; +erofs_mutex_t lock; } *dbufstrm;int erofs_diskbuf_getfd (struct erofs_diskbuf *db,u64 *fpos)@@ -34,6 +35,7 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db,int sid, u64 *off)
lib/diskbuf.c int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off)
Quoted 6 lines
lib/diskbuf.c +1
-0{struct erofs_diskbufstrm *strm = dbufstrm + sid; +erofs_mutex_lock (&strm->lock);if (strm->tailoffset & (strm->alignsize -1 )) { strm->tailoffset = round_up(strm->tailoffset,
strm->alignsize);lib/diskbuf.c int erofs_diskbuf_reserve(struct erofs_diskbuf *db,
Quoted 2 lines
lib/diskbuf.c +0
-0}@@ -42,7 +44,6 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db,int sid, u64 *off)
lib/diskbuf.c void erofs_diskbuf_commit(struct erofs_diskbuf *db,
Quoted 8 lines
lib/diskbuf.c +0
-1*off = db->offset + strm->devpos; db->sp = strm; (void )erofs_atomic_inc_return (&strm->count); - strm->locked = true;/* TODO: need a real lock for MT */ return strm->fd; }@@ -51,9 +52,9 @@ void erofs_diskbuf_commit(struct erofs_diskbuf *db,u64 len)
Quoted 9 lines
lib/diskbuf.c +1
-1struct erofs_diskbufstrm *strm = db->sp;DBG_BUGON (!strm); -DBG_BUGON (!strm->locked);DBG_BUGON (strm->tailoffset != db->offset); strm->tailoffset += len; +erofs_mutex_unlock (&strm->lock); }
Quoted 11 lines
lib/diskbuf.c int erofs_diskbuf_init(unsigned int nstrms) · line 122
Quoted 9 lines
lib/diskbuf.c +1
-0void erofs_diskbuf_close (struct erofs_diskbuf *db)@@ -115,6 +116,7 @@ int erofs_diskbuf_init(unsigned int nstrms) setupone: strm->tailoffset =0 ;erofs_atomic_set (&strm->count,1 ); +erofs_mutex_init (&strm->lock);if (fstat (strm->fd, &st))return -errno; strm->alignsize =max_t (u32, st.st_blksize,getpagesize ());
{
"emoji": "👍",
"version": 1
}
Hi, This is my first time submitting patches via git send-email to a mailing list, so I apologize if there are any formatting issues or mistakes in the submission process. Any feedback or suggestions for improvement are appreciated.
Thanks, Ajay Rajera.
Quoted 67 lines · Ajay Rajera
Replace the bool locked field in
erofs_diskbufstrmwitherofs_mutex_tlock to provide proper mutual exclusion for multi-threaded disk buffer operations. This addresses the TODO comment 'need a real lock for MT' by using the erofs mutex API (erofs_mutex_lock/erofs_mutex_unlock/erofs_mutex_init) instead of a simple boolean flag that provided no actual synchronization.Signed-off-by Ajay Rajera <[email protected]>--- lib/diskbuf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)lib/diskbuf.c +5
-4@@ -3,6 +3,7 @@#include "erofs/internal.h" #include "erofs/print.h" #include <stdio.h> +#include "erofs/lock.h" #include <errno.h> #include <sys/stat.h> #include <unistd.h> @@ -14,7 +15,7 @@ static struct erofs_diskbufstrm {u64 tailoffset, devpos;int fd;unsigned int alignsize; -bool locked; +erofs_mutex_t lock; } *dbufstrm;int erofs_diskbuf_getfd (struct erofs_diskbuf *db,u64 *fpos)@@ -34,6 +35,7 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off) {struct erofs_diskbufstrm *strm = dbufstrm + sid; +erofs_mutex_lock (&strm->lock);if (strm->tailoffset & (strm->alignsize -1 )) { strm->tailoffset =round_up (strm->tailoffset, strm->alignsize); }@@ -42,7 +44,6 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off) *off = db->offset + strm->devpos; db->sp = strm; (void )erofs_atomic_inc_return (&strm->count); - strm->locked = true;/* TODO: need a real lock for MT */ return strm->fd; }@@ -51,9 +52,9 @@ void erofs_diskbuf_commit(struct erofs_diskbuf *db, u64 len)struct erofs_diskbufstrm *strm = db->sp;DBG_BUGON (!strm); -DBG_BUGON (!strm->locked);DBG_BUGON (strm->tailoffset != db->offset); strm->tailoffset += len; +erofs_mutex_unlock (&strm->lock); }void erofs_diskbuf_close (struct erofs_diskbuf *db)@@ -115,6 +116,7 @@ int erofs_diskbuf_init(unsigned int nstrms) setupone: strm->tailoffset =0 ;erofs_atomic_set (&strm->count,1 ); +erofs_mutex_init (&strm->lock);if (fstat (strm->fd, &st))return -errno; strm->alignsize =max_t (u32, st.st_blksize,getpagesize ()); --2.51.0.windows.1
PATCH v2 1/2 erofs-utils: lib: replace bool locked with erofs_mutex_t for MT safety
In reply to Ajay RajeraReplace the bool locked field in erofs_diskbufstrm with erofs_mutex_t lock to provide proper mutual exclusion for multi-threaded disk buffer operations.
This addresses the TODO comment 'need a real lock for MT' by using the erofs mutex API (erofs_mutex_lock/ erofs_mutex_unlock/erofs_mutex_init) instead of a simple boolean flag that provided no actual synchronization.
--- lib/diskbuf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
lib/diskbuf.c +5 -3
@@ -3,6 +3,7 @@#include "erofs/internal.h" #include "erofs/print.h" #include <stdio.h> +#include "erofs/lock.h" #include <errno.h> #include <sys/stat.h> #include <unistd.h>
@@ -14,7 +15,7 @@ static struct erofs_diskbufstrm {u64 tailoffset, devpos;int fd;unsigned int alignsize; -bool locked; +erofs_mutex_t lock; } *dbufstrm;int erofs_diskbuf_getfd (struct erofs_diskbuf *db,u64 *fpos)
@@ -34,6 +35,7 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off) {struct erofs_diskbufstrm *strm = dbufstrm + sid; +erofs_mutex_lock (&strm->lock);if (strm->tailoffset & (strm->alignsize -1 )) { strm->tailoffset =round_up (strm->tailoffset, strm->alignsize); }
@@ -42,7 +44,6 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off) *off = db->offset + strm->devpos; db->sp = strm; (void )erofs_atomic_inc_return (&strm->count); - strm->locked = true;/* TODO: need a real lock for MT */ return strm->fd; }
@@ -51,9 +52,9 @@ void erofs_diskbuf_commit(struct erofs_diskbuf *db, u64 len)struct erofs_diskbufstrm *strm = db->sp;DBG_BUGON (!strm); -DBG_BUGON (!strm->locked);DBG_BUGON (strm->tailoffset != db->offset); strm->tailoffset += len; +erofs_mutex_unlock (&strm->lock); }void erofs_diskbuf_close (struct erofs_diskbuf *db)
@@ -115,6 +116,7 @@ int erofs_diskbuf_init(unsigned int nstrms) setupone: strm->tailoffset =0 ;erofs_atomic_set (&strm->count,1 ); +erofs_mutex_init (&strm->lock);if (fstat (strm->fd, &st))return -errno; strm->alignsize =max_t (u32, st.st_blksize,getpagesize ());
Signature · 1 line
2.51.0.windows.1