linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Race between cat /proc/kallsyms and rmmod
@ 2016-01-30 10:23 Weilong Chen
  2016-01-30 10:45 ` kbuild test robot
  0 siblings, 1 reply; 5+ messages in thread
From: Weilong Chen @ 2016-01-30 10:23 UTC (permalink / raw)
  To: linux-kernel, jcm, rusty

Iterating code of /proc/kallsyms calls module_get_kallsym() which grabs
and drops module_mutex internally and returns "struct module *",
module is removed, aforementioned "struct module *" is used in non-trivial
way.

So, grab module_mutex for entire operation like /proc/modules does.

Steps to reproduce:
while true; do modprobe xfs; rmmod xfs; done
vs
while true; do cat /proc/kallsyms >/dev/null; done

ref:http://lkml.iu.edu/hypermail/linux/kernel/0703.1/2582.html
    https://bugzilla.kernel.org/show_bug.cgi?id=111541

Signed-off-by: Weilong Chen <chenweilong@huawei.com>
---
 kernel/kallsyms.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
index 3127ad5..aaecf19 100644
--- a/kernel/kallsyms.c
+++ b/kernel/kallsyms.c
@@ -515,6 +515,7 @@ static void *s_next(struct seq_file *m, void *p, loff_t *pos)
 
 static void *s_start(struct seq_file *m, loff_t *pos)
 {
+	mutex_lock(&module_mutex);
 	if (!update_iter(m->private, *pos))
 		return NULL;
 	return m->private;
@@ -522,6 +523,7 @@ static void *s_start(struct seq_file *m, loff_t *pos)
 
 static void s_stop(struct seq_file *m, void *p)
 {
+	mutex_unlock(&module_mutex);
 }
 
 static int s_show(struct seq_file *m, void *p)
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] Race between cat /proc/kallsyms and rmmod
  2016-01-30 10:23 [PATCH] Race between cat /proc/kallsyms and rmmod Weilong Chen
@ 2016-01-30 10:45 ` kbuild test robot
  0 siblings, 0 replies; 5+ messages in thread
From: kbuild test robot @ 2016-01-30 10:45 UTC (permalink / raw)
  To: Weilong Chen; +Cc: kbuild-all, linux-kernel, jcm, rusty

