Skip to contentSkip to conversation

Make UTF-8 encoding loadable

PATCH Superseded Shreeya Patel 2 participants 6 messages Mar 31, 2021 – Apr 1, 2021

Subsystems (4)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover Make UTF-8 encoding loadable

utf8data.h_shipped has a large database table which is an auto-generated decodification trie for the unicode normalization functions and it is not necessary to carry this large table in the kernel.
Goal is to make UTF-8 encoding loadable by converting it into a module and adding a layer between the filesystems and the utf8 module which will load the module whenever any filesystem that needs unicode is mounted. Currently only UTF-8 encoding is supported but if any other encodings are supported in future then the layer file would be responsible for loading the desired encoding module.

1st patch in the series resolves the warning reported by kernel test robot by using strscpy instead of strncpy.

Unicode is the subsystem and utf8 is a charachter encoding for the subsystem, hence 2nd and 3rd patches in the series are renaming functions and file name to unicode for better understanding the difference between UTF-8 module and unicode layer.

Last patch in the series adds the layer and utf8 module and also uses static calls which gives performance benefit when compared to indirect calls using function pointers.

---
Changes in v6
- Add spinlock to protect utf8mod and avoid NULL pointer dereference.
- Change the static call function names for being consistent with kernel coding style.
- Merge the unicode_load_module function with unicode_load as it is not really needed to have a separate function.
- Use try_then_module_get instead of module_get to avoid loading the module even when it is already loaded.
- Improve the commit message.

Changes in v5
- Remove patch which adds NULL check in ext4/super.c and f2fs/super.c before calling unicode_unload().
- Rename global variables and default static call functions for better understanding
- Make only config UNICODE_UTF8 visible and config UNICODE to be always enabled provided UNICODE_UTF8 is enabled.
- Improve the documentation for Kconfig
- Improve the commit message.

Changes in v4
- Return error from the static calls instead of doing nothing and succeeding even without loading the module.
- Remove the complete usage of utf8_ops and use static calls at all places.
- Restore the static calls to default values when module is unloaded.
- Decrement the reference of module after calling the unload function.
- Remove spinlock as there will be no race conditions after removing

    utf8_ops.

Changes in v3
- Add a patch which checks if utf8 is loaded before calling utf8_unload() in ext4 and f2fs filesystems
- Return error if strscpy() returns value < 0
- Correct the conditions to prevent NULL pointer dereference while accessing functions via utf8_ops variable.
- Add spinlock to avoid race conditions.
- Use static_call() for preventing speculative execution attacks.

Changes in v2
- Remove the duplicate file from the last patch.
- Make the wrapper functions inline.
- Remove msleep and use try_module_get() and module_put() for ensuring that module is loaded correctly and also doesn't get unloaded while in use.
- Resolve the warning reported by kernel test robot.
- Resolve all the checkpatch.pl warnings.

Shreeya Patel (4):
fs: unicode: Use strscpy() instead of strncpy()
fs: unicode: Rename function names from utf8 to unicode
fs: unicode: Rename utf8-core file to unicode-core
fs: unicode: Add utf8 module and a unicode layer

 fs/ext4/hash.c                             |   2 +-
 fs/ext4/namei.c                            |  12 +-
 fs/ext4/super.c                            |   6 +-
 fs/f2fs/dir.c                              |  12 +-
 fs/f2fs/super.c                            |   6 +-
 fs/libfs.c                                 |   6 +-
 fs/unicode/Kconfig                         |  17 ++-
 fs/unicode/Makefile                        |   5 +-
 fs/unicode/unicode-core.c                  | 152 +++++++++++++++++++++
 fs/unicode/{utf8-core.c => unicode-utf8.c} |  98 ++++++++-----
 fs/unicode/utf8-selftest.c                 |   8 +-
 include/linux/unicode.h                    | 100 +++++++++++---
 12 files changed, 344 insertions(+), 80 deletions(-)
 create mode 100644 fs/unicode/unicode-core.c
 rename fs/unicode/{utf8-core.c => unicode-utf8.c} (57%)
Signature · 8 lines
2.30.2



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

PATCH v6 1/4 fs: unicode: Use strscpy() instead of strncpy()

Following warning was reported by Kernel Test Robot.

