9
9
*/
10
10
11
11
#include <linux/acpi_iort.h>
12
+ #include <linux/atomic.h>
13
+ #include <linux/crash_dump.h>
12
14
#include <linux/device.h>
13
- #include <linux/dma-map-ops .h>
15
+ #include <linux/dma-direct .h>
14
16
#include <linux/dma-iommu.h>
17
+ #include <linux/dma-map-ops.h>
15
18
#include <linux/gfp.h>
16
19
#include <linux/huge_mm.h>
17
20
#include <linux/iommu.h>
20
23
#include <linux/mm.h>
21
24
#include <linux/mutex.h>
22
25
#include <linux/pci.h>
23
- #include <linux/swiotlb.h>
24
26
#include <linux/scatterlist.h>
27
+ #include <linux/spinlock.h>
28
+ #include <linux/swiotlb.h>
25
29
#include <linux/vmalloc.h>
26
- #include <linux/crash_dump.h>
27
- #include <linux/dma-direct.h>
28
30
29
31
struct iommu_dma_msi_page {
30
32
struct list_head list ;
@@ -41,7 +43,19 @@ struct iommu_dma_cookie {
41
43
enum iommu_dma_cookie_type type ;
42
44
union {
43
45
/* Full allocator for IOMMU_DMA_IOVA_COOKIE */
44
- struct iova_domain iovad ;
46
+ struct {
47
+ struct iova_domain iovad ;
48
+
49
+ struct iova_fq __percpu * fq ; /* Flush queue */
50
+ /* Number of TLB flushes that have been started */
51
+ atomic64_t fq_flush_start_cnt ;
52
+ /* Number of TLB flushes that have been finished */
53
+ atomic64_t fq_flush_finish_cnt ;
54
+ /* Timer to regularily empty the flush queues */
55
+ struct timer_list fq_timer ;
56
+ /* 1 when timer is active, 0 when not */
57
+ atomic_t fq_timer_on ;
58
+ };
45
59
/* Trivial linear page allocator for IOMMU_DMA_MSI_COOKIE */
46
60
dma_addr_t msi_iova ;
47
61
};
@@ -64,6 +78,27 @@ static int __init iommu_dma_forcedac_setup(char *str)
64
78
}
65
79
early_param ("iommu.forcedac" , iommu_dma_forcedac_setup );
66
80
81
+ /* Number of entries per flush queue */
82
+ #define IOVA_FQ_SIZE 256
83
+
84
+ /* Timeout (in ms) after which entries are flushed from the queue */
85
+ #define IOVA_FQ_TIMEOUT 10
86
+
87
+ /* Flush queue entry for deferred flushing */
88
+ struct iova_fq_entry {
89
+ unsigned long iova_pfn ;
90
+ unsigned long pages ;
91
+ struct list_head freelist ;
92
+ u64 counter ; /* Flush counter when this entry was added */
93
+ };
94
+
95
+ /* Per-CPU flush queue structure */
96
+ struct iova_fq {
97
+ struct iova_fq_entry entries [IOVA_FQ_SIZE ];
98
+ unsigned int head , tail ;
99
+ spinlock_t lock ;
100
+ };
101
+
67
102
#define fq_ring_for_each (i , fq ) \
68
103
for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
69
104
@@ -73,9 +108,9 @@ static inline bool fq_full(struct iova_fq *fq)
73
108
return (((fq -> tail + 1 ) % IOVA_FQ_SIZE ) == fq -> head );
74
109
}
75
110
76
- static inline unsigned fq_ring_add (struct iova_fq * fq )
111
+ static inline unsigned int fq_ring_add (struct iova_fq * fq )
77
112
{
78
- unsigned idx = fq -> tail ;
113
+ unsigned int idx = fq -> tail ;
79
114
80
115
assert_spin_locked (& fq -> lock );
81
116
@@ -84,10 +119,10 @@ static inline unsigned fq_ring_add(struct iova_fq *fq)
84
119
return idx ;
85
120
}
86
121
87
- static void fq_ring_free (struct iova_domain * iovad , struct iova_fq * fq )
122
+ static void fq_ring_free (struct iommu_dma_cookie * cookie , struct iova_fq * fq )
88
123
{
89
- u64 counter = atomic64_read (& iovad -> fq_flush_finish_cnt );
90
- unsigned idx ;
124
+ u64 counter = atomic64_read (& cookie -> fq_flush_finish_cnt );
125
+ unsigned int idx ;
91
126
92
127
assert_spin_locked (& fq -> lock );
93
128
@@ -97,124 +132,125 @@ static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq)
97
132
break ;
98
133
99
134
put_pages_list (& fq -> entries [idx ].freelist );
100
- free_iova_fast (iovad ,
135
+ free_iova_fast (& cookie -> iovad ,
101
136
fq -> entries [idx ].iova_pfn ,
102
137
fq -> entries [idx ].pages );
103
138
104
139
fq -> head = (fq -> head + 1 ) % IOVA_FQ_SIZE ;
105
140
}
106
141
}
107
142
108
- static void iova_domain_flush (struct iova_domain * iovad )
143
+ static void fq_flush_iotlb (struct iommu_dma_cookie * cookie )
109
144
{
110
- atomic64_inc (& iovad -> fq_flush_start_cnt );
111
- iovad -> fq_domain -> ops -> flush_iotlb_all (iovad -> fq_domain );
112
- atomic64_inc (& iovad -> fq_flush_finish_cnt );
145
+ atomic64_inc (& cookie -> fq_flush_start_cnt );
146
+ cookie -> fq_domain -> ops -> flush_iotlb_all (cookie -> fq_domain );
147
+ atomic64_inc (& cookie -> fq_flush_finish_cnt );
113
148
}
114
149
115
150
static void fq_flush_timeout (struct timer_list * t )
116
151
{
117
- struct iova_domain * iovad = from_timer (iovad , t , fq_timer );
152
+ struct iommu_dma_cookie * cookie = from_timer (cookie , t , fq_timer );
118
153
int cpu ;
119
154
120
- atomic_set (& iovad -> fq_timer_on , 0 );
121
- iova_domain_flush ( iovad );
155
+ atomic_set (& cookie -> fq_timer_on , 0 );
156
+ fq_flush_iotlb ( cookie );
122
157
123
158
for_each_possible_cpu (cpu ) {
124
159
unsigned long flags ;
125
160
struct iova_fq * fq ;
126
161
127
- fq = per_cpu_ptr (iovad -> fq , cpu );
162
+ fq = per_cpu_ptr (cookie -> fq , cpu );
128
163
spin_lock_irqsave (& fq -> lock , flags );
129
- fq_ring_free (iovad , fq );
164
+ fq_ring_free (cookie , fq );
130
165
spin_unlock_irqrestore (& fq -> lock , flags );
131
166
}
132
167
}
133
168
134
- void queue_iova (struct iova_domain * iovad ,
169
+ static void queue_iova (struct iommu_dma_cookie * cookie ,
135
170
unsigned long pfn , unsigned long pages ,
136
171
struct list_head * freelist )
137
172
{
138
173
struct iova_fq * fq ;
139
174
unsigned long flags ;
140
- unsigned idx ;
175
+ unsigned int idx ;
141
176
142
177
/*
143
178
* Order against the IOMMU driver's pagetable update from unmapping
144
- * @pte, to guarantee that iova_domain_flush () observes that if called
179
+ * @pte, to guarantee that fq_flush_iotlb () observes that if called
145
180
* from a different CPU before we release the lock below. Full barrier
146
181
* so it also pairs with iommu_dma_init_fq() to avoid seeing partially
147
182
* written fq state here.
148
183
*/
149
184
smp_mb ();
150
185
151
- fq = raw_cpu_ptr (iovad -> fq );
186
+ fq = raw_cpu_ptr (cookie -> fq );
152
187
spin_lock_irqsave (& fq -> lock , flags );
153
188
154
189
/*
155
190
* First remove all entries from the flush queue that have already been
156
191
* flushed out on another CPU. This makes the fq_full() check below less
157
192
* likely to be true.
158
193
*/
159
- fq_ring_free (iovad , fq );
194
+ fq_ring_free (cookie , fq );
160
195
161
196
if (fq_full (fq )) {
162
- iova_domain_flush ( iovad );
163
- fq_ring_free (iovad , fq );
197
+ fq_flush_iotlb ( cookie );
198
+ fq_ring_free (cookie , fq );
164
199
}
165
200
166
201
idx = fq_ring_add (fq );
167
202
168
203
fq -> entries [idx ].iova_pfn = pfn ;
169
204
fq -> entries [idx ].pages = pages ;
170
- fq -> entries [idx ].counter = atomic64_read (& iovad -> fq_flush_start_cnt );
205
+ fq -> entries [idx ].counter = atomic64_read (& cookie -> fq_flush_start_cnt );
171
206
list_splice (freelist , & fq -> entries [idx ].freelist );
172
207
173
208
spin_unlock_irqrestore (& fq -> lock , flags );
174
209
175
210
/* Avoid false sharing as much as possible. */
176
- if (!atomic_read (& iovad -> fq_timer_on ) &&
177
- !atomic_xchg (& iovad -> fq_timer_on , 1 ))
178
- mod_timer (& iovad -> fq_timer ,
211
+ if (!atomic_read (& cookie -> fq_timer_on ) &&
212
+ !atomic_xchg (& cookie -> fq_timer_on , 1 ))
213
+ mod_timer (& cookie -> fq_timer ,
179
214
jiffies + msecs_to_jiffies (IOVA_FQ_TIMEOUT ));
180
215
}
181
216
182
- static void free_iova_flush_queue (struct iova_domain * iovad )
217
+ static void iommu_dma_free_fq (struct iommu_dma_cookie * cookie )
183
218
{
184
219
int cpu , idx ;
185
220
186
- if (!iovad -> fq )
221
+ if (!cookie -> fq )
187
222
return ;
188
223
189
- del_timer_sync (& iovad -> fq_timer );
190
- /*
191
- * This code runs when the iova_domain is being detroyed, so don't
192
- * bother to free iovas, just free any remaining pagetable pages.
193
- */
224
+ del_timer_sync (& cookie -> fq_timer );
225
+ /* The IOVAs will be torn down separately, so just free our queued pages */
194
226
for_each_possible_cpu (cpu ) {
195
- struct iova_fq * fq = per_cpu_ptr (iovad -> fq , cpu );
227
+ struct iova_fq * fq = per_cpu_ptr (cookie -> fq , cpu );
196
228
197
229
fq_ring_for_each (idx , fq )
198
230
put_pages_list (& fq -> entries [idx ].freelist );
199
231
}
200
232
201
- free_percpu (iovad -> fq );
202
-
203
- iovad -> fq = NULL ;
204
- iovad -> fq_domain = NULL ;
233
+ free_percpu (cookie -> fq );
205
234
}
206
235
207
- int init_iova_flush_queue (struct iova_domain * iovad , struct iommu_domain * fq_domain )
236
+ /* sysfs updates are serialised by the mutex of the group owning @domain */
237
+ int iommu_dma_init_fq (struct iommu_domain * domain )
208
238
{
239
+ struct iommu_dma_cookie * cookie = domain -> iova_cookie ;
209
240
struct iova_fq __percpu * queue ;
210
241
int i , cpu ;
211
242
212
- atomic64_set (& iovad -> fq_flush_start_cnt , 0 );
213
- atomic64_set (& iovad -> fq_flush_finish_cnt , 0 );
243
+ if (cookie -> fq_domain )
244
+ return 0 ;
245
+
246
+ atomic64_set (& cookie -> fq_flush_start_cnt , 0 );
247
+ atomic64_set (& cookie -> fq_flush_finish_cnt , 0 );
214
248
215
249
queue = alloc_percpu (struct iova_fq );
216
- if (!queue )
250
+ if (!queue ) {
251
+ pr_warn ("iova flush queue initialization failed\n" );
217
252
return - ENOMEM ;
253
+ }
218
254
219
255
for_each_possible_cpu (cpu ) {
220
256
struct iova_fq * fq = per_cpu_ptr (queue , cpu );
@@ -228,12 +264,16 @@ int init_iova_flush_queue(struct iova_domain *iovad, struct iommu_domain *fq_dom
228
264
INIT_LIST_HEAD (& fq -> entries [i ].freelist );
229
265
}
230
266
231
- iovad -> fq_domain = fq_domain ;
232
- iovad -> fq = queue ;
233
-
234
- timer_setup (& iovad -> fq_timer , fq_flush_timeout , 0 );
235
- atomic_set (& iovad -> fq_timer_on , 0 );
267
+ cookie -> fq = queue ;
236
268
269
+ timer_setup (& cookie -> fq_timer , fq_flush_timeout , 0 );
270
+ atomic_set (& cookie -> fq_timer_on , 0 );
271
+ /*
272
+ * Prevent incomplete fq state being observable. Pairs with path from
273
+ * __iommu_dma_unmap() through iommu_dma_free_iova() to queue_iova()
274
+ */
275
+ smp_wmb ();
276
+ WRITE_ONCE (cookie -> fq_domain , domain );
237
277
return 0 ;
238
278
}
239
279
@@ -318,7 +358,7 @@ void iommu_put_dma_cookie(struct iommu_domain *domain)
318
358
return ;
319
359
320
360
if (cookie -> type == IOMMU_DMA_IOVA_COOKIE && cookie -> iovad .granule ) {
321
- free_iova_flush_queue ( & cookie -> iovad );
361
+ iommu_dma_free_fq ( cookie );
322
362
put_iova_domain (& cookie -> iovad );
323
363
}
324
364
@@ -467,29 +507,6 @@ static bool dev_use_swiotlb(struct device *dev)
467
507
return IS_ENABLED (CONFIG_SWIOTLB ) && dev_is_untrusted (dev );
468
508
}
469
509
470
- /* sysfs updates are serialised by the mutex of the group owning @domain */
471
- int iommu_dma_init_fq (struct iommu_domain * domain )
472
- {
473
- struct iommu_dma_cookie * cookie = domain -> iova_cookie ;
474
- int ret ;
475
-
476
- if (cookie -> fq_domain )
477
- return 0 ;
478
-
479
- ret = init_iova_flush_queue (& cookie -> iovad , domain );
480
- if (ret ) {
481
- pr_warn ("iova flush queue initialization failed\n" );
482
- return ret ;
483
- }
484
- /*
485
- * Prevent incomplete iovad->fq being observable. Pairs with path from
486
- * __iommu_dma_unmap() through iommu_dma_free_iova() to queue_iova()
487
- */
488
- smp_wmb ();
489
- WRITE_ONCE (cookie -> fq_domain , domain );
490
- return 0 ;
491
- }
492
-
493
510
/**
494
511
* iommu_dma_init_domain - Initialise a DMA mapping domain
495
512
* @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
@@ -620,7 +637,7 @@ static void iommu_dma_free_iova(struct iommu_dma_cookie *cookie,
620
637
if (cookie -> type == IOMMU_DMA_MSI_COOKIE )
621
638
cookie -> msi_iova -= size ;
622
639
else if (gather && gather -> queued )
623
- queue_iova (iovad , iova_pfn (iovad , iova ),
640
+ queue_iova (cookie , iova_pfn (iovad , iova ),
624
641
size >> iova_shift (iovad ),
625
642
& gather -> freelist );
626
643
else
0 commit comments