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 1024
54 vhost_user_iotlb_cache_remove_all(struct vhost_virtqueue *vq)
56 struct vhost_iotlb_entry *node, *temp_node;
58 rte_rwlock_write_lock(&vq->iotlb_lock);
60 TAILQ_FOREACH_SAFE(node, &vq->iotlb_list, next, temp_node) {
61 TAILQ_REMOVE(&vq->iotlb_list, node, next);
62 rte_mempool_put(vq->iotlb_pool, node);
65 vq->iotlb_cache_nr = 0;
67 rte_rwlock_write_unlock(&vq->iotlb_lock);
71 vhost_user_iotlb_cache_random_evict(struct vhost_virtqueue *vq)
73 struct vhost_iotlb_entry *node, *temp_node;
76 rte_rwlock_write_lock(&vq->iotlb_lock);
78 entry_idx = rte_rand() % vq->iotlb_cache_nr;
80 TAILQ_FOREACH_SAFE(node, &vq->iotlb_list, next, temp_node) {
82 TAILQ_REMOVE(&vq->iotlb_list, node, next);
83 rte_mempool_put(vq->iotlb_pool, node);
90 rte_rwlock_write_unlock(&vq->iotlb_lock);
94 vhost_user_iotlb_cache_insert(struct vhost_virtqueue *vq, uint64_t iova,
95 uint64_t uaddr, uint64_t size, uint8_t perm)
97 struct vhost_iotlb_entry *node, *new_node;
100 ret = rte_mempool_get(vq->iotlb_pool, (void **)&new_node);
102 RTE_LOG(DEBUG, VHOST_CONFIG, "IOTLB pool empty, evict one entry\n");
103 vhost_user_iotlb_cache_random_evict(vq);
104 ret = rte_mempool_get(vq->iotlb_pool, (void **)&new_node);
106 RTE_LOG(ERR, VHOST_CONFIG, "IOTLB pool still empty, failure\n");
111 new_node->iova = iova;
112 new_node->uaddr = uaddr;
113 new_node->size = size;
114 new_node->perm = perm;
116 rte_rwlock_write_lock(&vq->iotlb_lock);
118 TAILQ_FOREACH(node, &vq->iotlb_list, next) {
120 * Entries must be invalidated before being updated.
121 * So if iova already in list, assume identical.
123 if (node->iova == new_node->iova) {
124 rte_mempool_put(vq->iotlb_pool, new_node);
126 } else if (node->iova > new_node->iova) {
127 TAILQ_INSERT_BEFORE(node, new_node, next);
128 vq->iotlb_cache_nr++;
133 TAILQ_INSERT_TAIL(&vq->iotlb_list, new_node, next);
134 vq->iotlb_cache_nr++;
137 rte_rwlock_write_unlock(&vq->iotlb_lock);
141 vhost_user_iotlb_cache_remove(struct vhost_virtqueue *vq,
142 uint64_t iova, uint64_t size)
144 struct vhost_iotlb_entry *node, *temp_node;
149 rte_rwlock_write_lock(&vq->iotlb_lock);
151 TAILQ_FOREACH_SAFE(node, &vq->iotlb_list, next, temp_node) {
153 if (unlikely(iova + size < node->iova))
156 if (iova < node->iova + node->size) {
157 TAILQ_REMOVE(&vq->iotlb_list, node, next);
158 rte_mempool_put(vq->iotlb_pool, node);
159 vq->iotlb_cache_nr--;
163 rte_rwlock_write_unlock(&vq->iotlb_lock);
167 vhost_user_iotlb_cache_find(struct vhost_virtqueue *vq, uint64_t iova,
168 uint64_t *size, uint8_t perm)
170 struct vhost_iotlb_entry *node;
171 uint64_t offset, vva = 0, mapped = 0;
173 if (unlikely(!*size))
176 TAILQ_FOREACH(node, &vq->iotlb_list, next) {
177 /* List sorted by iova */
178 if (unlikely(iova < node->iova))
181 if (iova >= node->iova + node->size)
184 if (unlikely((perm & node->perm) != perm)) {
189 offset = iova - node->iova;
191 vva = node->uaddr + offset;
193 mapped += node->size - offset;
194 iova = node->iova + node->size;
201 /* Only part of the requested chunk is mapped */
202 if (unlikely(mapped < *size))
209 vhost_user_iotlb_init(struct virtio_net *dev, int vq_index)
211 char pool_name[RTE_MEMPOOL_NAMESIZE];
212 struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
213 int ret = -1, socket;
215 if (vq->iotlb_pool) {
217 * The cache has already been initialized,
218 * just drop all entries
220 vhost_user_iotlb_cache_remove_all(vq);
224 #ifdef RTE_LIBRTE_VHOST_NUMA
225 ret = get_mempolicy(&socket, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR);
230 rte_rwlock_init(&vq->iotlb_lock);
232 TAILQ_INIT(&vq->iotlb_list);
234 snprintf(pool_name, sizeof(pool_name), "iotlb_cache_%d_%d",
237 /* If already created, free it and recreate */
238 vq->iotlb_pool = rte_mempool_lookup(pool_name);
240 rte_mempool_free(vq->iotlb_pool);
242 vq->iotlb_pool = rte_mempool_create(pool_name,
243 IOTLB_CACHE_SIZE, sizeof(struct vhost_iotlb_entry), 0,
244 0, 0, NULL, NULL, NULL, socket,
245 MEMPOOL_F_NO_CACHE_ALIGN |
248 if (!vq->iotlb_pool) {
249 RTE_LOG(ERR, VHOST_CONFIG,
250 "Failed to create IOTLB cache pool (%s)\n",
255 vq->iotlb_cache_nr = 0;