History

Merge tag 'hyperv-next-signed-20220114' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux

Pull hyperv updates from Wei Liu: - More patches for Hyper-V isolation VM support (Tianyu Lan) - Bug fixes and clean-up patches from various people * tag 'hyperv-next-signed-20220114' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux: scsi: storvsc: Fix storvsc_queuecommand() memory leak x86/hyperv: Properly deal with empty cpumasks in hyperv_flush_tlb_multi() Drivers: hv: vmbus: Initialize request offers message for Isolation VM scsi: storvsc: Fix unsigned comparison to zero swiotlb: Add CONFIG_HAS_IOMEM check around swiotlb_mem_remap() x86/hyperv: Fix definition of hv_ghcb_pg variable Drivers: hv: Fix definition of hypercall input & output arg variables net: netvsc: Add Isolation VM support for netvsc driver scsi: storvsc: Add Isolation VM support for storvsc driver hyper-v: Enable swiotlb bounce buffer for Isolation VM x86/hyper-v: Add hyperv Isolation VM check in the cc_platform_has() swiotlb: Add swiotlb bounce buffer remap function for HV IVM

Linus Torvalds committed 4 years ago

Commit cb3f09f2 parents 4d66020 4eea53318 files · +328 −45Browse files

Unified · Split

18 files
  1. arch/x86/hyperv/hv_init.c +13 −1
  2. arch/x86/hyperv/ivm.c +28 −0
  3. arch/x86/hyperv/mmu.c +9 −10
  4. arch/x86/include/asm/mshyperv.h +1 −1
  5. arch/x86/kernel/cc_platform.c +8 −0
  6. arch/x86/kernel/cpu/mshyperv.c +14 −1
  7. drivers/hv/channel_mgmt.c +1 −1
  8. drivers/hv/hv_common.c +13 −2
  9. drivers/hv/vmbus_drv.c +4 −0
  10. drivers/net/hyperv/hyperv_net.h +5 −0
  11. drivers/net/hyperv/netvsc.c +133 −3
  12. drivers/net/hyperv/netvsc_drv.c +1 −0
  13. drivers/net/hyperv/rndis_filter.c +2 −0
  14. drivers/scsi/storvsc_drv.c +32 −22
  15. include/asm-generic/mshyperv.h +4 −2
  16. include/linux/hyperv.h +6 −0
  17. include/linux/swiotlb.h +6 −0
  18. kernel/dma/swiotlb.c +48 −2
