Skip to content

Commit b4c3a87

Browse files
antonblanchardozbenh
authored andcommitted
powerpc/iommu: Implement IOMMU pools to improve multiqueue adapter performance
At the moment all queues in a multiqueue adapter will serialise against the IOMMU table lock. This is proving to be a big issue, especially with 10Gbit ethernet. This patch creates 4 pools and tries to spread the load across them. If the table is under 1GB in size we revert back to the original behaviour of 1 pool and 1 largealloc pool. We create a hash to map CPUs to pools. Since we prefer interrupts to be affinitised to primary CPUs, without some form of hashing we are very likely to end up using the same pool. As an example, POWER7 has 4 way SMT and with 4 pools all primary threads will map to the same pool. The largealloc pool is reduced from 1/2 to 1/4 of the space to partially offset the overhead of breaking the table up into pools. Some performance numbers were obtained with a Chelsio T3 adapter on two POWER7 boxes, running a 100 session TCP round robin test. Performance improved 69% with this patch applied. Signed-off-by: Anton Blanchard <anton@samba.org> Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
1 parent d362213 commit b4c3a87

3 files changed

Lines changed: 128 additions & 39 deletions

File tree

‎arch/powerpc/include/asm/iommu.h‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ static __inline__ __attribute_const__ int get_iommu_order(unsigned long size)
5353
*/
5454
#define IOMAP_MAX_ORDER 13
5555

