Skip to content

Commit 821dcab

Browse files
Sherry Sungregkh
Sherry Sun
authored andcommitted
misc: vop: add round_up(x,4) for vring_size to avoid kernel panic
[ Upstream commit cc1a267 ] Since struct _mic_vring_info and vring are allocated together and follow vring, if the vring_size() is not four bytes aligned, which will cause the start address of struct _mic_vring_info is not four byte aligned. For example, when vring entries is 128, the vring_size() will be 5126 bytes. The _mic_vring_info struct layout in ddr looks like: 0x90002400: 00000000 00390000 EE010000 0000C0FF Here 0x39 is the avail_idx member, and 0xC0FFEE01 is the magic member. When EP use ioread32(magic) to reads the magic in RC's share memory, it will cause kernel panic on ARM64 platform due to the cross-byte io read. Here read magic in user space use le32toh(vr0->info->magic) will meet the same issue. So add round_up(x,4) for vring_size, then the struct _mic_vring_info will store in this way: 0x90002400: 00000000 00000000 00000039 C0FFEE01 Which will avoid kernel panic when read magic in struct _mic_vring_info. Signed-off-by: Sherry Sun <[email protected]> Signed-off-by: Joakim Zhang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 85efddd commit 821dcab

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

drivers/misc/mic/vop/vop_main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ static struct virtqueue *vop_find_vq(struct virtio_device *dev,
320320
/* First assign the vring's allocated in host memory */
321321
vqconfig = _vop_vq_config(vdev->desc) + index;
322322
memcpy_fromio(&config, vqconfig, sizeof(config));
323-
_vr_size = vring_size(le16_to_cpu(config.num), MIC_VIRTIO_RING_ALIGN);
323+
_vr_size = round_up(vring_size(le16_to_cpu(config.num), MIC_VIRTIO_RING_ALIGN), 4);
324324
vr_size = PAGE_ALIGN(_vr_size + sizeof(struct _mic_vring_info));
325325
va = vpdev->hw_ops->remap(vpdev, le64_to_cpu(config.address), vr_size);
326326
if (!va)

drivers/misc/mic/vop/vop_vringh.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ static int vop_virtio_add_device(struct vop_vdev *vdev,
296296

297297
num = le16_to_cpu(vqconfig[i].num);
298298
mutex_init(&vvr->vr_mutex);
299-
vr_size = PAGE_ALIGN(vring_size(num, MIC_VIRTIO_RING_ALIGN) +
299+
vr_size = PAGE_ALIGN(round_up(vring_size(num, MIC_VIRTIO_RING_ALIGN), 4) +
300300
sizeof(struct _mic_vring_info));
301301
vr->va = (void *)
302302
__get_free_pages(GFP_KERNEL | __GFP_ZERO,
@@ -308,7 +308,7 @@ static int vop_virtio_add_device(struct vop_vdev *vdev,
308308
goto err;
309309
}
310310
vr->len = vr_size;
311-
vr->info = vr->va + vring_size(num, MIC_VIRTIO_RING_ALIGN);
311+
vr->info = vr->va + round_up(vring_size(num, MIC_VIRTIO_RING_ALIGN), 4);
312312
vr->info->magic = cpu_to_le32(MIC_MAGIC + vdev->virtio_id + i);
313313
vr_addr = dma_map_single(&vpdev->dev, vr->va, vr_size,
314314
DMA_BIDIRECTIONAL);

samples/mic/mpssd/mpssd.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,9 @@ mic_virtio_copy(struct mic_info *mic, int fd,
403403

404404
static inline unsigned _vring_size(unsigned int num, unsigned long align)
405405
{
406-
return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (3 + num)
406+
return _ALIGN_UP(((sizeof(struct vring_desc) * num + sizeof(__u16) * (3 + num)
407407
+ align - 1) & ~(align - 1))
408-
+ sizeof(__u16) * 3 + sizeof(struct vring_used_elem) * num;
408+
+ sizeof(__u16) * 3 + sizeof(struct vring_used_elem) * num, 4);
409409
}
410410

411411
/*

0 commit comments

Comments
 (0)