arch/x86/hyperv/hv_init.c+13−1
@@ -28,6 +28,7 @@ · 27 unmodified lines
2828 #include <linux/syscore_ops.h>
2929 #include <clocksource/hyperv_timer.h>
3030 #include <linux/highmem.h>
31+#include <linux/swiotlb.h>
3132
3233 int hyperv_init_cpuhp;
3334 u64 hv_current_partition_id = ~0ull;
@@ -36,7 +37,7 @@ EXPORT_SYMBOL_GPL(hv_current_partition_id); · 2 unmodified lines
3637 void *hv_hypercall_pg;
3738 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
3839
39−union hv_ghcb __percpu **hv_ghcb_pg;
40+union hv_ghcb * __percpu *hv_ghcb_pg;
4041
4142 /* Storage to save the hypercall page temporarily for hibernation */
4243 static void *hv_hypercall_pg_saved;
@@ -498,6 +499,17 @@ void __init hyperv_init(void) · 455 unmodified lines
498499
499500 /* Query the VMs extended capability once, so that it can be cached. */
500501 hv_query_ext_cap(0);
502+
503+#ifdef CONFIG_SWIOTLB
504+ /*
505+ * Swiotlb bounce buffer needs to be mapped in extra address
506+ * space. Map function doesn't work in the early place and so
507+ * call swiotlb_update_mem_attributes() here.
508+ */
509+ if (hv_is_isolation_supported())
510+ swiotlb_update_mem_attributes();
511+#endif
512+
501513 return;
502514
503515 clean_guest_os_id:
arch/x86/hyperv/ivm.c+28−0
@@ -287,3 +287,31 @@ int hv_set_mem_host_visibility(unsigned long kbuffer, int pagecount, bool visibl · 286 unmodified lines
287287 kfree(pfn_array);
288288 return ret;
289289 }
290+
291+/*
292+ * hv_map_memory - map memory to extra space in the AMD SEV-SNP Isolation VM.
293+ */
294+void *hv_map_memory(void *addr, unsigned long size)
295+{
296+ unsigned long *pfns = kcalloc(size / PAGE_SIZE,
297+ sizeof(unsigned long), GFP_KERNEL);
298+ void *vaddr;
299+ int i;
300+
301+ if (!pfns)
302+ return NULL;
303+
304+ for (i = 0; i < size / PAGE_SIZE; i++)
305+ pfns[i] = vmalloc_to_pfn(addr + i * PAGE_SIZE) +
306+ (ms_hyperv.shared_gpa_boundary >> PAGE_SHIFT);
307+
308+ vaddr = vmap_pfn(pfns, size / PAGE_SIZE, PAGE_KERNEL_IO);
309+ kfree(pfns);
310+
311+ return vaddr;
312+}
313+
314+void hv_unmap_memory(void *addr)
315+{
316+ vunmap(addr);
317+}
arch/x86/hyperv/mmu.c+9−10
@@ -68,15 +68,6 @@ static void hyperv_flush_tlb_multi(const struct cpumask *cpus, · 67 unmodified lines
6868
6969 local_irq_save(flags);
7070
71− /*
72− * Only check the mask _after_ interrupt has been disabled to avoid the
73− * mask changing under our feet.
74− */
75− if (cpumask_empty(cpus)) {
76− local_irq_restore(flags);
77− return;
78− }
79−
8071 flush_pcpu = (struct hv_tlb_flush **)
8172 this_cpu_ptr(hyperv_pcpu_input_arg);
8273
@@ -115,7 +106,9 @@ static void hyperv_flush_tlb_multi(const struct cpumask *cpus, · 32 unmodified lines
115106 * must. We will also check all VP numbers when walking the
116107 * supplied CPU set to remain correct in all cases.
117108 */
118− if (hv_cpu_number_to_vp_number(cpumask_last(cpus)) >= 64)
109+ cpu = cpumask_last(cpus);
110+
111+ if (cpu < nr_cpumask_bits && hv_cpu_number_to_vp_number(cpu) >= 64)
119112 goto do_ex_hypercall;
120113
121114 for_each_cpu(cpu, cpus) {
@@ -131,6 +124,12 @@ static void hyperv_flush_tlb_multi(const struct cpumask *cpus, · 9 unmodified lines
131124 __set_bit(vcpu, (unsigned long *)
132125 &flush->processor_mask);
133126 }
127+
128+ /* nothing to flush if 'processor_mask' ends up being empty */
129+ if (!flush->processor_mask) {
130+ local_irq_restore(flags);
131+ return;
132+ }
134133 }
135134
136135 /*
arch/x86/include/asm/mshyperv.h+1−1
@@ -30,7 +30,7 @@ extern void *hv_hypercall_pg; · 29 unmodified lines
3030
3131 extern u64 hv_current_partition_id;
3232
33−extern union hv_ghcb __percpu **hv_ghcb_pg;
33+extern union hv_ghcb * __percpu *hv_ghcb_pg;
3434
3535 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
3636 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
arch/x86/kernel/cc_platform.c+8−0
@@ -11,6 +11,7 @@ · 10 unmodified lines
1111 #include <linux/cc_platform.h>
1212 #include <linux/mem_encrypt.h>
1313
14+#include <asm/mshyperv.h>
1415 #include <asm/processor.h>
1516
1617 static bool __maybe_unused intel_cc_platform_has(enum cc_attr attr)
@@ -66,12 +67,19 @@ static bool amd_cc_platform_has(enum cc_attr attr) · 49 unmodified lines
6667 #endif
6768 }
6869
70+static bool hyperv_cc_platform_has(enum cc_attr attr)
71+{
72+ return attr == CC_ATTR_GUEST_MEM_ENCRYPT;
73+}
6974
7075 bool cc_platform_has(enum cc_attr attr)
7176 {
7277 if (sme_me_mask)
7378 return amd_cc_platform_has(attr);
7479
80+ if (hv_is_isolation_supported())
81+ return hyperv_cc_platform_has(attr);
82+
7583 return false;
7684 }
7785 EXPORT_SYMBOL_GPL(cc_platform_has);
arch/x86/kernel/cpu/mshyperv.c+14−1
@@ -18,6 +18,7 @@ · 17 unmodified lines
1818 #include <linux/kexec.h>
1919 #include <linux/i8253.h>
2020 #include <linux/random.h>
21+#include <linux/swiotlb.h>
2122 #include <asm/processor.h>
2223 #include <asm/hypervisor.h>
2324 #include <asm/hyperv-tlfs.h>
@@ -329,8 +330,20 @@ static void __init ms_hyperv_init_platform(void) · 305 unmodified lines
329330 pr_info("Hyper-V: Isolation Config: Group A 0x%x, Group B 0x%x\n",
330331 ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b);
331332
332− if (hv_get_isolation_type() == HV_ISOLATION_TYPE_SNP)
333+ if (hv_get_isolation_type() == HV_ISOLATION_TYPE_SNP) {
333334 static_branch_enable(&isolation_type_snp);
335+#ifdef CONFIG_SWIOTLB
336+ swiotlb_unencrypted_base = ms_hyperv.shared_gpa_boundary;
337+#endif
338+ }
339+
340+#ifdef CONFIG_SWIOTLB
341+ /*
342+ * Enable swiotlb force mode in Isolation VM to
343+ * use swiotlb bounce buffer for dma transaction.
344+ */
345+ swiotlb_force = SWIOTLB_FORCE;
346+#endif
334347 }
335348
336349 if (hv_max_functions_eax >= HYPERV_CPUID_NESTED_FEATURES) {
drivers/hv/channel_mgmt.c+1−1
@@ -1554,7 +1554,7 @@ int vmbus_request_offers(void) · 1553 unmodified lines
15541554 struct vmbus_channel_msginfo *msginfo;
15551555 int ret;
15561556
1557− msginfo = kmalloc(sizeof(*msginfo) +
1557+ msginfo = kzalloc(sizeof(*msginfo) +
15581558 sizeof(struct vmbus_channel_message_header),
15591559 GFP_KERNEL);
15601560 if (!msginfo)
drivers/hv/hv_common.c+13−2
@@ -44,10 +44,10 @@ EXPORT_SYMBOL_GPL(hv_vp_index); · 43 unmodified lines
4444 u32 hv_max_vp_index;
4545 EXPORT_SYMBOL_GPL(hv_max_vp_index);
4646
47−void __percpu **hyperv_pcpu_input_arg;
47+void * __percpu *hyperv_pcpu_input_arg;
4848 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
4949
50−void __percpu **hyperv_pcpu_output_arg;
50+void * __percpu *hyperv_pcpu_output_arg;
5151 EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg);
5252
5353 /*
@@ -295,3 +295,14 @@ u64 __weak hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_s · 241 unmodified lines
295295 return HV_STATUS_INVALID_PARAMETER;
296296 }
297297 EXPORT_SYMBOL_GPL(hv_ghcb_hypercall);
298+
299+void __weak *hv_map_memory(void *addr, unsigned long size)
300+{
301+ return NULL;
302+}
303+EXPORT_SYMBOL_GPL(hv_map_memory);
304+
305+void __weak hv_unmap_memory(void *addr)
306+{
307+}
308+EXPORT_SYMBOL_GPL(hv_unmap_memory);
drivers/hv/vmbus_drv.c+4−0
@@ -33,6 +33,7 @@ · 32 unmodified lines
3333 #include <linux/random.h>
3434 #include <linux/kernel.h>
3535 #include <linux/syscore_ops.h>
36+#include <linux/dma-map-ops.h>
3637 #include <clocksource/hyperv_timer.h>
3738 #include "hyperv_vmbus.h"
3839
@@ -2078,6 +2079,7 @@ struct hv_device *vmbus_device_create(const guid_t *type, · 2039 unmodified lines
20782079 return child_device_obj;
20792080 }
20802081
2082+static u64 vmbus_dma_mask = DMA_BIT_MASK(64);
20812083 /*
20822084 * vmbus_device_register - Register the child device
20832085 */
@@ -2118,6 +2120,8 @@ int vmbus_device_register(struct hv_device *child_device_obj) · 34 unmodified lines
21182120 }
21192121 hv_debug_add_dev_dir(child_device_obj);
21202122
2123+ child_device_obj->device.dma_mask = &vmbus_dma_mask;
2124+ child_device_obj->device.dma_parms = &child_device_obj->dma_parms;
21212125 return 0;
21222126
21232127 err_kset_unregister:
drivers/net/hyperv/hyperv_net.h+5−0
@@ -164,6 +164,7 @@ struct hv_netvsc_packet { · 163 unmodified lines
164164 u32 total_bytes;
165165 u32 send_buf_index;
166166 u32 total_data_buflen;
167+ struct hv_dma_range *dma_range;
167168 };
168169
169170 #define NETVSC_HASH_KEYLEN 40
@@ -1074,6 +1075,7 @@ struct netvsc_device { · 904 unmodified lines
10741075
10751076 /* Receive buffer allocated by us but manages by NetVSP */
10761077 void *recv_buf;
1078+ void *recv_original_buf;
10771079 u32 recv_buf_size; /* allocated bytes */
10781080 struct vmbus_gpadl recv_buf_gpadl_handle;
10791081 u32 recv_section_cnt;
@@ -1082,6 +1084,7 @@ struct netvsc_device { · 2 unmodified lines
10821084
10831085 /* Send buffer allocated by us */
10841086 void *send_buf;
1087+ void *send_original_buf;
10851088 u32 send_buf_size;
10861089 struct vmbus_gpadl send_buf_gpadl_handle;
10871090 u32 send_section_cnt;
@@ -1731,4 +1734,6 @@ struct rndis_message { · 643 unmodified lines
17311734 #define RETRY_US_HI 10000
17321735 #define RETRY_MAX 2000 /* >10 sec */
17331736
1737+void netvsc_dma_unmap(struct hv_device *hv_dev,
1738+ struct hv_netvsc_packet *packet);
17341739 #endif /* _HYPERV_NET_H */
drivers/net/hyperv/netvsc.c+133−3
@@ -153,8 +153,21 @@ static void free_netvsc_device(struct rcu_head *head) · 152 unmodified lines
153153 int i;
154154
155155 kfree(nvdev->extension);
156− vfree(nvdev->recv_buf);
157− vfree(nvdev->send_buf);
156+
157+ if (nvdev->recv_original_buf) {
158+ hv_unmap_memory(nvdev->recv_buf);
159+ vfree(nvdev->recv_original_buf);
160+ } else {
161+ vfree(nvdev->recv_buf);
162+ }
163+
164+ if (nvdev->send_original_buf) {
165+ hv_unmap_memory(nvdev->send_buf);
166+ vfree(nvdev->send_original_buf);
167+ } else {
168+ vfree(nvdev->send_buf);
169+ }
170+
158171 bitmap_free(nvdev->send_section_map);
159172
160173 for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
@@ -337,6 +350,7 @@ static int netvsc_init_buf(struct hv_device *device, · 176 unmodified lines
337350 struct nvsp_message *init_packet;
338351 unsigned int buf_size;
339352 int i, ret = 0;
353+ void *vaddr;
340354
341355 /* Get receive buffer area. */
342356 buf_size = device_info->recv_sections * device_info->recv_section_size;
@@ -372,6 +386,17 @@ static int netvsc_init_buf(struct hv_device *device, · 29 unmodified lines
372386 goto cleanup;
373387 }
374388
389+ if (hv_isolation_type_snp()) {
390+ vaddr = hv_map_memory(net_device->recv_buf, buf_size);
391+ if (!vaddr) {
392+ ret = -ENOMEM;
393+ goto cleanup;
394+ }
395+
396+ net_device->recv_original_buf = net_device->recv_buf;
397+ net_device->recv_buf = vaddr;
398+ }
399+
375400 /* Notify the NetVsp of the gpadl handle */
376401 init_packet = &net_device->channel_init_pkt;
377402 memset(init_packet, 0, sizeof(struct nvsp_message));
@@ -475,6 +500,17 @@ static int netvsc_init_buf(struct hv_device *device, · 97 unmodified lines
475500 goto cleanup;
476501 }
477502
503+ if (hv_isolation_type_snp()) {
504+ vaddr = hv_map_memory(net_device->send_buf, buf_size);
505+ if (!vaddr) {
506+ ret = -ENOMEM;
507+ goto cleanup;
508+ }
509+
510+ net_device->send_original_buf = net_device->send_buf;
511+ net_device->send_buf = vaddr;
512+ }
513+
478514 /* Notify the NetVsp of the gpadl handle */
479515 init_packet = &net_device->channel_init_pkt;
480516 memset(init_packet, 0, sizeof(struct nvsp_message));
@@ -764,7 +800,7 @@ static void netvsc_send_tx_complete(struct net_device *ndev, · 283 unmodified lines
764800
765801 /* Notify the layer above us */
766802 if (likely(skb)) {
767− const struct hv_netvsc_packet *packet
803+ struct hv_netvsc_packet *packet
768804 = (struct hv_netvsc_packet *)skb->cb;
769805 u32 send_index = packet->send_buf_index;
770806 struct netvsc_stats *tx_stats;
@@ -780,6 +816,7 @@ static void netvsc_send_tx_complete(struct net_device *ndev, · 9 unmodified lines
780816 tx_stats->bytes += packet->total_bytes;
781817 u64_stats_update_end(&tx_stats->syncp);
782818
819+ netvsc_dma_unmap(ndev_ctx->device_ctx, packet);
783820 napi_consume_skb(skb, budget);
784821 }
785822
@@ -944,6 +981,88 @@ static void netvsc_copy_to_send_buf(struct netvsc_device *net_device, · 158 unmodified lines
944981 memset(dest, 0, padding);
945982 }
946983
984+void netvsc_dma_unmap(struct hv_device *hv_dev,
985+ struct hv_netvsc_packet *packet)
986+{
987+ u32 page_count = packet->cp_partial ?
988+ packet->page_buf_cnt - packet->rmsg_pgcnt :
989+ packet->page_buf_cnt;
990+ int i;
991+
992+ if (!hv_is_isolation_supported())
993+ return;
994+
995+ if (!packet->dma_range)
996+ return;
997+
998+ for (i = 0; i < page_count; i++)
999+ dma_unmap_single(&hv_dev->device, packet->dma_range[i].dma,
1000+ packet->dma_range[i].mapping_size,
1001+ DMA_TO_DEVICE);
1002+
1003+ kfree(packet->dma_range);
1004+}
1005+
1006+/* netvsc_dma_map - Map swiotlb bounce buffer with data page of
1007+ * packet sent by vmbus_sendpacket_pagebuffer() in the Isolation
1008+ * VM.
1009+ *
1010+ * In isolation VM, netvsc send buffer has been marked visible to
1011+ * host and so the data copied to send buffer doesn't need to use
1012+ * bounce buffer. The data pages handled by vmbus_sendpacket_pagebuffer()
1013+ * may not be copied to send buffer and so these pages need to be
1014+ * mapped with swiotlb bounce buffer. netvsc_dma_map() is to do
1015+ * that. The pfns in the struct hv_page_buffer need to be converted
1016+ * to bounce buffer's pfn. The loop here is necessary because the
1017+ * entries in the page buffer array are not necessarily full
1018+ * pages of data. Each entry in the array has a separate offset and
1019+ * len that may be non-zero, even for entries in the middle of the
1020+ * array. And the entries are not physically contiguous. So each
1021+ * entry must be individually mapped rather than as a contiguous unit.
1022+ * So not use dma_map_sg() here.
1023+ */
1024+static int netvsc_dma_map(struct hv_device *hv_dev,
1025+ struct hv_netvsc_packet *packet,
1026+ struct hv_page_buffer *pb)
1027+{
1028+ u32 page_count = packet->cp_partial ?
1029+ packet->page_buf_cnt - packet->rmsg_pgcnt :
1030+ packet->page_buf_cnt;
1031+ dma_addr_t dma;
1032+ int i;
1033+
1034+ if (!hv_is_isolation_supported())
1035+ return 0;
1036+
1037+ packet->dma_range = kcalloc(page_count,
1038+ sizeof(*packet->dma_range),
1039+ GFP_KERNEL);
1040+ if (!packet->dma_range)
1041+ return -ENOMEM;
1042+
1043+ for (i = 0; i < page_count; i++) {
1044+ char *src = phys_to_virt((pb[i].pfn << HV_HYP_PAGE_SHIFT)
1045+ + pb[i].offset);
1046+ u32 len = pb[i].len;
1047+
1048+ dma = dma_map_single(&hv_dev->device, src, len,
1049+ DMA_TO_DEVICE);
1050+ if (dma_mapping_error(&hv_dev->device, dma)) {
1051+ kfree(packet->dma_range);
1052+ return -ENOMEM;
1053+ }
1054+
1055+ /* pb[].offset and pb[].len are not changed during dma mapping
1056+ * and so not reassign.
1057+ */
1058+ packet->dma_range[i].dma = dma;
1059+ packet->dma_range[i].mapping_size = len;
1060+ pb[i].pfn = dma >> HV_HYP_PAGE_SHIFT;
1061+ }
1062+
1063+ return 0;
1064+}
1065+
9471066 static inline int netvsc_send_pkt(
9481067 struct hv_device *device,
9491068 struct hv_netvsc_packet *packet,
@@ -984,14 +1103,24 @@ static inline int netvsc_send_pkt( · 34 unmodified lines
9841103
9851104 trace_nvsp_send_pkt(ndev, out_channel, rpkt);
9861105
1106+ packet->dma_range = NULL;
9871107 if (packet->page_buf_cnt) {
9881108 if (packet->cp_partial)
9891109 pb += packet->rmsg_pgcnt;
9901110
1111+ ret = netvsc_dma_map(ndev_ctx->device_ctx, packet, pb);
1112+ if (ret) {
1113+ ret = -EAGAIN;
1114+ goto exit;
1115+ }
1116+
9911117 ret = vmbus_sendpacket_pagebuffer(out_channel,
9921118 pb, packet->page_buf_cnt,
9931119 &nvmsg, sizeof(nvmsg),
9941120 req_id);
1121+
1122+ if (ret)
1123+ netvsc_dma_unmap(ndev_ctx->device_ctx, packet);
9951124 } else {
9961125 ret = vmbus_sendpacket(out_channel,
9971126 &nvmsg, sizeof(nvmsg),
@@ -999,6 +1128,7 @@ static inline int netvsc_send_pkt( · 1 unmodified line
9991128 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
10001129 }
10011130
1131+exit:
10021132 if (ret == 0) {
10031133 atomic_inc_return(&nvchan->queue_sends);
10041134
drivers/net/hyperv/netvsc_drv.c+1−0
@@ -2516,6 +2516,7 @@ static int netvsc_probe(struct hv_device *dev, · 2515 unmodified lines
25162516 net->netdev_ops = &device_ops;
25172517 net->ethtool_ops = &ethtool_ops;
25182518 SET_NETDEV_DEV(net, &dev->device);
2519+ dma_set_min_align_mask(&dev->device, HV_HYP_PAGE_SIZE - 1);
25192520
25202521 /* We always need headroom for rndis header */
25212522 net->needed_headroom = RNDIS_AND_PPI_SIZE;
drivers/net/hyperv/rndis_filter.c+2−0
@@ -361,6 +361,8 @@ static void rndis_filter_receive_response(struct net_device *ndev, · 360 unmodified lines
361361 }
362362 }
363363
364+ netvsc_dma_unmap(((struct net_device_context *)
365+ netdev_priv(ndev))->device_ctx, &request->pkt);
364366 complete(&request->wait_event);
365367 } else {
366368 netdev_err(ndev,
drivers/scsi/storvsc_drv.c+32−22
@@ -21,6 +21,8 @@ · 20 unmodified lines
2121 #include <linux/device.h>
2222 #include <linux/hyperv.h>
2323 #include <linux/blkdev.h>
24+#include <linux/dma-mapping.h>
25+
2426 #include <scsi/scsi.h>
2527 #include <scsi/scsi_cmnd.h>
2628 #include <scsi/scsi_host.h>
@@ -1336,6 +1338,7 @@ static void storvsc_on_channel_callback(void *context) · 1309 unmodified lines
13361338 continue;
13371339 }
13381340 request = (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd);
1341+ scsi_dma_unmap(scmnd);
13391342 }
13401343
13411344 storvsc_on_receive(stor_device, packet, request);
@@ -1749,9 +1752,7 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd) · 407 unmodified lines
17491752 struct hv_host_device *host_dev = shost_priv(host);
17501753 struct hv_device *dev = host_dev->dev;
17511754 struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd);
1752− int i;
17531755 struct scatterlist *sgl;
1754− unsigned int sg_count;
17551756 struct vmscsi_request *vm_srb;
17561757 struct vmbus_packet_mpb_array *payload;
17571758 u32 payload_sz;
@@ -1824,17 +1825,17 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd) · 66 unmodified lines
18241825 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
18251826
18261827 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1827− sg_count = scsi_sg_count(scmnd);
18281828
18291829 length = scsi_bufflen(scmnd);
18301830 payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb;
18311831 payload_sz = sizeof(cmd_request->mpb);
18321832
1833− if (sg_count) {
1834− unsigned int hvpgoff, hvpfns_to_add;
1833+ if (scsi_sg_count(scmnd)) {
18351834 unsigned long offset_in_hvpg = offset_in_hvpage(sgl->offset);
18361835 unsigned int hvpg_count = HVPFN_UP(offset_in_hvpg + length);
1837− u64 hvpfn;
1836+ struct scatterlist *sg;
1837+ unsigned long hvpfn, hvpfns_to_add;
1838+ int j, i = 0, sg_count;
18381839
18391840 if (hvpg_count > MAX_PAGE_BUFFER_COUNT) {
18401841
@@ -1848,21 +1849,24 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd) · 7 unmodified lines
18481849 payload->range.len = length;
18491850 payload->range.offset = offset_in_hvpg;
18501851
1852+ sg_count = scsi_dma_map(scmnd);
1853+ if (sg_count < 0) {
1854+ ret = SCSI_MLQUEUE_DEVICE_BUSY;
1855+ goto err_free_payload;
1856+ }
18511857
1852− for (i = 0; sgl != NULL; sgl = sg_next(sgl)) {
1858+ for_each_sg(sgl, sg, sg_count, j) {
18531859 /*
1854− * Init values for the current sgl entry. hvpgoff
1855− * and hvpfns_to_add are in units of Hyper-V size
1856− * pages. Handling the PAGE_SIZE != HV_HYP_PAGE_SIZE
1857− * case also handles values of sgl->offset that are
1858− * larger than PAGE_SIZE. Such offsets are handled
1859− * even on other than the first sgl entry, provided
1860− * they are a multiple of PAGE_SIZE.
1860+ * Init values for the current sgl entry. hvpfns_to_add
1861+ * is in units of Hyper-V size pages. Handling the
1862+ * PAGE_SIZE != HV_HYP_PAGE_SIZE case also handles
1863+ * values of sgl->offset that are larger than PAGE_SIZE.
1864+ * Such offsets are handled even on other than the first
1865+ * sgl entry, provided they are a multiple of PAGE_SIZE.
18611866 */
1862− hvpgoff = HVPFN_DOWN(sgl->offset);
1863− hvpfn = page_to_hvpfn(sg_page(sgl)) + hvpgoff;
1864− hvpfns_to_add = HVPFN_UP(sgl->offset + sgl->length) -
1865− hvpgoff;
1867+ hvpfn = HVPFN_DOWN(sg_dma_address(sg));
1868+ hvpfns_to_add = HVPFN_UP(sg_dma_address(sg) +
1869+ sg_dma_len(sg)) - hvpfn;
18661870
18671871 /*
18681872 * Fill the next portion of the PFN array with
@@ -1872,7 +1876,7 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd) · 3 unmodified lines
18721876 * the PFN array is filled.
18731877 */
18741878 while (hvpfns_to_add--)
1875− payload->range.pfn_array[i++] = hvpfn++;
1879+ payload->range.pfn_array[i++] = hvpfn++;
18761880 }
18771881 }
18781882
@@ -1884,13 +1888,18 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd) · 5 unmodified lines
18841888 put_cpu();
18851889
18861890 if (ret == -EAGAIN) {
1887− if (payload_sz > sizeof(cmd_request->mpb))
1888− kfree(payload);
18891891 /* no more space */
1890− return SCSI_MLQUEUE_DEVICE_BUSY;
1892+ ret = SCSI_MLQUEUE_DEVICE_BUSY;
1893+ goto err_free_payload;
18911894 }
18921895
18931896 return 0;
1897+
1898+err_free_payload:
1899+ if (payload_sz > sizeof(cmd_request->mpb))
1900+ kfree(payload);
1901+
1902+ return ret;
18941903 }
18951904
18961905 static struct scsi_host_template scsi_driver = {
@@ -2016,6 +2025,7 @@ static int storvsc_probe(struct hv_device *device, · 119 unmodified lines
20162025 stor_device->vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
20172026 spin_lock_init(&stor_device->lock);
20182027 hv_set_drvdata(device, stor_device);
2028+ dma_set_min_align_mask(&device->device, HV_HYP_PAGE_SIZE - 1);
20192029
20202030 stor_device->port_number = host->host_no;
20212031 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc);
include/asm-generic/mshyperv.h+4−2
@@ -49,8 +49,8 @@ struct ms_hyperv_info { · 48 unmodified lines
4949 };
5050 extern struct ms_hyperv_info ms_hyperv;
5151
52−extern void __percpu **hyperv_pcpu_input_arg;
53−extern void __percpu **hyperv_pcpu_output_arg;
52+extern void * __percpu *hyperv_pcpu_input_arg;
53+extern void * __percpu *hyperv_pcpu_output_arg;
5454
5555 extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr);
5656 extern u64 hv_do_fast_hypercall8(u16 control, u64 input8);
@@ -269,6 +269,8 @@ bool hv_isolation_type_snp(void); · 212 unmodified lines
269269 u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size);
270270 void hyperv_cleanup(void);
271271 bool hv_query_ext_cap(u64 cap_query);
272+void *hv_map_memory(void *addr, unsigned long size);
273+void hv_unmap_memory(void *addr);
272274 #else /* CONFIG_HYPERV */
273275 static inline bool hv_is_hyperv_initialized(void) { return false; }
274276 static inline bool hv_is_hibernation_supported(void) { return false; }
include/linux/hyperv.h+6−0
@@ -1261,6 +1261,7 @@ struct hv_device { · 1260 unmodified lines
12611261
12621262 struct vmbus_channel *channel;
12631263 struct kset *channels_kset;
1264+ struct device_dma_parameters dma_parms;
12641265
12651266 /* place holder to keep track of the dir for hv device in debugfs */
12661267 struct dentry *debug_dir;
@@ -1583,6 +1584,11 @@ struct hyperv_service_callback { · 316 unmodified lines
15831584 void (*callback)(void *context);
15841585 };
15851586
1587+struct hv_dma_range {
1588+ dma_addr_t dma;
1589+ u32 mapping_size;
1590+};
1591+
15861592 #define MAX_SRV_VER 0x7ffffff
15871593 extern bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, u8 *buf, u32 buflen,
15881594 const int *fw_version, int fw_vercnt,
include/linux/swiotlb.h+6−0
@@ -73,6 +73,9 @@ extern enum swiotlb_force swiotlb_force; · 72 unmodified lines
7373 * @end: The end address of the swiotlb memory pool. Used to do a quick
7474 * range check to see if the memory was in fact allocated by this
7575 * API.
76+ * @vaddr: The vaddr of the swiotlb memory pool. The swiotlb memory pool
77+ * may be remapped in the memory encrypted case and store virtual
78+ * address for bounce buffer operation.
7679 * @nslabs: The number of IO TLB blocks (in groups of 64) between @start and
7780 * @end. For default swiotlb, this is command line adjustable via
7881 * setup_io_tlb_npages.
@@ -92,6 +95,7 @@ extern enum swiotlb_force swiotlb_force; · 13 unmodified lines
9295 struct io_tlb_mem {
9396 phys_addr_t start;
9497 phys_addr_t end;
98+ void *vaddr;
9599 unsigned long nslabs;
96100 unsigned long used;
97101 unsigned int index;
@@ -186,4 +190,6 @@ static inline bool is_swiotlb_for_alloc(struct device *dev) · 88 unmodified lines
186190 }
187191 #endif /* CONFIG_DMA_RESTRICTED_POOL */
188192
193+extern phys_addr_t swiotlb_unencrypted_base;
194+
189195 #endif /* __LINUX_SWIOTLB_H */
kernel/dma/swiotlb.c+48−2
@@ -50,6 +50,7 @@ · 49 unmodified lines
5050 #include <asm/io.h>
5151 #include <asm/dma.h>
5252
53+#include <linux/io.h>
5354 #include <linux/init.h>
5455 #include <linux/memblock.h>
5556 #include <linux/iommu-helper.h>
@@ -72,6 +73,8 @@ enum swiotlb_force swiotlb_force; · 16 unmodified lines
7273
7374 struct io_tlb_mem io_tlb_default_mem;
7475
76+phys_addr_t swiotlb_unencrypted_base;
77+
7578 /*
7679 * Max segment that we can provide which (if pages are contingous) will
7780 * not be bounced (unless SWIOTLB_FORCE is set).
@@ -155,6 +158,34 @@ static inline unsigned long nr_slots(u64 val) · 77 unmodified lines
155158 return DIV_ROUND_UP(val, IO_TLB_SIZE);
156159 }
157160
161+/*
162+ * Remap swioltb memory in the unencrypted physical address space
163+ * when swiotlb_unencrypted_base is set. (e.g. for Hyper-V AMD SEV-SNP
164+ * Isolation VMs).
165+ */
166+#ifdef CONFIG_HAS_IOMEM
167+static void *swiotlb_mem_remap(struct io_tlb_mem *mem, unsigned long bytes)
168+{
169+ void *vaddr = NULL;
170+
171+ if (swiotlb_unencrypted_base) {
172+ phys_addr_t paddr = mem->start + swiotlb_unencrypted_base;
173+
174+ vaddr = memremap(paddr, bytes, MEMREMAP_WB);
175+ if (!vaddr)
176+ pr_err("Failed to map the unencrypted memory %pa size %lx.\n",
177+ &paddr, bytes);
178+ }
179+
180+ return vaddr;
181+}
182+#else
183+static void *swiotlb_mem_remap(struct io_tlb_mem *mem, unsigned long bytes)
184+{
185+ return NULL;
186+}
187+#endif
188+
158189 /*
159190 * Early SWIOTLB allocation may be too early to allow an architecture to
160191 * perform the desired operations. This function allows the architecture to
@@ -172,7 +203,12 @@ void __init swiotlb_update_mem_attributes(void) · 11 unmodified lines
172203 vaddr = phys_to_virt(mem->start);
173204 bytes = PAGE_ALIGN(mem->nslabs << IO_TLB_SHIFT);
174205 set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT);
175− memset(vaddr, 0, bytes);
206+
207+ mem->vaddr = swiotlb_mem_remap(mem, bytes);
208+ if (!mem->vaddr)
209+ mem->vaddr = vaddr;
210+
211+ memset(mem->vaddr, 0, bytes);
176212 }
177213
178214 static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start,
@@ -196,7 +232,17 @@ static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start, · 17 unmodified lines
196232 mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
197233 mem->slots[i].alloc_size = 0;
198234 }
235+
236+ /*
237+ * If swiotlb_unencrypted_base is set, the bounce buffer memory will
238+ * be remapped and cleared in swiotlb_update_mem_attributes.
239+ */
240+ if (swiotlb_unencrypted_base)
241+ return;
242+
199243 memset(vaddr, 0, bytes);
244+ mem->vaddr = vaddr;
245+ return;
200246 }
201247
202248 int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
@@ -371,7 +417,7 @@ static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size · 168 unmodified lines
371417 phys_addr_t orig_addr = mem->slots[index].orig_addr;
372418 size_t alloc_size = mem->slots[index].alloc_size;
373419 unsigned long pfn = PFN_DOWN(orig_addr);
374− unsigned char *vaddr = phys_to_virt(tlb_addr);
420+ unsigned char *vaddr = mem->vaddr + tlb_addr - mem->start;
375421 unsigned int tlb_offset, orig_addr_offset;
376422
377423 if (orig_addr == INVALID_PHYS_ADDR)