History

genirq/affinity: Add new callback for (re)calculating interrupt sets

The interrupt affinity spreading mechanism supports to spread out affinities for one or more interrupt sets. A interrupt set contains one or more interrupts. Each set is mapped to a specific functionality of a device, e.g. general I/O queues and read I/O queus of multiqueue block devices. The number of interrupts per set is defined by the driver. It depends on the total number of available interrupts for the device, which is determined by the PCI capabilites and the availability of underlying CPU resources, and the number of queues which the device provides and the driver wants to instantiate. The driver passes initial configuration for the interrupt allocation via a pointer to struct irq_affinity. Right now the allocation mechanism is complex as it requires to have a loop in the driver to determine the maximum number of interrupts which are provided by the PCI capabilities and the underlying CPU resources. This loop would have to be replicated in every driver which wants to utilize this mechanism. That's unwanted code duplication and error prone. In order to move this into generic facilities it is required to have a mechanism, which allows the recalculation of the interrupt sets and their size, in the core code. As the core code does not have any knowledge about the underlying device, a driver specific callback is required in struct irq_affinity, which can be invoked by the core code. The callback gets the number of available interupts as an argument, so the driver can calculate the corresponding number and size of interrupt sets. At the moment the struct irq_affinity pointer which is handed in from the driver and passed through to several core functions is marked 'const', but for the callback to be able to modify the data in the struct it's required to remove the 'const' qualifier. Add the optional callback to struct irq_affinity, which allows drivers to recalculate the number and size of interrupt sets and remove the 'const' qualifier. For simple invocations, which do not supply a callback, a default callback is installed, which just sets nr_sets to 1 and transfers the number of spreadable vectors to the set_size array at index 0. This is for now guarded by a check for nr_sets != 0 to keep the NVME driver working until it is converted to the callback mechanism. To make sure that the driver configuration is correct under all circumstances the callback is invoked even when there are no interrupts for queues left, i.e. the pre/post requirements already exhaust the numner of available interrupts. At the PCI layer irq_create_affinity_masks() has to be invoked even for the case where the legacy interrupt is used. That ensures that the callback is invoked and the device driver can adjust to that situation. [ tglx: Fixed the simple case (no sets required). Moved the sanity check for nr_sets after the invocation of the callback so it catches broken drivers. Fixed the kernel doc comments for struct irq_affinity and de-'This patch'-ed the changelog ]

Ming Lei committed 7 years ago

Commit c66d4bd1 parent 9cfef555 files · +71 −32Browse files

Unified · Split

5 files
  1. drivers/pci/msi.c +16 −9
  2. drivers/scsi/be2iscsi/be_main.c +1 −1
  3. include/linux/interrupt.h +8 −2
  4. include/linux/pci.h +2 −2
  5. kernel/irq/affinity.c +44 −18
