Linux Hardening
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Alexandru Ciobotaru <alcioa@amazon.com>,
	The AWS Nitro Enclaves Team <aws-nitro-enclaves-devel@amazon.com>,
	Kees Cook <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not
	covered by other areas):Keyword:\b__counted_by(_le|_be)?\b)
Subject: [PATCH] virt: nitro_enclaves: Use flexible array for memory pages
Date: Sun, 10 May 2026 21:20:41 -0700	[thread overview]
Message-ID: <20260511042041.378366-1-rosenp@gmail.com> (raw)

Store the per-region page pointers in the memory region allocation
instead of allocating them separately.

Keep the allocated page-pointer capacity separate from the number of
pinned pages so __counted_by() describes the flexible array bounds while
nr_pages continues to track pages that need to be released.

Assisted-by: Codex:GPT-5.5
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/virt/nitro_enclaves/ne_misc_dev.c | 15 ++-------------
 drivers/virt/nitro_enclaves/ne_misc_dev.h |  2 +-
 2 files changed, 3 insertions(+), 14 deletions(-)

diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
index c91300a73f50..c0045b2cf7f3 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
@@ -927,19 +927,11 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
 	if (rc < 0)
 		return rc;
 
-	ne_mem_region = kzalloc_obj(*ne_mem_region);
+	max_nr_pages = mem_region.memory_size / NE_MIN_MEM_REGION_SIZE;
+	ne_mem_region = kzalloc_flex(*ne_mem_region, pages, max_nr_pages);
 	if (!ne_mem_region)
 		return -ENOMEM;
 
-	max_nr_pages = mem_region.memory_size / NE_MIN_MEM_REGION_SIZE;
-
-	ne_mem_region->pages = kzalloc_objs(*ne_mem_region->pages, max_nr_pages);
-	if (!ne_mem_region->pages) {
-		rc = -ENOMEM;
-
-		goto free_mem_region;
-	}
-
 	phys_contig_mem_regions.regions = kzalloc_objs(*phys_contig_mem_regions.regions,
 						       max_nr_pages);
 	if (!phys_contig_mem_regions.regions) {
@@ -1049,7 +1041,6 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
 		put_page(ne_mem_region->pages[i]);
 free_mem_region:
 	kfree(phys_contig_mem_regions.regions);
-	kfree(ne_mem_region->pages);
 	kfree(ne_mem_region);
 
 	return rc;
@@ -1398,8 +1389,6 @@ static void ne_enclave_remove_all_mem_region_entries(struct ne_enclave *ne_encla
 		for (i = 0; i < ne_mem_region->nr_pages; i++)
 			put_page(ne_mem_region->pages[i]);
 
-		kfree(ne_mem_region->pages);
-
 		kfree(ne_mem_region);
 	}
 }
diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.h b/drivers/virt/nitro_enclaves/ne_misc_dev.h
index 2a4d2224baba..f419e04007f8 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.h
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.h
@@ -28,8 +28,8 @@ struct ne_mem_region {
 	struct list_head	mem_region_list_entry;
 	u64			memory_size;
 	unsigned long		nr_pages;
-	struct page		**pages;
 	u64			userspace_addr;
+	struct page		*pages[];
 };
 
 /**
-- 
2.54.0


                 reply	other threads:[~2026-05-11  4:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260511042041.378366-1-rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=alcioa@amazon.com \
    --cc=aws-nitro-enclaves-devel@amazon.com \
    --cc=gustavoars@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox