Skip to contentSkip to conversation

Error checks omitted in init_tmpfs() in mm/tiny-shmem.c

TRIVIAL Hareesh Nagarajan 3 participants 7 messages Oct 24, 2005 – Oct 25, 2005 Memory management

lore
Share & tools
lore
#0 lore raw

The existing code in init_tmpfs() in mm/tiny-shmem.c does not handle the cases when the calls to register_filesystem() and kern_mount() fail. This patch adds those checks.

mm/tiny-shmem.c +16 -1
@@ -31,12 +31,27 @@
 
 static int __init init_tmpfs(void)
 {
-	register_filesystem(&tmpfs_fs_type);
+	int error;
+
+	error = register_filesystem(&tmpfs_fs_type);
+	if (error) {
+		goto out2;
+	}
+
 #ifdef CONFIG_TMPFS
 	devfs_mk_dir("shm");
 #endif
 	shm_mnt = kern_mount(&tmpfs_fs_type);
+	if (IS_ERR(shm_mnt)) {
+		error = PTR_ERR(shm_mnt);
+		goto out1;
+	}
+
 	return 0;
+out1:
+	unregister_filesystem(&tmpfs_fs_type);
+out2:
+	return error;
 }
 module_init(init_tmpfs)
 
#1 lore raw
Quoted 3 lines · Hareesh Nagarajan

The existing code in init_tmpfs() in mm/tiny-shmem.c does not handle the cases when the calls to register_filesystem() and kern_mount() fail. This patch adds those checks.

Hmm. Did you actually encounter this?

I'd rather use BUG_ON. Passing up errors is only useful when the code above can and will do something useful with the information.

Here, we're talking about a built-in getting initialized at boot that can quietly fail and the upper layer will simply shrug and go on, leaving the system in a possibly useless state. Which is worse than not attempting error handling at all, because we've added complexity for no gain.

And what could the higher level, which is simply looping through init functions, do to handle the error? Retry? Print a warning? Better to stop everything outright when we encounter a problem we expect should never happen so it doesn't go by undiagnosed.

Quoted 31 lines
mm/tiny-shmem.c +16 -1
@@ -31,12 +31,27 @@
 
 static int __init init_tmpfs(void)
 {
-	register_filesystem(&tmpfs_fs_type);
+	int error;
+
+	error = register_filesystem(&tmpfs_fs_type);
+	if (error) {
+		goto out2;
+	}
+
 #ifdef CONFIG_TMPFS
 	devfs_mk_dir("shm");
 #endif
 	shm_mnt = kern_mount(&tmpfs_fs_type);
+	if (IS_ERR(shm_mnt)) {
+		error = PTR_ERR(shm_mnt);
+		goto out1;
+	}
+
 	return 0;
+out1:
+	unregister_filesystem(&tmpfs_fs_type);
+out2:
+	return error;
 }
 module_init(init_tmpfs)
 
Signature · 1 line
Mathematics is the supreme nostalgia of our time.
#2 lore raw
Quoted 4 lines · Matt Mackall
Quoted 1 line · Hareesh Nagarajan

The existing code in init_tmpfs() in mm/tiny-shmem.c does not handle the cases when the calls to register_filesystem() and kern_mount() fail. This patch adds those checks.

Hmm. Did you actually encounter this?

No, I haven't. I was just reading the source code when I chanced upon these trivial error checking omissions.

Quoted 2 lines

I'd rather use BUG_ON. Passing up errors is only useful when the code above can and will do something useful with the information.

[ Snip ]

Quoted 4 lines

And what could the higher level, which is simply looping through init functions, do to handle the error? Retry? Print a warning? Better to stop everything outright when we encounter a problem we expect should never happen so it doesn't go by undiagnosed.

Makes sense. New patch attached.

mm/tiny-shmem.c +8 -2
@@ -31,12 +31,18 @@
 
 static int __init init_tmpfs(void)
 {
-	register_filesystem(&tmpfs_fs_type);
+	int error;
+
+	error = register_filesystem(&tmpfs_fs_type);
+	BUG_ON(error);
+
 #ifdef CONFIG_TMPFS
 	devfs_mk_dir("shm");
 #endif
 	shm_mnt = kern_mount(&tmpfs_fs_type);
-	return 0;
+	BUG_ON(IS_ERR(shm_mnt));
+
+	return error;
 }
 module_init(init_tmpfs)
 
#3 lore raw
Quoted 14 lines · Hareesh Nagarajan
Quoted 6 lines · Matt Mackall
Quoted 3 lines · Hareesh Nagarajan

The existing code in init_tmpfs() in mm/tiny-shmem.c does not handle the cases when the calls to register_filesystem() and kern_mount() fail. This patch adds those checks.

Hmm. Did you actually encounter this?

No, I haven't. I was just reading the source code when I chanced upon these trivial error checking omissions.

Quoted 2 lines

I'd rather use BUG_ON. Passing up errors is only useful when the code above can and will do something useful with the information.

Quoted 8 lines

[ Snip ]

Quoted 4 lines

And what could the higher level, which is simply looping through init functions, do to handle the error? Retry? Print a warning? Better to stop everything outright when we encounter a problem we expect should never happen so it doesn't go by undiagnosed.

