linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bjorn Helgaas <helgaas@kernel.org>
To: Ming Lei <ming.lei@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org,
	Christoph Hellwig <hch@lst.de>
Subject: Re: [PATCH] genirq/affinity: add helper of irq_affinity_calc_sets
Date: Thu, 15 Jul 2021 09:27:14 -0500	[thread overview]
Message-ID: <20210715142714.GA1957636@bjorn-Precision-5520> (raw)
In-Reply-To: <20210715111827.569756-1-ming.lei@redhat.com>

On Thu, Jul 15, 2021 at 07:18:27PM +0800, Ming Lei wrote:
> When driver requests to allocate irq affinity managed vectors,
> pci_alloc_irq_vectors_affinity() may fallback to single vector
> allocation. In this situation, we don't need to call
> irq_create_affinity_masks for calling into ->calc_sets() for
> avoiding potential memory leak, so add the helper for this purpose.
> 
> Fixes: c66d4bd110a1 ("genirq/affinity: Add new callback for (re)calculating interrupt sets")
> Reported-by: Bjorn Helgaas <helgaas@kernel.org>
> Cc: linux-pci@vger.kernel.org
> Cc: Christoph Hellwig <hch@lst.de>
> Signed-off-by: Ming Lei <ming.lei@redhat.com>
> ---
>  drivers/pci/msi.c         |  3 ++-
>  include/linux/interrupt.h |  7 +++++++
>  kernel/irq/affinity.c     | 29 ++++++++++++++++++-----------
>  3 files changed, 27 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/pci/msi.c b/drivers/pci/msi.c
> index 9232255c8515..3d6db20d1b2b 100644
> --- a/drivers/pci/msi.c
> +++ b/drivers/pci/msi.c
> @@ -1224,7 +1224,8 @@ int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
>  			 * for the single interrupt case.
>  			 */
>  			if (affd)
> -				irq_create_affinity_masks(1, affd);
> +				WARN_ON_ONCE(irq_affinity_calc_sets(1, affd));

Hmmm.  Not sure I like this yet:

  - I prefer required code to be on its own, not hidden inside a
    WARN() (personal preference, I know).

  - WARN() doesn't seem like the right thing here.  I think this
    generates a backtrace but the driver that called this has no
    indication.  Isn't the problem that a .calc_sets() method set
    "affd->nr_sets > IRQ_AFFINITY_MAX_SETS"?

    It looks like those methods are supplied by drivers
    (nvme_calc_irq_sets(), csio_calc_sets()) and it seems like they
    should find out about this somehow.

>  			pci_intx(dev, 1);
>  			return 1;
>  		}
> diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h
> index 2ed65b01c961..c7ff84d60465 100644
> --- a/include/linux/interrupt.h
> +++ b/include/linux/interrupt.h
> @@ -340,6 +340,7 @@ irq_create_affinity_masks(unsigned int nvec, struct irq_affinity *affd);
>  
>  unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
>  				       const struct irq_affinity *affd);
> +int irq_affinity_calc_sets(unsigned int affvecs, struct irq_affinity *affd);
>  
>  #else /* CONFIG_SMP */
>  
> @@ -391,6 +392,12 @@ irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
>  	return maxvec;
>  }
>  
> +static inline int irq_affinity_calc_sets(unsigned int affvecs,
> +					 struct irq_affinity *affd)
> +{
> +	return 0;
> +}
> +
>  #endif /* CONFIG_SMP */
>  
>  /*
> diff --git a/kernel/irq/affinity.c b/kernel/irq/affinity.c
> index 4d89ad4fae3b..735f697d7d15 100644
> --- a/kernel/irq/affinity.c
> +++ b/kernel/irq/affinity.c
> @@ -405,6 +405,23 @@ static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)
>  	affd->set_size[0] = affvecs;
>  }
>  
> +int irq_affinity_calc_sets(unsigned int affvecs, struct irq_affinity *affd)
> +{
> +	/*
> +	 * Simple invocations do not provide a calc_sets() callback. Install
> +	 * the generic one.
> +	 */
> +	if (!affd->calc_sets)
> +		affd->calc_sets = default_calc_sets;
> +
> +	/* Recalculate the sets */
> +	affd->calc_sets(affd, affvecs);
> +
> +	if (affd->nr_sets > IRQ_AFFINITY_MAX_SETS)
> +		return -ERANGE;
> +	return 0;
> +}
> +
>  /**
>   * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
>   * @nvecs:	The total number of vectors
> @@ -429,17 +446,7 @@ irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
>  	else
>  		affvecs = 0;
>  
> -	/*
> -	 * Simple invocations do not provide a calc_sets() callback. Install
> -	 * the generic one.
> -	 */
> -	if (!affd->calc_sets)
> -		affd->calc_sets = default_calc_sets;
> -
> -	/* Recalculate the sets */
> -	affd->calc_sets(affd, affvecs);
> -
> -	if (WARN_ON_ONCE(affd->nr_sets > IRQ_AFFINITY_MAX_SETS))
> +	if (WARN_ON_ONCE(irq_affinity_calc_sets(affvecs, affd)))
>  		return NULL;
>  
>  	/* Nothing to assign? */
> -- 
> 2.31.1
> 

  reply	other threads:[~2021-07-15 14:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-15 11:18 [PATCH] genirq/affinity: add helper of irq_affinity_calc_sets Ming Lei
2021-07-15 14:27 ` Bjorn Helgaas [this message]
2021-07-16  2:10   ` Ming Lei
2021-07-19  9:41 ` Christoph Hellwig

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=20210715142714.GA1957636@bjorn-Precision-5520 \
    --to=helgaas@kernel.org \
    --cc=hch@lst.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=ming.lei@redhat.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).