Skip to contentSkip to conversation

Race condition: calling remove_proc_entry in cleanup_module (module_exit) while someone's using procfile

DISCUSSION anon... anon.al 2 participants 4 messages Sep 4, 2007

lore
Share & tools
lore
#0 lore raw

Hi!

There is a race condition if an instance is executing "__exit device_exit" and calls remove_proc_entry, while someone is still using the procfile, right?.

static void __exit device_exit(void)
{
// what if the procfile is still in use?
remove_proc_entry(PROC_FILE_NAME, &proc_root);
}

To remove this race condition, the code in "__exit device_exit" must
a) be sure that no other instance is in procfile functions
b) call remove_proc_entry before any other instance accesses the procfile

*** Is this at all possible if a race-condition is to be avoided? If yes: which mechanism can be used?

Thanks -Albert

#1 lore raw

On 9/4/07, anon... anon.al <[email protected]> wrote:
<snip>

Quoted 1 line

If yes: which mechanism can be used?

I was thinking about using an atomic counter in procfile_write

  proc_f = create_proc_entry(PROC_FILE_NAME, 0644, NULL);
  //...
  proc_f->write_proc = procfile_write;

int procfile_write(struct file *filp, const char *buffer, \
unsigned long len, void *data)
{
//"StackXXX"
atomic_inc(&cnt_procfile_users);

  printk(KERN_ALERT "Hi there!\n");

atomic_dec(&cnt_procfile_users);
wake_up_interruptible(&queue);
return len;
}

and then in cleanup_module using:

wait_event_interruptible(queue, \
( \
spin_lock_irqsave(&lock, flags), \
cnt = atomic_read(&cnt_procfile_users), \
((cnt == 0) \
? 1 \
: (spin_unlock_irqrestore(&lock, flags), 0))\
));
remove_proc_entry(PROC_FILE_NAME, &proc_root);
spin_unlock_irqrestore(&lock, flags);

But:
x1)
Could it happen that code is already in function procfile_write at "StackXXX" (before atomic_inc(&cnt_procfile_users)) when the scheduler switches to another task??
((Or is the "entering into a function, up to the function's first statement" atomic??))

x2)
Could it happen that the scheduler switches, after atomic_dev(&cnt_procfile_users) but before return len??

If so, then it could happen that we're in spin_lock_irqsave, while someone else is still using the procfile; and then this code still fails miserably.
?

Regards -Albert

#2 lore raw
In reply to anon... anon.al
Quoted 9 lines · anon... anon.al

There is a race condition if an instance is executing "__exit device_exit" and calls remove_proc_entry, while someone is still using the procfile, right?.

static void __exit device_exit(void)
{
// what if the procfile is still in use?
remove_proc_entry(PROC_FILE_NAME, &proc_root);
}

For regular proc files, this is fixed in 2.6.23-rc1 and later.

#3 lore raw
Quoted 1 line · Alexey Dobriyan

For regular proc files, this is fixed in 2.6.23-rc1 and later.

Thanks!

I see you've been working on it:
fix-rmmod-read-write-races-in-proc-entries...
http://www.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.22-rc6/2.6.22-rc6-mm1/broken-out/

((
older relevant posts:
http://groups.google.com/group/linux.kernel/browse_thread/thread/4c74bbea17727e6e/809c15bd0e6fa8f9?
http://groups.google.com/group/fa.linux.kernel/browse_thread/thread/fb03e1a500fcb258/563a7f11acdec992?
))

Thanks, Albert