| 1216 | } |
| 1217 | |
| 1218 | static int |
| 1219 | vhost_user_mmap_region(struct virtio_net *dev, |
| 1220 | struct rte_vhost_mem_region *region, |
| 1221 | uint64_t mmap_offset) |
| 1222 | { |
| 1223 | void *mmap_addr; |
| 1224 | uint64_t mmap_size; |
| 1225 | uint64_t alignment; |
| 1226 | int populate; |
| 1227 | |
| 1228 | /* Check for memory_size + mmap_offset overflow */ |
| 1229 | if (mmap_offset >= -region->size) { |
| 1230 | VHOST_LOG_CONFIG(dev->ifname, ERR, |
| 1231 | "mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow\n", |
| 1232 | mmap_offset, region->size); |
| 1233 | return -1; |
| 1234 | } |
| 1235 | |
| 1236 | mmap_size = region->size + mmap_offset; |
| 1237 | |
| 1238 | /* mmap() without flag of MAP_ANONYMOUS, should be called with length |
| 1239 | * argument aligned with hugepagesz at older longterm version Linux, |
| 1240 | * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL. |
| 1241 | * |
| 1242 | * To avoid failure, make sure in caller to keep length aligned. |
| 1243 | */ |
| 1244 | alignment = get_blk_size(region->fd); |
| 1245 | if (alignment == (uint64_t)-1) { |
| 1246 | VHOST_LOG_CONFIG(dev->ifname, ERR, "couldn't get hugepage size through fstat\n"); |
| 1247 | return -1; |
| 1248 | } |
| 1249 | mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment); |
| 1250 | if (mmap_size == 0) { |
| 1251 | /* |
| 1252 | * It could happen if initial mmap_size + alignment overflows |
| 1253 | * the sizeof uint64, which could happen if either mmap_size or |
| 1254 | * alignment value is wrong. |
| 1255 | * |
| 1256 | * mmap() kernel implementation would return an error, but |
| 1257 | * better catch it before and provide useful info in the logs. |
| 1258 | */ |
| 1259 | VHOST_LOG_CONFIG(dev->ifname, ERR, |
| 1260 | "mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid\n", |
| 1261 | region->size + mmap_offset, alignment); |
| 1262 | return -1; |
| 1263 | } |
| 1264 | |
| 1265 | populate = dev->async_copy ? MAP_POPULATE : 0; |
| 1266 | mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, |
| 1267 | MAP_SHARED | populate, region->fd, 0); |
| 1268 | |
| 1269 | if (mmap_addr == MAP_FAILED) { |
| 1270 | VHOST_LOG_CONFIG(dev->ifname, ERR, "mmap failed (%s).\n", strerror(errno)); |
| 1271 | return -1; |
| 1272 | } |
| 1273 | |
| 1274 | region->mmap_addr = mmap_addr; |
| 1275 | region->mmap_size = mmap_size; |
no test coverage detected