In function 'utf8_parse_version',
inlined from 'utf8_load' at fs/unicode/utf8mod.c:195:7:

Quoted 1 line
Quoted 1 line

fs/unicode/utf8mod.c:175:2: warning: 'strncpy' specified bound 12 equals

destination size [-Wstringop-truncation]

175 |  strncpy(version_string, version, sizeof(version_string));
    |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The -Wstringop-truncation warning highlights the unintended uses of the strncpy function that truncate the terminating NULL character from the source string.
Unlike strncpy(), strscpy() always null-terminates the destination string, hence use strscpy() instead of strncpy().

Fixes 9d53690f0d4e5 (unicode: implement higher level API for string handling)
---
 fs/unicode/utf8-core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/unicode/utf8-core.c +3 -1
index dc25823bfed9..f9e6a2718aba 100644
@@ -179,8 +179,10 @@ static int utf8_parse_version(const char *version, unsigned int *maj,
 		{1, "%d.%d.%d"},
 		{0, NULL}
 	};
+	int ret = strscpy(version_string, version, sizeof(version_string));
 
-	strncpy(version_string, version, sizeof(version_string));
+	if (ret < 0)
+		return ret;
 
 	if (match_token(version_string, token, args) != 1)
 		return -EINVAL;
Signature · 8 lines
2.30.2



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#2 lore raw

PATCH v6 2/4 fs: unicode: Rename function names from utf8 to unicode

In reply to Shreeya Patel

utf8data.h_shipped has a large database table which is an auto-generated decodification trie for the unicode normalization functions and it is not necessary to carry this large table in the kernel.
Goal is to make UTF-8 encoding loadable by converting it into a module and adding a unicode subsystem layer between the filesystems and the utf8 module.

This layer will load the module whenever any filesystem that needs unicode is mounted.
Currently, only UTF-8 encoding is supported but if any other encodings are supported in future then the layer file would be responsible for loading the desired encoding module.

utf8-core will be converted into this layer file in the future patches. Rename the function names from utf8 to unicode which will denote the functions as the unicode subsystem layer functions and this will also be the first step towards the transformation of utf8-core file into the unicode subsystem layer file.

---
Changes in v6
- Improve the commit message

 fs/ext4/hash.c             |  2 +-
 fs/ext4/namei.c            | 12 ++++----
 fs/ext4/super.c            |  6 ++--
 fs/f2fs/dir.c              | 12 ++++----
 fs/f2fs/super.c            |  6 ++--
 fs/libfs.c                 |  6 ++--
 fs/unicode/utf8-core.c     | 57 +++++++++++++++++++-------------------
 fs/unicode/utf8-selftest.c |  8 +++---
 include/linux/unicode.h    | 32 ++++++++++-----------
 9 files changed, 70 insertions(+), 71 deletions(-)
fs/ext4/hash.c +1 -1
index a92eb79de0cc..8890a76abe86 100644
@@ -285,7 +285,7 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
 		if (!buff)
 			return -ENOMEM;
 
