Re: [RFC 2/5] serial: core: Add framework to allow NMI aware serial drivers

From: Daniel Thompson
Date: Mon Aug 17 2020 - 10:28:54 EST


On Mon, Aug 17, 2020 at 05:57:03PM +0530, Sumit Garg wrote:
> On Fri, 14 Aug 2020 at 19:43, Daniel Thompson
> <daniel.thompson@xxxxxxxxxx> wrote:
> > On Fri, Aug 14, 2020 at 04:47:11PM +0530, Sumit Garg wrote:
> > Does it look better if you create a new type to map the two structures
> > together. Alternatively are there enough existing use-cases to want to
> > extend irq_work_queue() with irq_work_schedule() or something similar?
> >
>
> Thanks for your suggestion, irq_work_schedule() looked even better
> without any overhead, see below:
>
> diff --git a/include/linux/irq_work.h b/include/linux/irq_work.h
> index 3082378..1eade89 100644
> --- a/include/linux/irq_work.h
> +++ b/include/linux/irq_work.h
> @@ -3,6 +3,7 @@
> #define _LINUX_IRQ_WORK_H
>
> #include <linux/smp_types.h>
> +#include <linux/workqueue.h>
>
> /*
> * An entry can be in one of four states:
> @@ -24,6 +25,11 @@ struct irq_work {
> void (*func)(struct irq_work *);
> };
>
> +struct irq_work_schedule {
> + struct irq_work work;
> + struct work_struct *sched_work;
> +};
> +
> static inline
> void init_irq_work(struct irq_work *work, void (*func)(struct irq_work *))
> {
> {
> @@ -39,6 +45,7 @@ void init_irq_work(struct irq_work *work, void
> (*func)(struct irq_work *))
>
> bool irq_work_queue(struct irq_work *work);
> bool irq_work_queue_on(struct irq_work *work, int cpu);
> +bool irq_work_schedule(struct work_struct *sched_work);
>
> void irq_work_tick(void);
> void irq_work_sync(struct irq_work *work);
> diff --git a/kernel/irq_work.c b/kernel/irq_work.c
> index eca8396..3880316 100644
> --- a/kernel/irq_work.c
> +++ b/kernel/irq_work.c
> @@ -24,6 +24,8 @@
> static DEFINE_PER_CPU(struct llist_head, raised_list);
> static DEFINE_PER_CPU(struct llist_head, lazy_list);
>
> +static struct irq_work_schedule irq_work_sched;
> +
> /*
> * Claim the entry so that no one else will poke at it.
> */
> @@ -79,6 +81,25 @@ bool irq_work_queue(struct irq_work *work)
> }
> EXPORT_SYMBOL_GPL(irq_work_queue);
>
> +static void irq_work_schedule_fn(struct irq_work *work)
> +{
> + struct irq_work_schedule *irq_work_sched =
> + container_of(work, struct irq_work_schedule, work);
> +
> + if (irq_work_sched->sched_work)
> + schedule_work(irq_work_sched->sched_work);
> +}
> +
> +/* Schedule work via irq work queue */
> +bool irq_work_schedule(struct work_struct *sched_work)
> +{
> + init_irq_work(&irq_work_sched.work, irq_work_schedule_fn);
> + irq_work_sched.sched_work = sched_work;
> +
> + return irq_work_queue(&irq_work_sched.work);
> +}
> +EXPORT_SYMBOL_GPL(irq_work_schedule);
> +

This is irredeemably broken.

Even if we didn't care about dropping events (which we do) then when you
overwrite irq_work_sched with a copy of another work_struct, either of
which could currently be enqueued somewhere, then you will cause some
very nasty corruption.


Daniel.