All of lore.kernel.org
 help / color / mirror / Atom feed
* Calling queue_work() multiple times with the same work_struct
@ 2021-05-20 12:39 Jason Andryuk
  2021-05-20 17:01 ` Tejun Heo
  0 siblings, 1 reply; 3+ messages in thread
From: Jason Andryuk @ 2021-05-20 12:39 UTC (permalink / raw)
  To: Tejun Heo, Lai Jiangshan, open list

Hi,

I'm looking for clarification of the behavior of queue_work().  From
the header description, I'm not sure if I need additional complexity
for re-queuing work.

I want to ensure the following cases all occur when calling queue_work().
1) work_struct not queued -> work_struct queued
2) work_struct queued -> work_struct queued (no change)
3) work_struct running -> work_struct queued (so it will run again)

1 & 2 look supported from workqueue.h.  Is the 3rd case true and
guaranteed?  Is it okay to re-use the same work_struct in that case
while it's being executed?  A work_struct function can re-queue
itself, so I hope #3 is supported.

The virtual device I'm working on has a single interrupt shared for
multiple rings.  So whenever an interrupt occurs, I need to re-queue
the single work_struct on the workqueue to ensure we don't miss an
event.  That is, we need to re-iterate all the rings if the interrupt
comes halfway through processing the list of rings.  If multiple
interrupts come through while running, we only need to re-queue a
single work item for all of them.

Below is what I hope works for the code to give something tangible.

Thank you,
Jason

DECLARE_WORK(argo_work, argo_work_fn);
static struct workqueue_struct *argo_wq = alloc_workqueue("argo_wq",
                    WQ_UNBOUND | WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_SYSFS, 1);

static void argo_work_fn(struct work_struct *work)
{
    argo_interrupt_rx();  /* iterates multiple "rings" */
    argo_notify();
}

static irqreturn_t argo_interrupt(int irq, void *dev_id)
{
    queue_work(argo_wq, &argo_work);

    return IRQ_HANDLED;
}

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

* Re: Calling queue_work() multiple times with the same work_struct
  2021-05-20 12:39 Calling queue_work() multiple times with the same work_struct Jason Andryuk
@ 2021-05-20 17:01 ` Tejun Heo
  2021-05-20 17:31   ` Jason Andryuk
  0 siblings, 1 reply; 3+ messages in thread
From: Tejun Heo @ 2021-05-20 17:01 UTC (permalink / raw)
  To: Jason Andryuk; +Cc: Lai Jiangshan, open list

Hello, Jason.

On Thu, May 20, 2021 at 08:39:56AM -0400, Jason Andryuk wrote:
> 1 & 2 look supported from workqueue.h.  Is the 3rd case true and
> guaranteed?  Is it okay to re-use the same work_struct in that case

Yes.

> while it's being executed?  A work_struct function can re-queue

Yes.

> itself, so I hope #3 is supported.

Yes.

> DECLARE_WORK(argo_work, argo_work_fn);
> static struct workqueue_struct *argo_wq = alloc_workqueue("argo_wq",
>                     WQ_UNBOUND | WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_SYSFS, 1);

I don't know what the device is but does it need both HIGHPRI and
CPU_INTENSIVE?

> static void argo_work_fn(struct work_struct *work)
> {
>     argo_interrupt_rx();  /* iterates multiple "rings" */
>     argo_notify();
> }
> 
> static irqreturn_t argo_interrupt(int irq, void *dev_id)
> {
>     queue_work(argo_wq, &argo_work);
> 
>     return IRQ_HANDLED;
> }

Yeah, the above will guarantee that the work function would run at least
once since the last invocation of argo_interrupt().

Thanks.

-- 
tejun

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

* Re: Calling queue_work() multiple times with the same work_struct
  2021-05-20 17:01 ` Tejun Heo
@ 2021-05-20 17:31   ` Jason Andryuk
  0 siblings, 0 replies; 3+ messages in thread
From: Jason Andryuk @ 2021-05-20 17:31 UTC (permalink / raw)
  To: Tejun Heo; +Cc: Lai Jiangshan, open list

Hello, Tejun.

On Thu, May 20, 2021 at 1:01 PM Tejun Heo <tj@kernel.org> wrote:
>
> Hello, Jason.
>
> On Thu, May 20, 2021 at 08:39:56AM -0400, Jason Andryuk wrote:
> > 1 & 2 look supported from workqueue.h.  Is the 3rd case true and
> > guaranteed?  Is it okay to re-use the same work_struct in that case
>
> Yes.
>
> > while it's being executed?  A work_struct function can re-queue
>
> Yes.
>
> > itself, so I hope #3 is supported.
>
> Yes.
>
> > DECLARE_WORK(argo_work, argo_work_fn);
> > static struct workqueue_struct *argo_wq = alloc_workqueue("argo_wq",
> >                     WQ_UNBOUND | WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_SYSFS, 1);
>
> I don't know what the device is but does it need both HIGHPRI and
> CPU_INTENSIVE?

argo is a Xen inter-vm communication mechanism, so networking-ish.  I
wanted HIGHPRI to ensure it is run promptly.  I wasn't sure about
CPU_INTENSIVE, but high data rates could potentially copy a fair
amount of data.  HIGHPRI alone may be sufficient.

> > static void argo_work_fn(struct work_struct *work)
> > {
> >     argo_interrupt_rx();  /* iterates multiple "rings" */
> >     argo_notify();
> > }
> >
> > static irqreturn_t argo_interrupt(int irq, void *dev_id)
> > {
> >     queue_work(argo_wq, &argo_work);
> >
> >     return IRQ_HANDLED;
> > }
>
> Yeah, the above will guarantee that the work function would run at least
> once since the last invocation of argo_interrupt().

Awesome.  Thank you, Tejun.

Regards,
Jason

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

end of thread, other threads:[~2021-05-20 17:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-20 12:39 Calling queue_work() multiple times with the same work_struct Jason Andryuk
2021-05-20 17:01 ` Tejun Heo
2021-05-20 17:31   ` Jason Andryuk

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.