Discussion:
Ordering / preemption of work in a workqueue preempt?
Rajat Jain
2013-11-15 15:16:24 UTC
Permalink
Hi,

I have a single work queue, on which I have scheduled a worker function [using queue_work(wq, fn)] in interrupt context.

I get the interrupt twice before the work queue gets a chance to run, and hence the same function will get queued twice (with different private context - arguments etc) which is fine and expected.

Questions:

1) Is it possible that the instance that was queued by 2nd interrupt, can get to run BEFORE the instance that was queued by 1st interrupt? In other words, is reordering possible?

2) Is it possible that one running instance of the function, can get preempted by second instance of the same work queue? I read through http://lwn.net/Articles/511421/ and it talks about same work queue cannot run on different CPU, but I have doubt about single CPU. If If I am writing a worker function, does my code have to be ready that it can be preempted by another instance of the same function?

Please note that I understand that my worker function can preempted by other processes, my doubts are related to the same workqueue.

Thanks,

Rajat

--
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Rajat Sharma
2013-11-16 01:01:49 UTC
Permalink
Hi Rajat,
Post by Rajat Jain
Hi,
I have a single work queue, on which I have scheduled a worker function
[using queue_work(wq, fn)] in interrupt context.
I get the interrupt twice before the work queue gets a chance to run, and
hence the same function will get queued twice (with different private
context - arguments etc) which is fine and expected.
1) Is it possible that the instance that was queued by 2nd interrupt, can
get to run BEFORE the instance that was queued by 1st interrupt? In other
words, is reordering possible?
It is unlikely as workqueue would have internal queing of tasks.
Post by Rajat Jain
2) Is it possible that one running instance of the function, can get
preempted by second instance of the same work queue? I read through
http://lwn.net/Articles/511421/ and it talks about same work queue cannot
run on different CPU, but I have doubt about single CPU. If If I am writing
a worker function, does my code have to be ready that it can be preempted
by another instance of the same function?
Do you mean the system has just one CPU, then there would be just one
worker thread in the workqueue which will pick up requests one by one. Do
you see multiple threads?
Post by Rajat Jain
Please note that I understand that my worker function can preempted by
other processes, my doubts are related to the same workqueue.
Thanks,
Rajat
Rajat
Post by Rajat Jain
_______________________________________________
Kernelnewbies mailing list
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
anish singh
2013-11-16 05:11:43 UTC
Permalink
Post by Rajat Sharma
Hi Rajat,
Post by Rajat Jain
Hi,
I have a single work queue, on which I have scheduled a worker function
[using queue_work(wq, fn)] in interrupt context.
I get the interrupt twice before the work queue gets a chance to run, and
hence the same function will get queued twice (with different private
context - arguments etc) which is fine and expected.
1) Is it possible that the instance that was queued by 2nd interrupt, can
get to run BEFORE the instance that was queued by 1st interrupt? In other
words, is reordering possible?
It is unlikely as workqueue would have internal queing of tasks.
And where is the code to back that up?
Post by Rajat Sharma
Post by Rajat Jain
2) Is it possible that one running instance of the function, can get
preempted by second instance of the same work queue? I read through
http://lwn.net/Articles/511421/ and it talks about same work queue cannot
run on different CPU, but I have doubt about single CPU. If If I am writing
a worker function, does my code have to be ready that it can be preempted by
another instance of the same function?
What do you mean by 'one running instance of the function', did you mean
workqueue function?AFAIK same instance of the work queue can't run on different
CPU.You should write code such that it can be preempted by another
instanceof the
same workqueue but I think if you don't want that then you should call workqueue
API with proper input parameters.
Have a look at Documentation/workqueue.txt
Post by Rajat Sharma
Do you mean the system has just one CPU, then there would be just one worker
thread in the workqueue which will pick up requests one by one. Do you see
multiple threads?
Post by Rajat Jain
Please note that I understand that my worker function can preempted by
other processes, my doubts are related to the same workqueue.
Thanks,
Rajat
Rajat
Post by Rajat Jain
_______________________________________________
Kernelnewbies mailing list
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
_______________________________________________
Kernelnewbies mailing list
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
--
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
anish singh
2013-11-17 04:10:49 UTC
Permalink
Post by Rajat Sharma
Hi Rajat,
Post by Rajat Jain
Hi,
I have a single work queue, on which I have scheduled a worker function
[using queue_work(wq, fn)] in interrupt context.
I get the interrupt twice before the work queue gets a chance to run, and
hence the same function will get queued twice (with different private
context - arguments etc) which is fine and expected.
1) Is it possible that the instance that was queued by 2nd interrupt, can
get to run BEFORE the instance that was queued by 1st interrupt? In other
words, is reordering possible?
use alloc_ordered_workqueue: only one workqueue will be active at a time
and it will execute in the queued sequence.
Post by Rajat Sharma
It is unlikely as workqueue would have internal queing of tasks.
Post by Rajat Jain
2) Is it possible that one running instance of the function, can get
preempted by second instance of the same work queue? I read through
http://lwn.net/Articles/511421/ and it talks about same work queue cannot
run on different CPU, but I have doubt about single CPU. If If I am writing
a worker function, does my code have to be ready that it can be preempted by
another instance of the same function?
Do you mean the system has just one CPU, then there would be just one worker
thread in the workqueue which will pick up requests one by one. Do you see
multiple threads?
Post by Rajat Jain
Please note that I understand that my worker function can preempted by
other processes, my doubts are related to the same workqueue.
Thanks,
Rajat
Rajat
Post by Rajat Jain
_______________________________________________
Kernelnewbies mailing list
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
_______________________________________________
Kernelnewbies mailing list
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
--
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Arun KS
2013-11-19 06:20:19 UTC
Permalink
Hi Rajat,
Post by Rajat Jain
Hi,
I have a single work queue, on which I have scheduled a worker function [using queue_work(wq, fn)] in interrupt context.
I get the interrupt twice before the work queue gets a chance to run, and hence the same function will get queued twice (with different private context - arguments etc) which is fine and expected.
You got it wrong here.

