Setting processor affinity within a Linux kernel module - linux-kernel

I was wondering if there was way of setting (and getting) processor affinity in a kernel module without having to change the kernel code.
Edit:
I do know about "sched.h" but surely that is user space code and hence cannot be included in the kernel module code. So I want to know how to achieve the same functionality in the kernel.

sched_setaffinity can do the trick for you, without having to change the kernel code

Related

Best way to convert a Linux kernel trace point into a plain old printk

I work in a system that does not allow me to enable tracepoints at runtime. To work around this, I manually add printks near a trace point to during debugging. This seems to be very inefficient and I am looking for methods to enable a tracepoint at compile time and have it pushed to the kernel log ring buffer.
You can use the kernel kprobes to dynamically register probe functions into the kernel. probe functions are just kernel modules that are dynamically inserted into your kernel and from there you can print to ring buffer. kprobes are automatically enabled on nearly all major linux distro's by default. see the documentation to know how to register and unregister kprobes.

force linux kernel to run code without preempting

I'm writing a Linux kernel(android Linux kernel) module. I have a general question . is it possible to force kernel run a part of code in non-preemptive mode?
Yes, you can wrap the code in preempt_disable(); / preempt_enable();. Note that this does not block interrupts.

Change vm page protection attributes from XNU kernel driver

As a part of an experiment i need to make a read-only page writable in kernel address space from a non-IOKit kext. In user mode i can do vm_protect, but there is surprisingly little info on how this can be done in kernel mode (or i am completely blind). Is there a call to do this?
In the kernel, it's declared in <mach/vm_map.h>, and is part of the "unsupported" KPI. (Add com.apple.kpi.unsupported to OSBundleRequired of your kext's info.plist) I unfortunately haven't used this function from the kernel before, so I can't really comment beyond that. vm_map_t appears to be a Mach Port reference, but I'm not sure where you'd get it from.

When are the contents of .exit.text section in an ARM vmlinux file discarded once it is loaded in memory?

I understand that the .exit.text section will be filled by the linker with functions defined with __exit macro. I can see the section on dissassembly of the vmlinux file. But I did not understand at what point are the contents discarded once the file is loaded in memory? Does it happen in the kernel code? Can anyone explain this more?
Thanks in advance!
The files vmlinux.lds.S and module.c handle this. The handling depends on your kernel version and configuration; neither have been given. My assumption is you mean the .exit.text section in the kernel and not some user space task. Generally, the kernel doesn't exit, so they are discarded by the linker script unless you have some debugging configurations.
Edit: The android kernel was made with a .config file which enables Linux kernel features at compile time. The .config for CONFIG_HOTPLUG_CPU is a CPU; some big servers can keep running even when the CPU is exchanged. It is hard to answer your question. I don't know where your Android kernel source is or what .config options it was built with.
The sections are kept with GENERIC_BUG; so it is possible your kernel was built with this. From the stock vmlinux.lds.S, the .exit.data section is put before __init_end, so it is freed and returned to memory after the init portion runs. Ie, present only during boot. Under these circumstances, you will see it in the vmlinux ELF, but not in the runtime kcore or however you are dumping it.
Specifically, main.c's start_kernel(), rest_init(), kernel_init(), and free_initmem() are the place where this stuff is discarded.

ARM Gdb break on processor mode change

Im currently debugging Linux ARM kernel, and have always wondered if it is possible in gdb to break when the cpu mode change (usr, svc, abt etc). Currently, when i'm not sure which mode we are in, I usually have to look at the psr register multiple times, but maybe there is a more effective way, such as break on mode change?
I know I can put breakpoints on the exception vector, but that means I only detect mode changes to privileged mode and not the other way around. Maybe there is a command to check if the psr changes to 0x10(usr mode) ?
Thanks
All processes are scheduled in entry-common.S. This file has a macro named arch_ret_to_user. Define it to be a BKPT instruction, perhaps conditional on a global set via /proc.
You can not detect a switch to user mode generically using the CPU alone (you need support code), as the supervisor code can change anything. ETM may have some means if your CPU has the functionality.
There are also thread_nofity.h which has call-backs for when a user task is re-scheduled. You can hook this with your own logic if you don't need the debugger or put some breakpoint on a null(){} function which you only call from the notifier when the condition is meant.

Resources