Setting CPU affinity for current process on kernel module?
Hi, I have a kernel module which needs to perform CPU core-specific operations. I know that there's a kernel mode version of sched_setaffinity, however it's not exported to modules and can only be accessed through e.g. kallsyms_lookup_name, which is ugly. The alternative is to spawn a new kthread and bind it to the desired CPU before starting it, but I was wondering if there's a more straightforward method of doing this. Thanks!
On Wed, Sep 11, 2019 at 03:52:06PM -0300, Martin Galvan wrote:
Hi,
I have a kernel module which needs to perform CPU core-specific operations. I know that there's a kernel mode version of sched_setaffinity, however it's not exported to modules and can only be accessed through e.g. kallsyms_lookup_name, which is ugly. The alternative is to spawn a new kthread and bind it to the desired CPU before starting it, but I was wondering if there's a more straightforward method of doing this. Thanks!
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies So one thing that pops to my head is using cgroups. Create a cgroup, modify the cpu file in cpusets cgroup and add the required task to it. But I am not sure whether you can do all of this from a kernel module...
Thanks Bharath
El mié., 11 sept. 2019 a las 16:17, Bharath Vedartham (<linux.bhar@gmail.com>) escribió:
So one thing that pops to my head is using cgroups. Create a cgroup, modify the cpu file in cpusets cgroup and add the required task to it. But I am not sure whether you can do all of this from a kernel module...
Thanks, but that's not feasible. This should all be done programmatically from the module, and be as minimally invasive as possible.
On Wed, Sep 11, 2019 at 04:19:41PM -0300, Martin Galvan wrote:
El mié., 11 sept. 2019 a las 16:17, Bharath Vedartham (<linux.bhar@gmail.com>) escribió:
So one thing that pops to my head is using cgroups. Create a cgroup, modify the cpu file in cpusets cgroup and add the required task to it. But I am not sure whether you can do all of this from a kernel module...
Thanks, but that's not feasible. This should all be done programmatically from the module, and be as minimally invasive as possible. Just to be clear, What do you mean by current process. Is it the process which is executing the kernel module? That would be a kernel thread I believe or do you want to be able to set the cpu affinity for any process given its PID? And are you looking at setting CPU affinity of kernel threads or user threads.
Thank you Bharath
El jue., 12 sept. 2019 a las 6:26, Bharath Vedartham (<linux.bhar@gmail.com>) escribió:
Just to be clear, What do you mean by current process. Is it the process which is executing the kernel module? That would be a kernel thread I believe or do you want to be able to set the cpu affinity for any process given its PID? And are you looking at setting CPU affinity of kernel threads or user threads.
Sorry, I wasn't clear: I want to set the affinity of 'current', which is a kernel thread. El jue., 12 sept. 2019 a las 6:38, Piotr Figiel (<figiel@gmail.com>) escribió:
set_cpus_allowed_ptr() is exported, maybe this is what you're looking for?
Yes, that's exactly what I needed. Thanks! I actually ended up using work_on_cpu, which fits my use case better.
On Thu, Sep 12, 2019 at 11:03:20AM -0300, Martin Galvan wrote:
El jue., 12 sept. 2019 a las 6:26, Bharath Vedartham (<linux.bhar@gmail.com>) escribió:
Just to be clear, What do you mean by current process. Is it the process which is executing the kernel module? That would be a kernel thread I believe or do you want to be able to set the cpu affinity for any process given its PID? And are you looking at setting CPU affinity of kernel threads or user threads.
Sorry, I wasn't clear: I want to set the affinity of 'current', which is a kernel thread.
I really hate to ask, but _why_ do you feel that this is the correct solution to your problem? What is the problem you are attempting to solve by doing this? thanks, greg k-h
On Fri, Sep 13, 2019 at 09:43:33AM -0300, Martin Galvan wrote:
El vie., 13 sept. 2019 a las 1:50, Greg KH (<greg@kroah.com>) escribió:
I really hate to ask, but _why_ do you feel that this is the correct solution to your problem? What is the problem you are attempting to solve by doing this?
I'm reading and comparing per-core MSR values.
And doing what with that information? We have a msr driver for userspace access to those values, does that not work correctly for you? greg k-h
El vie., 13 sept. 2019 a las 9:55, Greg KH (<greg@kroah.com>) escribió:
And doing what with that information? We have a msr driver for userspace access to those values, does that not work correctly for you?
My use case requires this to be done in kernel space. In any case, thanks for the help, this seems to be working just fine.
On Fri, Sep 13, 2019 at 09:56:24AM -0300, Martin Galvan wrote:
El vie., 13 sept. 2019 a las 9:55, Greg KH (<greg@kroah.com>) escribió:
And doing what with that information? We have a msr driver for userspace access to those values, does that not work correctly for you?
My use case requires this to be done in kernel space.
So, again, I hate to ask but _why_ do you need access to the msr from within the kernel? What useful information can you do with that? Do you have a pointer to your code anywhere? thanks, greg k-h
I'm sure that some of us(readers of the kernelNewbies e-mail list) really wants to understand the same thing Greg was asking. Best Regards, -engur On 2019-09-13 17:13, Greg KH wrote:
On Fri, Sep 13, 2019 at 09:56:24AM -0300, Martin Galvan wrote:
El vie., 13 sept. 2019 a las 9:55, Greg KH (<greg@kroah.com>) escribió:
And doing what with that information? We have a msr driver for userspace access to those values, does that not work correctly for you?
My use case requires this to be done in kernel space.
So, again, I hate to ask but _why_ do you need access to the msr from within the kernel? What useful information can you do with that? Do you have a pointer to your code anywhere?
thanks,
greg k-h
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
participants (4)
-
Bharath Vedartham -
Greg KH -
Martin Galvan -
R. Engür Pişirici