bool queue_work_on(int cpu, struct workqueue_struct *wq,
struct work_struct *work)
{
bool ret = false;
unsigned long flags;

local_irq_save(flags);

if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
__queue_work(cpu, wq, work);
ret = true;
}

local_irq_restore(flags);
return ret;
}
EXPORT_SYMBOL(queue_work_on);

If you look at function queue_work_on, the function returns
immediately if WORK_STRUCT_PENDING_BIT was already set.
So in effect your second call to queue_work will just return.

HTH

Thanks,
Arun
Post by Rajat Jain
1) Is it possible that the instance that was queued by 2nd interrupt, can get to run BEFORE the instance that was queued by 1st interrupt? In other words, is reordering possible?
2) Is it possible that one running instance of the function, can get preempted by second instance of the same work queue? I read through http://lwn.net/Articles/511421/ and it talks about same work queue cannot run on different CPU, but I have doubt about single CPU. If If I am writing a worker function, does my code have to be ready that it can be preempted by another instance of the same function?
Please note that I understand that my worker function can preempted by other processes, my doubts are related to the same workqueue.
Thanks,
Rajat
_______________________________________________
Kernelnewbies mailing list
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
--
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
anish singh
2013-11-19 07:39:05 UTC
Permalink
Post by Rajat Sharma
Hi Rajat,
Post by Rajat Jain
Hi,
I have a single work queue, on which I have scheduled a worker function [using queue_work(wq, fn)] in interrupt context.
I get the interrupt twice before the work queue gets a chance to run, and hence the same function will get queued twice (with different private context - arguments etc) which is fine and expected.
You got it wrong here.
bool queue_work_on(int cpu, struct workqueue_struct *wq,
struct work_struct *work)
{
bool ret = false;
unsigned long flags;
local_irq_save(flags);
if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
__queue_work(cpu, wq, work);
ret = true;
}
local_irq_restore(flags);
return ret;
}
EXPORT_SYMBOL(queue_work_on);
If you look at function queue_work_on, the function returns
immediately if WORK_STRUCT_PENDING_BIT was already set.
So in effect your second call to queue_work will just return.
Best thing to do is just disable interrupt as soon as you receive
first interrupt
and re-enable it when you think you can handle it.I hope you are not writing
network driver where this sequence would cause unacceptable delays.
Post by Rajat Sharma
HTH
Thanks,
Arun
Post by Rajat Jain
1) Is it possible that the instance that was queued by 2nd interrupt, can get to run BEFORE the instance that was queued by 1st interrupt? In other words, is reordering possible?
2) Is it possible that one running instance of the function, can get preempted by second instance of the same work queue? I read through http://lwn.net/Articles/511421/ and it talks about same work queue cannot run on different CPU, but I have doubt about single CPU. If If I am writing a worker function, does my code have to be ready that it can be preempted by another instance of the same function?
Please note that I understand that my worker function can preempted by other processes, my doubts are related to the same workqueue.
Thanks,
Rajat
_______________________________________________
Kernelnewbies mailing list
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
_______________________________________________
Kernelnewbies mailing list
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
--
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Loading...