On Mon, May 03, 2004 at 22:16:21 +0900, Tetsuo Handa wrote: > Hello. > > I'm developing a small patch in fs/*.c that needs to refer > '(struct task_struct *) current'. And I want to know whether > the value of 'current' is valid or not. > > According to 'Unreliable Guide To Hacking The Linux Kernel', > there are 4 players. > > (1) not associated with any process, serving a hardware interrupt; > (2) not associated with any process, serving a softirq, tasklet or bh; > (3) running in kernel space, associated with a process; > (4) running a process in user space. > > And also says, "value of 'current' is not NULL in interrupt context". > I think it means in the state (1) or (2), I must not refer to 'current', > but I can't determine the state by checking 'current' == NULL. Yes, it's right. > I don't know whether functions defined in fs/*.c is called > by the players (1) and (2). > But if these functions could be called by (1) and (2), > I need to confirm whether I can refer to 'current' or not. Every function in fs/*.c has known calling rules that state, whether it can be called in interrupt (either (1) or (2)) context or not. If it can, it must never use current. > How can I determine the value of 'current' is valid or not > (if these functions could be called by (1) and (2)) ? current must only be used in (3) and is *always* valid in that context. In (1) and (2) it points to a randomly chosen task struct (the one currently scheduled). > I want to know 'current' is valid or not > even when interrupts or bottom halves are disabled > outside of my patch functions, > so it is not enough just checking "in_interrupt() returns 0". Having a function, that can be called from interrupt context and may not and if it's not does something to current sounds like a broken semantics. Either the function should do something with current -- and must not be called in interrupt context (which you may check, if you want to be sure -- BUG_ON(in_interrupt())), or it does not do anything to current (nor any other things forbidden in interrupt) and can be called there. (There are some functions -- eg. kmalloc -- that sometimes need current -- but it's not based on context but on parameter passed to it). Generaly, you should think whether the function will be called "on behalf of a process". If it does (eg. everything called from syscalls), it will have valid "current", always. If it is, or may be, called by other means that syscall, the current it gets does not need to make sense (it may be valid (it always is), may be even useable (process context), but may be the one of kswapd or something like that). ------------------------------------------------------------------------------- Jan 'Bulb' Hudec <bulb@ucw.cz>
Attachment:
signature.asc
Description: Digital signature