[-- Attachment #1: Type: text/plain, Size: 2227 bytes --]

Hi Weilong,

[auto build test ERROR on v4.5-rc1]
[also build test ERROR on next-20160129]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Weilong-Chen/Race-between-cat-proc-kallsyms-and-rmmod/20160130-182606
config: i386-randconfig-a0-201604 (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

   In file included from include/linux/notifier.h:13:0,
                    from include/linux/memory_hotplug.h:6,
                    from include/linux/mmzone.h:728,
                    from include/linux/gfp.h:5,
                    from include/linux/kmod.h:22,
                    from include/linux/module.h:13,
                    from kernel/kallsyms.c:15:
   kernel/kallsyms.c: In function 's_start':
>> kernel/kallsyms.c:519:14: error: 'module_mutex' undeclared (first use in this function)
     mutex_lock(&module_mutex);
                 ^
   include/linux/mutex.h:146:44: note: in definition of macro 'mutex_lock'
    #define mutex_lock(lock) mutex_lock_nested(lock, 0)
                                               ^
   kernel/kallsyms.c:519:14: note: each undeclared identifier is reported only once for each function it appears in
     mutex_lock(&module_mutex);
                 ^
   include/linux/mutex.h:146:44: note: in definition of macro 'mutex_lock'
    #define mutex_lock(lock) mutex_lock_nested(lock, 0)
                                               ^
   kernel/kallsyms.c: In function 's_stop':
   kernel/kallsyms.c:527:16: error: 'module_mutex' undeclared (first use in this function)
     mutex_unlock(&module_mutex);
                   ^

vim +/module_mutex +519 kernel/kallsyms.c

   513			return NULL;
   514		return p;
   515	}
   516	
   517	static void *s_start(struct seq_file *m, loff_t *pos)
   518	{
 > 519		mutex_lock(&module_mutex);
   520		if (!update_iter(m->private, *pos))
   521			return NULL;
   522		return m->private;

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/octet-stream, Size: 25004 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Race between cat /proc/kallsyms and rmmod
  2007-03-14 11:13 Alexey Dobriyan
  2007-03-14 12:11 ` Andi Kleen
@ 2007-03-14 12:55 ` Paulo Marques
  1 sibling, 0 replies; 5+ messages in thread
From: Paulo Marques @ 2007-03-14 12:55 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: akpm, linux-kernel, devel

Alexey Dobriyan wrote:
> Iterating code of /proc/kallsyms calls module_get_kallsym() which grabs
> and drops module_mutex internally and returns "struct module *",
> module is removed, aforementioned "struct module *" is used in non-trivial
> way.
> So, grab module_mutex for entire operation like /proc/modules does.

I would still prefer the other solution to avoid exposing "module_mutex" 
outside of module.c like this :(

I'll try to send in a patch today for review.

-- 
Paulo Marques - www.grupopie.com

"As far as we know, our computer has never had an undetected error."
Weisert

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Race between cat /proc/kallsyms and rmmod
  2007-03-14 11:13 Alexey Dobriyan
@ 2007-03-14 12:11 ` Andi Kleen
  2007-03-14 12:55 ` Paulo Marques
  1 sibling, 0 replies; 5+ messages in thread
From: Andi Kleen @ 2007-03-14 12:11 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: akpm, linux-kernel, devel, pmarques

Alexey Dobriyan <adobriyan@sw.ru> writes:
> 
> --- a/kernel/kallsyms.c
> +++ b/kernel/kallsyms.c
> @@ -40,6 +40,8 @@ extern const u16 kallsyms_token_index[] 
>  
>  extern const unsigned long kallsyms_markers[] __attribute__((weak));
>  
> +extern struct mutex module_mutex;

No externs in .c files

-Andi

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] Race between cat /proc/kallsyms and rmmod
@ 2007-03-14 11:13 Alexey Dobriyan
  2007-03-14 12:11 ` Andi Kleen
  2007-03-14 12:55 ` Paulo Marques
  0 siblings, 2 replies; 5+ messages in thread
From: Alexey Dobriyan @ 2007-03-14 11:13 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel, devel, pmarques

Iterating code of /proc/kallsyms calls module_get_kallsym() which grabs
and drops module_mutex internally and returns "struct module *",
module is removed, aforementioned "struct module *" is used in non-trivial
way.

So, grab module_mutex for entire operation like /proc/modules does.

Steps to reproduce:
	while true; do modprobe xfs; rmmod xfs; done
		vs
	while true; do cat /proc/kallsyms >/dev/null; done

[where xfs could be any module, I haven't tried]

BUG: unable to handle kernel paging request at virtual address e19f808c
 printing eip:
c01dc361
*pde = 1ff5f067
*pte = 00000000
Oops: 0000 [#1]
PREEMPT
Modules linked in:
CPU:    0
EIP:    0060:[<c01dc361>]    Not tainted VLI
EFLAGS: 00010297   (2.6.21-rc3-8b9909ded6922c33c221b105b26917780cfa497d #2)
EIP is at vsnprintf+0x2af/0x48c
eax: e19f808c   ebx: ffffffff   ecx: e19f808c   edx: fffffffe
esi: dbe7aa84   edi: dbe2bf3c   ebp: ffffffff   esp: dbe2bec4
ds: 007b   es: 007b   fs: 00d8  gs: 0033  ss: 0068
Process cat (pid: 7242, ti=dbe2b000 task=df5790b0 task.ti=dbe2b000)
Stack: e19d6fde 00000000 00000010 00000008 ffffffff 00000001 00000598 dbe7aa68
       0002f362 00000010 dbe7b000 00000000 ffffffff c034bbe0 dbe7aa68 dfd31880
       dfa31e80 00001000 c01586b0 dbe2bf2c dbe2bf2c dfd31880 dfd31880 c01289f6
Call Trace:
 [<c01586b0>] seq_printf+0x2e/0x4b
 [<c01289f6>] s_show+0x4b/0x7f
 [<c0158c6e>] seq_read+0x196/0x278
 [<c0158ad8>] seq_read+0x0/0x278
 [<c0143c35>] vfs_read+0x72/0x93
 [<c0143f1c>] sys_read+0x41/0x67
 [<c0102486>] sysenter_past_esp+0x5f/0x85
=======================
Code: 74 24 28 73 03 c6 06 20 46 4d 85 ed 7f f1 e9 b9 00 00 00 8b 0f 81 f9 ff 0f 00 00 b8 ea 45 36 c0 0f 46 c8 8b 54 24 30 89 c8 eb 06 <80> 38 00 74 07 40 4a 83 fa ff 75 f4 29 c8 89 c3 89 e8 f6 44 24
EIP: [<c01dc361>] vsnprintf+0x2af/0x48c SS:ESP 0068:dbe2bec4

Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
---

 kernel/kallsyms.c |    4 ++++
 kernel/module.c   |    5 +----
 2 files changed, 5 insertions(+), 4 deletions(-)

--- a/kernel/kallsyms.c
+++ b/kernel/kallsyms.c
@@ -40,6 +40,8 @@ extern const u16 kallsyms_token_index[] 
 
 extern const unsigned long kallsyms_markers[] __attribute__((weak));
 
+extern struct mutex module_mutex;
+
 static inline int is_kernel_inittext(unsigned long addr)
 {
 	if (addr >= (unsigned long)_sinittext
@@ -369,6 +371,7 @@ static void *s_next(struct seq_file *m, 
 
 static void *s_start(struct seq_file *m, loff_t *pos)
 {
+	mutex_lock(&module_mutex);
 	if (!update_iter(m->private, *pos))
 		return NULL;
 	return m->private;
@@ -376,6 +379,7 @@ static void *s_start(struct seq_file *m,
 
 static void s_stop(struct seq_file *m, void *p)
 {
+	mutex_unlock(&module_mutex);
 }
 
 static int s_show(struct seq_file *m, void *p)
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -62,7 +62,7 @@ #define INIT_OFFSET_MASK (1UL << (BITS_P
 static DEFINE_SPINLOCK(modlist_lock);
 
 /* List of modules, protected by module_mutex AND modlist_lock */
-static DEFINE_MUTEX(module_mutex);
+DEFINE_MUTEX(module_mutex);
 static LIST_HEAD(modules);
 
 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
@@ -2124,19 +2124,16 @@ struct module *module_get_kallsym(unsign
 {
 	struct module *mod;
 
-	mutex_lock(&module_mutex);
 	list_for_each_entry(mod, &modules, list) {
 		if (symnum < mod->num_symtab) {
 			*value = mod->symtab[symnum].st_value;
 			*type = mod->symtab[symnum].st_info;
 			strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
 				namelen);
-			mutex_unlock(&module_mutex);
 			return mod;
 		}
 		symnum -= mod->num_symtab;
 	}
-	mutex_unlock(&module_mutex);
 	return NULL;
 }
 


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-01-30 10:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-30 10:23 [PATCH] Race between cat /proc/kallsyms and rmmod Weilong Chen
2016-01-30 10:45 ` kbuild test robot
  -- strict thread matches above, loose matches on Subject: below --
2007-03-14 11:13 Alexey Dobriyan
2007-03-14 12:11 ` Andi Kleen
2007-03-14 12:55 ` Paulo Marques

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).