gregoiregentil
gregoiregentil

Reputation: 1899

Run a kernel thread on an isolated cpu core

I boot my kernel

with isolcpus=3-7

and I want to run a thread on one of those isolated CPU cores.

Based on this idea, I do:

ctx->workq = create_singlethread_workqueue("my_work");
struct workqueue_attrs *attr = alloc_workqueue_attrs(GFP_KERNEL);
alloc_cpumask_var(&attr->cpumask, GFP_KERNEL);
cpumask_clear(attr->cpumask);
cpumask_set_cpu(5, attr->cpumask);
apply_workqueue_attrs(ctx->workq, attr);
INIT_WORK(&ctx->work, my_work);

But it's not working. The following code reports 0:

static void my_work(struct work_struct *w) {
    printk("CPU is: %d\n", get_cpu());
    put_cpu();

How can I run this workqueue thread on a specific core (if possible an isolated one)?

Upvotes: 2

Views: 1807

Answers (1)

vinod maverick
vinod maverick

Reputation: 678

There is already one API schedule_work_on in the mainline kernel which you can use to run your workqueue thread on a specific core.

Few years ago I have used same API for the same purpose. Have a look in the sample code.

static void
 myworkmod_work_handler(struct work_struct *w)
{
    printk(KERN_ERR "CPU is: %d\n", get_cpu());
    pr_info("work %u jiffies\n", (unsigned)onesec);
    put_cpu();
 }


 static int myworkmod_init(void)
 {
    onesec = msecs_to_jiffies(1000);
    pr_info("module loaded: %u jiffies\n", (unsigned)onesec);

    if (!wq)
            wq = create_singlethread_workqueue("myworkmod");
    if (wq)
            queue_delayed_work_on(2,wq, &myworkmod_work, onesec); //2 CPU no

    return 0;
  }

In your case I think you are using the schedule_work API which always hold the default CPU number. That is why you are getting the CPU 0. So you have to try the below one:

schedule_work_on(cpu_nr, &ctx->work);  //cpu_nr will the CPU no to be used.

Upvotes: 3

Related Questions