2021-10-09 01:35:16

by Longpeng(Mike)

[permalink] [raw]
Subject: [PATCH v3 0/4] 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

--
1.8.3.1


2021-10-09 01:35:26

by Longpeng(Mike)

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

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

Signed-off-by: Longpeng <[email protected]>
---
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(+)

diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev_test.c b/drivers/virt/nitro_enclaves/ne_misc_dev_test.c
index bcb755e..7bd6b34 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev_test.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev_test.c
@@ -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),
{}
};

--
1.8.3.1

2021-10-09 01:36:13

by Longpeng(Mike)

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

From: Longpeng <[email protected]>

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

Signed-off-by: Longpeng <[email protected]>
---
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

diff --git a/drivers/virt/nitro_enclaves/Kconfig b/drivers/virt/nitro_enclaves/Kconfig
index 8c9387a..90802b1 100644
--- a/drivers/virt/nitro_enclaves/Kconfig
+++ b/drivers/virt/nitro_enclaves/Kconfig
@@ -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.
diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
index a8fa56b..f895fc3 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
@@ -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);
diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev_test.c b/drivers/virt/nitro_enclaves/ne_misc_dev_test.c
new file mode 100644
index 0000000..bcb755e
--- /dev/null
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev_test.c
@@ -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
+};
--
1.8.3.1

2021-10-11 15:52:14

by Paraschiv, Andra-Irina

[permalink] [raw]
Subject: Re: [PATCH v3 0/4] Merge contiguous physical memory regions



On 09/10/2021 04:32, Longpeng(Mike) wrote:
> 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
>

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.

2021-10-16 10:51:14

by Paraschiv, Andra-Irina

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



On 09/10/2021 04:32, Longpeng(Mike) wrote:
> From: Longpeng <[email protected]>
>
> Add the initial setup for the KUnit tests that will target the Nitro
> Enclaves misc device functionality.
>
> Signed-off-by: Longpeng <[email protected]>
> ---
> 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
>
> diff --git a/drivers/virt/nitro_enclaves/Kconfig b/drivers/virt/nitro_enclaves/Kconfig
> index 8c9387a..90802b1 100644
> --- a/drivers/virt/nitro_enclaves/Kconfig
> +++ b/drivers/virt/nitro_enclaves/Kconfig
> @@ -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

> + 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.
> diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
> index a8fa56b..f895fc3 100644
> --- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
> +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
> @@ -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();

Let's have:

int rc = 0;

and then check the return code of "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);
> diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev_test.c b/drivers/virt/nitro_enclaves/ne_misc_dev_test.c
> new file mode 100644
> index 0000000..bcb755e
> --- /dev/null
> +++ b/drivers/virt/nitro_enclaves/ne_misc_dev_test.c
> @@ -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

> +
> +#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
> +};
>



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.

2021-10-16 12:35:40

by Paraschiv, Andra-Irina

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



On 09/10/2021 04:32, Longpeng(Mike) wrote:
> 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
>
> Signed-off-by: Longpeng <[email protected]>
> ---
> 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(+)
>
> diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev_test.c b/drivers/virt/nitro_enclaves/ne_misc_dev_test.c
> index bcb755e..7bd6b34 100644
> --- a/drivers/virt/nitro_enclaves/ne_misc_dev_test.c
> +++ b/drivers/virt/nitro_enclaves/ne_misc_dev_test.c
> @@ -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;

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.

> + 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},

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.

> +};
> +
> +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 = {};

> + 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);

> + 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;

> +
> + 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;

> + 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

> +}
> +
> 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.