Setting Timer in ngx_core_module - c

I want a particular function to be called every 15seconds (i.e I need to setup a timer) in a core nginx module that i have written.
Here is the code that I have written by referring some examples online. The ngx_monitoring_init_process() is the 'init process' function in my 'ngx_monitoring_module'
The module compiles OK, but the timer doesn't work, any idea why ?

Related

wait_event_timeout equivalent in U-boot

I am looking for wait_event_timeout[1] equivalent function in u-boot but could not find the same.
is there such fuction do exist in u-boot ?
[1] https://github.com/torvalds/linux/blob/master/include/linux/wait.h#L371
U-Boot runs with a single process and a single thread. Interrupts are not enabled for many platforms. So a function like wait_event_timeout() cannot be implemented.
Instead you will have to create a loop that constantly checks if the event has occurred and in the loop execute whatever it takes to let the event occur.
For example look at the efi_wait_for_event() function which in a loop calls efi_timer_check(). efi_timer_check() runs all registered timer based functions like checking the network interface for received packages.

Can't get kernel message from module

I'm trying a driver for a custom hardware component, the source code can be found here:
https://github.com/godspeed1989/zedboard/blob/master/led_drv/driver/myled.c
the problem is that when i do:
insmod myled.ko
nothing is shown in the console or in the dmesg log. I'm reading Linux Device Driver 3 and on it is written that you always must do:
module_init(init_function);
module_exit(exit_function);
in the source code there are none of them, instead there is:
module_platform_driver(myled_driver);
But when i load a module with this function nothing is print, instead if I use module_init and module_exit messages appear, what are the difference between this two kind of istructions?
"but why the latter statement doesn't work while the first it's ok? "
The first methond will register the driver to system and bus by module_platform_driver macro.
The latter statement will not register your driver to system and bus. To to this, you need register driver in the init_function() routine by calling platform_device_register().

Capture hrtimer interrupts with linux kernel module .. possible?

I would like to be able to 'capture' an hrtimer interrupt with a linux kernel module and replay the interrupt at a later period in time. Any thoughts on how to go about doing this?
Use case: A program that calls sleep(1). My module will grab the hrtimer interrupt when it fires after 1 second, wait for 'x' amount of time, then re-fire the interrupt, waking the process.
Note: I do not want to hook the sleep system call.
Thanks!
Quite honestly, writing a Linux kernel module just to modify the behavior of sleep() for a single application sounds like overkill.
For most cases you should be able to use a preloadable shared object to intercept/override the sleep() function family with your own implementations. The application will call your implementation and your code may then call the real function with a modified parameter list.
This method is much simpler and less intrusive than anything involving kernel programming, although it will not work if your application is statically linked or if it uses direct system calls instead of library functions.

Hijacking the realtime clock in linux

I want to write a LKM (Linux Kernel Module) that hijacks the realtime clock (interrupt 8). So I want the interrupt to be set to my function and at some point send it back to the old function.
I have tried to use the request_irq function without any success, probably because the kernel function that is there is not willing to share the interrupt (which is a good decision I guess).
I also tried to edit the IDT (Interrupt Descriptor Table), according to some pages I found. Non of them worked, most didn't even compile since they where written for kernel 2.6, and I'm working with 3.10.
This is a simplified code that I have just to give you the idea of what I'm doing.
kpage =__get_free_page( GFP_KERNEL);
asm("sidt %0": : "m"(*idtr) : );
memcpy(kpage, idtr, 256*sizeof(kpage));
newidt = (unsigned long long *)(*(unsigned long*)(idtr+1));
newidt[8] = &my_function;
asm("lidt %0": "=m"(newidt):);
All my attempts ended in good times with a segmentation fault, and in bad times with the kernel crashing forcing me to reboot (luckily I work with a virtual machine and snapshots).
So how can I hijack the realtime interrupt so it does my stuff? (And then send it back to the original function to get executed.)
Here is some nice code to change the pagefault function on the IDT. I couldn't make it work, since it's also written for kernel 2.6. This question is also worth looking into.
To get the bounty please publish working code, or at least sufficient info to make it run.
This can help you : http://cormander.com/2011/12/how-to-hook-into-hijack-linux-kernel-functions-via-lkm/
Why not you simply hook a function that is call every x steps like you want and execute what ever you need ?

How to solve "BUG: scheduling while atomic: swapper /0x00000103/0, CPU#0"? in TSC2007 Driver?

I found tsc2007 driver and modified according to our needs. Our firm is producing its own TI DM365 board. In this board we used TSC2007 and connected PENIRQ pin to GPIO0 of DM365. It is being seen OK on driver. when i touch to touchscreen cursor is moving but at the same time i am getting
BUG: scheduling while atomic: swapper /0x00000103/0, CPU#0
warning and embedded Linux is being crashed. there are 2 files that i modified and uploaded to http://www.muhendislikhizmeti.com/touchscreen.zip one is with timer the other is not. it is giving this error in any case.
I found a solution on web that i need to use work queue and call with using schedule_work() API. but they are blur for me now. Is anybody have any idea how to solve this problem and can give me some advice where to start to use work queue.
"Scheduling while atomic" indicates that you've tried to sleep somewhere that you shouldn't - like within a spinlock-protected critical section or an interrupt handler.
Common examples of things that can sleep are mutex_lock(), kmalloc(..., GFP_KERNEL), get_user() and put_user().
Exactly as said in 1st answer, scheduling while atomic happens when the scheduler gets confused and therefore unable to work properly and this because the scheduler tried to perform a "schedule()" in a section that contains a schedulable code inside of a non schedulable one.
For example using sleeps inside of a section protected by a spinlock. Trying to use another lock(semaphores,mutexes..) inside of a spinlock-proteced code may also disturb the scheduler. In addition using spinlocks in user space can drive the scheduler to behave as such. Hope this helps
For anyone else with a similar error - I had this problem because I had a function, called from an atomic context, that used kzalloc(..., GFP_KERN) when it should have used GFP_NOWAIT or GFP_ATOMIC.
This is just one example of a function sleeping when you don't want to, which is something you have to be careful of in kernel programming.
Hope this is useful to somebody else!
Thanks for the former two answers, in my case it was enough to disable the preemption:
preempt_disable();
// Your code with locks and schedule()
preempt_enable();

Resources