Skip to contentSkip to conversation

Merge contiguous physical memory regions

PATCH Superseded Longpeng(Mike) 2 participants 12 messages Oct 9, 2021 – Nov 3, 2021 Nitro enclaves (ne)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Merge contiguous physical memory regions

From: Longpeng <[email protected]>

Hi guys,

This patchset try to merge the contiguous physical memory regions when set user memory regions, you can see message in PATCH 1 for details. Please review when you free, thank!

Changes v2 -> v3:
Patch 1:
- update the commit title and commit message. [Andra]
- use 'struct range' to instead of 'struct phys_mem_region'. [Andra, Greg KH]
- add comments before the function definition. [Andra]
- rename several variables, parameters and function. [Andra]
Patch 2:
- update the commit title and commit message. [Andra]
- add comments before the function definition. [Andra]
- remove 'inline' attribute of ne_sanity_check_phys_mem_region. [Andra]
- leave a blank line before return. [Andra]
- move sanity check in ne_merge_phys_contig_memory_regions to
the beginning of the function. [Andra]
- double sanity checking after the merge of physical contiguous
memory regions has been completed. [Andra]
Patch 3:
- update the commit title and commit message. [Andra]
- use "misc_dev"/"misc device"/"MISC_DEV" to be more specific. [Andra]
Patch 4:
- update the commit title and commit message. [Andra]
- align the fileds in 'struct phys_regions_test'. [Andra]
- rename 'phys_regions_testcases' to 'phys_regions_test_cases'. [Andra]
- add comments before each test cases. [Andra]
- initialize the variables in ne_misc_dev_test_merge_phys_contig_memory_regions. [Andra]

Changes v1 -> v2:
- update the commit message as Andra's suggestion [Andra]
- remove TODO completely in ne_set_user_memory_region_ioctl [Andra]
- extract the physical memory regions setup into individual function
- add kunit tests [Andra]

Longpeng (4):
nitro_enclaves: Merge contiguous physical memory regions
nitro_enclaves: Sanity check physical memory regions during merging
nitro_enclaves: Add KUnit tests setup for the misc device
functionality
nitro_enclaves: Add KUnit tests for contiguous physical memory regions
merging

 drivers/virt/nitro_enclaves/Kconfig            |   9 ++
 drivers/virt/nitro_enclaves/ne_misc_dev.c      | 163 +++++++++++++++++++------
 drivers/virt/nitro_enclaves/ne_misc_dev_test.c | 153 +++++++++++++++++++++++
 3 files changed, 285 insertions(+), 40 deletions(-)
 create mode 100644 drivers/virt/nitro_enclaves/ne_misc_dev_test.c
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v3 1/4 nitro_enclaves: Merge contiguous physical memory regions

From: Longpeng <[email protected]>

There can be cases when there are more memory regions that need to be set for an enclave than the maximum supported number of memory regions per enclave. One example can be when the memory regions are backed by 2 MiB hugepages (the minimum supported hugepage size).

Let's merge the adjacent regions if they are physical contiguous. This way the final number of memory regions is less than before merging and could potentially avoid reaching maximum.

---
Changes v2 -> v3:
- update the commit title and commit message. [Andra]
- use 'struct range' to instead of 'struct phys_mem_region'. [Andra, Greg KH]
- add comments before the function definition. [Andra]
- rename several variables, parameters and function. [Andra]

---
 drivers/virt/nitro_enclaves/ne_misc_dev.c | 79 +++++++++++++++++++++----------
 1 file changed, 55 insertions(+), 24 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +55 -24
index e21e1e8..eea53e9 100644
@@ -26,6 +26,7 @@
 #include <linux/poll.h>
 #include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/range.h>
 #include <uapi/linux/vm_sockets.h>
 
 #include "ne_misc_dev.h"
@@ -126,6 +127,16 @@ struct ne_cpu_pool {
 static struct ne_cpu_pool ne_cpu_pool;
 
 /**
+ * struct phys_contig_mem_regions - Physical contiguous memory regions
+ * @num:	The number of regions that currently has.
+ * @region:	The array of physical memory regions.
+ */
+struct phys_contig_mem_regions {
+	unsigned long num;
+	struct range region[0];
+};
+
+/**
  * ne_check_enclaves_created() - Verify if at least one enclave has been created.
  * @void:	No parameters provided.
  *
@@ -825,6 +836,33 @@ static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,
 }
 
 /**
+ * ne_merge_phys_contig_memory_regions() - Add a memory region and merge the adjacent
+ *                                         regions if they are physical contiguous.
+ * @regions   : Private data associated with the physical contiguous memory regions.
+ * @page_paddr: Physical start address of the region to be added.
+ * @page_size : Length of the region to be added.
+ *
+ * Return:
+ * * No return value.
+ */
+static void
+ne_merge_phys_contig_memory_regions(struct phys_contig_mem_regions *regions,
+				    u64 page_paddr, u64 page_size)
+{
+	/* Physical contiguous, just merge */
+	if (regions->num &&
+	    (regions->region[regions->num - 1].end + 1) == page_paddr) {
+		regions->region[regions->num - 1].end += page_size;
+
+		return;
+	}
+
+	regions->region[regions->num].start = page_paddr;
+	regions->region[regions->num].end = page_paddr + page_size - 1;
+	regions->num++;
+}
+
+/**
  * ne_set_user_memory_region_ioctl() - Add user space memory region to the slot
  *				       associated with the current enclave.
  * @ne_enclave :	Private data associated with the current enclave.
@@ -843,9 +881,9 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
 	unsigned long max_nr_pages = 0;
 	unsigned long memory_size = 0;
 	struct ne_mem_region *ne_mem_region = NULL;
-	unsigned long nr_phys_contig_mem_regions = 0;
 	struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
-	struct page **phys_contig_mem_regions = NULL;
+	struct phys_contig_mem_regions *phys_contig_mem_regions = NULL;
+	size_t size_to_alloc = 0;
 	int rc = -EINVAL;
 
 	rc = ne_sanity_check_user_mem_region(ne_enclave, mem_region);
@@ -866,8 +904,9 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
 		goto free_mem_region;
 	}
 
-	phys_contig_mem_regions = kcalloc(max_nr_pages, sizeof(*phys_contig_mem_regions),
-					  GFP_KERNEL);
+	size_to_alloc = sizeof(*phys_contig_mem_regions) +
+			max_nr_pages * sizeof(struct range);
+	phys_contig_mem_regions = kzalloc(size_to_alloc, GFP_KERNEL);
 	if (!phys_contig_mem_regions) {
 		rc = -ENOMEM;
 
@@ -901,26 +940,16 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
 		if (rc < 0)
 			goto put_pages;
 
-		/*
-		 * TODO: Update once handled non-contiguous memory regions
-		 * received from user space or contiguous physical memory regions
-		 * larger than 2 MiB e.g. 8 MiB.
-		 */
-		phys_contig_mem_regions[i] = ne_mem_region->pages[i];
+		ne_merge_phys_contig_memory_regions(phys_contig_mem_regions,
+						    page_to_phys(ne_mem_region->pages[i]),
+						    page_size(ne_mem_region->pages[i]));
 
 		memory_size += page_size(ne_mem_region->pages[i]);
 
 		ne_mem_region->nr_pages++;
 	} while (memory_size < mem_region.memory_size);
 
