exfat: memory optimisations and stringent integrity checks for up-case table
Subsystems (2)
cover exfat: memory optimisations and stringent integrity checks for up-case table
Reroll v1:
- Mark the volume read-only if the up-case table seems damaged
- Inline exfat_lookup_upcase_ptable()
- Fix uninitialised variable(ret) in exfat_load_upcase_table()
Revised coverletter:
This round of patches introduces efficient upcase table implementation to exFAT as well as more stringent check against the upcase table when mounting the volume.
**Theoretical trade offs**
The use of "paged upcase table" saw runtime memory footprint reduced from 131KB to 8KB(+some slab overhead). Other trade offs include:
- Reduced .rodata usage from 5KB to 2KB at the cost of added delay in module initialisation for populating the default upcase table
- Slight performance increase from reduced cache misses when traversing directories with entries with a wide range of unicode code points
**Tests**
Upcase table test cases are available in my repo(https://github.com/dxdxdt/gists/tree/master/writeups/exfat):
- exfat-default-upcase:
- -c option: uncompresses and prints the default compressed upcase table
- -p option: tests if exfat_populate_upcase_ptable() produces the same default upcase table included in the kernel prior to the patches
- -pc option: generates .rodata included in fs/exfat/tables.c
- exfat-test-upcase: brute forces combinations(2^32) of unicode upcase conversion to ensure that no regression is introduced. The test finishes within 24 hours ;)
- exfat-profile-upcase.sh and exfat-print-all-allowed: profile kernel memory use per mount and the worse-case directory traversal, entries filled with a range of unicode code points. Run with make profile
**Profiling**
When tested with 16 volumes(make profile), ~1MB saving in memory usage per volume is observed(available mem 122528 -> 140884).
**NOTES**
Errors found in the "recommended" upcase table are outlined in fs/exfat/tables.c.
The value of EXFAT_UPTBL_PAGESIZE(512 __u16 entries or 1024 bytes) is determined to be the best based on the data:
``` $ ./exfat-default-upcase > test-control ... entries: 874 (1748 bytes) non-empty pages: 8 (nb_page * pagesize * 2 = 512 * 8 * 2 = 8192 bytes) ```
The program reports that the total of 8 "pages" of 1KiB are used by the table.
Regression test:
``` (with the mainline exfat module) $ ./exfat-test-upcase /PATH/TO/EXFAT/MOUNTPOINT > a ... (with the patched exfat module) $ ./exfat-test-upcase /PATH/TO/EXFAT/MOUNTPOINT > b ... $ diff a b ```
Let me know if the test programs need to be added in the source tree (perhaps in /tools/testing/selftests/filesystems/exfat/ ??)
David Timber (4):
exfat: refactor nls.c (tables)
exfat: use upcase_ptable and upcase_range_info to reduce memory
footprint
exfat: add default_upcase option (read-only)
exfat: more pedantic upcase table validity check
fs/exfat/Makefile | 2 +- fs/exfat/exfat_fs.h | 56 ++- fs/exfat/nls.c | 552 +++++------------------------ fs/exfat/super.c | 10 + fs/exfat/tables.c | 845 ++++++++++++++++++++++++++++++++++++++++++++ fs/exfat/upcase.c | 70 ++++ 6 files changed, 1073 insertions(+), 462 deletions(-) create mode 100644 fs/exfat/tables.c create mode 100644 fs/exfat/upcase.c
Signature · 1 line
2.53.0.1.ga224b40d3f.dirty
PATCH 1/4 exfat: refactor nls.c (tables)
Relocate large data tables in nls.c to tables.c.
Add const to the return type of exfat_wstrchr() for consistency.
No functional change.
--- fs/exfat/Makefile | 2 +- fs/exfat/exfat_fs.h | 8 + fs/exfat/nls.c | 404 +------------------------------------------- fs/exfat/tables.c | 393 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 409 insertions(+), 398 deletions(-) create mode 100644 fs/exfat/tables.c
fs/exfat/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_EXFAT_FS) += exfat.o exfat-y := inode.o namei.o dir.o super.o fatent.o cache.o nls.o misc.o \ - file.o balloc.o + file.o balloc.o tables.o
fs/exfat/exfat_fs.h +8 -0
@@ -608,6 +608,14 @@ void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)__exfat_fs_error (sb,__ratelimit (&EXFAT_SB (sb)->ratelimit), \ fmt, ## args) +/* exfat/tables.c */ +/* Upcase table macro */ +#define EXFAT_NUM_UPCASE (2918) +#define EXFAT_UTBL_COUNT (0x10000) + +extern const unsigned short exfat_uni_def_upcase[EXFAT_NUM_UPCASE]; +extern const unsigned short exfat_bad_uni_chars[]; +/* expand to pr_*() with prefix */ #define exfat_err (sb, fmt, ...) \ pr_err("exFAT-fs (%s): " fmt "\n", (sb)->s_id, ##__VA_ARGS__)
fs/exfat/nls.c +7 -397
446 lines not on this page · raw message
fs/exfat/tables.c new file +393 -0
393 lines not on this page · raw message
Signature · 1 line
2.53.0.1.ga224b40d3f.dirty
[email protected]
reviewerRe: [PATCH v1 1/4] exfat: refactor nls.c (tables)
fs/exfat/exfat_fs.h void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) · line 617
Quoted 7 lines
fs/exfat/exfat_fs.h +7
-0+/* exfat/tables.c */ +/* Upcase table macro */ +#define EXFAT_NUM_UPCASE (2918) +#define EXFAT_UTBL_COUNT (0x10000) + +extern const unsigned short exfat_uni_def_upcase[EXFAT_NUM_UPCASE ]; +extern const unsigned short exfat_bad_uni_chars[];
The global variable exfat_bad_uni_chars[] is unnecessary, as is the generic function exfat_wstrchr().
We can implement a dedicated function exfat_is_bad_uni_char(), in which we can define static exfat_bad_uni_chars, or we can use a switch...case... statement.
Re: [PATCH v1 1/4] exfat: refactor nls.c (tables)
Quoted 10 lines · On 4/30/26 16:53, [email protected] wrote:
fs/exfat/exfat_fs.h void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) · line 617
Quoted 7 lines
fs/exfat/exfat_fs.h +7
-0+/* exfat/tables.c */ +/* Upcase table macro */ +#define EXFAT_NUM_UPCASE (2918) +#define EXFAT_UTBL_COUNT (0x10000) + +extern const unsigned short exfat_uni_def_upcase[EXFAT_NUM_UPCASE ]; +extern const unsigned short exfat_bad_uni_chars[];The global variable exfat_bad_uni_chars[] is unnecessary, as is the generic function
exfat_wstrchr().
Quoted 3 lines
We can implement a dedicated function
exfat_is_bad_uni_char(), in which we can define staticexfat_bad_uni_chars, or we can use a switch...case... statement.
I agree.
I too think that switch a case statement is suitable here because there's only a few __u16's to compare so there's really no need to leave room for cache misses. Let the CPU prefetch the characters along with the instructions.
Will make a separate commit if no objection.
Re: [PATCH v1 1/4] exfat: refactor nls.c (tables)
Quoted 14 lines · David Timber
Quoted 10 lines · On 4/30/26 16:53, [email protected] wrote:
fs/exfat/exfat_fs.h void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) · line 617
Quoted 7 lines
fs/exfat/exfat_fs.h +7
-0+/* exfat/tables.c */ +/* Upcase table macro */ +#define EXFAT_NUM_UPCASE (2918) +#define EXFAT_UTBL_COUNT (0x10000) + +extern const unsigned short exfat_uni_def_upcase[EXFAT_NUM_UPCASE ]; +extern const unsigned short exfat_bad_uni_chars[];The global variable exfat_bad_uni_chars[] is unnecessary, as is the generic function
exfat_wstrchr().Quoted 3 lines
We can implement a dedicated function
exfat_is_bad_uni_char(), in which we can define staticexfat_bad_uni_chars, or we can use a switch...case... statement.
Quoted 8 lines
I agree.
I too think that switch a case statement is suitable here because there's only a few __u16's to compare so there's really no need to leave room for cache misses. Let the CPU prefetch the characters along with the instructions.
Will make a separate commit if no objection.
Did some fiddling and found out that that could risk performance regression. But it's a problem specific to Clang, which is not so popular choice of compiler in the embedded world where it could have a significant impact.
If it doesn't bother anyone, I'll send out the reroll.
https://github.com/llvm/llvm-project/issues/127365#issuecomment-4362781091
PATCH 2/4 exfat: use upcase_ptable and upcase_range_info to reduce memory footprint
In reply to David TimberIntroduce upcase_ptable("paged up-case table"). Instead of allocating the entire 131KB of memory(2^16 possible entries times 2 bytes), break up the upcase table into 1024-byte pages and do not allocate memory for pages containing no data. The size of default upcase table populated during module initialisation is 8KB.
Reduce the size of .rodata section further by employing exfat_upcase_range_info, each representing a run in the unicode BMP. Each entry in exfat_def_utbl_ri represent the exact copy of the "recommended upcase table" from the exFAT spec but compressed in a format of our own(less than 1KB of .rodata compared to 5836 bytes of the compressed format in the spec).
When the contents of the upcase table loaded from the volume is identical to that of the default upcase table during mounting process, free the memory dynamically allocated to load the table and set up the super block to use the default table already populated during module initialisation.
As a custom upcase table is almost never used, keeping a copy of upcase table for every super block is wasteful. If one is used, it means that name hashing behaviour may not work as expected so warn the user by printing a kernel message.
--- fs/exfat/Makefile | 2 +- fs/exfat/exfat_fs.h | 54 +- fs/exfat/nls.c | 140 ++--- fs/exfat/super.c | 7 + fs/exfat/tables.c | 1190 +++++++++++++++++++++++++++++-------------- fs/exfat/upcase.c | 70 +++ 6 files changed, 1029 insertions(+), 434 deletions(-) create mode 100644 fs/exfat/upcase.c
fs/exfat/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_EXFAT_FS) += exfat.o exfat-y := inode.o namei.o dir.o super.o fatent.o cache.o nls.o misc.o \ - file.o balloc.o tables.o + file.o balloc.o tables.o upcase.o
fs/exfat/exfat_fs.h +50 -4
87 lines not on this page · raw message
fs/exfat/nls.c +80 -60
219 lines not on this page · raw message
fs/exfat/super.c +7 -0
@@ -896,6 +896,12 @@ static int __init init_exfat_fs(void) {int err; + err =exfat_init_default_upcase_ptable (); +if (err) { +WARN_ON (err == -EINVAL ); +return err; + } + err =exfat_cache_init ();if (err)return err;
@@ -932,6 +938,7 @@ static void __exit exit_exfat_fs(void)kmem_cache_destroy (exfat_inode_cachep);unregister_filesystem (&exfat_fs_type);exfat_cache_shutdown (); +exfat_free_default_upcase_ptable (); }module_init (init_exfat_fs);
fs/exfat/tables.c +821 -369
1,197 lines not on this page · raw message
fs/exfat/upcase.c new file +70 -0
70 lines not on this page · raw message
Signature · 1 line
2.53.0.1.ga224b40d3f.dirty
[email protected]
reviewerRe: [PATCH v1 2/4] exfat: use upcase_ptable and upcase_range_info to reduce memory footprint
fs/exfat/exfat_fs.h void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) · line 649
Quoted 7 lines
fs/exfat/exfat_fs.h +7
-0+static inline __u16 exfat_lookup_upcase_ptable (const struct exfat_upcase_ptable *ptbl, +const __u16 index) +{ +const size_t page_idx = index /EXFAT_UPTBL_PAGESIZE ; +const size_t idx_in_page = index %EXFAT_UPTBL_PAGESIZE ; + +return ptbl->pages[page_idx] ==NULL ?0 : ptbl->pages[page_idx][idx_in_page];
How about this? Then we won't need to check it again in exfat_toupper().
return ptbl->pages[page_idx] == NULL ? index : ptbl->pages[page_idx][idx_in_page];
Quoted 6 lines
fs/exfat/exfat_fs.h +6
-0+} + +void exfat_free_upcase_ptable (struct exfat_upcase_ptable *ptbl); +int exfat_populate_upcase_ptable (struct exfat_upcase_ptable *ptbl, +const struct exfat_upcase_range_info *ri, +const size_t cnt);
fs/exfat/exfat_fs.h void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) · line 662
Quoted 10 lines
fs/exfat/exfat_fs.h +4
-3+/* exfat/tables.c */ /* Upcase table macro */ -#define EXFAT_NUM_UPCASE (2918) -#define EXFAT_UTBL_COUNT (0x10000) +#define EXFAT_DEF_UTBL_RI_COUNT (112) +#define EXFAT_DEF_UTBL_CHKSUM (0xE619D30D) -extern const unsigned short exfat_uni_def_upcase[EXFAT_NUM_UPCASE ]; +extern const struct exfat_upcase_range_info exfat_def_utbl_ri[EXFAT_DEF_UTBL_RI_COUNT ];
Is there a way to reduce the size of exfat_def_utbl_ri so that we can put it on the stack of exfat_init_default_upcase_ptable()?
If not, add __initconst for exfat_def_utbl_ri.
Quoted 1 line
+int
exfat_init_default_upcase_ptable(void)
Add __init.
Quoted 1 line
+void
exfat_free_default_upcase_ptable(void)
Add __exit.
fs/exfat/super.c static int __init init_exfat_fs(void) · line 907
Quoted 13 lines
fs/exfat/super.c +6
-0@@ -896,6 +896,12 @@ static int __init init_exfat_fs(void) {int err; + err =exfat_init_default_upcase_ptable (); +if (err) { +WARN_ON (err == -EINVAL ); +return err; + } + err =exfat_cache_init ();if (err)return err;
Please add error handling to release default_upcase_ptable.
fs/exfat/upcase.c line 45
Quoted 4 lines
fs/exfat/upcase.c new file +4
-0+ +int exfat_populate_upcase_ptable (struct exfat_upcase_ptable *ptbl, +const struct exfat_upcase_range_info *ri, +const size_t cnt)
This function is only called by exfat_init_default_upcase_ptable(), how about merge this function to it?
Re: [PATCH v1 2/4] exfat: use upcase_ptable and upcase_range_info to reduce memory footprint
Quoted 11 lines · On 4/30/26 16:58, [email protected] wrote:
fs/exfat/exfat_fs.h void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) · line 649
Quoted 7 lines
fs/exfat/exfat_fs.h +7
-0+static inline __u16 exfat_lookup_upcase_ptable (const struct exfat_upcase_ptable *ptbl, +const __u16 index) +{ +const size_t page_idx = index /EXFAT_UPTBL_PAGESIZE ; +const size_t idx_in_page = index %EXFAT_UPTBL_PAGESIZE ; + +return ptbl->pages[page_idx] ==NULL ?0 : ptbl->pages[page_idx][idx_in_page];How about this? Then we won't need to check it again in
exfat_toupper().return ptbl->pages[page_idx] == NULL ? index : ptbl->pages[page_idx][idx_in_page];
Zero(invalid character in exFAT spec) is used as identity within the kernel. We'll have to be a bit careful. I'd have to change this logic in exfat_load_upcase_table()
ret = exfat_set_upcase_ptable(upcase_table, index, uni); if (ret) {
brelse(bh);
goto err;
}
def_upcase =
exfat_lookup_upcase_ptable(&default_upcase_table,
index);
is_default &= def_upcase == uni;
Some OOB data check will be ignored:
- the character is "skipped": zero
- the character is not skipped or specified as identity: non-zero
Subtle difference. If we change exfat_lookup_upcase_ptable() to return identity on non-existent mapping, that information is lost, I think. But again, the resulting contents would be the same regardless so I think there isn't really a side effect.
Quoted 14 lines
Quoted 14 lines
fs/exfat/exfat_fs.h +9
-2+} + +void exfat_free_upcase_ptable (struct exfat_upcase_ptable *ptbl); +int exfat_populate_upcase_ptable (struct exfat_upcase_ptable *ptbl, +const struct exfat_upcase_range_info *ri, +const size_t cnt); +/* exfat/tables.c */ /* Upcase table macro */ -#define EXFAT_NUM_UPCASE (2918) -#define EXFAT_UTBL_COUNT (0x10000) +#define EXFAT_DEF_UTBL_RI_COUNT (112) +#define EXFAT_DEF_UTBL_CHKSUM (0xE619D30D)
Quoted 8 lines
fs/exfat/exfat_fs.h void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) · line 662
Quoted 2 lines
fs/exfat/exfat_fs.h +1
-1-extern const unsigned short exfat_uni_def_upcase[EXFAT_NUM_UPCASE ]; +extern const struct exfat_upcase_range_info exfat_def_utbl_ri[EXFAT_DEF_UTBL_RI_COUNT ];Is there a way to reduce the size of
exfat_def_utbl_riso that we can put it on the stack ofexfat_init_default_upcase_ptable()?If not, add
__initconstforexfat_def_utbl_ri.
Nah. It's as packed as it could get without breaking the alignment. We could pack it down to 784 bytes, but that's still a lot of stack usage IMO.
Quoted 7 lines
Quoted 1 line
+int
exfat_init_default_upcase_ptable(void)Add
__init.Quoted 1 line
+void
exfat_free_default_upcase_ptable(void)Add
__exit.
Thanks.
Quoted 16 lines
fs/exfat/super.c static int __init init_exfat_fs(void) · line 907
Quoted 13 lines
fs/exfat/super.c +6
-0@@ -896,6 +896,12 @@ static int __init init_exfat_fs(void) {int err; + err =exfat_init_default_upcase_ptable (); +if (err) { +WARN_ON (err == -EINVAL ); +return err; + } + err =exfat_cache_init ();if (err)return err;Please add error handling to release
default_upcase_ptable.
Quoted 8 lines
fs/exfat/upcase.c line 45
Quoted 4 lines
fs/exfat/upcase.c new file +4
-0+ +int exfat_populate_upcase_ptable (struct exfat_upcase_ptable *ptbl, +const struct exfat_upcase_range_info *ri, +const size_t cnt)This function is only called by
exfat_init_default_upcase_ptable(), how about merge this function to it?
default_upcase_ptable is already freed up before returning from exfat_init_default_upcase_ptable() upon error. I made exfat_populate_upcase_ptable() as a separate function to use it on my test cases. The compiler inlines the function anyway(although appropriate attributes are missing in the original patch, my bad) so there's no ELF overhead either. I'd like to leave it as it is, if that's okay.
[email protected]
reviewerRe: [PATCH v1 2/4] exfat: use upcase_ptable and upcase_range_info to reduce memory footprint
Quoted 96 lines · On 4/30/26 16:58, [email protected] wrote:
Quoted 11 lines
fs/exfat/exfat_fs.h void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) · line 649
Quoted 7 lines
fs/exfat/exfat_fs.h +7
-0+static inline __u16 exfat_lookup_upcase_ptable (const struct exfat_upcase_ptable *ptbl, + a const __u16 index) +{ +const size_t page_idx = index /EXFAT_UPTBL_PAGESIZE ; +const size_t idx_in_page = index %EXFAT_UPTBL_PAGESIZE ; + +return ptbl->pages[page_idx] ==NULL ?0 : ptbl->pages[page_idx][idx_in_page];How about this? Then we won't need to check it again in
exfat_toupper().return ptbl->pages[page_idx] == NULL ? index : ptbl->pages[page_idx][idx_in_page];
Zero(invalid character in exFAT spec) is used as identity within the kernel. We'll have to be a bit careful. I'd have to change this logic in
exfat_load_upcase_table()ret = exfat_set_upcase_ptable(upcase_table, index, uni); if (ret) { brelse(bh); goto err; } def_upcase = exfat_lookup_upcase_ptable(&default_upcase_table, index); is_default &= def_upcase == uni;Some OOB data check will be ignored:
- the character is "skipped": zero
- the character is not skipped or specified as identity: non-zeroSubtle difference. If we change
exfat_lookup_upcase_ptable()to return identity on non-existent mapping, that information is lost, I think. But again, the resulting contents would be the same regardless so I think there isn't really a side effect.Quoted 14 lines
Quoted 14 lines
fs/exfat/exfat_fs.h +9
-2+} + +void exfat_free_upcase_ptable (struct exfat_upcase_ptable *ptbl); +int exfat_populate_upcase_ptable (struct exfat_upcase_ptable *ptbl, +const struct exfat_upcase_range_info *ri, +const size_t cnt); +/* exfat/tables.c */ /* Upcase table macro */ -#define EXFAT_NUM_UPCASE (2918) -#define EXFAT_UTBL_COUNT (0x10000) +#define EXFAT_DEF_UTBL_RI_COUNT (112) +#define EXFAT_DEF_UTBL_CHKSUM (0xE619D30D)Quoted 8 lines
fs/exfat/exfat_fs.h void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) · line 662
Quoted 2 lines
fs/exfat/exfat_fs.h +1
-1-extern const unsigned short exfat_uni_def_upcase[EXFAT_NUM_UPCASE ]; +extern const struct exfat_upcase_range_info exfat_def_utbl_ri[EXFAT_DEF_UTBL_RI_COUNT ];Is there a way to reduce the size of
exfat_def_utbl_riso that we can put it on the stack ofexfat_init_default_upcase_ptable()?If not, add
__initconstforexfat_def_utbl_ri.Nah. It's as packed as it could get without breaking the alignment. We could pack it down to 784 bytes, but that's still a lot of stack usage IMO.
Quoted 7 lines
Quoted 1 line
+int
exfat_init_default_upcase_ptable(void)Add
__init.Quoted 1 line
+void
exfat_free_default_upcase_ptable(void)Add
__exit.Thanks.
Quoted 16 lines
fs/exfat/super.c static int __init init_exfat_fs(void) · line 907
Quoted 13 lines
fs/exfat/super.c +6
-0@@ -896,6 +896,12 @@ static int __init init_exfat_fs(void) {int err; + err =exfat_init_default_upcase_ptable (); +if (err) { +WARN_ON (err == -EINVAL ); +return err; + } + err =exfat_cache_init ();if (err)return err;Please add error handling to release
default_upcase_ptable.Quoted 8 lines
fs/exfat/upcase.c line 45
Quoted 4 lines
fs/exfat/upcase.c new file +4
-0+ +int exfat_populate_upcase_ptable (struct exfat_upcase_ptable *ptbl, +const struct exfat_upcase_range_info *ri, +const size_t cnt)This function is only called by
exfat_init_default_upcase_ptable(), how about merge this function to it?
Quoted 2 lines
default_upcase_ptableis already freed up before returning fromexfat_init_default_upcase_ptable()upon error. I made
What I mean is that if the subsequent operations fail, such as exfat_cache_init() returning an error, exfat_free_default_upcase_ptable() needs to be called to release default_upcase_ptable.
Quoted 5 lines
exfat_populate_upcase_ptable()as a separate function to use it on my test cases. The compiler inlines the functionanyway(although appropriate attributes are missing in the original patch, my bad)so there's no ELF overhead either. I'd like to leave it as it is, if that's okay.
Your test cases are only for the current development phase and will not be needed later, right?
If not, keeping it is fine. Otherwise, we should try to keep the code as concise as possible.
Re: [PATCH v1 2/4] exfat: use upcase_ptable and upcase_range_info to reduce memory footprint
Quoted 3 lines · On 4/30/26 20:48, [email protected] wrote:
What I mean is that if the subsequent operations fail, such as
exfat_cache_init()returning an error,exfat_free_default_upcase_ptable()needs to be called to releasedefault_upcase_ptable.
Oh, you're right. Totally missed that. Thank you for being patient and clearing that out. Disaster averted.
Quoted 11 lines
Quoted 6 lines
exfat_populate_upcase_ptable()as a separate function to use it on my test cases. The compiler inlines the functionanyway(although appropriate attributes are missing in the original patch, my bad)so there's no ELF overhead either. I'd like to leave it as it is, if that's okay.Your test cases are only for the current development phase and will not be needed later, right?
If not, keeping it is fine. Otherwise, we should try to keep the code as concise as possible.
I'll just get rid of exfat_init_default_upcase_ptable() and see if I can just open code it in init_exfat_fs(). That should get rid of the problem above as well.
Thanks,
Davo
PATCH 3/4 exfat: add default_upcase option (read-only)
In reply to David TimberShow "default_upcase" in the mount options if the default upcase table is being used for the volume for diagnostic purposes.
--- fs/exfat/super.c | 3 +++ 1 file changed, 3 insertions(+)
fs/exfat/super.c +3 -0
@@ -149,6 +149,9 @@ static int exfat_show_options(struct seq_file *m, struct dentry *root)seq_printf (m,",time_offset=%d" , opts->time_offset);if (opts->zero_size_dir)seq_puts (m,",zero_size_dir" ); +if (sbi->vol_utbl_own ==NULL ) +seq_puts (m,",default_upcase" ); +return 0 ; }
Signature · 1 line
2.53.0.1.ga224b40d3f.dirty
PATCH 4/4 exfat: more pedantic upcase table validity check
In reply to David TimberIt is observed that most exFAT implementations reject a volume with an upcase table whose index of the last entry is not 0xFFFF and treat the volume as damaged.
Upon encoutering an incomplete or malformed upcase table:
- whose the index of last entry is not 0xFFFF
- that has extra data after the end of the table
Raise exfat_fs_error() to mark the volume read-only.
--- fs/exfat/nls.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-)
fs/exfat/nls.c +14 -12
@@ -260,17 +260,16 @@ int exfat_nls_to_utf16(struct super_block *sb, const unsigned char *p_cstring, }static int exfat_load_upcase_table (struct super_block *sb, -sector_t sector,unsigned long long num_sectors, -unsigned int utbl_checksum) +unsigned long long tbl_size,sector_t sector, +unsigned long long num_sectors,unsigned int utbl_checksum) {struct exfat_sb_info *sbi =EXFAT_SB (sb);unsigned int sect_size = sb->s_blocksize;unsigned int i, index =0 ;u32 chksum =0 ; -unsigned char skip = false; +bool skip = false, is_default = true;struct exfat_upcase_ptable *upcase_table;unsigned short def_upcase; -bool is_default;unsigned int entries =0 ;int ret = -EINVAL ;
@@ -279,9 +278,8 @@ static int exfat_load_upcase_table(struct super_block *sb,return -ENOMEM ; num_sectors += sector; - is_default = sector < num_sectors; -while (sector < num_sectors) { +while (tbl_size >1 && sector < num_sectors) {struct buffer_head *bh; bh =sb_bread (sb, sector);
@@ -292,7 +290,8 @@ static int exfat_load_upcase_table(struct super_block *sb, goto err; } sector++; -for (i =0 ; i < sect_size && index <=0xFFFF ; i +=2 ) { +for (i =0 ; i < sect_size && index <=0xFFFF && tbl_size >1 ; + i +=2 , tbl_size -=2 ) {unsigned short uni =get_unaligned_le16 (bh->b_data + i);if (skip) {
@@ -319,9 +318,13 @@ static int exfat_load_upcase_table(struct super_block *sb, } chksum =exfat_calc_chksum32 (bh->b_data, i, chksum,CS_DEFAULT );brelse (bh); + +/* the contents of the table went out of bounds with still more data to read */ +if (tbl_size >0 && index >=0x10000 ) + goto err; } -if (index >=0xFFFF && utbl_checksum == chksum) { +if (index ==0x10000 && utbl_checksum == chksum) {/* * is_default being set does not necessarily mean the contents are exact same as the * upcase table loaded from the volume may be missing some entries. The checksum
@@ -339,10 +342,9 @@ static int exfat_load_upcase_table(struct super_block *sb,return 0 ; } -exfat_err (sb,"failed to load upcase table (idx : 0x%08x, chksum : 0x%08x, utbl_chksum : 0x%08x)" , - index, chksum, utbl_checksum); - err: +exfat_fs_error (sb,"damaged upcase table. Please run fsck (idx : 0x%08x, chksum : 0x%08x, utbl_chksum : 0x%08x)" , + index, chksum, utbl_checksum);exfat_free_upcase_ptable (upcase_table);kvfree (upcase_table);
@@ -390,7 +392,7 @@ int exfat_create_upcase_table(struct super_block *sb)if (tbl_size) { sector =exfat_cluster_to_sector (sbi, tbl_clu); num_sectors = ((tbl_size -1 ) >> blksize_bits) +1 ; - ret =exfat_load_upcase_table (sb, sector, num_sectors, + ret =exfat_load_upcase_table (sb, tbl_size, sector, num_sectors,le32_to_cpu (ep->dentry.upcase.checksum)); }else exfat_fs_error(sb,
Signature · 1 line
2.53.0.1.ga224b40d3f.dirty
[email protected]
reviewerRe: [PATCH v1 4/4] exfat: more pedantic upcase table validity check
fs/exfat/nls.c int exfat_nls_to_utf16(struct super_block *sb, const unsigned char *p_cstring, · line 264
Quoted 5 lines
static int exfat_load_upcase_table(struct
super_block*sb,fs/exfat/nls.c +2
-2- sector_t sector, unsigned long long num_sectors, - unsigned int utbl_checksum) + unsigned long long tbl_size, sector_t sector, + unsigned long long num_sectors, unsigned int utbl_checksum)
After adding the parameter tbl_size, the parameter num_sectors is no longer needed.
Re: [PATCH v1 4/4] exfat: more pedantic upcase table validity check
Quoted 7 lines · On 4/30/26 16:58, [email protected] wrote:
fs/exfat/nls.c int exfat_nls_to_utf16(struct super_block *sb, const unsigned char *p_cstring, · line 264
Quoted 5 lines
static int exfat_load_upcase_table(struct
super_block*sb,fs/exfat/nls.c +2
-2- sector_t sector, unsigned long long num_sectors, - unsigned int utbl_checksum) + unsigned long long tbl_size, sector_t sector, + unsigned long long num_sectors, unsigned int utbl_checksum)After adding the parameter
tbl_size, the parameternum_sectorsis no longer needed.
Change the func signature? I'd like to hear Jeon's opinion on this.
Namjae Jeon
maintainerRe: [PATCH v1 4/4] exfat: more pedantic upcase table validity check
Quoted 10 lines · David Timber
Quoted 7 lines · On 4/30/26 16:58, [email protected] wrote:
fs/exfat/nls.c int exfat_nls_to_utf16(struct super_block *sb, const unsigned char *p_cstring, · line 264
Quoted 5 lines
static int exfat_load_upcase_table(struct
super_block*sb,fs/exfat/nls.c +2
-2- sector_t sector, unsigned long long num_sectors, - unsigned int utbl_checksum) + unsigned long long tbl_size, sector_t sector, + unsigned long long num_sectors, unsigned int utbl_checksum)After adding the parameter
tbl_size, the parameternum_sectorsis no longer needed.Change the func signature? I'd like to hear Jeon's opinion on this.
I agree. It is cleaner to remove num_sectors from the arguments and calculate it inside exfat_load_upcase_table() using tbl_size and sb->s_blocksize_bits.
Re: [PATCH v1 4/4] exfat: more pedantic upcase table validity check
Quoted 7 lines · Namjae Jeon
Quoted 6 lines · David Timber
Quoted 5 lines · On 4/30/26 16:58, [email protected] wrote:
Quoted 5 lines
static int exfat_load_upcase_table(struct
super_block*sb,fs/exfat/nls.c +2
-2- sector_t sector, unsigned long long num_sectors, - unsigned int utbl_checksum) + unsigned long long tbl_size, sector_t sector, + unsigned long long num_sectors, unsigned int utbl_checksum)
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
After adding the parameter
tbl_size, the parameternum_sectorsis no longer needed.Change the func signature? I'd like to hear Jeon's opinion on this.
I agree. It is cleaner to remove
num_sectorsfrom the arguments and calculate it insideexfat_load_upcase_table()usingtbl_sizeandsb->s_blocksize_bits.
Ack'd