Kernel Newbies archive on lore.kernel.org
 help / color / Atom feed
* Setting CPU affinity for current process on kernel module?
@ 2019-09-11 18:52 Martin Galvan
  2019-09-11 19:16 ` Bharath Vedartham
  0 siblings, 1 reply; 11+ messages in thread
From: Martin Galvan @ 2019-09-11 18:52 UTC (permalink / raw)
  To: kernelnewbies

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Setting CPU affinity for current process on kernel module?
  2019-09-11 18:52 Setting CPU affinity for current process on kernel module? Martin Galvan
@ 2019-09-11 19:16 ` Bharath Vedartham
  2019-09-11 19:19   ` Martin Galvan
  0 siblings, 1 reply; 11+ messages in thread
From: Bharath Vedartham @ 2019-09-11 19:16 UTC (permalink / raw)
  To: Martin Galvan; +Cc: kernelnewbies

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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Setting CPU affinity for current process on kernel module?
  2019-09-11 19:16 ` Bharath Vedartham
@ 2019-09-11 19:19   ` Martin Galvan
  2019-09-12  9:26     ` Bharath Vedartham
  0 siblings, 1 reply; 11+ messages in thread
From: Martin Galvan @ 2019-09-11 19:19 UTC (permalink / raw)
  To: Bharath Vedartham; +Cc: kernelnewbies

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.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Setting CPU affinity for current process on kernel module?
  2019-09-11 19:19   ` Martin Galvan
@ 2019-09-12  9:26     ` Bharath Vedartham
  2019-09-12 14:03       ` Martin Galvan
  0 siblings, 1 reply; 11+ messages in thread
From: Bharath Vedartham @ 2019-09-12  9:26 UTC (permalink / raw)
  To: Martin Galvan; +Cc: kernelnewbies

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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Setting CPU affinity for current process on kernel module?
  2019-09-12  9:26     ` Bharath Vedartham
@ 2019-09-12 14:03       ` Martin Galvan
  2019-09-13  4:50         ` Greg KH
  0 siblings, 1 reply; 11+ messages in thread
From: Martin Galvan @ 2019-09-12 14:03 UTC (permalink / raw)
  To: Bharath Vedartham; +Cc: kernelnewbies

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.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Setting CPU affinity for current process on kernel module?
  2019-09-12 14:03       ` Martin Galvan
@ 2019-09-13  4:50         ` Greg KH
  2019-09-13 12:43           ` Martin Galvan
  0 siblings, 1 reply; 11+ messages in thread
From: Greg KH @ 2019-09-13  4:50 UTC (permalink / raw)
  To: Martin Galvan; +Cc: Bharath Vedartham, kernelnewbies

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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Setting CPU affinity for current process on kernel module?
  2019-09-13  4:50         ` Greg KH
@ 2019-09-13 12:43           ` Martin Galvan
  2019-09-13 12:55             ` Greg KH
  0 siblings, 1 reply; 11+ messages in thread
From: Martin Galvan @ 2019-09-13 12:43 UTC (permalink / raw)
  To: Greg KH; +Cc: Bharath Vedartham, kernelnewbies

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.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Setting CPU affinity for current process on kernel module?
  2019-09-13 12:43           ` Martin Galvan
@ 2019-09-13 12:55             ` Greg KH
  2019-09-13 12:56               ` Martin Galvan
  0 siblings, 1 reply; 11+ messages in thread
From: Greg KH @ 2019-09-13 12:55 UTC (permalink / raw)
  To: Martin Galvan; +Cc: Bharath Vedartham, kernelnewbies

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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Setting CPU affinity for current process on kernel module?
  2019-09-13 12:55             ` Greg KH
@ 2019-09-13 12:56               ` Martin Galvan
  2019-09-13 14:13                 ` Greg KH
  0 siblings, 1 reply; 11+ messages in thread
From: Martin Galvan @ 2019-09-13 12:56 UTC (permalink / raw)
  To: Greg KH; +Cc: Bharath Vedartham, kernelnewbies

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.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Setting CPU affinity for current process on kernel module?
  2019-09-13 12:56               ` Martin Galvan
@ 2019-09-13 14:13                 ` Greg KH
  2019-09-19  5:43                   ` R. Engür Pişirici
  0 siblings, 1 reply; 11+ messages in thread
From: Greg KH @ 2019-09-13 14:13 UTC (permalink / raw)
  To: Martin Galvan; +Cc: Bharath Vedartham, kernelnewbies

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Setting CPU affinity for current process on kernel module?
  2019-09-13 14:13                 ` Greg KH
@ 2019-09-19  5:43                   ` R. Engür Pişirici
  0 siblings, 0 replies; 11+ messages in thread
From: R. Engür Pişirici @ 2019-09-19  5:43 UTC (permalink / raw)
  To: Martin Galvan; +Cc: kernelnewbies

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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, back to index

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-11 18:52 Setting CPU affinity for current process on kernel module? Martin Galvan
2019-09-11 19:16 ` Bharath Vedartham
2019-09-11 19:19   ` Martin Galvan
2019-09-12  9:26     ` Bharath Vedartham
2019-09-12 14:03       ` Martin Galvan
2019-09-13  4:50         ` Greg KH
2019-09-13 12:43           ` Martin Galvan
2019-09-13 12:55             ` Greg KH
2019-09-13 12:56               ` Martin Galvan
2019-09-13 14:13                 ` Greg KH
2019-09-19  5:43                   ` R. Engür Pişirici

Kernel Newbies archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kernelnewbies/0 kernelnewbies/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kernelnewbies kernelnewbies/ https://lore.kernel.org/kernelnewbies \
		kernelnewbies@kernelnewbies.org kernelnewbies@archiver.kernel.org
	public-inbox-index kernelnewbies

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernelnewbies.kernelnewbies


AGPL code for this site: git clone https://public-inbox.org/ public-inbox