4 * Copyright (c) 2017 Red Hat, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ifdef RTE_LIBRTE_VHOST_NUMA
37 #include <rte_tailq.h>
42 struct vhost_iotlb_entry {
43 TAILQ_ENTRY(vhost_iotlb_entry) next;
51 #define IOTLB_CACHE_SIZE 2048
54 vhost_user_iotlb_cache_random_evict(struct vhost_virtqueue *vq);
57 vhost_user_iotlb_pending_remove_all(struct vhost_virtqueue *vq)
59 struct vhost_iotlb_entry *node, *temp_node;
61 rte_rwlock_write_lock(&vq->iotlb_pending_lock);
63 TAILQ_FOREACH_SAFE(node, &vq->iotlb_pending_list, next, temp_node) {
64 TAILQ_REMOVE(&vq->iotlb_pending_list, node, next);
65 rte_mempool_put(vq->iotlb_pool, node);
68 rte_rwlock_write_unlock(&vq->iotlb_pending_lock);
72 vhost_user_iotlb_pending_miss(struct vhost_virtqueue *vq, uint64_t iova,
75 struct vhost_iotlb_entry *node;
78 rte_rwlock_read_lock(&vq->iotlb_pending_lock);
80 TAILQ_FOREACH(node, &vq->iotlb_pending_list, next) {
81 if ((node->iova == iova) && (node->perm == perm)) {
87 rte_rwlock_read_unlock(&vq->iotlb_pending_lock);
93 vhost_user_iotlb_pending_insert(struct vhost_virtqueue *vq,
94 uint64_t iova, uint8_t perm)
96 struct vhost_iotlb_entry *node;
99 ret = rte_mempool_get(vq->iotlb_pool, (void **)&node);
101 RTE_LOG(DEBUG, VHOST_CONFIG, "IOTLB pool empty, clear entries\n");
102 if (!TAILQ_EMPTY(&vq->iotlb_pending_list))
103 vhost_user_iotlb_pending_remove_all(vq);
105 vhost_user_iotlb_cache_random_evict(vq);
106 ret = rte_mempool_get(vq->iotlb_pool, (void **)&node);
108 RTE_LOG(ERR, VHOST_CONFIG, "IOTLB pool still empty, failure\n");
116 rte_rwlock_write_lock(&vq->iotlb_pending_lock);
118 TAILQ_INSERT_TAIL(&vq->iotlb_pending_list, node, next);
120 rte_rwlock_write_unlock(&vq->iotlb_pending_lock);
124 vhost_user_iotlb_pending_remove(struct vhost_virtqueue *vq,
125 uint64_t iova, uint64_t size, uint8_t perm)
127 struct vhost_iotlb_entry *node, *temp_node;
129 rte_rwlock_write_lock(&vq->iotlb_pending_lock);
131 TAILQ_FOREACH_SAFE(node, &vq->iotlb_pending_list, next, temp_node) {
132 if (node->iova < iova)
134 if (node->iova >= iova + size)
136 if ((node->perm & perm) != node->perm)
138 TAILQ_REMOVE(&vq->iotlb_pending_list, node, next);
139 rte_mempool_put(vq->iotlb_pool, node);
142 rte_rwlock_write_unlock(&vq->iotlb_pending_lock);
146 vhost_user_iotlb_cache_remove_all(struct vhost_virtqueue *vq)
148 struct vhost_iotlb_entry *node, *temp_node;
150 rte_rwlock_write_lock(&vq->iotlb_lock);
152 TAILQ_FOREACH_SAFE(node, &vq->iotlb_list, next, temp_node) {
153 TAILQ_REMOVE(&vq->iotlb_list, node, next);
154 rte_mempool_put(vq->iotlb_pool, node);
157 vq->iotlb_cache_nr = 0;
159 rte_rwlock_write_unlock(&vq->iotlb_lock);
163 vhost_user_iotlb_cache_random_evict(struct vhost_virtqueue *vq)
165 struct vhost_iotlb_entry *node, *temp_node;
168 rte_rwlock_write_lock(&vq->iotlb_lock);
170 entry_idx = rte_rand() % vq->iotlb_cache_nr;
172 TAILQ_FOREACH_SAFE(node, &vq->iotlb_list, next, temp_node) {
174 TAILQ_REMOVE(&vq->iotlb_list, node, next);
175 rte_mempool_put(vq->iotlb_pool, node);
176 vq->iotlb_cache_nr--;
182 rte_rwlock_write_unlock(&vq->iotlb_lock);
186 vhost_user_iotlb_cache_insert(struct vhost_virtqueue *vq, uint64_t iova,
187 uint64_t uaddr, uint64_t size, uint8_t perm)
189 struct vhost_iotlb_entry *node, *new_node;
192 ret = rte_mempool_get(vq->iotlb_pool, (void **)&new_node);
194 RTE_LOG(DEBUG, VHOST_CONFIG, "IOTLB pool empty, clear entries\n");
195 if (!TAILQ_EMPTY(&vq->iotlb_list))
196 vhost_user_iotlb_cache_random_evict(vq);
198 vhost_user_iotlb_pending_remove_all(vq);
199 ret = rte_mempool_get(vq->iotlb_pool, (void **)&new_node);
201 RTE_LOG(ERR, VHOST_CONFIG, "IOTLB pool still empty, failure\n");
206 new_node->iova = iova;
207 new_node->uaddr = uaddr;
208 new_node->size = size;
209 new_node->perm = perm;
211 rte_rwlock_write_lock(&vq->iotlb_lock);
213 TAILQ_FOREACH(node, &vq->iotlb_list, next) {
215 * Entries must be invalidated before being updated.
216 * So if iova already in list, assume identical.
218 if (node->iova == new_node->iova) {
219 rte_mempool_put(vq->iotlb_pool, new_node);
221 } else if (node->iova > new_node->iova) {
222 TAILQ_INSERT_BEFORE(node, new_node, next);
223 vq->iotlb_cache_nr++;
228 TAILQ_INSERT_TAIL(&vq->iotlb_list, new_node, next);
229 vq->iotlb_cache_nr++;
232 vhost_user_iotlb_pending_remove(vq, iova, size, perm);
234 rte_rwlock_write_unlock(&vq->iotlb_lock);
239 vhost_user_iotlb_cache_remove(struct vhost_virtqueue *vq,
240 uint64_t iova, uint64_t size)
242 struct vhost_iotlb_entry *node, *temp_node;
247 rte_rwlock_write_lock(&vq->iotlb_lock);
249 TAILQ_FOREACH_SAFE(node, &vq->iotlb_list, next, temp_node) {
251 if (unlikely(iova + size < node->iova))
254 if (iova < node->iova + node->size) {
255 TAILQ_REMOVE(&vq->iotlb_list, node, next);
256 rte_mempool_put(vq->iotlb_pool, node);
257 vq->iotlb_cache_nr--;
261 rte_rwlock_write_unlock(&vq->iotlb_lock);
265 vhost_user_iotlb_cache_find(struct vhost_virtqueue *vq, uint64_t iova,
266 uint64_t *size, uint8_t perm)
268 struct vhost_iotlb_entry *node;
269 uint64_t offset, vva = 0, mapped = 0;
271 if (unlikely(!*size))
274 TAILQ_FOREACH(node, &vq->iotlb_list, next) {
275 /* List sorted by iova */
276 if (unlikely(iova < node->iova))
279 if (iova >= node->iova + node->size)
282 if (unlikely((perm & node->perm) != perm)) {
287 offset = iova - node->iova;
289 vva = node->uaddr + offset;
291 mapped += node->size - offset;
292 iova = node->iova + node->size;
299 /* Only part of the requested chunk is mapped */
300 if (unlikely(mapped < *size))
307 vhost_user_iotlb_flush_all(struct vhost_virtqueue *vq)
309 vhost_user_iotlb_cache_remove_all(vq);
310 vhost_user_iotlb_pending_remove_all(vq);
314 vhost_user_iotlb_init(struct virtio_net *dev, int vq_index)
316 char pool_name[RTE_MEMPOOL_NAMESIZE];
317 struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
320 if (vq->iotlb_pool) {
322 * The cache has already been initialized,
323 * just drop all cached and pending entries.
325 vhost_user_iotlb_flush_all(vq);
328 #ifdef RTE_LIBRTE_VHOST_NUMA
329 if (get_mempolicy(&socket, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR) != 0)
333 rte_rwlock_init(&vq->iotlb_lock);
334 rte_rwlock_init(&vq->iotlb_pending_lock);
336 TAILQ_INIT(&vq->iotlb_list);
337 TAILQ_INIT(&vq->iotlb_pending_list);
339 snprintf(pool_name, sizeof(pool_name), "iotlb_cache_%d_%d",
342 /* If already created, free it and recreate */
343 vq->iotlb_pool = rte_mempool_lookup(pool_name);
345 rte_mempool_free(vq->iotlb_pool);
347 vq->iotlb_pool = rte_mempool_create(pool_name,
348 IOTLB_CACHE_SIZE, sizeof(struct vhost_iotlb_entry), 0,
349 0, 0, NULL, NULL, NULL, socket,
350 MEMPOOL_F_NO_CACHE_ALIGN |
353 if (!vq->iotlb_pool) {
354 RTE_LOG(ERR, VHOST_CONFIG,
355 "Failed to create IOTLB cache pool (%s)\n",
360 vq->iotlb_cache_nr = 0;