drivers/pci/msi.c+16−9
@@ -532,7 +532,7 @@ error_attrs: · 531 unmodified lines
532532 }
533533
534534 static struct msi_desc *
535−msi_setup_entry(struct pci_dev *dev, int nvec, const struct irq_affinity *affd)
535+msi_setup_entry(struct pci_dev *dev, int nvec, struct irq_affinity *affd)
536536 {
537537 struct irq_affinity_desc *masks = NULL;
538538 struct msi_desc *entry;
@@ -597,7 +597,7 @@ static int msi_verify_entries(struct pci_dev *dev) · 58 unmodified lines
597597 * which could have been allocated.
598598 */
599599 static int msi_capability_init(struct pci_dev *dev, int nvec,
600− const struct irq_affinity *affd)
600+ struct irq_affinity *affd)
601601 {
602602 struct msi_desc *entry;
603603 int ret;
@@ -669,7 +669,7 @@ static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries) · 65 unmodified lines
669669
670670 static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
671671 struct msix_entry *entries, int nvec,
672− const struct irq_affinity *affd)
672+ struct irq_affinity *affd)
673673 {
674674 struct irq_affinity_desc *curmsk, *masks = NULL;
675675 struct msi_desc *entry;
@@ -736,7 +736,7 @@ static void msix_program_entries(struct pci_dev *dev, · 60 unmodified lines
736736 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
737737 **/
738738 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
739− int nvec, const struct irq_affinity *affd)
739+ int nvec, struct irq_affinity *affd)
740740 {
741741 int ret;
742742 u16 control;
@@ -932,7 +932,7 @@ int pci_msix_vec_count(struct pci_dev *dev) · 189 unmodified lines
932932 EXPORT_SYMBOL(pci_msix_vec_count);
933933
934934 static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
935− int nvec, const struct irq_affinity *affd)
935+ int nvec, struct irq_affinity *affd)
936936 {
937937 int nr_entries;
938938 int i, j;
@@ -1018,7 +1018,7 @@ int pci_msi_enabled(void) · 79 unmodified lines
10181018 EXPORT_SYMBOL(pci_msi_enabled);
10191019
10201020 static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
1021− const struct irq_affinity *affd)
1021+ struct irq_affinity *affd)
10221022 {
10231023 int nvec;
10241024 int rc;
@@ -1086,7 +1086,7 @@ EXPORT_SYMBOL(pci_enable_msi); · 61 unmodified lines
10861086
10871087 static int __pci_enable_msix_range(struct pci_dev *dev,
10881088 struct msix_entry *entries, int minvec,
1089− int maxvec, const struct irq_affinity *affd)
1089+ int maxvec, struct irq_affinity *affd)
10901090 {
10911091 int rc, nvec = maxvec;
10921092
@@ -1165,9 +1165,9 @@ EXPORT_SYMBOL(pci_enable_msix_range); · 72 unmodified lines
11651165 */
11661166 int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
11671167 unsigned int max_vecs, unsigned int flags,
1168− const struct irq_affinity *affd)
1168+ struct irq_affinity *affd)
11691169 {
1170− static const struct irq_affinity msi_default_affd;
1170+ struct irq_affinity msi_default_affd = {0};
11711171 int msix_vecs = -ENOSPC;
11721172 int msi_vecs = -ENOSPC;
11731173
@@ -1196,6 +1196,13 @@ int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs, · 22 unmodified lines
11961196 /* use legacy irq if allowed */
11971197 if (flags & PCI_IRQ_LEGACY) {
11981198 if (min_vecs == 1 && dev->irq) {
1199+ /*
1200+ * Invoke the affinity spreading logic to ensure that
1201+ * the device driver can adjust queue configuration
1202+ * for the single interrupt case.
1203+ */
1204+ if (affd)
1205+ irq_create_affinity_masks(1, affd);
11991206 pci_intx(dev, 1);
12001207 return 1;
12011208 }
drivers/scsi/be2iscsi/be_main.c+1−1
@@ -3566,7 +3566,7 @@ static void be2iscsi_enable_msix(struct beiscsi_hba *phba) · 3565 unmodified lines
35663566
35673567 /* if eqid_count == 1 fall back to INTX */
35683568 if (enable_msix && nvec > 1) {
3569− const struct irq_affinity desc = { .post_vectors = 1 };
3569+ struct irq_affinity desc = { .post_vectors = 1 };
35703570
35713571 if (pci_alloc_irq_vectors_affinity(phba->pcidev, 2, nvec,
35723572 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY, &desc) < 0) {
include/linux/interrupt.h+8−2
@@ -252,12 +252,18 @@ struct irq_affinity_notify { · 251 unmodified lines
252252 * @nr_sets: The number of interrupt sets for which affinity
253253 * spreading is required
254254 * @set_size: Array holding the size of each interrupt set
255+ * @calc_sets: Callback for calculating the number and size
256+ * of interrupt sets
257+ * @priv: Private data for usage by @calc_sets, usually a
258+ * pointer to driver/device specific data.
255259 */
256260 struct irq_affinity {
257261 unsigned int pre_vectors;
258262 unsigned int post_vectors;
259263 unsigned int nr_sets;
260264 unsigned int set_size[IRQ_AFFINITY_MAX_SETS];
265+ void (*calc_sets)(struct irq_affinity *, unsigned int nvecs);
266+ void *priv;
261267 };
262268
263269 /**
@@ -317,7 +323,7 @@ extern int · 53 unmodified lines
317323 irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);
318324
319325 struct irq_affinity_desc *
320−irq_create_affinity_masks(unsigned int nvec, const struct irq_affinity *affd);
326+irq_create_affinity_masks(unsigned int nvec, struct irq_affinity *affd);
321327
322328 unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
323329 const struct irq_affinity *affd);
@@ -354,7 +360,7 @@ irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify) · 30 unmodified lines
354360 }
355361
356362 static inline struct irq_affinity_desc *
357−irq_create_affinity_masks(unsigned int nvec, const struct irq_affinity *affd)
363+irq_create_affinity_masks(unsigned int nvec, struct irq_affinity *affd)
358364 {
359365 return NULL;
360366 }
include/linux/pci.h+2−2
@@ -1393,7 +1393,7 @@ static inline int pci_enable_msix_exact(struct pci_dev *dev, · 1392 unmodified lines
13931393 }
13941394 int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
13951395 unsigned int max_vecs, unsigned int flags,
1396− const struct irq_affinity *affd);
1396+ struct irq_affinity *affd);
13971397
13981398 void pci_free_irq_vectors(struct pci_dev *dev);
13991399 int pci_irq_vector(struct pci_dev *dev, unsigned int nr);
@@ -1419,7 +1419,7 @@ static inline int pci_enable_msix_exact(struct pci_dev *dev, · 19 unmodified lines
14191419 static inline int
14201420 pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
14211421 unsigned int max_vecs, unsigned int flags,
1422− const struct irq_affinity *aff_desc)
1422+ struct irq_affinity *aff_desc)
14231423 {
14241424 if ((flags & PCI_IRQ_LEGACY) && min_vecs == 1 && dev->irq)
14251425 return 1;
kernel/irq/affinity.c+44−18
@@ -230,6 +230,12 @@ static int irq_build_affinity_masks(const struct irq_affinity *affd, · 229 unmodified lines
230230 return ret;
231231 }
232232
233+static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)
234+{
235+ affd->nr_sets = 1;
236+ affd->set_size[0] = affvecs;
237+}
238+
233239 /**
234240 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
235241 * @nvecs: The total number of vectors
@@ -240,20 +246,46 @@ static int irq_build_affinity_masks(const struct irq_affinity *affd, · 4 unmodified lines
240246 struct irq_affinity_desc *
241247 irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
242248 {
243− unsigned int affvecs, curvec, usedvecs, nr_sets, i;
244− unsigned int set_size[IRQ_AFFINITY_MAX_SETS];
249+ unsigned int affvecs, curvec, usedvecs, i;
245250 struct irq_affinity_desc *masks = NULL;
246251
247252 /*
248− * If there aren't any vectors left after applying the pre/post
249− * vectors don't bother with assigning affinity.
253+ * Determine the number of vectors which need interrupt affinities
254+ * assigned. If the pre/post request exhausts the available vectors
255+ * then nothing to do here except for invoking the calc_sets()
256+ * callback so the device driver can adjust to the situation. If there
257+ * is only a single vector, then managing the queue is pointless as
258+ * well.
250259 */
251− if (nvecs == affd->pre_vectors + affd->post_vectors)
252− return NULL;
260+ if (nvecs > 1 && nvecs > affd->pre_vectors + affd->post_vectors)
261+ affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
262+ else
263+ affvecs = 0;
264+
265+ /*
266+ * Simple invocations do not provide a calc_sets() callback. Install
267+ * the generic one. The check for affd->nr_sets is a temporary
268+ * workaround and will be removed after the NVME driver is converted
269+ * over.
270+ */
271+ if (!affd->nr_sets && !affd->calc_sets)
272+ affd->calc_sets = default_calc_sets;
273+
274+ /*
275+ * If the device driver provided a calc_sets() callback let it
276+ * recalculate the number of sets and their size. The check will go
277+ * away once the NVME driver is converted over.
278+ */
279+ if (affd->calc_sets)
280+ affd->calc_sets(affd, affvecs);
253281
254282 if (WARN_ON_ONCE(affd->nr_sets > IRQ_AFFINITY_MAX_SETS))
255283 return NULL;
256284
285+ /* Nothing to assign? */
286+ if (!affvecs)
287+ return NULL;
288+
257289 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
258290 if (!masks)
259291 return NULL;
@@ -261,21 +293,13 @@ irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd) · 1 unmodified line
261293 /* Fill out vectors at the beginning that don't need affinity */
262294 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
263295 cpumask_copy(&masks[curvec].mask, irq_default_affinity);
296+
264297 /*
265298 * Spread on present CPUs starting from affd->pre_vectors. If we
266299 * have multiple sets, build each sets affinity mask separately.
267300 */
268− affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
269− nr_sets = affd->nr_sets;
270− if (!nr_sets) {
271− nr_sets = 1;
272− set_size[0] = affvecs;
273− } else {
274− memcpy(set_size, affd->set_size, nr_sets * sizeof(unsigned int));
275− }
276−
277− for (i = 0, usedvecs = 0; i < nr_sets; i++) {
278− unsigned int this_vecs = set_size[i];
301+ for (i = 0, usedvecs = 0; i < affd->nr_sets; i++) {
302+ unsigned int this_vecs = affd->set_size[i];
279303 int ret;
280304
281305 ret = irq_build_affinity_masks(affd, curvec, this_vecs,
@@ -318,7 +342,9 @@ unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec, · 36 unmodified lines
318342 if (resv > minvec)
319343 return 0;
320344
321− if (affd->nr_sets) {
345+ if (affd->calc_sets) {
346+ set_vecs = maxvec - resv;
347+ } else if (affd->nr_sets) {
322348 unsigned int i;
323349
324350 for (i = 0, set_vecs = 0; i < affd->nr_sets; i++)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t