All of lore.kernel.org
 help / color / mirror / Atom feed
From: Josh Triplett <josh@joshtriplett.org>
To: David Decotigny <decot@googlers.com>
Cc: linux-kernel@vger.kernel.org,
	Ben Hutchings <bhutchings@solarflare.com>,
	"David S. Miller" <davem@davemloft.net>,
	Or Gerlitz <ogerlitz@mellanox.com>,
	Amir Vadai <amirv@mellanox.com>,
	"Paul E. McKenney" <paul.mckenney@linaro.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Andrew Morton <akpm@linux-foundation.org>,
	David Howells <dhowells@redhat.com>,
	Paul Gortmaker <paul.gortmaker@windriver.com>
Subject: Re: [PATCH v1] lib: cpu_rmap: avoid flushing all workqueues
Date: Thu, 27 Dec 2012 20:04:30 -0800	[thread overview]
Message-ID: <20121228040430.GA10244@leaf> (raw)
In-Reply-To: <82b0a7204d5cb4032b02f731a956ee9923ff150c.1356635879.git.decot@googlers.com>

On Thu, Dec 27, 2012 at 11:24:34AM -0800, David Decotigny wrote:
> In some cases, free_irq_cpu_rmap() is called while holding a lock
> (eg. rtnl). This can lead to deadlocks, because it invokes
> flush_scheduled_work() which ends up waiting for whole system
> workqueue to flush, but some pending works might try to acquire the
> lock we are already holding.
> 
> This commit uses reference-counting to replace
> irq_run_affinity_notifiers(). It also removes
> irq_run_affinity_notifiers() altogether.
> 
> Signed-off-by: David Decotigny <decot@googlers.com>

A couple of comments below; with those addressed,
Reviewed-by: Josh Triplett <josh@joshtriplett.org>

