Skip to contentSkip to conversation

9p/trans_virtio.c: Fix broken zero-copy on vmalloc() buffers

PATCH In review Richard Yao 3 participants 5 messages Dec 4, 2013 – Dec 6, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 9p/trans_virtio.c: Fix broken zero-copy on vmalloc() buffers

The 9p-virtio transport does zero copy on things larger than 1024 bytes in size. It accomplishes this by returning the physical addresses of pages to the virtio-pci device. At present, the translation is usually a bit shift.

However, that approach produces an invalid page address when we read/write to vmalloc buffers, such as those used for Linux kernle modules. This causes QEMU to die printing:

qemu-system-x86_64: virtio: trying to map MMIO memory

This patch enables 9p-virtio to correctly handle this case. This not only enables us to load Linux kernel modules off virtfs, but also enables ZFS file-based vdevs on virtfs to be used without killing QEMU.

Also, special thanks to both Avi Kivity and Alexander Graf for their interpretation of QEMU backtraces. Without their guidence, tracking down this bug would have taken much longer.

---
 net/9p/trans_virtio.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/9p/trans_virtio.c +4 -1
index 9c5a1aa..5d1d04b 100644
@@ -340,7 +340,10 @@ static int p9_get_mapped_pages(struct virtio_chan *chan,
 		int count = nr_pages;
 		while (nr_pages) {
 			s = rest_of_page(data);
-			pages[index++] = kmap_to_page(data);
+			if (is_vmalloc_or_module_addr(data))
+				pages[index++] = vmalloc_to_page(data);
+			else
+				pages[index++] = kmap_to_page(data);
 			data += s;
 			nr_pages--;
 		}
Signature · 1 line
1.8.3.2
Acked-by on v1 1/1
#1 lore raw
Quoted 15 lines · Richard Yao

The 9p-virtio transport does zero copy on things larger than 1024 bytes in size. It accomplishes this by returning the physical addresses of pages to the virtio-pci device. At present, the translation is usually a bit shift.

However, that approach produces an invalid page address when we read/write to vmalloc buffers, such as those used for Linux kernle modules. This causes QEMU to die printing:

qemu-system-x86_64: virtio: trying to map MMIO memory

This patch enables 9p-virtio to correctly handle this case. This not only enables us to load Linux kernel modules off virtfs, but also enables ZFS file-based vdevs on virtfs to be used without killing QEMU.

Quoted 5 lines

Also, special thanks to both Avi Kivity and Alexander Graf for their interpretation of QEMU backtraces. Without their guidence, tracking down this bug would have taken much longer.

Looks good and clean to me, but I'm not expert on the mm subsystem.

Alex

#2 lore raw
In reply to Richard Yao
Quoted 29 lines · Richard Yao

The 9p-virtio transport does zero copy on things larger than 1024 bytes in size. It accomplishes this by returning the physical addresses of pages to the virtio-pci device. At present, the translation is usually a bit shift.

However, that approach produces an invalid page address when we read/write to vmalloc buffers, such as those used for Linux kernle modules. This causes QEMU to die printing:

qemu-system-x86_64: virtio: trying to map MMIO memory

This patch enables 9p-virtio to correctly handle this case. This not only enables us to load Linux kernel modules off virtfs, but also enables ZFS file-based vdevs on virtfs to be used without killing QEMU.

Also, special thanks to both Avi Kivity and Alexander Graf for their interpretation of QEMU backtraces. Without their guidence, tracking down this bug would have taken much longer.

---
 net/9p/trans_virtio.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/9p/trans_virtio.c +0 -0
index 9c5a1aa..5d1d04b 100644

net/9p/trans_virtio.c static int p9_get_mapped_pages(struct virtio_chan *chan, · line 343

Quoted 5 lines
net/9p/trans_virtio.c +1 -1
@@ -340,7 +340,10 @@ static int p9_get_mapped_pages(struct virtio_chan *chan,
 		int count = nr_pages;
 		while (nr_pages) {
 			s = rest_of_page(data);
-			pages[index++] = kmap_to_page(data);
+			if (is_vmalloc_or_module_addr(data))

Can this really end up being a module address?

Will

#3 lore raw
Quoted 25 lines · Will Deacon
Quoted 24 lines · Richard Yao

The 9p-virtio transport does zero copy on things larger than 1024 bytes in size. It accomplishes this by returning the physical addresses of pages to the virtio-pci device. At present, the translation is usually a bit shift.

However, that approach produces an invalid page address when we read/write to vmalloc buffers, such as those used for Linux kernle modules. This causes QEMU to die printing:

qemu-system-x86_64: virtio: trying to map MMIO memory

This patch enables 9p-virtio to correctly handle this case. This not only enables us to load Linux kernel modules off virtfs, but also enables ZFS file-based vdevs on virtfs to be used without killing QEMU.

Also, special thanks to both Avi Kivity and Alexander Graf for their interpretation of QEMU backtraces. Without their guidence, tracking down this bug would have taken much longer.

---
 net/9p/trans_virtio.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 12 lines

net/9p/trans_virtio.c static int p9_get_mapped_pages(struct virtio_chan *chan, · line 343

Quoted 10 lines
net/9p/trans_virtio.c +1 -1
index 9c5a1aa..5d1d04b 100644
@@ -340,7 +340,10 @@ static int p9_get_mapped_pages(struct virtio_chan *chan,
 		int count = nr_pages;
 		while (nr_pages) {
 			s = rest_of_page(data);
-			pages[index++] = kmap_to_page(data);
+			if (is_vmalloc_or_module_addr(data))

Can this really end up being a module address?

Yes. Here is the stacktrace to prove it:

[<ffffffff814878ce>] p9_virtio_zc_request+0x45e/0x510
[<ffffffff814814ed>] p9_client_zc_rpc.constprop.16+0xfd/0x4f0
[<ffffffff814839dd>] p9_client_read+0x15d/0x240
[<ffffffff811c8440>] v9fs_fid_readn+0x50/0xa0
[<ffffffff811c84a0>] v9fs_file_readn+0x10/0x20
[<ffffffff811c84e7>] v9fs_file_read+0x37/0x70
[<ffffffff8114e3fb>] vfs_read+0x9b/0x160
[<ffffffff81153571>] kernel_read+0x41/0x60
[<ffffffff810c83ab>] copy_module_from_fd.isra.34+0xfb/0x180
[<ffffffff810cc420>] SyS_finit_module+0x70/0xd0
[<ffffffff814a08fd>] system_call_fastpath+0x1a/0x1f

[<ffffffffffffffff>] 0xffffffffffffffff

This is easily reproducible by trying to load a module off virtfs. QEMU will print the message that I cited in the commit message and then kill itself.

P.S. I omitted the CC list the first time I sent this, so I am resending with the correct CC list.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Reviewed-by
#4 lore raw
Quoted 28 lines · Richard Yao
Quoted 13 lines · Will Deacon

net/9p/trans_virtio.c static int p9_get_mapped_pages(struct virtio_chan *chan, · line 343

Quoted 10 lines · Richard Yao
net/9p/trans_virtio.c +1 -1
index 9c5a1aa..5d1d04b 100644
@@ -340,7 +340,10 @@ static int p9_get_mapped_pages(struct virtio_chan *chan,
 		int count = nr_pages;
 		while (nr_pages) {
 			s = rest_of_page(data);
-			pages[index++] = kmap_to_page(data);
+			if (is_vmalloc_or_module_addr(data))

Can this really end up being a module address?

Yes. Here is the stacktrace to prove it:

[<ffffffff814878ce>] p9_virtio_zc_request+0x45e/0x510
[<ffffffff814814ed>] p9_client_zc_rpc.constprop.16+0xfd/0x4f0
[<ffffffff814839dd>] p9_client_read+0x15d/0x240
[<ffffffff811c8440>] v9fs_fid_readn+0x50/0xa0
[<ffffffff811c84a0>] v9fs_file_readn+0x10/0x20
[<ffffffff811c84e7>] v9fs_file_read+0x37/0x70
[<ffffffff8114e3fb>] vfs_read+0x9b/0x160
[<ffffffff81153571>] kernel_read+0x41/0x60
[<ffffffff810c83ab>] copy_module_from_fd.isra.34+0xfb/0x180
[<ffffffff810cc420>] SyS_finit_module+0x70/0xd0
[<ffffffff814a08fd>] system_call_fastpath+0x1a/0x1f
Quoted 5 lines

[<ffffffffffffffff>] 0xffffffffffffffff

This is easily reproducible by trying to load a module off virtfs. QEMU will print the message that I cited in the commit message and then kill itself.

Ok, thanks for confirming!

In which case, this patch looks fine to me:

Will