56+
#define IOMMU_POOL_HASHBITS 2
57+
#define IOMMU_NR_POOLS (1 << IOMMU_POOL_HASHBITS)
58+
59+
struct iommu_pool {
60+
unsigned long start;
61+
unsigned long end;
62+
unsigned long hint;
63+
spinlock_t lock;
64+
} ____cacheline_aligned_in_smp;
65+
5666
struct iommu_table {
5767
unsigned long it_busno; /* Bus number this table belongs to */
5868
unsigned long it_size; /* Size of iommu table in entries */
@@ -61,10 +71,10 @@ struct iommu_table {
6171
unsigned long it_index; /* which iommu table this is */
6272
unsigned long it_type; /* type: PCI or Virtual Bus */
6373
unsigned long it_blocksize; /* Entries in each block (cacheline) */
64-
unsigned long it_hint; /* Hint for next alloc */
65-
unsigned long it_largehint; /* Hint for large allocs */
66-
unsigned long it_halfpoint; /* Breaking point for small/large allocs */
67-
spinlock_t it_lock; /* Protects it_map */
74+
unsigned long poolsize;
75+
unsigned long nr_pools;
76+
struct iommu_pool large_pool;
77+
struct iommu_pool pools[IOMMU_NR_POOLS];
6878
unsigned long *it_map; /* A simple allocation bitmap for now */
6979
};
7080

‎arch/powerpc/kernel/iommu.c‎

Lines changed: 114 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <linux/bitmap.h>
3434
#include <linux/iommu-helper.h>
3535
#include <linux/crash_dump.h>
36+
#include <linux/hash.h>
3637
#include <asm/io.h>
3738
#include <asm/prom.h>
3839
#include <asm/iommu.h>
@@ -58,6 +59,26 @@ static int __init setup_iommu(char *str)
5859

5960
__setup("iommu=", setup_iommu);
6061

62+
static DEFINE_PER_CPU(unsigned int, iommu_pool_hash);
63+
64+
/*
65+
* We precalculate the hash to avoid doing it on every allocation.
66+
*
67+
* The hash is important to spread CPUs across all the pools. For example,
68+
* on a POWER7 with 4 way SMT we want interrupts on the primary threads and
69+
* with 4 pools all primary threads would map to the same pool.
70+
*/
71+
static int __init setup_iommu_pool_hash(void)
72+
{
73+
unsigned int i;
74+
75+
for_each_possible_cpu(i)
76+
per_cpu(iommu_pool_hash, i) = hash_32(i, IOMMU_POOL_HASHBITS);
77+
78+
return 0;
79+
}
80+
subsys_initcall(setup_iommu_pool_hash);
81+
6182
static unsigned long iommu_range_alloc(struct device *dev,
6283
struct iommu_table *tbl,
6384
unsigned long npages,
@@ -72,6 +93,8 @@ static unsigned long iommu_range_alloc(struct device *dev,
7293
unsigned long align_mask;
7394
unsigned long boundary_size;
7495
unsigned long flags;
96+
unsigned int pool_nr;
97+
struct iommu_pool *pool;
7598

7699
align_mask = 0xffffffffffffffffl >> (64 - align_order);
77100

@@ -84,38 +107,46 @@ static unsigned long iommu_range_alloc(struct device *dev,
84107
return DMA_ERROR_CODE;
85108
}
86109

87-
spin_lock_irqsave(&(tbl->it_lock), flags);
110+
/*
111+
* We don't need to disable preemption here because any CPU can
112+
* safely use any IOMMU pool.
113+
*/
114+
pool_nr = __raw_get_cpu_var(iommu_pool_hash) & (tbl->nr_pools - 1);
88115

89-
if (handle && *handle)
90-
start = *handle;
116+
if (largealloc)
117+
pool = &(tbl->large_pool);
91118
else
92-
start = largealloc ? tbl->it_largehint : tbl->it_hint;
119+
pool = &(tbl->pools[pool_nr]);
93120

94-
/* Use only half of the table for small allocs (15 pages or less) */
95-
limit = largealloc ? tbl->it_size : tbl->it_halfpoint;
121+
spin_lock_irqsave(&(pool->lock), flags);
122+
123+
again:
124+
if ((pass == 0) && handle && *handle)
125+
start = *handle;
126+
else
127+
start = pool->hint;
96128

97-
if (largealloc && start < tbl->it_halfpoint)
98-
start = tbl->it_halfpoint;
129+
limit = pool->end;
99130

100131
/* The case below can happen if we have a small segment appended
101132
* to a large, or when the previous alloc was at the very end of
102133
* the available space. If so, go back to the initial start.
103134
*/
104135
if (start >= limit)
105-
start = largealloc ? tbl->it_largehint : tbl->it_hint;
106-
107-
again:
136+
start = pool->start;
108137

109138
if (limit + tbl->it_offset > mask) {
110139
limit = mask - tbl->it_offset + 1;
111140
/* If we're constrained on address range, first try
112141
* at the masked hint to avoid O(n) search complexity,
113-
* but on second pass, start at 0.
142+
* but on second pass, start at 0 in pool 0.
114143
*/
115-
if ((start & mask) >= limit || pass > 0)
116-
start = 0;
117-
else
144+
if ((start & mask) >= limit || pass > 0) {
145+
pool = &(tbl->pools[0]);
146+
start = pool->start;
147+
} else {
118148
start &= mask;
149+
}
119150
}
120151

121152
if (dev)
@@ -129,17 +160,25 @@ static unsigned long iommu_range_alloc(struct device *dev,
129160
tbl->it_offset, boundary_size >> IOMMU_PAGE_SHIFT,
130161
align_mask);
131162
if (n == -1) {
132-
if (likely(pass < 2)) {
133-
/* First failure, just rescan the half of the table.
134-
* Second failure, rescan the other half of the table.
135-
*/
136-
start = (largealloc ^ pass) ? tbl->it_halfpoint : 0;
137-
limit = pass ? tbl->it_size : limit;
163+
if (likely(pass == 0)) {
164+
/* First try the pool from the start */
165+
pool->hint = pool->start;
138166
pass++;
139167
goto again;
168+
169+
} else if (pass <= tbl->nr_pools) {
170+
/* Now try scanning all the other pools */
171+
spin_unlock(&(pool->lock));
172+
pool_nr = (pool_nr + 1) & (tbl->nr_pools - 1);
173+
pool = &tbl->pools[pool_nr];
174+
spin_lock(&(pool->lock));
175+
pool->hint = pool->start;
176+
pass++;
177+
goto again;
178+
140179
} else {
141-
/* Third failure, give up */
142-
spin_unlock_irqrestore(&(tbl->it_lock), flags);
180+
/* Give up */
181+
spin_unlock_irqrestore(&(pool->lock), flags);
143182
return DMA_ERROR_CODE;
144183
}
145184
}
@@ -149,18 +188,19 @@ static unsigned long iommu_range_alloc(struct device *dev,
149188
/* Bump the hint to a new block for small allocs. */
150189
if (largealloc) {
151190
/* Don't bump to new block to avoid fragmentation */
152-
tbl->it_largehint = end;
191+
pool->hint = end;
153192
} else {
154193
/* Overflow will be taken care of at the next allocation */
155-
tbl->it_hint = (end + tbl->it_blocksize - 1) &
194+
pool->hint = (end + tbl->it_blocksize - 1) &
156195
~(tbl->it_blocksize - 1);
157196
}
158197

159198
/* Update handle for SG allocations */
160199
if (handle)
161200
*handle = end;
162201

163-
spin_unlock_irqrestore(&(tbl->it_lock), flags);
202+
spin_unlock_irqrestore(&(pool->lock), flags);
203+
164204
return n;
165205
}
166206

@@ -235,23 +275,45 @@ static bool iommu_free_check(struct iommu_table *tbl, dma_addr_t dma_addr,
235275
return true;
236276
}
237277

278+
static struct iommu_pool *get_pool(struct iommu_table *tbl,
279+
unsigned long entry)
280+
{
281+
struct iommu_pool *p;
282+
unsigned long largepool_start = tbl->large_pool.start;
283+
284+
/* The large pool is the last pool at the top of the table */
285+
if (entry >= largepool_start) {
286+
p = &tbl->large_pool;
287+
} else {
288+
unsigned int pool_nr = entry / tbl->poolsize;
289+
290+
BUG_ON(pool_nr > tbl->nr_pools);
291+
p = &tbl->pools[pool_nr];
292+
}
293+
294+
return p;
295+
}
296+
238297
static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
239298
unsigned int npages)
240299
{
241300
unsigned long entry, free_entry;
242301
unsigned long flags;
302+
struct iommu_pool *pool;
243303

244304
entry = dma_addr >> IOMMU_PAGE_SHIFT;
245305
free_entry = entry - tbl->it_offset;
246306

307+
pool = get_pool(tbl, free_entry);
308+
247309
if (!iommu_free_check(tbl, dma_addr, npages))
248310
return;
249311

250312
ppc_md.tce_free(tbl, entry, npages);
251313

252-
spin_lock_irqsave(&(tbl->it_lock), flags);
314+
spin_lock_irqsave(&(pool->lock), flags);
253315
bitmap_clear(tbl->it_map, free_entry, npages);
254-
spin_unlock_irqrestore(&(tbl->it_lock), flags);
316+
spin_unlock_irqrestore(&(pool->lock), flags);
255317
}
256318

257319
static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
@@ -493,9 +555,8 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
493555
unsigned long sz;
494556
static int welcomed = 0;
495557
struct page *page;
496-
497-
/* Set aside 1/4 of the table for large allocations. */
498-
tbl->it_halfpoint = tbl->it_size * 3 / 4;
558+
unsigned int i;
559+
struct iommu_pool *p;
499560

500561
/* number of bytes needed for the bitmap */
501562
sz = (tbl->it_size + 7) >> 3;
@@ -514,9 +575,28 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
514575
if (tbl->it_offset == 0)
515576
set_bit(0, tbl->it_map);
516577

517-
tbl->it_hint = 0;
518-
tbl->it_largehint = tbl->it_halfpoint;
519-
spin_lock_init(&tbl->it_lock);
578+
/* We only split the IOMMU table if we have 1GB or more of space */
579+
if ((tbl->it_size << IOMMU_PAGE_SHIFT) >= (1UL * 1024 * 1024 * 1024))
580+
tbl->nr_pools = IOMMU_NR_POOLS;
581+
else
582+
tbl->nr_pools = 1;
583+
584+
/* We reserve the top 1/4 of the table for large allocations */
585+
tbl->poolsize = (tbl->it_size * 3 / 4) / IOMMU_NR_POOLS;
586+
587+
for (i = 0; i < IOMMU_NR_POOLS; i++) {
588+
p = &tbl->pools[i];
589+
spin_lock_init(&(p->lock));
590+
p->start = tbl->poolsize * i;
591+
p->hint = p->start;
592+
p->end = p->start + tbl->poolsize;
593+
}
594+
595+
p = &tbl->large_pool;
596+
spin_lock_init(&(p->lock));
597+
p->start = tbl->poolsize * i;
598+
p->hint = p->start;
599+
p->end = tbl->it_size;
520600

521601
iommu_table_clear(tbl);
522602

‎arch/powerpc/platforms/cell/iommu.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,6 @@ cell_iommu_setup_window(struct cbe_iommu *iommu, struct device_node *np,
518518
__set_bit(0, window->table.it_map);
519519
tce_build_cell(&window->table, window->table.it_offset, 1,
520520
(unsigned long)iommu->pad_page, DMA_TO_DEVICE, NULL);
521-
window->table.it_hint = window->table.it_blocksize;
522521

523522
return window;
524523
}

0 commit comments

Comments
 (0)