-		dlen = utf8_casefold(um, &qstr, buff, PATH_MAX);
+		dlen = unicode_casefold(um, &qstr, buff, PATH_MAX);
 		if (dlen < 0) {
 			kfree(buff);
 			goto opaque_seq;
fs/ext4/namei.c +6 -6
index 686bf982c84e..dde5ce795416 100644
@@ -1290,9 +1290,9 @@ int ext4_ci_compare(const struct inode *parent, const struct qstr *name,
 	int ret;
 
 	if (quick)
-		ret = utf8_strncasecmp_folded(um, name, entry);
+		ret = unicode_strncasecmp_folded(um, name, entry);
 	else
-		ret = utf8_strncasecmp(um, name, entry);
+		ret = unicode_strncasecmp(um, name, entry);
 
 	if (ret < 0) {
 		/* Handle invalid character sequence as either an error
@@ -1324,9 +1324,9 @@ void ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
 	if (!cf_name->name)
 		return;
 
-	len = utf8_casefold(dir->i_sb->s_encoding,
-			    iname, cf_name->name,
-			    EXT4_NAME_LEN);
+	len = unicode_casefold(dir->i_sb->s_encoding,
+			       iname, cf_name->name,
+			       EXT4_NAME_LEN);
 	if (len <= 0) {
 		kfree(cf_name->name);
 		cf_name->name = NULL;
@@ -2201,7 +2201,7 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
 
 #ifdef CONFIG_UNICODE
 	if (sb_has_strict_encoding(sb) && IS_CASEFOLDED(dir) &&
-	    sb->s_encoding && utf8_validate(sb->s_encoding, &dentry->d_name))
+	    sb->s_encoding && unicode_validate(sb->s_encoding, &dentry->d_name))
 		return -EINVAL;
 #endif
 
fs/ext4/super.c +3 -3
index ad34a37278cd..2fb845752c90 100644
@@ -1259,7 +1259,7 @@ static void ext4_put_super(struct super_block *sb)
 	fs_put_dax(sbi->s_daxdev);
 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
 #ifdef CONFIG_UNICODE
-	utf8_unload(sb->s_encoding);
+	unicode_unload(sb->s_encoding);
 #endif
 	kfree(sbi);
 }
@@ -4304,7 +4304,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
 			goto failed_mount;
 		}
 
-		encoding = utf8_load(encoding_info->version);
+		encoding = unicode_load(encoding_info->version);
 		if (IS_ERR(encoding)) {
 			ext4_msg(sb, KERN_ERR,
 				 "can't mount with superblock charset: %s-%s "
@@ -5165,7 +5165,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
 		crypto_free_shash(sbi->s_chksum_driver);
 
 #ifdef CONFIG_UNICODE
-	utf8_unload(sb->s_encoding);
+	unicode_unload(sb->s_encoding);
 #endif
 
 #ifdef CONFIG_QUOTA
fs/f2fs/dir.c +6 -6
index e6270a867be1..f160f9dd667d 100644
@@ -84,10 +84,10 @@ int f2fs_init_casefolded_name(const struct inode *dir,
 						   GFP_NOFS);
 		if (!fname->cf_name.name)
 			return -ENOMEM;
-		fname->cf_name.len = utf8_casefold(sb->s_encoding,
-						   fname->usr_fname,
-						   fname->cf_name.name,
-						   F2FS_NAME_LEN);
+		fname->cf_name.len = unicode_casefold(sb->s_encoding,
+						      fname->usr_fname,
+						      fname->cf_name.name,
+						      F2FS_NAME_LEN);
 		if ((int)fname->cf_name.len <= 0) {
 			kfree(fname->cf_name.name);
 			fname->cf_name.name = NULL;
@@ -237,7 +237,7 @@ static int f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
 		entry.len = decrypted_name.len;
 	}
 
-	res = utf8_strncasecmp_folded(um, name, &entry);
+	res = unicode_strncasecmp_folded(um, name, &entry);
 	/*
 	 * In strict mode, ignore invalid names.  In non-strict mode,
 	 * fall back to treating them as opaque byte sequences.
@@ -246,7 +246,7 @@ static int f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
 		res = name->len == entry.len &&
 				memcmp(name->name, entry.name, name->len) == 0;
 	} else {
-		/* utf8_strncasecmp_folded returns 0 on match */
+		/* unicode_strncasecmp_folded returns 0 on match */
 		res = (res == 0);
 	}
 out:
fs/f2fs/super.c +3 -3
index 7069793752f1..b4a92e763e27 100644
@@ -1430,7 +1430,7 @@ static void f2fs_put_super(struct super_block *sb)
 	for (i = 0; i < NR_PAGE_TYPE; i++)
 		kvfree(sbi->write_io[i]);
 #ifdef CONFIG_UNICODE
-	utf8_unload(sb->s_encoding);
+	unicode_unload(sb->s_encoding);
 #endif
 	kfree(sbi);
 }
@@ -3560,7 +3560,7 @@ static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
 			return -EINVAL;
 		}
 