Makes sense. New patch attached.

A couple more comments..

mm/tiny-shmem.c line 37

Quoted 13 lines
mm/tiny-shmem.c +4 -1
@@ -31,12 +31,18 @@
 
 static int __init init_tmpfs(void)
 {
-	register_filesystem(&tmpfs_fs_type);
+	int error;
+
+	error = register_filesystem(&tmpfs_fs_type);
+	BUG_ON(error);

Can we just do BUG_ON(register_filesystem() != 0)?

Strictly speaking, the != 0 is redundant, but as this goes slightly against the grain of normal usage, it's a good indicator of intent.

mm/tiny-shmem.c line 45

Quoted 9 lines
mm/tiny-shmem.c +4 -1
+
 #ifdef CONFIG_TMPFS
 	devfs_mk_dir("shm");
 #endif
 	shm_mnt = kern_mount(&tmpfs_fs_type);
-	return 0;
+	BUG_ON(IS_ERR(shm_mnt));
+
+	return error;

We can never return non-zero here. Returning error implies we can, so it's confusing.

Signature · 1 line
Mathematics is the supreme nostalgia of our time.
#4 lore raw
Quoted 4 lines · Matt Mackall
Quoted 4 lines
mm/tiny-shmem.c +0 -0
@@ -31,12 +31,18 @@
 
Quoted 9 lines

mm/tiny-shmem.c line 37

Quoted 7 lines
mm/tiny-shmem.c +4 -1
 static int __init init_tmpfs(void)
 {
-	register_filesystem(&tmpfs_fs_type);
+	int error;
+
+	error = register_filesystem(&tmpfs_fs_type);
+	BUG_ON(error);

Can we just do BUG_ON(register_filesystem() != 0)?

It seems a little risky to me to rely on a macro always evaluating its arguments, even though this one does on every kernel version I checked.

Cheers,
Muli

Signature · 2 lines
Muli Ben-Yehuda
http://www.mulix.org | http://mulix.livejournal.com/
#5 lore raw
Quoted 16 lines · Muli Ben-Yehuda
Quoted 4 lines · Matt Mackall
Quoted 4 lines
mm/tiny-shmem.c +0 -0
@@ -31,12 +31,18 @@
 
Quoted 9 lines

mm/tiny-shmem.c line 37

Quoted 7 lines
mm/tiny-shmem.c +4 -1
 static int __init init_tmpfs(void)
 {
-	register_filesystem(&tmpfs_fs_type);
+	int error;
+
+	error = register_filesystem(&tmpfs_fs_type);
+	BUG_ON(error);

Can we just do BUG_ON(register_filesystem() != 0)?

Quoted 3 lines

It seems a little risky to me to rely on a macro always evaluating its arguments, even though this one does on every kernel version I checked.

You must have missed my patch on 1 April that allows turning off all kernel bugs. It makes sure the arguments are still evaluated.

Signature · 1 line
Mathematics is the supreme nostalgia of our time.
#6 lore raw
In reply to Matt Mackall
Quoted 4 lines · Matt Mackall
Quoted 3 lines · Hareesh Nagarajan
Quoted 2 lines · Matt Mackall
Quoted 1 line · Hareesh Nagarajan

The existing code in init_tmpfs() in mm/tiny-shmem.c does not handle the cases when the calls to register_filesystem() and kern_mount() fail. This patch adds those checks.

[ Snip ]

Quoted 18 lines

A couple more comments..

mm/tiny-shmem.c line 37

Quoted 13 lines
mm/tiny-shmem.c +4 -1
@@ -31,12 +31,18 @@
 
 static int __init init_tmpfs(void)
 {
-	register_filesystem(&tmpfs_fs_type);
+	int error;
+
+	error = register_filesystem(&tmpfs_fs_type);
+	BUG_ON(error);

Can we just do BUG_ON(register_filesystem() != 0)?

Quoted 2 lines

Strictly speaking, the != 0 is redundant, but as this goes slightly against the grain of normal usage, it's a good indicator of intent.

It shows intent well. That goes into my book for good programming practices. No more BUG_ON(foo) :)

Quoted 11 lines

mm/tiny-shmem.c line 44

Quoted 8 lines
mm/tiny-shmem.c +3 -1
 #ifdef CONFIG_TMPFS
 	devfs_mk_dir("shm");
 #endif
 	shm_mnt = kern_mount(&tmpfs_fs_type);
-	return 0;
+	BUG_ON(IS_ERR(shm_mnt));
+
+	return error;

We can never return non-zero here. Returning error implies we can, so it's confusing.

Makes sense again. Patch follows.

mm/tiny-shmem.c +4 -1
@@ -31,11 +31,14 @@
 
 static int __init init_tmpfs(void)
 {
-	register_filesystem(&tmpfs_fs_type);
+	BUG_ON(register_filesystem(&tmpfs_fs_type) != 0);
+
 #ifdef CONFIG_TMPFS
 	devfs_mk_dir("shm");
 #endif
 	shm_mnt = kern_mount(&tmpfs_fs_type);
+	BUG_ON(IS_ERR(shm_mnt));
+
 	return 0;
 }
 module_init(init_tmpfs)