Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (c) 2017 Red Hat, Inc.
3 : : */
4 : :
5 : : #ifdef RTE_LIBRTE_VHOST_NUMA
6 : : #include <numaif.h>
7 : : #endif
8 : :
9 : : #include <rte_tailq.h>
10 : :
11 : : #include "iotlb.h"
12 : : #include "vhost.h"
13 : :
14 : : struct vhost_iotlb {
15 : : rte_rwlock_t pending_lock;
16 : : struct vhost_iotlb_entry *pool;
17 : : TAILQ_HEAD(, vhost_iotlb_entry) list;
18 : : TAILQ_HEAD(, vhost_iotlb_entry) pending_list;
19 : : int cache_nr;
20 : : rte_spinlock_t free_lock;
21 : : SLIST_HEAD(, vhost_iotlb_entry) free_list;
22 : : };
23 : :
24 : : struct vhost_iotlb_entry {
25 : : TAILQ_ENTRY(vhost_iotlb_entry) next;
26 : : SLIST_ENTRY(vhost_iotlb_entry) next_free;
27 : :
28 : : uint64_t iova;
29 : : uint64_t uaddr;
30 : : uint64_t uoffset;
31 : : uint64_t size;
32 : : uint8_t page_shift;
33 : : uint8_t perm;
34 : : };
35 : :
36 : : #define IOTLB_CACHE_SIZE 2048
37 : :
38 : : static void
39 : : vhost_user_iotlb_remove_notify(struct virtio_net *dev, struct vhost_iotlb_entry *entry)
40 : : {
41 : 0 : if (dev->backend_ops->iotlb_remove_notify == NULL)
42 : : return;
43 : :
44 : 0 : dev->backend_ops->iotlb_remove_notify(entry->uaddr, entry->uoffset, entry->size);
45 : : }
46 : :
47 : : static bool
48 : 0 : vhost_user_iotlb_share_page(struct vhost_iotlb_entry *a, struct vhost_iotlb_entry *b)
49 : : {
50 : : uint64_t a_start, a_end, b_start;
51 : :
52 [ # # ]: 0 : if (a == NULL || b == NULL)
53 : : return false;
54 : :
55 : 0 : a_start = a->uaddr + a->uoffset;
56 : 0 : b_start = b->uaddr + b->uoffset;
57 : :
58 : : /* Assumes entry a lower than entry b */
59 : : RTE_ASSERT(a_start < b_start);
60 : 0 : a_end = RTE_ALIGN_CEIL(a_start + a->size, RTE_BIT64(a->page_shift));
61 : 0 : b_start = RTE_ALIGN_FLOOR(b_start, RTE_BIT64(b->page_shift));
62 : :
63 : 0 : return a_end > b_start;
64 : : }
65 : :
66 : : static void
67 : : vhost_user_iotlb_set_dump(struct virtio_net *dev, struct vhost_iotlb_entry *node)
68 : : {
69 : : uint64_t start;
70 : :
71 : 0 : start = node->uaddr + node->uoffset;
72 : 0 : mem_set_dump(dev, (void *)(uintptr_t)start, node->size, true, RTE_BIT64(node->page_shift));
73 : : }
74 : :
75 : : static void
76 : 0 : vhost_user_iotlb_clear_dump(struct virtio_net *dev, struct vhost_iotlb_entry *node,
77 : : struct vhost_iotlb_entry *prev, struct vhost_iotlb_entry *next)
78 : : {
79 : : uint64_t start, end;
80 : :
81 : 0 : start = node->uaddr + node->uoffset;
82 : 0 : end = start + node->size;
83 : :
84 : : /* Skip first page if shared with previous entry. */
85 [ # # ]: 0 : if (vhost_user_iotlb_share_page(prev, node))
86 : 0 : start = RTE_ALIGN_CEIL(start, RTE_BIT64(node->page_shift));
87 : :
88 : : /* Skip last page if shared with next entry. */
89 [ # # ]: 0 : if (vhost_user_iotlb_share_page(node, next))
90 : 0 : end = RTE_ALIGN_FLOOR(end, RTE_BIT64(node->page_shift));
91 : :
92 [ # # ]: 0 : if (end > start)
93 : 0 : mem_set_dump(dev, (void *)(uintptr_t)start, end - start, false,
94 : 0 : RTE_BIT64(node->page_shift));
95 : 0 : }
96 : :
97 : : static struct vhost_iotlb_entry *
98 : 0 : vhost_user_iotlb_pool_get(struct virtio_net *dev, int asid)
99 : : {
100 : : struct vhost_iotlb_entry *node;
101 : :
102 : 0 : rte_spinlock_lock(&dev->iotlb[asid]->free_lock);
103 : 0 : node = SLIST_FIRST(&dev->iotlb[asid]->free_list);
104 [ # # ]: 0 : if (node != NULL)
105 : 0 : SLIST_REMOVE_HEAD(&dev->iotlb[asid]->free_list, next_free);
106 : 0 : rte_spinlock_unlock(&dev->iotlb[asid]->free_lock);
107 : 0 : return node;
108 : : }
109 : :
110 : : static void
111 : 0 : vhost_user_iotlb_pool_put(struct virtio_net *dev, int asid, struct vhost_iotlb_entry *node)
112 : : {
113 : 0 : rte_spinlock_lock(&dev->iotlb[asid]->free_lock);
114 : 0 : SLIST_INSERT_HEAD(&dev->iotlb[asid]->free_list, node, next_free);
115 : 0 : rte_spinlock_unlock(&dev->iotlb[asid]->free_lock);
116 : 0 : }
117 : :
118 : : static void
119 : : vhost_user_iotlb_cache_random_evict(struct virtio_net *dev, int asid);
120 : :
121 : : static void
122 : 0 : vhost_user_iotlb_pending_remove_all(struct virtio_net *dev, int asid)
123 : : {
124 : : struct vhost_iotlb_entry *node, *temp_node;
125 : :
126 : 0 : rte_rwlock_write_lock(&dev->iotlb[asid]->pending_lock);
127 : :
128 [ # # ]: 0 : RTE_TAILQ_FOREACH_SAFE(node, &dev->iotlb[asid]->pending_list, next, temp_node) {
129 [ # # ]: 0 : TAILQ_REMOVE(&dev->iotlb[asid]->pending_list, node, next);
130 : 0 : vhost_user_iotlb_pool_put(dev, asid, node);
131 : : }
132 : :
133 : 0 : rte_rwlock_write_unlock(&dev->iotlb[asid]->pending_lock);
134 : 0 : }
135 : :
136 : : bool
137 : 0 : vhost_user_iotlb_pending_miss(struct virtio_net *dev, int asid, uint64_t iova, uint8_t perm)
138 : : {
139 : : struct vhost_iotlb_entry *node;
140 : : bool found = false;
141 : :
142 : 0 : rte_rwlock_read_lock(&dev->iotlb[asid]->pending_lock);
143 : :
144 [ # # ]: 0 : TAILQ_FOREACH(node, &dev->iotlb[asid]->pending_list, next) {
145 [ # # # # ]: 0 : if ((node->iova == iova) && (node->perm == perm)) {
146 : : found = true;
147 : : break;
148 : : }
149 : : }
150 : :
151 : : rte_rwlock_read_unlock(&dev->iotlb[asid]->pending_lock);
152 : :
153 : 0 : return found;
154 : : }
155 : :
156 : : void
157 : 0 : vhost_user_iotlb_pending_insert(struct virtio_net *dev, int asid, uint64_t iova, uint8_t perm)
158 : : {
159 : : struct vhost_iotlb_entry *node;
160 : :
161 : 0 : node = vhost_user_iotlb_pool_get(dev, asid);
162 [ # # ]: 0 : if (node == NULL) {
163 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
164 : : "IOTLB pool empty, clear entries for pending insertion");
165 [ # # ]: 0 : if (!TAILQ_EMPTY(&dev->iotlb[asid]->pending_list))
166 : 0 : vhost_user_iotlb_pending_remove_all(dev, asid);
167 : : else
168 : 0 : vhost_user_iotlb_cache_random_evict(dev, asid);
169 : 0 : node = vhost_user_iotlb_pool_get(dev, asid);
170 [ # # ]: 0 : if (node == NULL) {
171 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
172 : : "IOTLB pool still empty, pending insertion failure");
173 : 0 : return;
174 : : }
175 : : }
176 : :
177 : 0 : node->iova = iova;
178 : 0 : node->perm = perm;
179 : :
180 : 0 : rte_rwlock_write_lock(&dev->iotlb[asid]->pending_lock);
181 : :
182 : 0 : TAILQ_INSERT_TAIL(&dev->iotlb[asid]->pending_list, node, next);
183 : :
184 : 0 : rte_rwlock_write_unlock(&dev->iotlb[asid]->pending_lock);
185 : : }
186 : :
187 : : void
188 : 0 : vhost_user_iotlb_pending_remove(struct virtio_net *dev, int asid,
189 : : uint64_t iova, uint64_t size, uint8_t perm)
190 : : {
191 : : struct vhost_iotlb_entry *node, *temp_node;
192 : :
193 : 0 : rte_rwlock_write_lock(&dev->iotlb[asid]->pending_lock);
194 : :
195 [ # # ]: 0 : RTE_TAILQ_FOREACH_SAFE(node, &dev->iotlb[asid]->pending_list, next,
196 : : temp_node) {
197 [ # # ]: 0 : if (node->iova < iova)
198 : 0 : continue;
199 [ # # ]: 0 : if (node->iova >= iova + size)
200 : 0 : continue;
201 [ # # ]: 0 : if ((node->perm & perm) != node->perm)
202 : 0 : continue;
203 [ # # ]: 0 : TAILQ_REMOVE(&dev->iotlb[asid]->pending_list, node, next);
204 : 0 : vhost_user_iotlb_pool_put(dev, asid, node);
205 : : }
206 : :
207 : 0 : rte_rwlock_write_unlock(&dev->iotlb[asid]->pending_lock);
208 : 0 : }
209 : :
210 : : static void
211 : 0 : vhost_user_iotlb_cache_remove_all(struct virtio_net *dev, int asid)
212 : : {
213 : : struct vhost_iotlb_entry *node, *temp_node;
214 : :
215 : : vhost_user_iotlb_wr_lock_all(dev);
216 : :
217 [ # # ]: 0 : RTE_TAILQ_FOREACH_SAFE(node, &dev->iotlb[asid]->list, next, temp_node) {
218 : 0 : vhost_user_iotlb_clear_dump(dev, node, NULL, NULL);
219 : :
220 [ # # # # ]: 0 : TAILQ_REMOVE(&dev->iotlb[asid]->list, node, next);
221 : : vhost_user_iotlb_remove_notify(dev, node);
222 : 0 : vhost_user_iotlb_pool_put(dev, asid, node);
223 : : }
224 : :
225 : 0 : dev->iotlb[asid]->cache_nr = 0;
226 : :
227 : : vhost_user_iotlb_wr_unlock_all(dev);
228 : 0 : }
229 : :
230 : : static void
231 : 0 : vhost_user_iotlb_cache_random_evict(struct virtio_net *dev, int asid)
232 : : {
233 : : struct vhost_iotlb_entry *node, *temp_node, *prev_node = NULL;
234 : : int entry_idx;
235 : :
236 : : vhost_user_iotlb_wr_lock_all(dev);
237 : :
238 : 0 : entry_idx = rte_rand() % dev->iotlb[asid]->cache_nr;
239 : :
240 [ # # ]: 0 : RTE_TAILQ_FOREACH_SAFE(node, &dev->iotlb[asid]->list, next, temp_node) {
241 [ # # ]: 0 : if (!entry_idx) {
242 : : struct vhost_iotlb_entry *next_node = RTE_TAILQ_NEXT(node, next);
243 : :
244 : 0 : vhost_user_iotlb_clear_dump(dev, node, prev_node, next_node);
245 : :
246 [ # # # # ]: 0 : TAILQ_REMOVE(&dev->iotlb[asid]->list, node, next);
247 : : vhost_user_iotlb_remove_notify(dev, node);
248 : 0 : vhost_user_iotlb_pool_put(dev, asid, node);
249 : 0 : dev->iotlb[asid]->cache_nr--;
250 : 0 : break;
251 : : }
252 : : prev_node = node;
253 : 0 : entry_idx--;
254 : : }
255 : :
256 : : vhost_user_iotlb_wr_unlock_all(dev);
257 : 0 : }
258 : :
259 : : void
260 : 0 : vhost_user_iotlb_cache_insert(struct virtio_net *dev, int asid, uint64_t iova, uint64_t uaddr,
261 : : uint64_t uoffset, uint64_t size, uint64_t page_size, uint8_t perm)
262 : : {
263 : : struct vhost_iotlb_entry *node, *new_node;
264 : :
265 : 0 : new_node = vhost_user_iotlb_pool_get(dev, asid);
266 [ # # ]: 0 : if (new_node == NULL) {
267 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
268 : : "IOTLB pool empty, clear entries for cache insertion");
269 [ # # ]: 0 : if (!TAILQ_EMPTY(&dev->iotlb[asid]->list))
270 : 0 : vhost_user_iotlb_cache_random_evict(dev, asid);
271 : : else
272 : 0 : vhost_user_iotlb_pending_remove_all(dev, asid);
273 : 0 : new_node = vhost_user_iotlb_pool_get(dev, asid);
274 [ # # ]: 0 : if (new_node == NULL) {
275 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
276 : : "IOTLB pool still empty, cache insertion failed");
277 : 0 : return;
278 : : }
279 : : }
280 : :
281 : 0 : new_node->iova = iova;
282 : 0 : new_node->uaddr = uaddr;
283 : 0 : new_node->uoffset = uoffset;
284 : 0 : new_node->size = size;
285 : 0 : new_node->page_shift = rte_ctz64(page_size);
286 : 0 : new_node->perm = perm;
287 : :
288 : : vhost_user_iotlb_wr_lock_all(dev);
289 : :
290 [ # # ]: 0 : TAILQ_FOREACH(node, &dev->iotlb[asid]->list, next) {
291 : : /*
292 : : * Entries must be invalidated before being updated.
293 : : * So if iova already in list, assume identical.
294 : : */
295 [ # # ]: 0 : if (node->iova == new_node->iova) {
296 : 0 : vhost_user_iotlb_pool_put(dev, asid, new_node);
297 : 0 : goto unlock;
298 [ # # ]: 0 : } else if (node->iova > new_node->iova) {
299 : : vhost_user_iotlb_set_dump(dev, new_node);
300 : :
301 : 0 : TAILQ_INSERT_BEFORE(node, new_node, next);
302 : 0 : dev->iotlb[asid]->cache_nr++;
303 : 0 : goto unlock;
304 : : }
305 : : }
306 : :
307 : : vhost_user_iotlb_set_dump(dev, new_node);
308 : :
309 : 0 : TAILQ_INSERT_TAIL(&dev->iotlb[asid]->list, new_node, next);
310 : 0 : dev->iotlb[asid]->cache_nr++;
311 : :
312 : 0 : unlock:
313 : 0 : vhost_user_iotlb_pending_remove(dev, asid, iova, size, perm);
314 : :
315 : : vhost_user_iotlb_wr_unlock_all(dev);
316 : : }
317 : :
318 : : void
319 : 0 : vhost_user_iotlb_cache_remove(struct virtio_net *dev, int asid, uint64_t iova, uint64_t size)
320 : : {
321 : : struct vhost_iotlb_entry *node, *temp_node, *prev_node = NULL;
322 : :
323 [ # # ]: 0 : if (unlikely(!size))
324 : : return;
325 : :
326 : : vhost_user_iotlb_wr_lock_all(dev);
327 : :
328 [ # # ]: 0 : RTE_TAILQ_FOREACH_SAFE(node, &dev->iotlb[asid]->list, next, temp_node) {
329 : : /* Sorted list */
330 [ # # ]: 0 : if (unlikely(iova + size < node->iova))
331 : : break;
332 : :
333 [ # # ]: 0 : if (iova < node->iova + node->size) {
334 : : struct vhost_iotlb_entry *next_node = RTE_TAILQ_NEXT(node, next);
335 : :
336 : 0 : vhost_user_iotlb_clear_dump(dev, node, prev_node, next_node);
337 : :
338 [ # # # # ]: 0 : TAILQ_REMOVE(&dev->iotlb[asid]->list, node, next);
339 : : vhost_user_iotlb_remove_notify(dev, node);
340 : 0 : vhost_user_iotlb_pool_put(dev, asid, node);
341 : 0 : dev->iotlb[asid]->cache_nr--;
342 : : } else {
343 : : prev_node = node;
344 : : }
345 : : }
346 : :
347 : : vhost_user_iotlb_wr_unlock_all(dev);
348 : : }
349 : :
350 : : uint64_t
351 : 0 : vhost_user_iotlb_cache_find(struct virtio_net *dev, int asid,
352 : : uint64_t iova, uint64_t *size, uint8_t perm)
353 : : {
354 : : struct vhost_iotlb_entry *node;
355 : : uint64_t offset, vva = 0, mapped = 0;
356 : :
357 [ # # ]: 0 : if (unlikely(!*size))
358 : 0 : goto out;
359 : :
360 [ # # ]: 0 : TAILQ_FOREACH(node, &dev->iotlb[asid]->list, next) {
361 : : /* List sorted by iova */
362 [ # # ]: 0 : if (unlikely(iova < node->iova))
363 : : break;
364 : :
365 [ # # ]: 0 : if (iova >= node->iova + node->size)
366 : 0 : continue;
367 : :
368 [ # # ]: 0 : if (unlikely((perm & node->perm) != perm)) {
369 : : vva = 0;
370 : : break;
371 : : }
372 : :
373 : 0 : offset = iova - node->iova;
374 [ # # ]: 0 : if (!vva)
375 : 0 : vva = node->uaddr + node->uoffset + offset;
376 : :
377 : 0 : mapped += node->size - offset;
378 : : iova = node->iova + node->size;
379 : :
380 [ # # ]: 0 : if (mapped >= *size)
381 : : break;
382 : : }
383 : :
384 : 0 : out:
385 : : /* Only part of the requested chunk is mapped */
386 [ # # ]: 0 : if (unlikely(mapped < *size))
387 : 0 : *size = mapped;
388 : :
389 : 0 : return vva;
390 : : }
391 : :
392 : : void
393 : 0 : vhost_user_iotlb_flush_all(struct virtio_net *dev, int asid)
394 : : {
395 : 0 : vhost_user_iotlb_cache_remove_all(dev, asid);
396 : 0 : vhost_user_iotlb_pending_remove_all(dev, asid);
397 : 0 : }
398 : :
399 : : static int
400 : 0 : vhost_user_iotlb_init_one(struct virtio_net *dev, int asid)
401 : : {
402 : : unsigned int i;
403 : 0 : int socket = 0;
404 : :
405 [ # # ]: 0 : if (dev->iotlb[asid] != NULL) {
406 [ # # ]: 0 : if (dev->iotlb[asid]->pool != NULL) {
407 : : /*
408 : : * The cache has already been initialized,
409 : : * just drop all cached and pending entries.
410 : : */
411 : 0 : vhost_user_iotlb_flush_all(dev, asid);
412 : 0 : rte_free(dev->iotlb[asid]->pool);
413 : : }
414 : 0 : rte_free(dev->iotlb[asid]);
415 : : }
416 : :
417 : : #ifdef RTE_LIBRTE_VHOST_NUMA
418 [ # # ]: 0 : if (get_mempolicy(&socket, NULL, 0, dev, MPOL_F_NODE | MPOL_F_ADDR) != 0)
419 : 0 : socket = 0;
420 : : #endif
421 : :
422 : 0 : dev->iotlb[asid] = rte_malloc_socket("iotlb", sizeof(struct vhost_iotlb), 0, socket);
423 [ # # ]: 0 : if (!dev->iotlb[asid]) {
424 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to allocate IOTLB");
425 : 0 : return -1;
426 : : }
427 : :
428 : : rte_spinlock_init(&dev->iotlb[asid]->free_lock);
429 : : rte_rwlock_init(&dev->iotlb[asid]->pending_lock);
430 : :
431 : 0 : SLIST_INIT(&dev->iotlb[asid]->free_list);
432 : 0 : TAILQ_INIT(&dev->iotlb[asid]->list);
433 : 0 : TAILQ_INIT(&dev->iotlb[asid]->pending_list);
434 : :
435 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_SUPPORT_IOMMU) {
436 : 0 : dev->iotlb[asid]->pool = rte_calloc_socket("iotlb_pool", IOTLB_CACHE_SIZE,
437 : : sizeof(struct vhost_iotlb_entry), 0, socket);
438 [ # # ]: 0 : if (!dev->iotlb[asid]->pool) {
439 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to create IOTLB cache pool");
440 : 0 : goto free_iotlb;
441 : : }
442 [ # # ]: 0 : for (i = 0; i < IOTLB_CACHE_SIZE; i++)
443 : 0 : vhost_user_iotlb_pool_put(dev, asid, &dev->iotlb[asid]->pool[i]);
444 : : }
445 : :
446 : 0 : dev->iotlb[asid]->cache_nr = 0;
447 : :
448 : 0 : return 0;
449 : :
450 : : free_iotlb:
451 : 0 : rte_free(dev->iotlb[asid]);
452 : 0 : dev->iotlb[asid] = NULL;
453 : 0 : return -1;
454 : : }
455 : :
456 : : int
457 : 0 : vhost_user_iotlb_init(struct virtio_net *dev)
458 : : {
459 : : int i;
460 : :
461 [ # # ]: 0 : for (i = 0; i < IOTLB_MAX_ASID; i++)
462 [ # # ]: 0 : if (vhost_user_iotlb_init_one(dev, i) < 0)
463 : 0 : goto fail;
464 : :
465 : : return 0;
466 : : fail:
467 [ # # ]: 0 : while (i--) {
468 : 0 : rte_free(dev->iotlb[i]->pool);
469 : 0 : dev->iotlb[i]->pool = NULL;
470 : 0 : rte_free(dev->iotlb[i]);
471 : 0 : dev->iotlb[i] = NULL;
472 : : }
473 : :
474 : : return -1;
475 : : }
476 : :
477 : : void
478 : 0 : vhost_user_iotlb_destroy(struct virtio_net *dev)
479 : : {
480 : : int i;
481 : :
482 [ # # ]: 0 : for (i = 0; i < IOTLB_MAX_ASID; i++) {
483 [ # # ]: 0 : if (dev->iotlb[i]) {
484 : 0 : rte_free(dev->iotlb[i]->pool);
485 : 0 : dev->iotlb[i]->pool = NULL;
486 : :
487 : 0 : rte_free(dev->iotlb[i]);
488 : 0 : dev->iotlb[i] = NULL;
489 : : }
490 : : }
491 : 0 : }
|