What happens if a running tasklet is interrupted by a hardware interrupt - linux-kernel

What happens if a running tasklet is interrupted by a hardware interrupt. I mean if there is a tasklet in the middle of execution and a hardware interrupt happens. Does the tasklet complete its execution before the interrupt code is run, or the tasklet is executed after the interrupt.

Ordinarily a hardware interrupt will be executed immediately. On return, the tasklet will resume execution.
It is possible and even common that a tasklet will disable interrupts during short critical sections while it manipulates shared data structures.

Taskelts can be interrupted by hardware interrupts. See, e.g this.

Related

Linux Kernel mutex_lock_interruptible

The mutex_lock_interruptible() function in the linux kernel basically tries to lock a mutex and will continue waiting until a task is interrupted. Well how do I actually interrupt a task?
Suffix _interruptible in Linux kernel means that waiting by the function will be interrupted if thread(process) receives the signal.
It can be signal sent by kill() user-space function, or signals generated by specific functions when condition met, e.g. by the timer (create_timer() when time is expired, or by asinchronous IO when pending operation has been completed.
Note, that uninterruptible wait cannot be interrupted even by SIGKILL, that is process cannot be finished until such wait ends.

Entry and exit of kernel schedule() function

I know that the kernel scheduler is run periodically. This period is determined by a timer.
However, I have been unable to find where the IRQ for the timer interrupt is and the entire flow from beginning to end of the scheduler code.
I understand that the schedule() function may potentially have several entry and exit points..
but could someone point me towards where to look for these?
From the kernel source, I've gathered that __schedule() is the main schedule function that seems to be called from schedule()..
but what calls schedule()..and what calls the function that calls schedule.. ..
There are actually two schedulers, or rather two scheduling codes in the Linux kernel. There is a core scheduler, which you yourself mentioned called schedule() which calls __schedule(). schedule() is called from many points in the kernel:
Explicit blocking, like in case of semaphores, mutexes etc.
A flag TIF_NEED_RESCHED is checked on interrupts and on return to userspace, if set then schedule is called.
A process wakes up.
There is another scheduler code with the name scheduler_tick()[this too resides in core.c], which is a periodic scheduler and is called by the timer code(timer.c) via interrupt with a frequency of HZ, i.e. scheduler_tick() is called HZ times in one second. HZ is hardware dependent and its value varies between 100-1024. scheduler_tick() calls the task_tick() of the scheduling class to which the current task on the processor belongs.

linux scheduler

In my kernel configuration CONFIG_PREEMPT is not set. Since schedule() is not allowed in interrupt handler how does round robin type of scheduling is implemented in linux kernel. i.e. Who calls the scheduler so often. In entry_32.S it calls preempt_schedule_irq only if CONFIG_PREEMPT is set.
What happens is the timer on the CPU is set to interrupt the kernel every so often. But we can't just call schedule from interrupt context right? So what the kernel does is a neat trick. It changes the currently executing task while executing the handler and then returns. What this effectively does is switch out the context from underneath the handler so the handler completes but at the same time the next context to run is now the next task that will execute. Read up on do_context_switch (IIRC I think that's what it's called) and you will see that it switches it's stack and context from underneath the current execution and resumes the same function in another context.
And CONFIG_PREEMPT only applies to kernel code preemption in kernel context. Userspace tasks will always preempt. All this means is that any kernel code that starts to execute runs to completion (unless you call schedule() yourself or block waiting for I/O, etc....). Normally the kernel can preempt as long as it does not hold any locks except in certain cases where acquiring a lock can put the thread to sleep.

Can anyone please let me know the usage of schedule() function in linux kernel

Can anyone please let me know the usage of schedule() function in linux kernel.
Who will schedule this scheduler thread.?
Thanks in advance
Two mechanism are available: voluntary or hardware-based.
http://lwn.net/Articles/95334/
Arising from a recent patch, voluntary preemption has been introduced into the kernel:
http://kerneltrap.org/node/3440
This means the CPU will explicitly surrender the current job and let the scheduler take over to select the next tasks on the active tasks list. It has been found that this way of voluntary preemption improved performance over involuntary preemption (which is timer clock-based)
More info:
http://wiki.osdev.org/Context_Switching (software vs hardware context switching - similar to what we are talking here)
http://wiki.osdev.org/Scheduling_Algorithms
There is no scheduler thread in the Linux kernel. There are specific situations in which the schedule() function is called. For example:
1) When a process or kernel thread explicitly calls it in kernel mode. A process generally calls schedule() function if it needs to wait for some event to occur; like availability of data from an input-output device.
2) When a process of priority higher than the current process was waiting for some event and the event occurs.
3) When the time slice allocated to the current process expires.

CriticalSection

i'm not sure about something.
when i use critical_section/mutex/semaphor in c++ for example , how does the busy_wait problem being prevented ?
what i mean is when a thread reaches a critical section and the critical section is occupied by other thread, what prevents the thread from wasting cycle time and wait for nothing ?
for example,
should i call TryEnterCriticalSection and check if the thread obtained ownership and otherwise call sleep(0) ?
i'm a bit perplexed
thanks
This is Windows specific, but Linux will be similar.
Windows has the concept of a ready queue of threads. These are threads that are ready to run, and will be run at some point on an available processor. Which threads are selected to run immediately is a bit complicated - threads can have different priorities, their priorities can be temporarily boosted, etc.
When a thread waits on a synchronization primitive like a CRITICAL_SECTION or mutex, it is not placed on the ready queue - Windows will not even attempt to run the thread and will run other threads if possible. At some point the thread will be moved back to the ready queue, for instance when the thread owning the CS or mutex releases it.
The thread is not going to be taking any system resources, because it will be marked as "waiting". As soon as the thread occupying the critical region finishes, it will send out a signal that will move the waiting thread to the ready queue.
These control structures stop the thread that can't enter from doing a busy wait by allowing it to sleep until an interrupt is generated by the thread that is in the critical section finishing execution. Because the thread is asleep it is not using processor cycles, so no busy_wait.

Resources