-		encoding = utf8_load(encoding_info->version);
+		encoding = unicode_load(encoding_info->version);
 		if (IS_ERR(encoding)) {
 			f2fs_err(sbi,
 				 "can't mount with superblock charset: %s-%s "
@@ -4073,7 +4073,7 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
 		kvfree(sbi->write_io[i]);
 
 #ifdef CONFIG_UNICODE
-	utf8_unload(sb->s_encoding);
+	unicode_unload(sb->s_encoding);
 	sb->s_encoding = NULL;
 #endif
 free_options:
fs/libfs.c +3 -3
index e2de5401abca..766556165bb5 100644
@@ -1404,7 +1404,7 @@ static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
 	 * If the dentry name is stored in-line, then it may be concurrently
 	 * modified by a rename.  If this happens, the VFS will eventually retry
 	 * the lookup, so it doesn't matter what ->d_compare() returns.
-	 * However, it's unsafe to call utf8_strncasecmp() with an unstable
+	 * However, it's unsafe to call unicode_strncasecmp() with an unstable
 	 * string.  Therefore, we have to copy the name into a temporary buffer.
 	 */
 	if (len <= DNAME_INLINE_LEN - 1) {
@@ -1414,7 +1414,7 @@ static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
 		/* prevent compiler from optimizing out the temporary buffer */
 		barrier();
 	}
-	ret = utf8_strncasecmp(um, name, &qstr);
+	ret = unicode_strncasecmp(um, name, &qstr);
 	if (ret >= 0)
 		return ret;
 
@@ -1443,7 +1443,7 @@ static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
 	if (!dir || !needs_casefold(dir))
 		return 0;
 
-	ret = utf8_casefold_hash(um, dentry, str);
+	ret = unicode_casefold_hash(um, dentry, str);
 	if (ret < 0 && sb_has_strict_encoding(sb))
 		return -EINVAL;
 	return 0;
fs/unicode/utf8-core.c +28 -29

136 lines not on this page · raw message

fs/unicode/utf8-selftest.c +4 -4
index 6fe8af7edccb..796c1ed922ea 100644
@@ -235,7 +235,7 @@ static void check_utf8_nfdicf(void)
 static void check_utf8_comparisons(void)
 {
 	int i;
-	struct unicode_map *table = utf8_load("12.1.0");
+	struct unicode_map *table = unicode_load("12.1.0");
 
 	if (IS_ERR(table)) {
 		pr_err("%s: Unable to load utf8 %d.%d.%d. Skipping.\n",
@@ -249,7 +249,7 @@ static void check_utf8_comparisons(void)
 		const struct qstr s2 = {.name = nfdi_test_data[i].dec,
 					.len = sizeof(nfdi_test_data[i].dec)};
 
-		test_f(!utf8_strncmp(table, &s1, &s2),
+		test_f(!unicode_strncmp(table, &s1, &s2),
 		       "%s %s comparison mismatch\n", s1.name, s2.name);
 	}
 
@@ -259,11 +259,11 @@ static void check_utf8_comparisons(void)
 		const struct qstr s2 = {.name = nfdicf_test_data[i].ncf,
 					.len = sizeof(nfdicf_test_data[i].ncf)};
 
-		test_f(!utf8_strncasecmp(table, &s1, &s2),
+		test_f(!unicode_strncasecmp(table, &s1, &s2),
 		       "%s %s comparison mismatch\n", s1.name, s2.name);
 	}
 
-	utf8_unload(table);
+	unicode_unload(table);
 }
 
 static void check_supported_versions(void)
include/linux/unicode.h +16 -16
index 74484d44c755..de23f9ee720b 100644
@@ -10,27 +10,27 @@ struct unicode_map {
 	int version;
 };
 
-int utf8_validate(const struct unicode_map *um, const struct qstr *str);
+int unicode_validate(const struct unicode_map *um, const struct qstr *str);
 
-int utf8_strncmp(const struct unicode_map *um,
-		 const struct qstr *s1, const struct qstr *s2);
+int unicode_strncmp(const struct unicode_map *um,
+		    const struct qstr *s1, const struct qstr *s2);
 
-int utf8_strncasecmp(const struct unicode_map *um,
-		 const struct qstr *s1, const struct qstr *s2);
-int utf8_strncasecmp_folded(const struct unicode_map *um,
-			    const struct qstr *cf,
-			    const struct qstr *s1);
+int unicode_strncasecmp(const struct unicode_map *um,
+			const struct qstr *s1, const struct qstr *s2);
+int unicode_strncasecmp_folded(const struct unicode_map *um,
+			       const struct qstr *cf,
+			       const struct qstr *s1);
 
-int utf8_normalize(const struct unicode_map *um, const struct qstr *str,
-		   unsigned char *dest, size_t dlen);
+int unicode_normalize(const struct unicode_map *um, const struct qstr *str,
+		      unsigned char *dest, size_t dlen);
 
-int utf8_casefold(const struct unicode_map *um, const struct qstr *str,
-		  unsigned char *dest, size_t dlen);
+int unicode_casefold(const struct unicode_map *um, const struct qstr *str,
+		     unsigned char *dest, size_t dlen);
 
-int utf8_casefold_hash(const struct unicode_map *um, const void *salt,
-		       struct qstr *str);
+int unicode_casefold_hash(const struct unicode_map *um, const void *salt,
+			  struct qstr *str);
 
-struct unicode_map *utf8_load(const char *version);
-void utf8_unload(struct unicode_map *um);
+struct unicode_map *unicode_load(const char *version);
+void unicode_unload(struct unicode_map *um);
 
 #endif /* _LINUX_UNICODE_H */
Signature · 8 lines
2.30.2



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#3 lore raw

PATCH v6 3/4 fs: unicode: Rename utf8-core file to unicode-core

In reply to Shreeya Patel

utf8data.h_shipped has a large database table which is an auto-generated decodification trie for the unicode normalization functions and it is not necessary to carry this large table in the kernel.
Goal is to make UTF-8 encoding loadable by converting it into a module and adding a unicode subsystem layer between the filesystems and the utf8 module.

This layer will load the module whenever any filesystem that needs unicode is mounted.
Currently, only UTF-8 encoding is supported but if any other encodings are supported in future then the layer file would be responsible for loading the desired encoding module.

Rename the file name from utf8-core to unicode-core for transformation of utf8-core file into the unicode subsystem layer file and also for better understanding.
Implementation for unicode-core file to act as layer will be added in the future patches.

---
Changes in v6
- Improve the commit message.

 fs/unicode/Makefile                        | 2 +-
 fs/unicode/{utf8-core.c => unicode-core.c} | 0
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename fs/unicode/{utf8-core.c => unicode-core.c} (100%)
fs/unicode/Makefile +1 -1
index b88aecc86550..fbf9a629ed0d 100644
@@ -3,7 +3,7 @@
 obj-$(CONFIG_UNICODE) += unicode.o
 obj-$(CONFIG_UNICODE_NORMALIZATION_SELFTEST) += utf8-selftest.o
 
-unicode-y := utf8-norm.o utf8-core.o
+unicode-y := utf8-norm.o unicode-core.o
 
 $(obj)/utf8-norm.o: $(obj)/utf8data.h
 
fs/unicode/unicode-core.c renamed from fs/unicode/utf8-core.c +0 -0
similarity index 100%
rename from fs/unicode/utf8-core.c
rename to fs/unicode/unicode-core.c
Signature · 8 lines
2.30.2



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#4 lore raw

PATCH v6 4/4 fs: unicode: Add utf8 module and a unicode layer

In reply to Shreeya Patel

utf8data.h_shipped has a large database table which is an auto-generated decodification trie for the unicode normalization functions. It is not necessary to load this large table in the kernel if no filesystem is using it, hence make UTF-8 encoding loadable by converting it into a module.

Modify the file called unicode-core which will act as a layer for unicode subsystem. It will load the UTF-8 module and access it's functions whenever any filesystem that needs unicode is mounted. Currently, only UTF-8 encoding is supported but if any other encodings are supported in future then the layer file would be responsible for loading the desired encoding module.

Also, indirect calls using function pointers are slow, use static calls to avoid overhead caused in case of repeated indirect calls. Static calls improves the performance by directly calling the functions as opposed to indirect calls.

---
Changes in v6
- Add spinlock to protect utf8mod and avoid NULL pointer dereference.
- Change the static call function names for being consistent with kernel coding style.
- Merge the unicode_load_module function with unicode_load as it is not really needed to have a separate function.
- Use try_then_module_get instead of module_get to avoid loading the module even when it is already loaded.
- Improve the commit message.

Changes in v5
- Rename global variables and default static call functions for better understanding
- Make only config UNICODE_UTF8 visible and config UNICODE to be always enabled provided UNICODE_UTF8 is enabled.
- Improve the documentation for Kconfig
- Improve the commit message.

Changes in v4
- Return error from the static calls instead of doing nothing and succeeding even without loading the module.
- Remove the complete usage of utf8_ops and use static calls at all places.
- Restore the static calls to default values when module is unloaded.
- Decrement the reference of module after calling the unload function.
- Remove spinlock as there will be no race conditions after removing

    utf8_ops.

Changes in v3
- Add a patch which checks if utf8 is loaded before calling utf8_unload() in ext4 and f2fs filesystems
- Return error if strscpy() returns value < 0
- Correct the conditions to prevent NULL pointer dereference while accessing functions via utf8_ops variable.
- Add spinlock to avoid race conditions.
- Use static_call() for preventing speculative execution attacks.

Changes in v2
- Remove the duplicate file from the last patch.
- Make the wrapper functions inline.
- Remove msleep and use try_module_get() and module_put() for ensuring that module is loaded correctly and also doesn't get unloaded while in use.
- Resolve the warning reported by kernel test robot.
- Resolve all the checkpatch.pl warnings.

 fs/unicode/Kconfig        |  17 ++-
 fs/unicode/Makefile       |   5 +-
 fs/unicode/unicode-core.c | 289 ++++++++++++++------------------------
 fs/unicode/unicode-utf8.c | 264 ++++++++++++++++++++++++++++++++++
 include/linux/unicode.h   |  96 +++++++++++--
 5 files changed, 467 insertions(+), 204 deletions(-)
 create mode 100644 fs/unicode/unicode-utf8.c
fs/unicode/Kconfig +15 -2
index 2c27b9a5cd6c..ad4b837f2eb2 100644
@@ -2,13 +2,26 @@
 #
 # UTF-8 normalization
 #
+# CONFIG_UNICODE will be automatically enabled if CONFIG_UNICODE_UTF8
+# is enabled. This config option adds the unicode subsystem layer which loads
+# the UTF-8 module whenever any filesystem needs it.
 config UNICODE
-	bool "UTF-8 normalization and casefolding support"
+	bool
+
+# utf8data.h_shipped has a large database table which is an auto-generated
+# decodification trie for the unicode normalization functions and it is not
+# necessary to carry this large table in the kernel.
+# Enabling UNICODE_UTF8 option will allow UTF-8 encoding to be built as a
+# module and this module will be loaded by the unicode subsystem layer only
+# when any filesystem needs it.
+config UNICODE_UTF8
+	tristate "UTF-8 module"
 	help
 	  Say Y here to enable UTF-8 NFD normalization and NFD+CF casefolding
 	  support.
+	select UNICODE
 
 config UNICODE_NORMALIZATION_SELFTEST
 	tristate "Test UTF-8 normalization support"
-	depends on UNICODE
+	depends on UNICODE_UTF8
 	default n
fs/unicode/Makefile +4 -1
index fbf9a629ed0d..49d50083e6ee 100644
@@ -1,11 +1,14 @@
 # SPDX-License-Identifier: GPL-2.0
 
 obj-$(CONFIG_UNICODE) += unicode.o
+obj-$(CONFIG_UNICODE_UTF8) += utf8.o
 obj-$(CONFIG_UNICODE_NORMALIZATION_SELFTEST) += utf8-selftest.o
 
-unicode-y := utf8-norm.o unicode-core.o
+unicode-y := unicode-core.o
+utf8-y := unicode-utf8.o utf8-norm.o
 
 $(obj)/utf8-norm.o: $(obj)/utf8data.h
+$(obj)/unicode-utf8.o: $(obj)/utf8-norm.o
 
 # In the normal build, the checked-in utf8data.h is just shipped.
 #
fs/unicode/unicode-core.c +102 -187

339 lines not on this page · raw message

fs/unicode/unicode-utf8.c new file +264 -0

264 lines not on this page · raw message

include/linux/unicode.h +82 -14

115 lines not on this page · raw message

Signature · 8 lines
2.30.2



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#5 lore raw

Re: [PATCH v6 4/4] fs: unicode: Add utf8 module and a unicode layer

fs/unicode/Kconfig line 21

Quoted 11 lines · Shreeya Patel
fs/unicode/Kconfig +8 -0
+# utf8data.h_shipped has a large database table which is an auto-generated
+# decodification trie for the unicode normalization functions and it is not
+# necessary to carry this large table in the kernel.
+# Enabling UNICODE_UTF8 option will allow UTF-8 encoding to be built as a
+# module and this module will be loaded by the unicode subsystem layer only
+# when any filesystem needs it.
+config UNICODE_UTF8
+	tristate "UTF-8 module"
 	help
 	  Say Y here to enable UTF-8 NFD normalization and NFD+CF casefolding
 	  support.

Please update this help text to properly describe this option, especially the consequences of setting it to 'm'.

fs/unicode/Kconfig line 22

Quoted 1 line
fs/unicode/Kconfig +1 -0
+	select UNICODE

'select' should go before 'help'.

Quoted 33 lines

struct unicode_map *unicode_load(const char *version) {

fs/unicode/unicode-core.c +20 -0
+	try_then_request_module(utf8mod, "utf8");
+	if (!utf8mod) {
+		pr_err("Failed to load UTF-8 module\n");
+		return ERR_PTR(-ENODEV);
 	}
 
+	spin_lock(&utf8mod_lock);
+	if (!utf8mod || !try_module_get(utf8mod)) {
+		spin_unlock(&utf8mod_lock);
+		return ERR_PTR(-ENODEV);
+	}
+	spin_unlock(&utf8mod_lock);
+	return static_call(unicode_load_static_call)(version);
 }
 EXPORT_SYMBOL(unicode_load);
 
 void unicode_unload(struct unicode_map *um)
 {
 	kfree(um);
+
+	spin_lock(&utf8mod_lock);
+	if (utf8mod)
+		module_put(utf8mod);
+	spin_unlock(&utf8mod_lock);
+
 }
 EXPORT_SYMBOL(unicode_unload);
 
+void unicode_register(struct module *owner)
+{
+	utf8mod = owner;

fs/unicode/unicode-core.c line 150

Quoted 10 lines
fs/unicode/unicode-core.c +10 -0
+}
+EXPORT_SYMBOL(unicode_register);
+
+void unicode_unregister(void)
+{
+	spin_lock(&utf8mod_lock);
+	utf8mod = NULL;
+	spin_unlock(&utf8mod_lock);
+}
+EXPORT_SYMBOL(unicode_unregister);

This all looks very broken. First, when !CONFIG_MODULES, utf8mod will always be NULL so unicode_load() will always fail.

Also, if the unicode_load_static_call() fails, a reference to the utf8mod will be leaked.

Also, unicode_unload() can put a reference to the utf8mod that was never acquired.

Also there is a data race on utf8mod because the accesses to it aren't properly synchronized.

Please consider something like the following, which I think would address all these bugs:

static bool utf8mod_get(void)

{
	bool ret;

	spin_lock(&utf8mod_lock);
	ret = utf8mod_loaded && try_module_get(utf8mod);
	spin_unlock(&utf8mod_lock);
	return ret;
}

struct unicode_map *unicode_load(const char *version)

{
	struct unicode_map *um;

	if (!try_then_request_module(utf8mod_get(), "utf8")) {
		pr_err("Failed to load UTF-8 module\n");
		return ERR_PTR(-ENODEV);
	}
	um = static_call(unicode_load_static_call)(version);
	if (IS_ERR(um))
		module_put(utf8mod);
	return um;
}

EXPORT_SYMBOL(unicode_load);

void unicode_unload(struct unicode_map *um)

{
	if (um) {
		kfree(um);
		module_put(utf8mod);
	}
}

EXPORT_SYMBOL(unicode_unload);

void unicode_register(struct module *owner)

{
	spin_lock(&utf8mod_lock);
	utf8mod = owner; /* note: will be NULL if !CONFIG_MODULES */
	utf8mod_loaded = true;
	spin_unlock(&utf8mod_lock);
}

EXPORT_SYMBOL(unicode_register);

void unicode_unregister(void)

{
	spin_lock(&utf8mod_lock);
	utf8mod = NULL;
	utf8mod_loaded = false;
	spin_unlock(&utf8mod_lock);
}

EXPORT_SYMBOL(unicode_unregister);

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel