1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (C) 2020 Red Hat, Inc. 3 * Author: Jason Wang <jasowang@redhat.com> 4 * 5 * IOTLB implementation for vhost. 6 */ 7 #include <linux/slab.h> 8 #include <linux/vhost_iotlb.h> 9 #include <linux/module.h> 10 11 #define MOD_VERSION "0.1" 12 #define MOD_DESC "VHOST IOTLB" 13 #define MOD_AUTHOR "Jason Wang <jasowang@redhat.com>" 14 #define MOD_LICENSE "GPL v2" 15 16 #define START(map) ((map)->start) 17 #define LAST(map) ((map)->last) 18 19 INTERVAL_TREE_DEFINE(struct vhost_iotlb_map, 20 rb, __u64, __subtree_last, 21 START, LAST, static inline, vhost_iotlb_itree); 22 23 /** 24 * vhost_iotlb_map_free - remove a map node and free it 25 * @iotlb: the IOTLB 26 * @map: the map that want to be remove and freed 27 */ 28 void vhost_iotlb_map_free(struct vhost_iotlb *iotlb, 29 struct vhost_iotlb_map *map) 30 { 31 vhost_iotlb_itree_remove(map, &iotlb->root); 32 list_del(&map->link); 33 kfree(map); 34 iotlb->nmaps--; 35 } 36 EXPORT_SYMBOL_GPL(vhost_iotlb_map_free); 37 38 /** 39 * vhost_iotlb_add_range_ctx - add a new range to vhost IOTLB 40 * @iotlb: the IOTLB 41 * @start: start of the IOVA range 42 * @last: last of IOVA range 43 * @addr: the address that is mapped to @start 44 * @perm: access permission of this range 45 * @opaque: the opaque pointer for the new mapping 46 * 47 * Returns an error last is smaller than start or memory allocation 48 * fails 49 */ 50 int vhost_iotlb_add_range_ctx(struct vhost_iotlb *iotlb, 51 u64 start, u64 last, 52 u64 addr, unsigned int perm, 53 void *opaque) 54 { 55 struct vhost_iotlb_map *map; 56 57 if (last < start) 58 return -EFAULT; 59 60 /* If the range being mapped is [0, ULONG_MAX], split it into two entries 61 * otherwise its size would overflow u64. 62 */ 63 if (start == 0 && last == ULONG_MAX) { 64 u64 mid = last / 2; 65 66 vhost_iotlb_add_range_ctx(iotlb, start, mid, addr, perm, opaque); 67 addr += mid + 1; 68 start = mid + 1; 69 } 70 71 if (iotlb->limit && 72 iotlb->nmaps == iotlb->limit && 73 iotlb->flags & VHOST_IOTLB_FLAG_RETIRE) { 74 map = list_first_entry(&iotlb->list, typeof(*map), link); 75 vhost_iotlb_map_free(iotlb, map); 76 } 77 78 map = kmalloc(sizeof(*map), GFP_ATOMIC); 79 if (!map) 80 return -ENOMEM; 81 82 map->start = start; 83 map->size = last - start + 1; 84 map->last = last; 85 map->addr = addr; 86 map->perm = perm; 87 map->opaque = opaque; 88 89 iotlb->nmaps++; 90 vhost_iotlb_itree_insert(map, &iotlb->root); 91 92 INIT_LIST_HEAD(&map->link); 93 list_add_tail(&map->link, &iotlb->list); 94 95 return 0; 96 } 97 EXPORT_SYMBOL_GPL(vhost_iotlb_add_range_ctx); 98 99 int vhost_iotlb_add_range(struct vhost_iotlb *iotlb, 100 u64 start, u64 last, 101 u64 addr, unsigned int perm) 102 { 103 return vhost_iotlb_add_range_ctx(iotlb, start, last, 104 addr, perm, NULL); 105 } 106 EXPORT_SYMBOL_GPL(vhost_iotlb_add_range); 107 108 /** 109 * vhost_iotlb_del_range - delete overlapped ranges from vhost IOTLB 110 * @iotlb: the IOTLB 111 * @start: start of the IOVA range 112 * @last: last of IOVA range 113 */ 114 void vhost_iotlb_del_range(struct vhost_iotlb *iotlb, u64 start, u64 last) 115 { 116 struct vhost_iotlb_map *map; 117 118 while ((map = vhost_iotlb_itree_iter_first(&iotlb->root, 119 start, last))) 120 vhost_iotlb_map_free(iotlb, map); 121 } 122 EXPORT_SYMBOL_GPL(vhost_iotlb_del_range); 123 124 /** 125 * vhost_iotlb_alloc - add a new vhost IOTLB 126 * @limit: maximum number of IOTLB entries 127 * @flags: VHOST_IOTLB_FLAG_XXX 128 * 129 * Returns an error is memory allocation fails 130 */ 131 struct vhost_iotlb *vhost_iotlb_alloc(unsigned int limit, unsigned int flags) 132 { 133 struct vhost_iotlb *iotlb = kzalloc(sizeof(*iotlb), GFP_KERNEL); 134 135 if (!iotlb) 136 return NULL; 137 138 iotlb->root = RB_ROOT_CACHED; 139 iotlb->limit = limit; 140 iotlb->nmaps = 0; 141 iotlb->flags = flags; 142 INIT_LIST_HEAD(&iotlb->list); 143 144 return iotlb; 145 } 146 EXPORT_SYMBOL_GPL(vhost_iotlb_alloc); 147 148 /** 149 * vhost_iotlb_reset - reset vhost IOTLB (free all IOTLB entries) 150 * @iotlb: the IOTLB to be reset 151 */ 152 void vhost_iotlb_reset(struct vhost_iotlb *iotlb) 153 { 154 vhost_iotlb_del_range(iotlb, 0ULL, 0ULL - 1); 155 } 156 EXPORT_SYMBOL_GPL(vhost_iotlb_reset); 157 158 /** 159 * vhost_iotlb_free - reset and free vhost IOTLB 160 * @iotlb: the IOTLB to be freed 161 */ 162 void vhost_iotlb_free(struct vhost_iotlb *iotlb) 163 { 164 if (iotlb) { 165 vhost_iotlb_reset(iotlb); 166 kfree(iotlb); 167 } 168 } 169 EXPORT_SYMBOL_GPL(vhost_iotlb_free); 170 171 /** 172 * vhost_iotlb_itree_first - return the first overlapped range 173 * @iotlb: the IOTLB 174 * @start: start of IOVA range 175 * @last: last byte in IOVA range 176 */ 177 struct vhost_iotlb_map * 178 vhost_iotlb_itree_first(struct vhost_iotlb *iotlb, u64 start, u64 last) 179 { 180 return vhost_iotlb_itree_iter_first(&iotlb->root, start, last); 181 } 182 EXPORT_SYMBOL_GPL(vhost_iotlb_itree_first); 183 184 /** 185 * vhost_iotlb_itree_next - return the next overlapped range 186 * @map: the starting map node 187 * @start: start of IOVA range 188 * @last: last byte IOVA range 189 */ 190 struct vhost_iotlb_map * 191 vhost_iotlb_itree_next(struct vhost_iotlb_map *map, u64 start, u64 last) 192 { 193 return vhost_iotlb_itree_iter_next(map, start, last); 194 } 195 EXPORT_SYMBOL_GPL(vhost_iotlb_itree_next); 196 197 MODULE_VERSION(MOD_VERSION); 198 MODULE_DESCRIPTION(MOD_DESC); 199 MODULE_AUTHOR(MOD_AUTHOR); 200 MODULE_LICENSE(MOD_LICENSE); 201