Name the shared-page selection and enumerate all eight combinations of
Hyper-V isolation, independent guest-memory encryption and channel
confidentiality. Either platform visibility signal selects shared
backing; the confidential-channel override keeps private backing. These
are decision tests, not confidential hardware qualification.

Signed-off-by: Emerson Busson <[email protected]>
---
 drivers/hv/channel.c           | 16 ++++++++++++++--
 drivers/hv/hyperv_vmbus.h      |  2 ++
 drivers/hv/vmbus_buffer_test.c | 29 +++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c
index 0c025a12808a..8ed155f2669f 100644
--- a/drivers/hv/channel.c
+++ b/drivers/hv/channel.c
@@ -45,6 +45,13 @@ int vmbus_buffer_round_size(u32 size, u32 *rounded_size)
        return 0;
 }
 
+bool
+vmbus_needs_shared_pages(bool hv_isolation, bool guest_mem_encrypted,
+                        bool confidential)
+{
+       return !confidential && (hv_isolation || guest_mem_encrypted);
+}
+
 unsigned int vmbus_buffer_order(unsigned long remaining,
                                unsigned int max_order)
 {
@@ -837,6 +844,8 @@ static void *__vmbus_alloc_buffer(struct vmbus_channel 
*channel,
        unsigned long nr_pages;
        unsigned long remaining;
        unsigned long page_idx = 0;
+       bool hv_isolation;
+       bool guest_mem_encrypted;
        struct page **chunks = NULL;
        struct page **pages = NULL;
        unsigned int order = MAX_PAGE_ORDER;
@@ -853,9 +862,12 @@ static void *__vmbus_alloc_buffer(struct vmbus_channel 
*channel,
        nr_pages = rounded_size >> PAGE_SHIFT;
        remaining = nr_pages;
 
+       hv_isolation = hv_is_isolation_supported();
+       guest_mem_encrypted = cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT);
+
        /* If the buffer does not need to be decrypted, just use vzalloc() */
-       if ((!hv_is_isolation_supported() &&
-            !cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) || confidential)
+       if (!vmbus_needs_shared_pages(hv_isolation, guest_mem_encrypted,
+                                     confidential))
                return vzalloc(rounded_size);
 
        /* Worst case: every chunk is a single page. */
diff --git a/drivers/hv/hyperv_vmbus.h b/drivers/hv/hyperv_vmbus.h
index e08c472041d5..06094000f2f1 100644
--- a/drivers/hv/hyperv_vmbus.h
+++ b/drivers/hv/hyperv_vmbus.h
@@ -557,6 +557,8 @@ int hv_remove_ring_sysfs(struct vmbus_channel *channel);
  * These are shared with vmbus_buffer_test.c, which is built into the
  * hv_vmbus object alongside channel.c. They stay unexported.
  */
+bool vmbus_needs_shared_pages(bool hv_isolation, bool guest_mem_encrypted,
+                             bool confidential);
 int vmbus_buffer_round_size(u32 size, u32 *rounded_size);
 unsigned int vmbus_buffer_order(unsigned long remaining,
                                unsigned int max_order);
diff --git a/drivers/hv/vmbus_buffer_test.c b/drivers/hv/vmbus_buffer_test.c
index 60fc526af1af..9b401ef2ceea 100644
--- a/drivers/hv/vmbus_buffer_test.c
+++ b/drivers/hv/vmbus_buffer_test.c
@@ -38,6 +38,34 @@ static void vmbus_buffer_size_overflow_test(struct kunit 
*test)
                        (u32)(U32_MAX - PAGE_SIZE + 1));
 }
 
+static void vmbus_buffer_private_shared_selection_test(struct kunit *test)
+{
+       static const struct {
+               bool isolation;
+               bool encrypted;
+               bool confidential;
+               bool shared;
+       } cases[] = {
+               { false, false, false, false },
+               { false, false, true, false },
+               { false, true, false, true },
+               { false, true, true, false },
+               { true, false, false, true },
+               { true, false, true, false },
+               { true, true, false, true },
+               { true, true, true, false },
+       };
+       unsigned int i;
+
+       /* Either platform signal requires visibility; confidential stays 
private. */
+       for (i = 0; i < ARRAY_SIZE(cases); i++)
+               KUNIT_EXPECT_EQ(test,
+                               vmbus_needs_shared_pages(cases[i].isolation,
+                                                        cases[i].encrypted,
+                                                        cases[i].confidential),
+                               cases[i].shared);
+}
+
 static void vmbus_ring_fallback_order_zero_test(struct kunit *test)
 {
        unsigned int order;
@@ -317,6 +345,7 @@ static void vmbus_buffer_order_zero_allocation_test(struct 
kunit *test)
 static struct kunit_case vmbus_buffer_test_cases[] = {
        KUNIT_CASE(vmbus_buffer_size_rounding_test),
        KUNIT_CASE(vmbus_buffer_size_overflow_test),
+       KUNIT_CASE(vmbus_buffer_private_shared_selection_test),
        KUNIT_CASE(vmbus_ring_fallback_order_zero_test),
        KUNIT_CASE(vmbus_buffer_failed_teardown_leaks_test),
        KUNIT_CASE(vmbus_buffer_partial_allocation_cleanup_test),
-- 
2.43.0


Reply via email to