I'm writing something on the linux scheduler and I need to know which process was running before my interrupt came in.. is the current structure available? If I do current->pid while in the interrupt handler, do I get the pid of the process I interrupted?
Asked
Active
Viewed 2,637 times
2 Answers
5
You can, current->pid
exists and is the process that was interrupted (may be the idle thread, or any).
If you're writing inside the Linux scheduler, you should be very careful. current
is changed by the scheduler as it chooses a new process to run, so its value depends on when exactly you read it.

ugoren
- 16,023
- 3
- 35
- 65
-
*current* just masks low bits of %esp, right? And there is something called interrupt stack in Linux. If the interrupt switched you to other stack and you mask %esp, then you're nowhere near any valid struct task_info. IOW task_info is part of stack, you won't find it on interrupt stack. I'm I missing something? – moorray May 13 '12 at 13:49
-
2@mooray, `current` works. `current` first gets the `thread_info` (in x86, by masking %esp), where it finds a pointer to `task_struct`. When switching to an interrupt stack, `do_IRQ` sets this pointer according to the process's kernel stack. – ugoren May 13 '12 at 14:10
-
@ugoren, sorry for waking this old thread but could you clarify one more point? As far as I can see in the kernel sources, `current` must indeed work on 32-bit x86 systems as you described, `execute_on_irq_stack()` stores the correct pointer to `task_struct` in the thread info for the IRQ. But what about x86-64? It seems, there is no thread info in or near `irq_stack_union` there, and I haven't found the code yet that sets `task` field appropriately on this architecture. Perhaps, I am missing something? – Eugene Feb 03 '13 at 19:39
-
@Eugene, I'm not sure about the mechanism, but it's handled. When a process is interrupted, `current` points to the process. Indeed x86_64 makes things more complicated due to stack replacement, but it still works. – ugoren Feb 03 '13 at 21:50
0
I wouldn't expect current to be valid outside process context. If you're working on scheduler maybe you can get hold of where it stores pointer to running task, e.g. struct cfs_rq.

moorray
- 577
- 3
- 9