Instead of using fs_initcall(), initialize sysfs with the rest of the
filesystem. This is the right way to do it because otherwise any error
during tmpfs_sysfs_init() would get silently ignored. It's also useful
if tmpfs' sysfs ever need to display runtime information.
Signed-off-by: André Almeida <andrealmeid@igalia.com>
---
mm/shmem.c | 130 ++++++++++++++++++++++++++++-------------------------
1 file changed, 68 insertions(+), 62 deletions(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index 6038e1d11987..8ff2f619f531 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -5126,6 +5126,66 @@ static struct file_system_type shmem_fs_type = {
.fs_flags = FS_USERNS_MOUNT | FS_ALLOW_IDMAP | FS_MGTIME,
};
+#if defined(CONFIG_SYSFS) && defined(CONFIG_TMPFS)
+
+#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
+{ \
+ .attr = { .name = __stringify(_name), .mode = _mode }, \
+ .show = _show, \
+ .store = _store, \
+}
+
+#define TMPFS_ATTR_W(_name, _store) \
+ static struct kobj_attribute tmpfs_attr_##_name = \
+ __INIT_KOBJ_ATTR(_name, 0200, NULL, _store)
+
+#define TMPFS_ATTR_RW(_name, _show, _store) \
+ static struct kobj_attribute tmpfs_attr_##_name = \
+ __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
+
+#define TMPFS_ATTR_RO(_name, _show) \
+ static struct kobj_attribute tmpfs_attr_##_name = \
+ __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
+
+#if IS_ENABLED(CONFIG_UNICODE)
+static ssize_t casefold_show(struct kobject *kobj, struct kobj_attribute *a,
+ char *buf)
+{
+ return sysfs_emit(buf, "supported\n");
+}
+TMPFS_ATTR_RO(casefold, casefold_show);
+#endif
+
+static struct attribute *tmpfs_attributes[] = {
+#if IS_ENABLED(CONFIG_UNICODE)
+ &tmpfs_attr_casefold.attr,
+#endif
+ NULL
+};
+
+static const struct attribute_group tmpfs_attribute_group = {
+ .attrs = tmpfs_attributes,
+ .name = "features"
+};
+
+static struct kobject *tmpfs_kobj;
+
+static int __init tmpfs_sysfs_init(void)
+{
+ int ret;
+
+ tmpfs_kobj = kobject_create_and_add("tmpfs", fs_kobj);
+ if (!tmpfs_kobj)
+ return -ENOMEM;
+
+ ret = sysfs_create_group(tmpfs_kobj, &tmpfs_attribute_group);
+ if (ret)
+ kobject_put(tmpfs_kobj);
+
+ return ret;
+}
+#endif /* CONFIG_SYSFS && CONFIG_TMPFS */
+
void __init shmem_init(void)
{
int error;
@@ -5149,6 +5209,14 @@ void __init shmem_init(void)
goto out1;
}
+#ifdef CONFIG_SYSFS
+ error = tmpfs_sysfs_init();
+ if (error) {
+ pr_err("Could not init tmpfs sysfs\n");
+ goto out1;
+ }
+#endif
+
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
@@ -5546,65 +5614,3 @@ struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
return page;
}
EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);
-
-#if defined(CONFIG_SYSFS) && defined(CONFIG_TMPFS)
-
-#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
-{ \
- .attr = { .name = __stringify(_name), .mode = _mode }, \
- .show = _show, \
- .store = _store, \
-}
-
-#define TMPFS_ATTR_W(_name, _store) \
- static struct kobj_attribute tmpfs_attr_##_name = \
- __INIT_KOBJ_ATTR(_name, 0200, NULL, _store)
-
-#define TMPFS_ATTR_RW(_name, _show, _store) \
- static struct kobj_attribute tmpfs_attr_##_name = \
- __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
-
-#define TMPFS_ATTR_RO(_name, _show) \
- static struct kobj_attribute tmpfs_attr_##_name = \
- __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
-
-#if IS_ENABLED(CONFIG_UNICODE)
-static ssize_t casefold_show(struct kobject *kobj, struct kobj_attribute *a,
- char *buf)
-{
- return sysfs_emit(buf, "supported\n");
-}
-TMPFS_ATTR_RO(casefold, casefold_show);
-#endif
-
-static struct attribute *tmpfs_attributes[] = {
-#if IS_ENABLED(CONFIG_UNICODE)
- &tmpfs_attr_casefold.attr,
-#endif
- NULL
-};
-
-static const struct attribute_group tmpfs_attribute_group = {
- .attrs = tmpfs_attributes,
- .name = "features"
-};
-
-static struct kobject *tmpfs_kobj;
-
-static int __init tmpfs_sysfs_init(void)
-{
- int ret;
-
- tmpfs_kobj = kobject_create_and_add("tmpfs", fs_kobj);
- if (!tmpfs_kobj)
- return -ENOMEM;
-
- ret = sysfs_create_group(tmpfs_kobj, &tmpfs_attribute_group);
- if (ret)
- kobject_put(tmpfs_kobj);
-
- return ret;
-}
-
-fs_initcall(tmpfs_sysfs_init);
-#endif /* CONFIG_SYSFS && CONFIG_TMPFS */
--
2.47.0
Hi André,
On Thu, Oct 31, 2024 at 10:37:41PM -0300, André Almeida wrote:
> Instead of using fs_initcall(), initialize sysfs with the rest of the
> filesystem. This is the right way to do it because otherwise any error
> during tmpfs_sysfs_init() would get silently ignored. It's also useful
> if tmpfs' sysfs ever need to display runtime information.
>
> Signed-off-by: André Almeida <andrealmeid@igalia.com>
> ---
> mm/shmem.c | 130 ++++++++++++++++++++++++++++-------------------------
> 1 file changed, 68 insertions(+), 62 deletions(-)
>
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 6038e1d11987..8ff2f619f531 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -5126,6 +5126,66 @@ static struct file_system_type shmem_fs_type = {
> .fs_flags = FS_USERNS_MOUNT | FS_ALLOW_IDMAP | FS_MGTIME,
> };
>
> +#if defined(CONFIG_SYSFS) && defined(CONFIG_TMPFS)
This condition...
> +static int __init tmpfs_sysfs_init(void)
> +{
> + int ret;
> +
> + tmpfs_kobj = kobject_create_and_add("tmpfs", fs_kobj);
> + if (!tmpfs_kobj)
> + return -ENOMEM;
> +
> + ret = sysfs_create_group(tmpfs_kobj, &tmpfs_attribute_group);
> + if (ret)
> + kobject_put(tmpfs_kobj);
> +
> + return ret;
> +}
> +#endif /* CONFIG_SYSFS && CONFIG_TMPFS */
> +
> void __init shmem_init(void)
> {
> int error;
> @@ -5149,6 +5209,14 @@ void __init shmem_init(void)
> goto out1;
> }
>
> +#ifdef CONFIG_SYSFS
and this condition are not the same, so there will be a compile error if
CONFIG_SHMEM and CONFIG_SYSFS are enabled but CONFIG_TMPFS is not, such
as with ARCH=x86_64 allnoconfig for me:
mm/shmem.c: In function 'shmem_init':
mm/shmem.c:5243:17: error: implicit declaration of function 'tmpfs_sysfs_init'; did you mean 'uids_sysfs_init'? [-Wimplicit-function-declaration]
5243 | error = tmpfs_sysfs_init();
| ^~~~~~~~~~~~~~~~
| uids_sysfs_init
> + error = tmpfs_sysfs_init();
> + if (error) {
> + pr_err("Could not init tmpfs sysfs\n");
> + goto out1;
> + }
> +#endif
Cheers,
Nathan
Em 01/11/2024 04:19, Nathan Chancellor escreveu:
> Hi André,
>
> On Thu, Oct 31, 2024 at 10:37:41PM -0300, André Almeida wrote:
>> Instead of using fs_initcall(), initialize sysfs with the rest of the
>> filesystem. This is the right way to do it because otherwise any error
>> during tmpfs_sysfs_init() would get silently ignored. It's also useful
>> if tmpfs' sysfs ever need to display runtime information.
>>
>> Signed-off-by: André Almeida <andrealmeid@igalia.com>
>> ---
>> mm/shmem.c | 130 ++++++++++++++++++++++++++++-------------------------
>> 1 file changed, 68 insertions(+), 62 deletions(-)
>>
>> diff --git a/mm/shmem.c b/mm/shmem.c
>> index 6038e1d11987..8ff2f619f531 100644
>> --- a/mm/shmem.c
>> +++ b/mm/shmem.c
>> @@ -5126,6 +5126,66 @@ static struct file_system_type shmem_fs_type = {
>> .fs_flags = FS_USERNS_MOUNT | FS_ALLOW_IDMAP | FS_MGTIME,
>> };
>>
>> +#if defined(CONFIG_SYSFS) && defined(CONFIG_TMPFS)
>
> This condition...
>
>> +static int __init tmpfs_sysfs_init(void)
>> +{
>> + int ret;
>> +
>> + tmpfs_kobj = kobject_create_and_add("tmpfs", fs_kobj);
>> + if (!tmpfs_kobj)
>> + return -ENOMEM;
>> +
>> + ret = sysfs_create_group(tmpfs_kobj, &tmpfs_attribute_group);
>> + if (ret)
>> + kobject_put(tmpfs_kobj);
>> +
>> + return ret;
>> +}
>> +#endif /* CONFIG_SYSFS && CONFIG_TMPFS */
>> +
>> void __init shmem_init(void)
>> {
>> int error;
>> @@ -5149,6 +5209,14 @@ void __init shmem_init(void)
>> goto out1;
>> }
>>
>> +#ifdef CONFIG_SYSFS
>
> and this condition are not the same, so there will be a compile error if
> CONFIG_SHMEM and CONFIG_SYSFS are enabled but CONFIG_TMPFS is not, such
> as with ARCH=x86_64 allnoconfig for me:
>
> mm/shmem.c: In function 'shmem_init':
> mm/shmem.c:5243:17: error: implicit declaration of function 'tmpfs_sysfs_init'; did you mean 'uids_sysfs_init'? [-Wimplicit-function-declaration]
> 5243 | error = tmpfs_sysfs_init();
> | ^~~~~~~~~~~~~~~~
> | uids_sysfs_init
>
Thanks for the catch! Fixed for v2
>> + error = tmpfs_sysfs_init();
>> + if (error) {
>> + pr_err("Could not init tmpfs sysfs\n");
>> + goto out1;
>> + }
>> +#endif
>
> Cheers,
> Nathan
© 2016 - 2026 Red Hat, Inc.