-	/*
-	 * TODO: Update once handled non-contiguous memory regions received
-	 * from user space or contiguous physical memory regions larger than
-	 * 2 MiB e.g. 8 MiB.
-	 */
-	nr_phys_contig_mem_regions = ne_mem_region->nr_pages;
-
-	if ((ne_enclave->nr_mem_regions + nr_phys_contig_mem_regions) >
+	if ((ne_enclave->nr_mem_regions + phys_contig_mem_regions->num) >
 	    ne_enclave->max_mem_regions) {
 		dev_err_ratelimited(ne_misc_dev.this_device,
 				    "Reached max memory regions %lld\n",
@@ -931,9 +960,10 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
 		goto put_pages;
 	}
 
-	for (i = 0; i < nr_phys_contig_mem_regions; i++) {
-		u64 phys_region_addr = page_to_phys(phys_contig_mem_regions[i]);
-		u64 phys_region_size = page_size(phys_contig_mem_regions[i]);
+	for (i = 0; i < phys_contig_mem_regions->num; i++) {
+		struct range *range = phys_contig_mem_regions->region + i;
+		u64 phys_region_addr = range->start;
+		u64 phys_region_size = range_len(range);
 
 		if (phys_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
 			dev_err_ratelimited(ne_misc_dev.this_device,
@@ -959,13 +989,14 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
 
 	list_add(&ne_mem_region->mem_region_list_entry, &ne_enclave->mem_regions_list);
 
-	for (i = 0; i < nr_phys_contig_mem_regions; i++) {
+	for (i = 0; i < phys_contig_mem_regions->num; i++) {
 		struct ne_pci_dev_cmd_reply cmd_reply = {};
 		struct slot_add_mem_req slot_add_mem_req = {};
+		struct range *range = phys_contig_mem_regions->region + i;
 
 		slot_add_mem_req.slot_uid = ne_enclave->slot_uid;
-		slot_add_mem_req.paddr = page_to_phys(phys_contig_mem_regions[i]);
-		slot_add_mem_req.size = page_size(phys_contig_mem_regions[i]);
+		slot_add_mem_req.paddr = range->start;
+		slot_add_mem_req.size = range_len(range);
 
 		rc = ne_do_request(pdev, SLOT_ADD_MEM,
 				   &slot_add_mem_req, sizeof(slot_add_mem_req),
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH v3 1/4] nitro_enclaves: Merge contiguous physical memory regions

Quoted 9 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

There can be cases when there are more memory regions that need to be set for an enclave than the maximum supported number of memory regions per enclave. One example can be when the memory regions are backed by 2 MiB hugepages (the minimum supported hugepage size).

Let's merge the adjacent regions if they are physical contiguous. This

physical contiguous => physically contiguous

Quoted 19 lines

way the final number of memory regions is less than before merging and could potentially avoid reaching maximum.

---
Changes v2 -> v3:
- update the commit title and commit message. [Andra]
- use 'struct range' to instead of 'struct phys_mem_region'. [Andra, Greg KH]
- add comments before the function definition. [Andra]
- rename several variables, parameters and function. [Andra]

---
  drivers/virt/nitro_enclaves/ne_misc_dev.c | 79 +++++++++++++++++++++----------
  1 file changed, 55 insertions(+), 24 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
index e21e1e8..eea53e9 100644

drivers/virt/nitro_enclaves/ne_misc_dev.c line 29

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
@@ -26,6 +26,7 @@
  #include <linux/poll.h>
  #include <linux/slab.h>
  #include <linux/types.h>
+#include <linux/range.h>

Please keep the alphabetical order and move this before "#include <linux/slab.h>."

drivers/virt/nitro_enclaves/ne_misc_dev.c struct ne_cpu_pool { · line 130

Quoted 8 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
  #include <uapi/linux/vm_sockets.h>
 
  #include "ne_misc_dev.h"
@@ -126,6 +127,16 @@ struct ne_cpu_pool {
  static struct ne_cpu_pool ne_cpu_pool;
 
  /**
+ * struct phys_contig_mem_regions - Physical contiguous memory regions

Physical contiguous memory regions => Contiguous physical memory regions

drivers/virt/nitro_enclaves/ne_misc_dev.c struct ne_cpu_pool { · line 136

Quoted 6 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +6 -0
+ * @num:       The number of regions that currently has.
+ * @region:    The array of physical memory regions.
+ */
+struct phys_contig_mem_regions {
+       unsigned long num;
+       struct range region[0];

Should use "struct range *regions" instead, to keep a similar style with regard to arrays throughout the code.

Please align the fields name (e.g. [1]) so that it's easier to distinguish between fields type and name.

Let's also prefix with "ne" the data structure naming e.g. ne_phys_contig_mem_regions. So that is similar to other data structures defined in the NE kernel driver codebase.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/virt/nitro_enclaves/ne_misc_dev.c#n118

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 840

Quoted 12 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +5 -0
+};
+
+/**
   * ne_check_enclaves_created() - Verify if at least one enclave has been created.
   * @void:      No parameters provided.
   *
@@ -825,6 +836,33 @@ static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,
  }
 
  /**
+ * ne_merge_phys_contig_memory_regions() - Add a memory region and merge the adjacent
+ *                                         regions if they are physical contiguous.

physical contiguous => physically contiguous

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 841

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+ * @regions   : Private data associated with the physical contiguous memory regions.

physical contiguous memory regions => contiguous physical memory regions

@regions => @phys_contig_regions

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 846

Quoted 5 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +5 -0
+ * @page_paddr: Physical start address of the region to be added.
+ * @page_size : Length of the region to be added.
+ *
+ * Return:
+ * * No return value.

Please add "Context: Process context. This function is called with the ne_enclave mutex held." before "Return", similar to other defined functions.

Can remove these lines, as for now there is no return value:

* Return:
* * No return value.

And then can add this in the second patch in the series:

* Context: Process context. This function is called with the ne_enclave mutex held. (note: already available in the first patch)
* Return: (note: added in the second patch)
* * 0 ...

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 850

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +4 -0
+ */
+static void
+ne_merge_phys_contig_memory_regions(struct phys_contig_mem_regions *regions,
+                                   u64 page_paddr, u64 page_size)

phys_contig_mem_regions => ne_phys_contig_mem_regions *regions => *phys_contig_regions

Can define something like this:

unsigned long num = phys_contig_regions->num;

and then use it inside the function, except the last line that increments the num.

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 852

Quoted 2 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +2 -0
+{
+       /* Physical contiguous, just merge */

Physical contiguous => Physically contiguous

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 854

Quoted 2 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +2 -0
+       if (regions->num &&
+           (regions->region[regions->num - 1].end + 1) == page_paddr) {

e.g. phys_contig_regions->regions[num - 1]

Quoted 15 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +11 -0
+               regions->region[regions->num - 1].end += page_size;
+
+               return;
+       }
+
+       regions->region[regions->num].start = page_paddr;
+       regions->region[regions->num].end = page_paddr + page_size - 1;
+       regions->num++;
+}
+
+/**
   * ne_set_user_memory_region_ioctl() - Add user space memory region to the slot
   *                                    associated with the current enclave.
   * @ne_enclave :       Private data associated with the current enclave.

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 885

Quoted 7 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -2
@@ -843,9 +881,9 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
         unsigned long max_nr_pages = 0;
         unsigned long memory_size = 0;
         struct ne_mem_region *ne_mem_region = NULL;
-       unsigned long nr_phys_contig_mem_regions = 0;
         struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
-       struct page **phys_contig_mem_regions = NULL;
+       struct phys_contig_mem_regions *phys_contig_mem_regions = NULL;

struct phys_contig_mem_regions *phys_contig_mem_regions = NULL; => struct ne_phys_contig_mem_regions phys_contig_mem_regions = {};

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 909

Quoted 13 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +4 -2
+       size_t size_to_alloc = 0;
         int rc = -EINVAL;
 
         rc = ne_sanity_check_user_mem_region(ne_enclave, mem_region);
@@ -866,8 +904,9 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
                 goto free_mem_region;
         }
 
-       phys_contig_mem_regions = kcalloc(max_nr_pages, sizeof(*phys_contig_mem_regions),
-                                         GFP_KERNEL);
+       size_to_alloc = sizeof(*phys_contig_mem_regions) +
+                       max_nr_pages * sizeof(struct range);
+       phys_contig_mem_regions = kzalloc(size_to_alloc, GFP_KERNEL);

Then can alloc memory for the regions field.

phys_contig_mem_regions.regions = kcalloc(max_nr_pages, sizeof(*phys_contig_mem_regions.regions), GFP_KERNEL);

The "size_of_alloc" will not be needed in this case.

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
         if (!phys_contig_mem_regions) {
                 rc = -ENOMEM;
 

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 943

Quoted 10 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -6
@@ -901,26 +940,16 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
                 if (rc < 0)
                         goto put_pages;
 
-               /*
-                * TODO: Update once handled non-contiguous memory regions
-                * received from user space or contiguous physical memory regions
-                * larger than 2 MiB e.g. 8 MiB.
-                */
-               phys_contig_mem_regions[i] = ne_mem_region->pages[i];
+               ne_merge_phys_contig_memory_regions(phys_contig_mem_regions,

And can pass it here like this: &phys_contig_mem_regions.

Quoted 21 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +3 -8
+                                                   page_to_phys(ne_mem_region->pages[i]),
+                                                   page_size(ne_mem_region->pages[i]));
 
                 memory_size += page_size(ne_mem_region->pages[i]);
 
                 ne_mem_region->nr_pages++;
         } while (memory_size < mem_region.memory_size);
 
-       /*
-        * TODO: Update once handled non-contiguous memory regions received
-        * from user space or contiguous physical memory regions larger than
-        * 2 MiB e.g. 8 MiB.
-        */
-       nr_phys_contig_mem_regions = ne_mem_region->nr_pages;
-
-       if ((ne_enclave->nr_mem_regions + nr_phys_contig_mem_regions) >
+       if ((ne_enclave->nr_mem_regions + phys_contig_mem_regions->num) >
             ne_enclave->max_mem_regions) {
                 dev_err_ratelimited(ne_misc_dev.this_device,
                                     "Reached max memory regions %lld\n",

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 966

Quoted 10 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +4 -3
@@ -931,9 +960,10 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
                 goto put_pages;
         }
 
-       for (i = 0; i < nr_phys_contig_mem_regions; i++) {
-               u64 phys_region_addr = page_to_phys(phys_contig_mem_regions[i]);
-               u64 phys_region_size = page_size(phys_contig_mem_regions[i]);
+       for (i = 0; i < phys_contig_mem_regions->num; i++) {
+               struct range *range = phys_contig_mem_regions->region + i;
+               u64 phys_region_addr = range->start;
+               u64 phys_region_size = range_len(range);

With the updated data structure field, could be:

u64 phys_region_addr = phys_contig_mem_regions.regions[i].start; u64 phys_region_size = range_len(&phys_contig_mem_regions.regions[i]);

Quoted 8 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -1
                 if (phys_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
                         dev_err_ratelimited(ne_misc_dev.this_device,
@@ -959,13 +989,14 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
 
         list_add(&ne_mem_region->mem_region_list_entry, &ne_enclave->mem_regions_list);
 
-       for (i = 0; i < nr_phys_contig_mem_regions; i++) {

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 999

Quoted 10 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +4 -2
+       for (i = 0; i < phys_contig_mem_regions->num; i++) {
                 struct ne_pci_dev_cmd_reply cmd_reply = {};
                 struct slot_add_mem_req slot_add_mem_req = {};
+               struct range *range = phys_contig_mem_regions->region + i;
 
                 slot_add_mem_req.slot_uid = ne_enclave->slot_uid;
-               slot_add_mem_req.paddr = page_to_phys(phys_contig_mem_regions[i]);
-               slot_add_mem_req.size = page_size(phys_contig_mem_regions[i]);
+               slot_add_mem_req.paddr = range->start;
+               slot_add_mem_req.size = range_len(range);

Similarly here:

slot_add_mem_req.paddr = phys_contig_mem_regions.regions[i].start; slot_add_mem_req.size = range_len(&phys_contig_mem_regions.regions[i]);

Then remain the kfree paths for "phys_contig_mem_regions.regions" instead of "phys_contig_mem_regions".

Thanks,
Andra

Quoted 6 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -1
                 rc = ne_do_request(pdev, SLOT_ADD_MEM,
                                    &slot_add_mem_req, sizeof(slot_add_mem_req),
--

1.8.3.1

Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.

#3 lore raw

RE: [PATCH v3 1/4] nitro_enclaves: Merge contiguous physical memory regions

Hi Andra,

Sorry for the long delay.

I've read all your suggestions in each patch, there's no objection. I'll send v4 later, please review when you free, thanks.

Quoted 320 lines

-----Original Message-----
From: Paraschiv, Andra-Irina [mailto:[email protected]]
Sent: Friday, October 15, 2021 9:34 PM
To: Longpeng (Mike, Cloud Infrastructure Service Product Dept.) <[email protected]>
Cc: Gonglei (Arei) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v3 1/4] nitro_enclaves: Merge contiguous physical memory regions

Quoted 9 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

There can be cases when there are more memory regions that need to be set for an enclave than the maximum supported number of memory regions per enclave. One example can be when the memory regions are backed by 2 MiB hugepages (the minimum supported hugepage size).

Let's merge the adjacent regions if they are physical contiguous. This

physical contiguous => physically contiguous

Quoted 8 lines

way the final number of memory regions is less than before merging and could potentially avoid reaching maximum.

---
Changes v2 -> v3:
- update the commit title and commit message. [Andra]
- use 'struct range' to instead of 'struct phys_mem_region'. [Andra, Greg

KH]

Quoted 4 lines

- add comments before the function definition. [Andra]
- rename several variables, parameters and function. [Andra]

---
  drivers/virt/nitro_enclaves/ne_misc_dev.c | 79

+++++++++++++++++++++----------

Review

Quoted 3 lines
  1 file changed, 55 insertions(+), 24 deletions(-)
(quoted diff) +0 -0

b/drivers/virt/nitro_enclaves/ne_misc_dev.c

drivers/virt/nitro_enclaves/ne_misc_dev.c line 29

Quoted 8 lines

index e21e1e8..eea53e9 100644

drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
@@ -26,6 +26,7 @@
  #include <linux/poll.h>
  #include <linux/slab.h>
  #include <linux/types.h>
+#include <linux/range.h>

Please keep the alphabetical order and move this before "#include <linux/slab.h>."

drivers/virt/nitro_enclaves/ne_misc_dev.c struct ne_cpu_pool { · line 130

Quoted 8 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
  #include <uapi/linux/vm_sockets.h>
 
  #include "ne_misc_dev.h"
@@ -126,6 +127,16 @@ struct ne_cpu_pool {
  static struct ne_cpu_pool ne_cpu_pool;
 
  /**
+ * struct phys_contig_mem_regions - Physical contiguous memory regions

Physical contiguous memory regions => Contiguous physical memory regions

drivers/virt/nitro_enclaves/ne_misc_dev.c struct ne_cpu_pool { · line 136

Quoted 6 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +6 -0
+ * @num:       The number of regions that currently has.
+ * @region:    The array of physical memory regions.
+ */
+struct phys_contig_mem_regions {
+       unsigned long num;
+       struct range region[0];

Should use "struct range *regions" instead, to keep a similar style with regard to arrays throughout the code.

Please align the fields name (e.g. [1]) so that it's easier to distinguish between fields type and name.

Let's also prefix with "ne" the data structure naming e.g. ne_phys_contig_mem_regions. So that is similar to other data structures defined in the NE kernel driver codebase.

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/dri
vers/virt/nitro_enclaves/ne_misc_dev.c#n118

drivers/virt/nitro_enclaves/ne_misc_dev.c struct ne_cpu_pool {

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +3 -0
+};
+
+/**
   * ne_check_enclaves_created() - Verify if at least one enclave has been

created.

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct

Quoted 3 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
   * @void:      No parameters provided.
   *
@@ -825,6 +836,33 @@ static int ne_sanity_check_user_mem_region_page(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct · line 839

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
  }
 
  /**
+ * ne_merge_phys_contig_memory_regions() - Add a memory region and merge the

adjacent

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct · line 840

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+ *                                         regions if they are physical

contiguous.

physical contiguous => physically contiguous

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct · line 841

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+ * @regions   : Private data associated with the physical contiguous memory

regions.

physical contiguous memory regions => contiguous physical memory regions

@regions => @phys_contig_regions

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 846

Quoted 5 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +5 -0
+ * @page_paddr: Physical start address of the region to be added.
+ * @page_size : Length of the region to be added.
+ *
+ * Return:
+ * * No return value.

Please add "Context: Process context. This function is called with the ne_enclave mutex held." before "Return", similar to other defined functions.

Can remove these lines, as for now there is no return value:

* Return:
* * No return value.

And then can add this in the second patch in the series:

* Context: Process context. This function is called with the ne_enclave mutex held. (note: already available in the first patch)
* Return: (note: added in the second patch)
* * 0 ...

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,

Quoted 3 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +3 -0
+ */
+static void
+ne_merge_phys_contig_memory_regions(struct phys_contig_mem_regions

*regions,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 850

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+                                   u64 page_paddr, u64 page_size)

phys_contig_mem_regions => ne_phys_contig_mem_regions *regions => *phys_contig_regions

Can define something like this:

unsigned long num = phys_contig_regions->num;

and then use it inside the function, except the last line that increments the num.

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 852

Quoted 2 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +2 -0
+{
+       /* Physical contiguous, just merge */

Physical contiguous => Physically contiguous

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 854

Quoted 2 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +2 -0
+       if (regions->num &&
+           (regions->region[regions->num - 1].end + 1) == page_paddr) {

e.g. phys_contig_regions->regions[num - 1]

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,

Quoted 12 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +11 -0
+               regions->region[regions->num - 1].end += page_size;
+
+               return;
+       }
+
+       regions->region[regions->num].start = page_paddr;
+       regions->region[regions->num].end = page_paddr + page_size - 1;
+       regions->num++;
+}
+
+/**
   * ne_set_user_memory_region_ioctl() - Add user space memory region to the

slot

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct

Quoted 3 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
   *                                    associated with the current enclave.
   * @ne_enclave :       Private data associated with the current enclave.
@@ -843,9 +881,9 @@ static int ne_set_user_memory_region_ioctl(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 885

Quoted 7 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -2
         unsigned long max_nr_pages = 0;
         unsigned long memory_size = 0;
         struct ne_mem_region *ne_mem_region = NULL;
-       unsigned long nr_phys_contig_mem_regions = 0;
         struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
-       struct page **phys_contig_mem_regions = NULL;
+       struct phys_contig_mem_regions *phys_contig_mem_regions = NULL;

struct phys_contig_mem_regions *phys_contig_mem_regions = NULL; => struct ne_phys_contig_mem_regions phys_contig_mem_regions = {};

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct

Quoted 5 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+       size_t size_to_alloc = 0;
         int rc = -EINVAL;
 
         rc = ne_sanity_check_user_mem_region(ne_enclave, mem_region);
@@ -866,8 +904,9 @@ static int ne_set_user_memory_region_ioctl(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -1
                 goto free_mem_region;
         }
 
-       phys_contig_mem_regions = kcalloc(max_nr_pages,

sizeof(*phys_contig_mem_regions),

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 909

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +3 -1
-                                         GFP_KERNEL);
+       size_to_alloc = sizeof(*phys_contig_mem_regions) +
+                       max_nr_pages * sizeof(struct range);
+       phys_contig_mem_regions = kzalloc(size_to_alloc, GFP_KERNEL);

Then can alloc memory for the regions field.

phys_contig_mem_regions.regions = kcalloc(max_nr_pages, sizeof(*phys_contig_mem_regions.regions), GFP_KERNEL);

The "size_of_alloc" will not be needed in this case.

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
         if (!phys_contig_mem_regions) {
                 rc = -ENOMEM;
 
@@ -901,26 +940,16 @@ static int ne_set_user_memory_region_ioctl(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,

Quoted 10 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -6
                 if (rc < 0)
                         goto put_pages;
 
-               /*
-                * TODO: Update once handled non-contiguous memory regions
-                * received from user space or contiguous physical memory regions
-                * larger than 2 MiB e.g. 8 MiB.
-                */
-               phys_contig_mem_regions[i] = ne_mem_region->pages[i];
+

ne_merge_phys_contig_memory_regions(phys_contig_mem_regions,

And can pass it here like this: &phys_contig_mem_regions.

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct · line 944

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+

page_to_phys(ne_mem_region->pages[i]),

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct · line 945

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+

page_size(ne_mem_region->pages[i]));

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct

Quoted 19 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -8
                 memory_size += page_size(ne_mem_region->pages[i]);
 
                 ne_mem_region->nr_pages++;
         } while (memory_size < mem_region.memory_size);
 
-       /*
-        * TODO: Update once handled non-contiguous memory regions received
-        * from user space or contiguous physical memory regions larger than
-        * 2 MiB e.g. 8 MiB.
-        */
-       nr_phys_contig_mem_regions = ne_mem_region->nr_pages;
-
-       if ((ne_enclave->nr_mem_regions + nr_phys_contig_mem_regions) >
+       if ((ne_enclave->nr_mem_regions + phys_contig_mem_regions->num) >
             ne_enclave->max_mem_regions) {
                 dev_err_ratelimited(ne_misc_dev.this_device,
                                     "Reached max memory regions %lld\n",
@@ -931,9 +960,10 @@ static int ne_set_user_memory_region_ioctl(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,

Quoted 5 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -2
                 goto put_pages;
         }
 
-       for (i = 0; i < nr_phys_contig_mem_regions; i++) {
-               u64 phys_region_addr =

page_to_phys(phys_contig_mem_regions[i]);

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 966

Quoted 5 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +4 -1
-               u64 phys_region_size = page_size(phys_contig_mem_regions[i]);
+       for (i = 0; i < phys_contig_mem_regions->num; i++) {
+               struct range *range = phys_contig_mem_regions->region + i;
+               u64 phys_region_addr = range->start;
+               u64 phys_region_size = range_len(range);

With the updated data structure field, could be:

u64 phys_region_addr = phys_contig_mem_regions.regions[i].start; u64 phys_region_size = range_len(&phys_contig_mem_regions.regions[i]);

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
                 if (phys_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
                         dev_err_ratelimited(ne_misc_dev.this_device,
@@ -959,13 +989,14 @@ static int ne_set_user_memory_region_ioctl(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct · line 989

Quoted 2 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
         list_add(&ne_mem_region->mem_region_list_entry,

&ne_enclave->mem_regions_list);

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,

Quoted 9 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +2 -2
-       for (i = 0; i < nr_phys_contig_mem_regions; i++) {
+       for (i = 0; i < phys_contig_mem_regions->num; i++) {
                 struct ne_pci_dev_cmd_reply cmd_reply = {};
                 struct slot_add_mem_req slot_add_mem_req = {};
+               struct range *range = phys_contig_mem_regions->region + i;
 
                 slot_add_mem_req.slot_uid = ne_enclave->slot_uid;
-               slot_add_mem_req.paddr =

page_to_phys(phys_contig_mem_regions[i]);

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct · line 966

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -1
-               slot_add_mem_req.size =

page_size(phys_contig_mem_regions[i]);

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 999

Quoted 2 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +2 -0
+               slot_add_mem_req.paddr = range->start;
+               slot_add_mem_req.size = range_len(range);

Similarly here:

slot_add_mem_req.paddr = phys_contig_mem_regions.regions[i].start; slot_add_mem_req.size = range_len(&phys_contig_mem_regions.regions[i]);

Then remain the kfree paths for "phys_contig_mem_regions.regions" instead of "phys_contig_mem_regions".

Thanks,
Andra

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct · line 999

Quoted 3 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
                 rc = ne_do_request(pdev, SLOT_ADD_MEM,
                                    &slot_add_mem_req,

sizeof(slot_add_mem_req),

Quoted 3 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -1
--

1.8.3.1

Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.

#4 lore raw

Re: [PATCH v3 1/4] nitro_enclaves: Merge contiguous physical memory regions

Quoted 7 lines · Cloud Infrastructure Service Product Dept.)

Hi Andra,

Sorry for the long delay.

I've read all your suggestions in each patch, there's no objection. I'll send v4 later, please review when you free, thanks.

Thank you, no problem. I'll review the patch series till the end of this week.

Andra

Quoted 318 lines
Quoted 318 lines

-----Original Message-----
From: Paraschiv, Andra-Irina [mailto:[email protected]]
Sent: Friday, October 15, 2021 9:34 PM
To: Longpeng (Mike, Cloud Infrastructure Service Product Dept.) <[email protected]>
Cc: Gonglei (Arei) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v3 1/4] nitro_enclaves: Merge contiguous physical memory regions

Quoted 9 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

There can be cases when there are more memory regions that need to be set for an enclave than the maximum supported number of memory regions per enclave. One example can be when the memory regions are backed by 2 MiB hugepages (the minimum supported hugepage size).

Let's merge the adjacent regions if they are physical contiguous. This

physical contiguous => physically contiguous

Quoted 8 lines

way the final number of memory regions is less than before merging and could potentially avoid reaching maximum.

---
Changes v2 -> v3:
- update the commit title and commit message. [Andra]
- use 'struct range' to instead of 'struct phys_mem_region'. [Andra, Greg

KH]

Quoted 4 lines

- add comments before the function definition. [Andra]
- rename several variables, parameters and function. [Andra]

---
   drivers/virt/nitro_enclaves/ne_misc_dev.c | 79

+++++++++++++++++++++----------

Review

Quoted 3 lines
   1 file changed, 55 insertions(+), 24 deletions(-)
(quoted diff) +0 -0

b/drivers/virt/nitro_enclaves/ne_misc_dev.c

drivers/virt/nitro_enclaves/ne_misc_dev.c line 29

Quoted 8 lines

index e21e1e8..eea53e9 100644

drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
@@ -26,6 +26,7 @@
   #include <linux/poll.h>
   #include <linux/slab.h>
   #include <linux/types.h>
+#include <linux/range.h>

Please keep the alphabetical order and move this before "#include <linux/slab.h>."

drivers/virt/nitro_enclaves/ne_misc_dev.c struct ne_cpu_pool { · line 130

Quoted 8 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
   #include <uapi/linux/vm_sockets.h>
 
   #include "ne_misc_dev.h"
@@ -126,6 +127,16 @@ struct ne_cpu_pool {
   static struct ne_cpu_pool ne_cpu_pool;
 
   /**
+ * struct phys_contig_mem_regions - Physical contiguous memory regions

Physical contiguous memory regions => Contiguous physical memory regions

drivers/virt/nitro_enclaves/ne_misc_dev.c struct ne_cpu_pool { · line 136

Quoted 6 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +6 -0
+ * @num:       The number of regions that currently has.
+ * @region:    The array of physical memory regions.
+ */
+struct phys_contig_mem_regions {
+       unsigned long num;
+       struct range region[0];

Should use "struct range *regions" instead, to keep a similar style with regard to arrays throughout the code.

Please align the fields name (e.g. [1]) so that it's easier to distinguish between fields type and name.

Let's also prefix with "ne" the data structure naming e.g. ne_phys_contig_mem_regions. So that is similar to other data structures defined in the NE kernel driver codebase.

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/dri
vers/virt/nitro_enclaves/ne_misc_dev.c#n118

drivers/virt/nitro_enclaves/ne_misc_dev.c struct ne_cpu_pool {

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +3 -0
+};
+
+/**
    * ne_check_enclaves_created() - Verify if at least one enclave has been

created.

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct

Quoted 3 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
    * @void:      No parameters provided.
    *
@@ -825,6 +836,33 @@ static int ne_sanity_check_user_mem_region_page(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct · line 839

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
   }
 
   /**
+ * ne_merge_phys_contig_memory_regions() - Add a memory region and merge the

adjacent

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct · line 840

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+ *                                         regions if they are physical

contiguous.

physical contiguous => physically contiguous

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct · line 841

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+ * @regions   : Private data associated with the physical contiguous memory

regions.

physical contiguous memory regions => contiguous physical memory regions

@regions => @phys_contig_regions

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 846

Quoted 5 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +5 -0
+ * @page_paddr: Physical start address of the region to be added.
+ * @page_size : Length of the region to be added.
+ *
+ * Return:
+ * * No return value.

Please add "Context: Process context. This function is called with the ne_enclave mutex held." before "Return", similar to other defined functions.

Can remove these lines, as for now there is no return value:

* Return:
* * No return value.

And then can add this in the second patch in the series:

* Context: Process context. This function is called with the ne_enclave mutex held. (note: already available in the first patch)
* Return: (note: added in the second patch)
* * 0 ...

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,

Quoted 3 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +3 -0
+ */
+static void
+ne_merge_phys_contig_memory_regions(struct phys_contig_mem_regions

*regions,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 850

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+                                   u64 page_paddr, u64 page_size)

phys_contig_mem_regions => ne_phys_contig_mem_regions *regions => *phys_contig_regions

Can define something like this:

unsigned long num = phys_contig_regions->num;

and then use it inside the function, except the last line that increments the num.

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 852

Quoted 2 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +2 -0
+{
+       /* Physical contiguous, just merge */

Physical contiguous => Physically contiguous

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 854

Quoted 2 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +2 -0
+       if (regions->num &&
+           (regions->region[regions->num - 1].end + 1) == page_paddr) {

e.g. phys_contig_regions->regions[num - 1]

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,

Quoted 12 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +11 -0
+               regions->region[regions->num - 1].end += page_size;
+
+               return;
+       }
+
+       regions->region[regions->num].start = page_paddr;
+       regions->region[regions->num].end = page_paddr + page_size - 1;
+       regions->num++;
+}
+
+/**
    * ne_set_user_memory_region_ioctl() - Add user space memory region to the

slot

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct

Quoted 3 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
    *                                    associated with the current enclave.
    * @ne_enclave :       Private data associated with the current enclave.
@@ -843,9 +881,9 @@ static int ne_set_user_memory_region_ioctl(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 885

Quoted 7 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -2
          unsigned long max_nr_pages = 0;
          unsigned long memory_size = 0;
          struct ne_mem_region *ne_mem_region = NULL;
-       unsigned long nr_phys_contig_mem_regions = 0;
          struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
-       struct page **phys_contig_mem_regions = NULL;
+       struct phys_contig_mem_regions *phys_contig_mem_regions = NULL;

struct phys_contig_mem_regions *phys_contig_mem_regions = NULL; => struct ne_phys_contig_mem_regions phys_contig_mem_regions = {};

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct

Quoted 5 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+       size_t size_to_alloc = 0;
          int rc = -EINVAL;
 
          rc = ne_sanity_check_user_mem_region(ne_enclave, mem_region);
@@ -866,8 +904,9 @@ static int ne_set_user_memory_region_ioctl(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -1
                  goto free_mem_region;
          }
 
-       phys_contig_mem_regions = kcalloc(max_nr_pages,

sizeof(*phys_contig_mem_regions),

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 909

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +3 -1
-                                         GFP_KERNEL);
+       size_to_alloc = sizeof(*phys_contig_mem_regions) +
+                       max_nr_pages * sizeof(struct range);
+       phys_contig_mem_regions = kzalloc(size_to_alloc, GFP_KERNEL);

Then can alloc memory for the regions field.

phys_contig_mem_regions.regions = kcalloc(max_nr_pages, sizeof(*phys_contig_mem_regions.regions), GFP_KERNEL);

The "size_of_alloc" will not be needed in this case.

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
          if (!phys_contig_mem_regions) {
                  rc = -ENOMEM;
 
@@ -901,26 +940,16 @@ static int ne_set_user_memory_region_ioctl(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,

Quoted 10 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -6
                  if (rc < 0)
                          goto put_pages;
 
-               /*
-                * TODO: Update once handled non-contiguous memory regions
-                * received from user space or contiguous physical memory regions
-                * larger than 2 MiB e.g. 8 MiB.
-                */
-               phys_contig_mem_regions[i] = ne_mem_region->pages[i];
+

ne_merge_phys_contig_memory_regions(phys_contig_mem_regions,

And can pass it here like this: &phys_contig_mem_regions.

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct · line 944

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+

page_to_phys(ne_mem_region->pages[i]),

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct · line 945

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -0
+

page_size(ne_mem_region->pages[i]));

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct

Quoted 19 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -8
                  memory_size += page_size(ne_mem_region->pages[i]);
 
                  ne_mem_region->nr_pages++;
          } while (memory_size < mem_region.memory_size);
 
-       /*
-        * TODO: Update once handled non-contiguous memory regions received
-        * from user space or contiguous physical memory regions larger than
-        * 2 MiB e.g. 8 MiB.
-        */
-       nr_phys_contig_mem_regions = ne_mem_region->nr_pages;
-
-       if ((ne_enclave->nr_mem_regions + nr_phys_contig_mem_regions) >
+       if ((ne_enclave->nr_mem_regions + phys_contig_mem_regions->num) >
              ne_enclave->max_mem_regions) {
                  dev_err_ratelimited(ne_misc_dev.this_device,
                                      "Reached max memory regions %lld\n",
@@ -931,9 +960,10 @@ static int ne_set_user_memory_region_ioctl(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,

Quoted 5 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -2
                  goto put_pages;
          }
 
-       for (i = 0; i < nr_phys_contig_mem_regions; i++) {
-               u64 phys_region_addr =

page_to_phys(phys_contig_mem_regions[i]);

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 966

Quoted 5 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +4 -1
-               u64 phys_region_size = page_size(phys_contig_mem_regions[i]);
+       for (i = 0; i < phys_contig_mem_regions->num; i++) {
+               struct range *range = phys_contig_mem_regions->region + i;
+               u64 phys_region_addr = range->start;
+               u64 phys_region_size = range_len(range);

With the updated data structure field, could be:

u64 phys_region_addr = phys_contig_mem_regions.regions[i].start; u64 phys_region_size = range_len(&phys_contig_mem_regions.regions[i]);

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
                  if (phys_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
                          dev_err_ratelimited(ne_misc_dev.this_device,
@@ -959,13 +989,14 @@ static int ne_set_user_memory_region_ioctl(struct

ne_enclave *ne_enclave,

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct · line 989

Quoted 2 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
          list_add(&ne_mem_region->mem_region_list_entry,

&ne_enclave->mem_regions_list);

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,

Quoted 9 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +2 -2
-       for (i = 0; i < nr_phys_contig_mem_regions; i++) {
+       for (i = 0; i < phys_contig_mem_regions->num; i++) {
                  struct ne_pci_dev_cmd_reply cmd_reply = {};
                  struct slot_add_mem_req slot_add_mem_req = {};
+               struct range *range = phys_contig_mem_regions->region + i;
 
                  slot_add_mem_req.slot_uid = ne_enclave->slot_uid;
-               slot_add_mem_req.paddr =

page_to_phys(phys_contig_mem_regions[i]);

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct · line 966

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -1
-               slot_add_mem_req.size =

page_size(phys_contig_mem_regions[i]);

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, · line 999

Quoted 2 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +2 -0
+               slot_add_mem_req.paddr = range->start;
+               slot_add_mem_req.size = range_len(range);

Similarly here:

slot_add_mem_req.paddr = phys_contig_mem_regions.regions[i].start; slot_add_mem_req.size = range_len(&phys_contig_mem_regions.regions[i]);

Then remain the kfree paths for "phys_contig_mem_regions.regions" instead of "phys_contig_mem_regions".

Thanks,
Andra

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_set_user_memory_region_ioctl(struct · line 999

Quoted 3 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
                  rc = ne_do_request(pdev, SLOT_ADD_MEM,
                                     &slot_add_mem_req,

sizeof(slot_add_mem_req),

Quoted 3 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -1
--

1.8.3.1

Quoted 3 lines
Quoted 3 lines

Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.

Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.

#5 lore raw

PATCH v3 2/4 nitro_enclaves: Sanity check physical memory regions during merging

In reply to Longpeng(Mike)

From: Longpeng <[email protected]>

Sanity check the physical memory regions during the merge of contiguous regions. Thus we can test the physical memory regions setup logic individually, including the error cases coming from the sanity checks.

---
Changes v2 -> v3:
- update the commit title and commit message. [Andra]
- add comments before the function definition. [Andra]
- remove 'inline' attribute of ne_sanity_check_phys_mem_region. [Andra]
- leave a blank line before return. [Andra]
- move sanity check in ne_merge_phys_contig_memory_regions to the beginning of the function. [Andra]
- double sanity checking after the merge of physical contiguous memory regions has been completed. [Andra]

---
 drivers/virt/nitro_enclaves/ne_misc_dev.c | 79 ++++++++++++++++++++-----------
 1 file changed, 52 insertions(+), 27 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +52 -27
index eea53e9..a8fa56b 100644
@@ -836,6 +836,36 @@ static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,
 }
 
 /**
+ * ne_sanity_check_phys_mem_region() - Sanity check the start address and the size
+ *                                     of a physical memory region.
+ * @phys_mem_region_paddr : Physical start address of the region to be sanity checked.
+ * @phys_mem_region_size  : Length of the region to be sanity checked.
+ *
+ * Return:
+ * * 0 on success.
+ * * Negative return value on failure.
+ */
+static int ne_sanity_check_phys_mem_region(u64 phys_mem_region_paddr,
+					   u64 phys_mem_region_size)
+{
+	if (phys_mem_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
+		dev_err_ratelimited(ne_misc_dev.this_device,
+				    "Physical mem region size is not multiple of 2 MiB\n");
+
+		return -EINVAL;
+	}
+
+	if (!IS_ALIGNED(phys_mem_region_paddr, NE_MIN_MEM_REGION_SIZE)) {
+		dev_err_ratelimited(ne_misc_dev.this_device,
+				    "Physical mem region address is not 2 MiB aligned\n");
+
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+/**
  * ne_merge_phys_contig_memory_regions() - Add a memory region and merge the adjacent
  *                                         regions if they are physical contiguous.
  * @regions   : Private data associated with the physical contiguous memory regions.
@@ -843,23 +873,30 @@ static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,
  * @page_size : Length of the region to be added.
  *
  * Return:
- * * No return value.
+ * * 0 on success.
+ * * Negative return value on failure.
  */
-static void
+static int
 ne_merge_phys_contig_memory_regions(struct phys_contig_mem_regions *regions,
 				    u64 page_paddr, u64 page_size)
 {
-	/* Physical contiguous, just merge */
+	int rc = 0;
+
+	rc = ne_sanity_check_phys_mem_region(page_paddr, page_size);
+	if (rc < 0)
+		return rc;
+
 	if (regions->num &&
 	    (regions->region[regions->num - 1].end + 1) == page_paddr) {
+		/* Physical contiguous, just merge */
 		regions->region[regions->num - 1].end += page_size;
-
-		return;
+	} else {
+		regions->region[regions->num].start = page_paddr;
+		regions->region[regions->num].end = page_paddr + page_size - 1;
+		regions->num++;
 	}
 
-	regions->region[regions->num].start = page_paddr;
-	regions->region[regions->num].end = page_paddr + page_size - 1;
-	regions->num++;
+	return 0;
 }
 
 /**
@@ -940,9 +977,11 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
 		if (rc < 0)
 			goto put_pages;
 
-		ne_merge_phys_contig_memory_regions(phys_contig_mem_regions,
-						    page_to_phys(ne_mem_region->pages[i]),
-						    page_size(ne_mem_region->pages[i]));
+		rc = ne_merge_phys_contig_memory_regions(phys_contig_mem_regions,
+							 page_to_phys(ne_mem_region->pages[i]),
+							 page_size(ne_mem_region->pages[i]));
+		if (rc < 0)
+			goto put_pages;
 
 		memory_size += page_size(ne_mem_region->pages[i]);
 
@@ -965,23 +1004,9 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
 		u64 phys_region_addr = range->start;
 		u64 phys_region_size = range_len(range);
 
-		if (phys_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
-			dev_err_ratelimited(ne_misc_dev.this_device,
-					    "Physical mem region size is not multiple of 2 MiB\n");
-
-			rc = -EINVAL;
-
-			goto put_pages;
-		}
-
-		if (!IS_ALIGNED(phys_region_addr, NE_MIN_MEM_REGION_SIZE)) {
-			dev_err_ratelimited(ne_misc_dev.this_device,
-					    "Physical mem region address is not 2 MiB aligned\n");
-
-			rc = -EINVAL;
-
+		rc = ne_sanity_check_phys_mem_region(phys_region_addr, phys_region_size);
+		if (rc < 0)
 			goto put_pages;
-		}
 	}
 
 	ne_mem_region->memory_size = mem_region.memory_size;
Signature · 1 line
1.8.3.1
#6 lore raw

Re: [PATCH v3 2/4] nitro_enclaves: Sanity check physical memory regions during merging

Quoted 27 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

Sanity check the physical memory regions during the merge of contiguous regions. Thus we can test the physical memory regions setup logic individually, including the error cases coming from the sanity checks.

---
Changes v2 -> v3:
- update the commit title and commit message. [Andra]
- add comments before the function definition. [Andra]
- remove 'inline' attribute of ne_sanity_check_phys_mem_region. [Andra]
- leave a blank line before return. [Andra]
- move sanity check in ne_merge_phys_contig_memory_regions to the beginning of the function. [Andra]
- double sanity checking after the merge of physical contiguous memory regions has been completed. [Andra]

---
  drivers/virt/nitro_enclaves/ne_misc_dev.c | 79 ++++++++++++++++++++-----------
  1 file changed, 52 insertions(+), 27 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +0 -0
index eea53e9..a8fa56b 100644

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 844

Quoted 9 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +6 -0
@@ -836,6 +836,36 @@ static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,
  }
 
  /**
+ * ne_sanity_check_phys_mem_region() - Sanity check the start address and the size
+ *                                     of a physical memory region.
+ * @phys_mem_region_paddr : Physical start address of the region to be sanity checked.
+ * @phys_mem_region_size  : Length of the region to be sanity checked.
+ *
+ * Return:

Please add this just before "Return", similar to other defined functions (e.g. [1]):

* Context: Process context. This function is called with the ne_enclave mutex held.
* Return:
......

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/virt/nitro_enclaves/ne_misc_dev.c#n725

Quoted 40 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +27 -2
+ * * 0 on success.
+ * * Negative return value on failure.
+ */
+static int ne_sanity_check_phys_mem_region(u64 phys_mem_region_paddr,
+                                          u64 phys_mem_region_size)
+{
+       if (phys_mem_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
+               dev_err_ratelimited(ne_misc_dev.this_device,
+                                   "Physical mem region size is not multiple of 2 MiB\n");
+
+               return -EINVAL;
+       }
+
+       if (!IS_ALIGNED(phys_mem_region_paddr, NE_MIN_MEM_REGION_SIZE)) {
+               dev_err_ratelimited(ne_misc_dev.this_device,
+                                   "Physical mem region address is not 2 MiB aligned\n");
+
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+/**
   * ne_merge_phys_contig_memory_regions() - Add a memory region and merge the adjacent
   *                                         regions if they are physical contiguous.
   * @regions   : Private data associated with the physical contiguous memory regions.
@@ -843,23 +873,30 @@ static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,
   * @page_size : Length of the region to be added.
   *
   * Return:
- * * No return value.
+ * * 0 on success.
+ * * Negative return value on failure.
   */
-static void
+static int
  ne_merge_phys_contig_memory_regions(struct phys_contig_mem_regions *regions,
                                     u64 page_paddr, u64 page_size)
  {

drivers/virt/nitro_enclaves/ne_misc_dev.c static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, · line 891

Quoted 10 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +7 -1
-       /* Physical contiguous, just merge */
+       int rc = 0;
+
+       rc = ne_sanity_check_phys_mem_region(page_paddr, page_size);
+       if (rc < 0)
+               return rc;
+
         if (regions->num &&
             (regions->region[regions->num - 1].end + 1) == page_paddr) {
+               /* Physical contiguous, just merge */

Can move this comment before the "if (...)" line.

Thanks,
Andra

Quoted 58 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +12 -25
                 regions->region[regions->num - 1].end += page_size;
-
-               return;
+       } else {
+               regions->region[regions->num].start = page_paddr;
+               regions->region[regions->num].end = page_paddr + page_size - 1;
+               regions->num++;
         }
 
-       regions->region[regions->num].start = page_paddr;
-       regions->region[regions->num].end = page_paddr + page_size - 1;
-       regions->num++;
+       return 0;
  }
 
  /**
@@ -940,9 +977,11 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
                 if (rc < 0)
                         goto put_pages;
 
-               ne_merge_phys_contig_memory_regions(phys_contig_mem_regions,
-                                                   page_to_phys(ne_mem_region->pages[i]),
-                                                   page_size(ne_mem_region->pages[i]));
+               rc = ne_merge_phys_contig_memory_regions(phys_contig_mem_regions,
+                                                        page_to_phys(ne_mem_region->pages[i]),
+                                                        page_size(ne_mem_region->pages[i]));
+               if (rc < 0)
+                       goto put_pages;
 
                 memory_size += page_size(ne_mem_region->pages[i]);
 
@@ -965,23 +1004,9 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
                 u64 phys_region_addr = range->start;
                 u64 phys_region_size = range_len(range);
 
-               if (phys_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
-                       dev_err_ratelimited(ne_misc_dev.this_device,
-                                           "Physical mem region size is not multiple of 2 MiB\n");
-
-                       rc = -EINVAL;
-
-                       goto put_pages;
-               }
-
-               if (!IS_ALIGNED(phys_region_addr, NE_MIN_MEM_REGION_SIZE)) {
-                       dev_err_ratelimited(ne_misc_dev.this_device,
-                                           "Physical mem region address is not 2 MiB aligned\n");
-
-                       rc = -EINVAL;
-
+               rc = ne_sanity_check_phys_mem_region(phys_region_addr, phys_region_size);
+               if (rc < 0)
                         goto put_pages;
-               }
         }
 
         ne_mem_region->memory_size = mem_region.memory_size;
--
Quoted 2 lines

1.8.3.1

Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.

#7 lore raw

PATCH v3 3/4 nitro_enclaves: Add KUnit tests setup for the misc device functionality

In reply to Longpeng(Mike)

From: Longpeng <[email protected]>

Add the initial setup for the KUnit tests that will target the Nitro Enclaves misc device functionality.

---
Changes v2 -> v3:
- update the commit title and commit message. [Andra]
- use "misc_dev"/"misc device"/"MISC_DEV" to be more specific. [Andra]

---
 drivers/virt/nitro_enclaves/Kconfig            |  9 +++++++++
 drivers/virt/nitro_enclaves/ne_misc_dev.c      | 27 ++++++++++++++++++++++++++
 drivers/virt/nitro_enclaves/ne_misc_dev_test.c | 17 ++++++++++++++++
 3 files changed, 53 insertions(+)
 create mode 100644 drivers/virt/nitro_enclaves/ne_misc_dev_test.c
drivers/virt/nitro_enclaves/Kconfig +9 -0
index 8c9387a..90802b1 100644
@@ -18,3 +18,12 @@ config NITRO_ENCLAVES
 
 	  To compile this driver as a module, choose M here.
 	  The module will be called nitro_enclaves.
+
+config NITRO_ENCLAVES_MISC_DEV_TEST
+	bool "Tests for the misc device functionality of the Nitro enclaves"
+	depends on NITRO_ENCLAVES && KUNIT=y
+	help
+	  Enable KUnit tests for the misc device functionality of the Nitro
+	  Enclaves. Select this option only if you will boot the kernel for
+	  the purpose of running unit tests (e.g. under UML or qemu). If
+	  unsure, say N.
drivers/virt/nitro_enclaves/ne_misc_dev.c +27 -0
index a8fa56b..f895fc3 100644
@@ -1756,8 +1756,33 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	return 0;
 }
 
+#if defined(CONFIG_NITRO_ENCLAVES_MISC_DEV_TEST)
+#include "ne_misc_dev_test.c"
+
+static inline int ne_misc_dev_test_init(void)
+{
+	return __kunit_test_suites_init(ne_misc_dev_test_suites);
+}
+
+static inline void ne_misc_dev_test_exit(void)
+{
+	__kunit_test_suites_exit(ne_misc_dev_test_suites);
+}
+#else
+static inline int ne_misc_dev_test_init(void)
+{
+	return 0;
+}
+
+static inline void ne_misc_dev_test_exit(void)
+{
+}
+#endif
+
 static int __init ne_init(void)
 {
+	ne_misc_dev_test_init();
+
 	mutex_init(&ne_cpu_pool.mutex);
 
 	return pci_register_driver(&ne_pci_driver);
@@ -1768,6 +1793,8 @@ static void __exit ne_exit(void)
 	pci_unregister_driver(&ne_pci_driver);
 
 	ne_teardown_cpu_pool();
+
+	ne_misc_dev_test_exit();
 }
 
 module_init(ne_init);
drivers/virt/nitro_enclaves/ne_misc_dev_test.c new file +17 -0
new file mode 100644
index 0000000..bcb755e
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <kunit/test.h>
+
+static struct kunit_case ne_misc_dev_test_cases[] = {
+	{}
+};
+
+static struct kunit_suite ne_misc_dev_test_suite = {
+	.name = "ne_misc_dev_test",
+	.test_cases = ne_misc_dev_test_cases,
+};
+
+static struct kunit_suite *ne_misc_dev_test_suites[] = {
+	&ne_misc_dev_test_suite,
+	NULL
+};
Signature · 1 line
1.8.3.1
#8 lore raw

Re: [PATCH v3 3/4] nitro_enclaves: Add KUnit tests setup for the misc device functionality

Quoted 22 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

Add the initial setup for the KUnit tests that will target the Nitro Enclaves misc device functionality.

---
Changes v2 -> v3:
- update the commit title and commit message. [Andra]
- use "misc_dev"/"misc device"/"MISC_DEV" to be more specific. [Andra]

---
  drivers/virt/nitro_enclaves/Kconfig            |  9 +++++++++
  drivers/virt/nitro_enclaves/ne_misc_dev.c      | 27 ++++++++++++++++++++++++++
  drivers/virt/nitro_enclaves/ne_misc_dev_test.c | 17 ++++++++++++++++
  3 files changed, 53 insertions(+)

create mode 100644 drivers/virt/nitro_enclaves/ne_misc_dev_test.c

drivers/virt/nitro_enclaves/Kconfig +0 -0
index 8c9387a..90802b1 100644

drivers/virt/nitro_enclaves/Kconfig config NITRO_ENCLAVES · line 23

Quoted 6 lines
drivers/virt/nitro_enclaves/Kconfig +3 -0
@@ -18,3 +18,12 @@ config NITRO_ENCLAVES
 
  	  To compile this driver as a module, choose M here.
  	  The module will be called nitro_enclaves.
+
+config NITRO_ENCLAVES_MISC_DEV_TEST
+	bool "Tests for the misc device functionality of the Nitro enclaves"

Nitro enclaves => Nitro Enclaves

Quoted 30 lines
drivers/virt/nitro_enclaves/Kconfig +6 -0
+	depends on NITRO_ENCLAVES && KUNIT=y
+	help
+	  Enable KUnit tests for the misc device functionality of the Nitro
+	  Enclaves. Select this option only if you will boot the kernel for
+	  the purpose of running unit tests (e.g. under UML or qemu). If
+	  unsure, say N.
drivers/virt/nitro_enclaves/ne_misc_dev.c +16 -0
index a8fa56b..f895fc3 100644
@@ -1756,8 +1756,33 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  	return 0;
  }
 
+#if defined(CONFIG_NITRO_ENCLAVES_MISC_DEV_TEST)
+#include "ne_misc_dev_test.c"
+
+static inline int ne_misc_dev_test_init(void)
+{
+	return __kunit_test_suites_init(ne_misc_dev_test_suites);
+}
+
+static inline void ne_misc_dev_test_exit(void)
+{
+	__kunit_test_suites_exit(ne_misc_dev_test_suites);
+}
+#else
+static inline int ne_misc_dev_test_init(void)
+{
+	return 0;

drivers/virt/nitro_enclaves/ne_misc_dev.c static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg) · line 1784

Quoted 10 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +8 -0
+}
+
+static inline void ne_misc_dev_test_exit(void)
+{
+}
+#endif
+
  static int __init ne_init(void)
  {
+	ne_misc_dev_test_init();

Let's have:

int rc = 0;

and then check the return code of "ne_misc_dev_test_init()".

Quoted 19 lines
drivers/virt/nitro_enclaves/ne_misc_dev.c +3 -0
+
  	mutex_init(&ne_cpu_pool.mutex);
 
  	return pci_register_driver(&ne_pci_driver);
@@ -1768,6 +1793,8 @@ static void __exit ne_exit(void)
  	pci_unregister_driver(&ne_pci_driver);
 
  	ne_teardown_cpu_pool();
+
+	ne_misc_dev_test_exit();
  }
 
  module_init(ne_init);
drivers/virt/nitro_enclaves/ne_misc_dev_test.c new file +0 -0
new file mode 100644
index 0000000..bcb755e

drivers/virt/nitro_enclaves/ne_misc_dev_test.c line 1

Quoted 1 line
drivers/virt/nitro_enclaves/ne_misc_dev_test.c new file +1 -0
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0-or-later

Let's keep the same marker as in the other files of the NE kernel driver codebase.

// SPDX-License-Identifier: GPL-2.0

Thanks,
Andra

Quoted 7 lines
drivers/virt/nitro_enclaves/ne_misc_dev_test.c new file +6 -0
+
+#include <kunit/test.h>
+
+static struct kunit_case ne_misc_dev_test_cases[] = {
+	{}
+};

drivers/virt/nitro_enclaves/ne_misc_dev_test.c line 17

Quoted 10 lines
drivers/virt/nitro_enclaves/ne_misc_dev_test.c new file +10 -0
+
+static struct kunit_suite ne_misc_dev_test_suite = {
+	.name = "ne_misc_dev_test",
+	.test_cases = ne_misc_dev_test_cases,
+};
+
+static struct kunit_suite *ne_misc_dev_test_suites[] = {
+	&ne_misc_dev_test_suite,
+	NULL
+};

Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.

#9 lore raw

PATCH v3 4/4 nitro_enclaves: Add KUnit tests for contiguous physical memory regions merging

In reply to Longpeng(Mike)

From: Longpeng <[email protected]>

Add KUnit tests for the contiguous physical memory regions merging functionality from the Nitro Enclaves misc device logic.

We'll see the following message using dmesg if everything goes well:

[...] # Subtest: ne_misc_dev_test
[...] 1..1
[...] (NULL device *): Physical mem region address is not 2 MiB aligned [...] (NULL device *): Physical mem region size is not multiple of 2 MiB [...] (NULL device *): Physical mem region address is not 2 MiB aligned
[...] ok 1 - ne_misc_dev_test_merge_phys_contig_memory_regions
[...] ok 1 - ne_misc_dev_test

---
Changes v2 -> v3:
- update the commit title and commit message. [Andra]
- align the fileds in 'struct phys_regions_test'. [Andra]
- rename 'phys_regions_testcases' to 'phys_regions_test_cases'. [Andra]
- add comments before each test cases. [Andra]
- initialize the variables in ne_misc_dev_test_merge_phys_contig_memory_regions. [Andra]

---
 drivers/virt/nitro_enclaves/ne_misc_dev_test.c | 136 +++++++++++++++++++++++++
 1 file changed, 136 insertions(+)
drivers/virt/nitro_enclaves/ne_misc_dev_test.c +136 -0
index bcb755e..7bd6b34 100644
@@ -2,7 +2,143 @@
 
 #include <kunit/test.h>
 
+#define MAX_PHYS_REGIONS	16
+#define INVALID_VALUE		(~0ull)
+
+struct phys_regions_test {
+	u64 paddr;
+	u64 size;
+	int expect_rc;
+	int expect_num;
+	u64 expect_last_paddr;
+	u64 expect_last_size;
+} phys_regions_test_cases[] = {
+	/*
+	 * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
+	 *   Expected result:
+	 *       Failed, start address is not 2M-aligned
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 0
+	 *   region = {}
+	 */
+	{0x1000, 0x200000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
+
+	/*
+	 * Add the region from 0x200000 to (0x200000 + 0x1000 - 1):
+	 *   Expected result:
+	 *       Failed, size is not 2M-aligned
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 0
+	 *   region = {}
+	 */
+	{0x200000, 0x1000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
+
+	/*
+	 * Add the region from 0x200000 to (0x200000 + 0x200000 - 1):
+	 *   Expected result:
+	 *       Successful
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 1
+	 *   region = {
+	 *       {start=0x200000, end=0x3fffff}, // len=0x200000
+	 *   }
+	 */
+	{0x200000, 0x200000, 0, 1, 0x200000, 0x200000},
+
+	/*
+	 * Add the region from 0x0 to (0x0 + 0x200000 - 1):
+	 *   Expected result:
+	 *       Successful
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 2
+	 *   region = {
+	 *       {start=0x200000, end=0x3fffff}, // len=0x200000
+	 *       {start=0x0,      end=0x1fffff}, // len=0x200000
+	 *   }
+	 */
+	{0x0, 0x200000, 0, 2, 0x0, 0x200000},
+
+	/*
+	 * Add the region from 0x600000 to (0x600000 + 0x400000 - 1):
+	 *   Expected result:
+	 *       Successful
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 3
+	 *   region = {
+	 *       {start=0x200000, end=0x3fffff}, // len=0x200000
+	 *       {start=0x0,      end=0x1fffff}, // len=0x200000
+	 *       {start=0x600000, end=0x9fffff}, // len=0x400000
+	 *   }
+	 */
+	{0x600000, 0x400000, 0, 3, 0x600000, 0x400000},
+
+	/*
+	 * Add the region from 0xa00000 to (0xa00000 + 0x400000 - 1):
+	 *   Expected result:
+	 *       Successful, merging case!
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 3
+	 *   region = {
+	 *       {start=0x200000, end=0x3fffff}, // len=0x200000
+	 *       {start=0x0,      end=0x1fffff}, // len=0x200000
+	 *       {start=0x600000, end=0xdfffff}, // len=0x800000
+	 *   }
+	 */
+	{0xa00000, 0x400000, 0, 3, 0x600000, 0x800000},
+
+	/*
+	 * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
+	 *   Expected result:
+	 *       Failed, start address is not 2M-aligned
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 3
+	 *   region = {
+	 *       {start=0x200000, end=0x3fffff}, // len=0x200000
+	 *       {start=0x0,      end=0x1fffff}, // len=0x200000
+	 *       {start=0x600000, end=0xdfffff}, // len=0x800000
+	 *   }
+	 */
+	{0x1000, 0x200000, -EINVAL, 3, 0x600000, 0x800000},
+};
+
+static void ne_misc_dev_test_merge_phys_contig_memory_regions(struct kunit *test)
+{
+	struct phys_contig_mem_regions *regions;
+	size_t sz = 0;
+	int rc = 0;
+	int i = 0;
+
+	sz = sizeof(*regions) + MAX_PHYS_REGIONS * sizeof(struct range);
+	regions = kunit_kzalloc(test, sz, GFP_KERNEL);
+	KUNIT_ASSERT_TRUE(test, regions != NULL);
+
+	for (i = 0; i < ARRAY_SIZE(phys_regions_test_cases); i++) {
+		struct phys_regions_test *entry = phys_regions_test_cases + i;
+
+		rc = ne_merge_phys_contig_memory_regions(regions,
+							 entry->paddr, entry->size);
+		KUNIT_EXPECT_EQ(test, rc, entry->expect_rc);
+		KUNIT_EXPECT_EQ(test, regions->num, entry->expect_num);
+
+		if (entry->expect_last_paddr == INVALID_VALUE)
+			continue;
+
+		KUNIT_EXPECT_EQ(test, regions->region[regions->num - 1].start,
+				entry->expect_last_paddr);
+		KUNIT_EXPECT_EQ(test, range_len(&regions->region[regions->num - 1]),
+				entry->expect_last_size);
+	}
+}
+
 static struct kunit_case ne_misc_dev_test_cases[] = {
+	KUNIT_CASE(ne_misc_dev_test_merge_phys_contig_memory_regions),
 	{}
 };
 
Signature · 1 line
1.8.3.1
#10 lore raw

Re: [PATCH v3 4/4] nitro_enclaves: Add KUnit tests for contiguous physical memory regions merging

Quoted 33 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

Add KUnit tests for the contiguous physical memory regions merging functionality from the Nitro Enclaves misc device logic.

We'll see the following message using dmesg if everything goes well:

[...] # Subtest: ne_misc_dev_test
[...] 1..1
[...] (NULL device *): Physical mem region address is not 2 MiB aligned [...] (NULL device *): Physical mem region size is not multiple of 2 MiB [...] (NULL device *): Physical mem region address is not 2 MiB aligned
[...] ok 1 - ne_misc_dev_test_merge_phys_contig_memory_regions
[...] ok 1 - ne_misc_dev_test

---
Changes v2 -> v3:
- update the commit title and commit message. [Andra]
- align the fileds in 'struct phys_regions_test'. [Andra]
- rename 'phys_regions_testcases' to 'phys_regions_test_cases'. [Andra]
- add comments before each test cases. [Andra]
- initialize the variables in ne_misc_dev_test_merge_phys_contig_memory_regions. [Andra]

---
  drivers/virt/nitro_enclaves/ne_misc_dev_test.c | 136 +++++++++++++++++++++++++
  1 file changed, 136 insertions(+)
drivers/virt/nitro_enclaves/ne_misc_dev_test.c +0 -0
index bcb755e..7bd6b34 100644
@@ -2,7 +2,143 @@
 

drivers/virt/nitro_enclaves/ne_misc_dev_test.c line 12

Quoted 10 lines
drivers/virt/nitro_enclaves/ne_misc_dev_test.c +8 -0
  #include <kunit/test.h>
 
+#define MAX_PHYS_REGIONS	16
+#define INVALID_VALUE		(~0ull)
+
+struct phys_regions_test {
+	u64 paddr;
+	u64 size;
+	int expect_rc;
+	int expect_num;

Please keep the same field type as "num": "unsigned long".

And just align the field names as mentioned in the first patch of the series, so they are easily visualized.

Quoted 86 lines
drivers/virt/nitro_enclaves/ne_misc_dev_test.c +86 -0
+	u64 expect_last_paddr;
+	u64 expect_last_size;
+} phys_regions_test_cases[] = {
+	/*
+	 * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
+	 *   Expected result:
+	 *       Failed, start address is not 2M-aligned
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 0
+	 *   region = {}
+	 */
+	{0x1000, 0x200000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
+
+	/*
+	 * Add the region from 0x200000 to (0x200000 + 0x1000 - 1):
+	 *   Expected result:
+	 *       Failed, size is not 2M-aligned
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 0
+	 *   region = {}
+	 */
+	{0x200000, 0x1000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
+
+	/*
+	 * Add the region from 0x200000 to (0x200000 + 0x200000 - 1):
+	 *   Expected result:
+	 *       Successful
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 1
+	 *   region = {
+	 *       {start=0x200000, end=0x3fffff}, // len=0x200000
+	 *   }
+	 */
+	{0x200000, 0x200000, 0, 1, 0x200000, 0x200000},
+
+	/*
+	 * Add the region from 0x0 to (0x0 + 0x200000 - 1):
+	 *   Expected result:
+	 *       Successful
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 2
+	 *   region = {
+	 *       {start=0x200000, end=0x3fffff}, // len=0x200000
+	 *       {start=0x0,      end=0x1fffff}, // len=0x200000
+	 *   }
+	 */
+	{0x0, 0x200000, 0, 2, 0x0, 0x200000},
+
+	/*
+	 * Add the region from 0x600000 to (0x600000 + 0x400000 - 1):
+	 *   Expected result:
+	 *       Successful
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 3
+	 *   region = {
+	 *       {start=0x200000, end=0x3fffff}, // len=0x200000
+	 *       {start=0x0,      end=0x1fffff}, // len=0x200000
+	 *       {start=0x600000, end=0x9fffff}, // len=0x400000
+	 *   }
+	 */
+	{0x600000, 0x400000, 0, 3, 0x600000, 0x400000},
+
+	/*
+	 * Add the region from 0xa00000 to (0xa00000 + 0x400000 - 1):
+	 *   Expected result:
+	 *       Successful, merging case!
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 3
+	 *   region = {
+	 *       {start=0x200000, end=0x3fffff}, // len=0x200000
+	 *       {start=0x0,      end=0x1fffff}, // len=0x200000
+	 *       {start=0x600000, end=0xdfffff}, // len=0x800000
+	 *   }
+	 */
+	{0xa00000, 0x400000, 0, 3, 0x600000, 0x800000},
+
+	/*
+	 * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
+	 *   Expected result:
+	 *       Failed, start address is not 2M-aligned

drivers/virt/nitro_enclaves/ne_misc_dev_test.c line 108

Quoted 10 lines
drivers/virt/nitro_enclaves/ne_misc_dev_test.c +10 -0
+	 *
+	 * Now the instance of struct phys_contig_mem_regions is:
+	 *   num = 3
+	 *   region = {
+	 *       {start=0x200000, end=0x3fffff}, // len=0x200000
+	 *       {start=0x0,      end=0x1fffff}, // len=0x200000
+	 *       {start=0x600000, end=0xdfffff}, // len=0x800000
+	 *   }
+	 */
+	{0x1000, 0x200000, -EINVAL, 3, 0x600000, 0x800000},

Nice, I like how the comments are structured.

Please also update from "region" to "regions" and "struct phys_contig_mem_regions" to "ne_phys_contig_mem_regions", after the suggested naming changes in the first patch of the series.

drivers/virt/nitro_enclaves/ne_misc_dev_test.c line 113

Quoted 5 lines
drivers/virt/nitro_enclaves/ne_misc_dev_test.c +5 -0
+};
+
+static void ne_misc_dev_test_merge_phys_contig_memory_regions(struct kunit *test)
+{
+	struct phys_contig_mem_regions *regions;

struct phys_contig_mem_regions *regions; => struct ne_phys_contig_mem_regions phys_contig_mem_regions = {};

drivers/virt/nitro_enclaves/ne_misc_dev_test.c line 119

Quoted 6 lines
drivers/virt/nitro_enclaves/ne_misc_dev_test.c +6 -0
+	size_t sz = 0;
+	int rc = 0;
+	int i = 0;
+
+	sz = sizeof(*regions) + MAX_PHYS_REGIONS * sizeof(struct range);
+	regions = kunit_kzalloc(test, sz, GFP_KERNEL);

And then can alloc the necessary memory for the "regions" field:

phys_contig_mem_regions.regions = kunit_kcalloc(test, MAX_PHYS_REGIONS, sizeof(*phys_contig_mem_regions.regions), GFP_KERNEL);

drivers/virt/nitro_enclaves/ne_misc_dev_test.c line 123

Quoted 4 lines
drivers/virt/nitro_enclaves/ne_misc_dev_test.c +4 -0
+	KUNIT_ASSERT_TRUE(test, regions != NULL);
+
+	for (i = 0; i < ARRAY_SIZE(phys_regions_test_cases); i++) {
+		struct phys_regions_test *entry = phys_regions_test_cases + i;

Could rename "entry" to "test_case".

Can also add:

unsigned long num = 0;

drivers/virt/nitro_enclaves/ne_misc_dev_test.c line 126

Quoted 3 lines
drivers/virt/nitro_enclaves/ne_misc_dev_test.c +3 -0
+
+		rc = ne_merge_phys_contig_memory_regions(regions,
+							 entry->paddr, entry->size);

And then can update it here and use it further on:

num = phys_contig_mem_regions.num;

drivers/virt/nitro_enclaves/ne_misc_dev_test.c line 137

Quoted 11 lines
drivers/virt/nitro_enclaves/ne_misc_dev_test.c +11 -0
+		KUNIT_EXPECT_EQ(test, rc, entry->expect_rc);
+		KUNIT_EXPECT_EQ(test, regions->num, entry->expect_num);
+
+		if (entry->expect_last_paddr == INVALID_VALUE)
+			continue;
+
+		KUNIT_EXPECT_EQ(test, regions->region[regions->num - 1].start,
+				entry->expect_last_paddr);
+		KUNIT_EXPECT_EQ(test, range_len(&regions->region[regions->num - 1]),
+				entry->expect_last_size);
+	}

At the end need to also free the allocated memory for the "regions" field using "kunit_free" [1].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/kunit/test.h#n631

Thanks,
Andra

drivers/virt/nitro_enclaves/ne_misc_dev_test.c line 144

Quoted 8 lines
drivers/virt/nitro_enclaves/ne_misc_dev_test.c +3 -0
+}
+
  static struct kunit_case ne_misc_dev_test_cases[] = {
+	KUNIT_CASE(ne_misc_dev_test_merge_phys_contig_memory_regions),
  	{}
  };
 

Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.

#11 lore raw
In reply to Longpeng(Mike)
Quoted 48 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

Hi guys,

This patchset try to merge the contiguous physical memory regions when set user memory regions, you can see message in PATCH 1 for details. Please review when you free, thank!

Changes v2 -> v3:
Patch 1:
- update the commit title and commit message. [Andra]
- use 'struct range' to instead of 'struct phys_mem_region'. [Andra, Greg KH]
- add comments before the function definition. [Andra]
- rename several variables, parameters and function. [Andra]
Patch 2:
- update the commit title and commit message. [Andra]
- add comments before the function definition. [Andra]
- remove 'inline' attribute of ne_sanity_check_phys_mem_region. [Andra]
- leave a blank line before return. [Andra]
- move sanity check in ne_merge_phys_contig_memory_regions to
the beginning of the function. [Andra]
- double sanity checking after the merge of physical contiguous
memory regions has been completed. [Andra]
Patch 3:
- update the commit title and commit message. [Andra]
- use "misc_dev"/"misc device"/"MISC_DEV" to be more specific. [Andra]
Patch 4:
- update the commit title and commit message. [Andra]
- align the fileds in 'struct phys_regions_test'. [Andra]
- rename 'phys_regions_testcases' to 'phys_regions_test_cases'. [Andra]
- add comments before each test cases. [Andra]
- initialize the variables in ne_misc_dev_test_merge_phys_contig_memory_regions. [Andra]

Changes v1 -> v2:
- update the commit message as Andra's suggestion [Andra]
- remove TODO completely in ne_set_user_memory_region_ioctl [Andra]
- extract the physical memory regions setup into individual function
- add kunit tests [Andra]

Longpeng (4):
nitro_enclaves: Merge contiguous physical memory regions
nitro_enclaves: Sanity check physical memory regions during merging
nitro_enclaves: Add KUnit tests setup for the misc device
functionality
nitro_enclaves: Add KUnit tests for contiguous physical memory regions
merging

Quoted 6 lines
  drivers/virt/nitro_enclaves/Kconfig            |   9 ++
  drivers/virt/nitro_enclaves/ne_misc_dev.c      | 163 +++++++++++++++++++------
  drivers/virt/nitro_enclaves/ne_misc_dev_test.c | 153 +++++++++++++++++++++++
  3 files changed, 285 insertions(+), 40 deletions(-)

create mode 100644 drivers/virt/nitro_enclaves/ne_misc_dev_test.c

Thank you. I'll go through them till the end of this week.

Andra

Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.