>  include/linux/cpu_rmap.h  |   13 ++++--------
>  include/linux/interrupt.h |    5 -----
>  lib/cpu_rmap.c            |   48 +++++++++++++++++++++++++++++++++++++++------
>  3 files changed, 46 insertions(+), 20 deletions(-)
> 
> diff --git a/include/linux/cpu_rmap.h b/include/linux/cpu_rmap.h
> index ac3bbb5..3be2813 100644
> --- a/include/linux/cpu_rmap.h
> +++ b/include/linux/cpu_rmap.h
> @@ -13,9 +13,11 @@
>  #include <linux/cpumask.h>
>  #include <linux/gfp.h>
>  #include <linux/slab.h>
> +#include <linux/kref.h>
>  
>  /**
>   * struct cpu_rmap - CPU affinity reverse-map
> + * @refcount: kref for object
>   * @size: Number of objects to be reverse-mapped
>   * @used: Number of objects added
>   * @obj: Pointer to array of object pointers
> @@ -23,6 +25,7 @@
>   *      based on affinity masks
>   */
>  struct cpu_rmap {
> +	struct kref	refcount;
>  	u16		size, used;
>  	void		**obj;
>  	struct {
> @@ -33,15 +36,7 @@ struct cpu_rmap {
>  #define CPU_RMAP_DIST_INF 0xffff
>  
>  extern struct cpu_rmap *alloc_cpu_rmap(unsigned int size, gfp_t flags);
> -
> -/**
> - * free_cpu_rmap - free CPU affinity reverse-map
> - * @rmap: Reverse-map allocated with alloc_cpu_rmap(), or %NULL
> - */
> -static inline void free_cpu_rmap(struct cpu_rmap *rmap)
> -{
> -	kfree(rmap);
> -}
> +extern void free_cpu_rmap(struct cpu_rmap *rmap);
>  
>  extern int cpu_rmap_add(struct cpu_rmap *rmap, void *obj);
>  extern int cpu_rmap_update(struct cpu_rmap *rmap, u16 index,
> diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h
> index 5e4e617..5fa5afe 100644
> --- a/include/linux/interrupt.h
> +++ b/include/linux/interrupt.h
> @@ -268,11 +268,6 @@ struct irq_affinity_notify {
>  extern int
>  irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);
>  
> -static inline void irq_run_affinity_notifiers(void)
> -{
> -	flush_scheduled_work();
> -}
> -
>  #else /* CONFIG_SMP */
>  
>  static inline int irq_set_affinity(unsigned int irq, const struct cpumask *m)
> diff --git a/lib/cpu_rmap.c b/lib/cpu_rmap.c
> index 145dec5..295cbf8 100644
> --- a/lib/cpu_rmap.c
> +++ b/lib/cpu_rmap.c
> @@ -45,6 +45,7 @@ struct cpu_rmap *alloc_cpu_rmap(unsigned int size, gfp_t flags)
>  	if (!rmap)
>  		return NULL;
>  
> +	kref_init(&rmap->refcount);
>  	rmap->obj = (void **)((char *)rmap + obj_offset);
>  
>  	/* Initially assign CPUs to objects on a rota, since we have
> @@ -63,6 +64,26 @@ struct cpu_rmap *alloc_cpu_rmap(unsigned int size, gfp_t flags)
>  }
>  EXPORT_SYMBOL(alloc_cpu_rmap);
>  
> +/**
> + * reclaim_cpu_rmap - internal reclaiming helper called from kref_put
> + * @ref: kref to struct cpu_rmap
> + */
> +static void reclaim_cpu_rmap(struct kref *ref)
> +{
> +	struct cpu_rmap *rmap = container_of(ref, struct cpu_rmap, refcount);
> +	kfree(rmap);
> +}
> +
> +/**
> + * free_cpu_rmap - free CPU affinity reverse-map
> + * @rmap: Reverse-map allocated with alloc_cpu_rmap(), or %NULL
> + */
> +void free_cpu_rmap(struct cpu_rmap *rmap)
> +{
> +	kref_put(&rmap->refcount, reclaim_cpu_rmap);
> +}
> +EXPORT_SYMBOL(free_cpu_rmap);
> +
>  /* Reevaluate nearest object for given CPU, comparing with the given
>   * neighbours at the given distance.
>   */
> @@ -197,8 +218,7 @@ struct irq_glue {
>   * free_irq_cpu_rmap - free a CPU affinity reverse-map used for IRQs
>   * @rmap: Reverse-map allocated with alloc_irq_cpu_map(), or %NULL
>   *
> - * Must be called in process context, before freeing the IRQs, and
> - * without holding any locks required by global workqueue items.
> + * Must be called in process context, before freeing the IRQs.
>   */
>  void free_irq_cpu_rmap(struct cpu_rmap *rmap)
>  {
> @@ -212,12 +232,18 @@ void free_irq_cpu_rmap(struct cpu_rmap *rmap)
>  		glue = rmap->obj[index];
>  		irq_set_affinity_notifier(glue->notify.irq, NULL);
>  	}
> -	irq_run_affinity_notifiers();
>  
> -	kfree(rmap);
> +	kref_put(&rmap->refcount, reclaim_cpu_rmap);

This should call free_cpu_rmap, rather than duplicating its body.

>  }
>  EXPORT_SYMBOL(free_irq_cpu_rmap);
>  
> +/**
> + * irq_cpu_rmap_notify - callback for IRQ subsystem when IRQ affinity updated
> + * @notify: struct irq_affinity_notify passed by irq/manage.c
> + * @mask: cpu mask for new SMP affinity
> + *
> + * This is executed in workqueue context.
> + */
>  static void
>  irq_cpu_rmap_notify(struct irq_affinity_notify *notify, const cpumask_t *mask)
>  {
> @@ -230,16 +256,23 @@ irq_cpu_rmap_notify(struct irq_affinity_notify *notify, const cpumask_t *mask)
>  		pr_warning("irq_cpu_rmap_notify: update failed: %d\n", rc);
>  }
>  
> +/**
> + * irq_cpu_rmap_release - reclaiming callback for IRQ subsystem
> + * @ref: kref to struct irq_affinity_notify passed by irq/manage.c
> + */
>  static void irq_cpu_rmap_release(struct kref *ref)
>  {
>  	struct irq_glue *glue =
>  		container_of(ref, struct irq_glue, notify.kref);
> +	struct cpu_rmap *rmap = glue->rmap;
> +
>  	kfree(glue);
> +	kref_put(&rmap->refcount, reclaim_cpu_rmap);

Likewise, but also, why not call free_cpu_rmap(glue->rmap) before
kfree(glue) so you don't need the local copy?

>  }
>  
>  /**
>   * irq_cpu_rmap_add - add an IRQ to a CPU affinity reverse-map
> - * @rmap: The reverse-map
> + * @rmap: The per-IRQ reverse-map
>   * @irq: The IRQ number
>   *
>   * This adds an IRQ affinity notifier that will update the reverse-map
> @@ -259,9 +292,12 @@ int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq)
>  	glue->notify.release = irq_cpu_rmap_release;
>  	glue->rmap = rmap;
>  	glue->index = cpu_rmap_add(rmap, glue);
> +	kref_get(&rmap->refcount);
>  	rc = irq_set_affinity_notifier(irq, &glue->notify);
> -	if (rc)
> +	if (rc) {
>  		kfree(glue);
> +		kref_put(&rmap->refcount, reclaim_cpu_rmap);

Likewise.

> +	}
>  	return rc;
>  }
>  EXPORT_SYMBOL(irq_cpu_rmap_add);
> -- 
> 1.7.10.2.5.g20d7bc9
> 

  reply	other threads:[~2012-12-28  4:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1356635879.git.decot@googlers.com>
2012-12-27 19:24 ` [PATCH v1] lib: cpu_rmap: avoid flushing all workqueues David Decotigny
2012-12-28  4:04   ` Josh Triplett [this message]
2012-12-28 18:18     ` David Decotigny
2012-12-29  0:16       ` Josh Triplett

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20121228040430.GA10244@leaf \
    --to=josh@joshtriplett.org \
    --cc=akpm@linux-foundation.org \
    --cc=amirv@mellanox.com \
    --cc=bhutchings@solarflare.com \
    --cc=davem@davemloft.net \
    --cc=decot@googlers.com \
    --cc=dhowells@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ogerlitz@mellanox.com \
    --cc=paul.gortmaker@windriver.com \
    --cc=paul.mckenney@linaro.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.