nitro_enclaves: Fix enclave release, command retries and the CPU pool
Subsystems (5)
v2 cover nitro_enclaves: Fix enclave release, command retries and the CPU pool
While working with AI on the nitro_enclaves driver, it stumbled over a few issues in the code. None of them are out of the ordinary, but they all fix legit corner cases that are better to have fixed upstream.
While at it, I also realized that the MAINTAINERS file is outdated. Alexandru Ciobotaru no longer works on Nitro Enclaves. I'm taking over the maintainer role instead.
Alex
Changes in v2:
- Rebase onto v7.4-rc4+
- Move the MAINTAINERS update to the front
- New patch "nitro_enclaves: Request write access to enclave memory regions" to require write permission when pinning an enclave's memory
- New patch "nitro_enclaves: Online and offline pool CPUs with no pool lock held" so that no driver lock is held across CPU hotplug (Sashiko)
- New patch "nitro_enclaves: Free the CPU pool when driver registration fails" so that a failed module init does not leave the pool CPUs offline (Sashiko)
- New patch "nitro_enclaves: Free the CPU pool that ne_exit() does not see" for a module load that fails or an rmmod that races a ne_cpus write
- Add the Fixes: tag on the interrupt setup reorder (Sashiko)
- Clear a rejected boot-time ne_cpus value (Sashiko)
- Refuse a CPU list that does not fit the parameter buffer
v1: https://lore.kernel.org/r/[email protected]
Alexander Graf (23):
MAINTAINERS: Take over the Nitro Enclaves driver
nitro_enclaves: Release the reference on a rejected region page
nitro_enclaves: Request write access to enclave memory regions
nitro_enclaves: Split freeing the slot from freeing the enclave
nitro_enclaves: Ignore the reply of a failed slot info command
nitro_enclaves: Retry a command the device asks us to retry
nitro_enclaves: Stop sending commands to a broken device
nitro_enclaves: Set up the device state before requesting its
interrupts
nitro_enclaves: Online and offline pool CPUs with no pool lock held
nitro_enclaves: Wait for file operations before unbinding the PCI
device
nitro_enclaves: Let the region tracking arrays fall back to vmalloc
nitro_enclaves: Do not return enclave threads to a replaced CPU pool
nitro_enclaves: Do not report a torn down CPU pool as set
nitro_enclaves: Keep the CPU pool when the new CPU list does not parse
nitro_enclaves: Put the CPU pool back when the new CPU list is refused
nitro_enclaves: Initialise the CPU pool mutex statically
nitro_enclaves: Free the CPU pool when driver registration fails
nitro_enclaves: Free the CPU pool that ne_exit() does not see
nitro_enclaves: Build the boot-time CPU pool once the CPUs are online
nitro_enclaves: Set up the CPU pool under the CPU hotplug read lock
nitro_enclaves: Let 32-bit processes use the ioctl interface
nitro_enclaves: Index CPU pool cores by sibling mask
nitro_enclaves: Time the wait for the device to turn off by the clock
MAINTAINERS | 2 +- drivers/virt/nitro_enclaves/ne_misc_dev.c | 804 ++++++++++++++++------ drivers/virt/nitro_enclaves/ne_misc_dev.h | 24 +- drivers/virt/nitro_enclaves/ne_pci_dev.c | 236 ++++++- drivers/virt/nitro_enclaves/ne_pci_dev.h | 4 + 5 files changed, 814 insertions(+), 256 deletions(-)
base-commit: 4f069ab1ec243e6b60dc438772905855c60774e8
Signature · 1 line
2.47.1
PATCH v2 1/23 MAINTAINERS: Take over the Nitro Enclaves driver
Alexandru Ciobotaru has moved on from Nitro Enclaves, so the driver needs a new maintainer. I take it over.
--- MAINTAINERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
MAINTAINERS +1 -1
@@ -19896,7 +19896,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/dinguyen/linux.git F: arch/nios2/ NITRO ENCLAVES (NE) -M: Alexandru Ciobotaru <[email protected]> +M: Alexander Graf <[email protected]> R: The AWS Nitro Enclaves Team <[email protected]> L: [email protected] S: Supported
Signature · 1 line
2.47.1
PATCH v2 2/23 nitro_enclaves: Release the reference on a rejected region page
In reply to Alexander GrafA process that passes ordinary anonymous memory to NE_SET_USER_MEMORY_REGION can leak one page reference each time the driver rejects it with NE_ERR_MEM_NOT_HUGE_PAGE. Any process with permission to open /dev/nitro_enclaves and create an enclave can repeat that, which may lead to situations where the system runs out of memory.
The ioctl takes a reference on one page at a time and only counts it in ne_mem_region->nr_pages once that page has passed both of the checks that follow, ne_sanity_check_user_mem_region_page() and ne_merge_phys_contig_memory_regions(). The put_pages unwind walks [0, nr_pages), so it misses the page whose check just failed.
Count the reference as soon as get_user_pages_unlocked() reports it, so that the unwind covers it.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +8 -4
@@ -963,8 +963,8 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, gup_rc =get_user_pages_unlocked (mem_region.userspace_addr + memory_size,1 , ne_mem_region->pages + i,FOLL_GET ); -if (gup_rc <0 ) { - rc = gup_rc; +if (gup_rc !=1 ) { + rc = gup_rc <0 ? gup_rc : -EFAULT ;dev_err_ratelimited (ne_misc_dev.this_device,"Error in get user pages [rc=%d]\n" , rc);
@@ -972,6 +972,12 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, goto put_pages; } +/* +* Count the reference before the checks below, so that the +* put_pages unwind releases this page as well. +*/ + ne_mem_region->nr_pages++; + rc =ne_sanity_check_user_mem_region_page (ne_enclave, ne_mem_region->pages[i]);if (rc <0 ) goto put_pages;
@@ -983,8 +989,6 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, goto put_pages; memory_size +=page_size (ne_mem_region->pages[i]); - - ne_mem_region->nr_pages++; }while (memory_size < mem_region.memory_size); if ((ne_enclave->nr_mem_regions + phys_contig_mem_regions.num) >
Signature · 1 line
2.47.1
PATCH v2 3/23 nitro_enclaves: Request write access to enclave memory regions
In reply to Alexander GrafNE_SET_USER_MEMORY_REGION takes its page references with get_user_pages_unlocked() and plain FOLL_GET, which only asks for read access. The Nitro Enclaves device then writes to those pages for as long as the enclave owns them. So we accept regions that the calling process cannot write itself, for example a PROT_READ mapping.
Enclave memory has to be writable by the process that owns it, because the device writes it. Pass FOLL_WRITE as well, so that get_user_pages_unlocked() faults the pages in for writing and fails with -EFAULT when the mapping does not allow writes.
User space can see this change: a region without write permission used to be accepted and now fails with -EFAULT. Callers that map enclave memory PROT_READ | PROT_WRITE, like samples/nitro_enclaves/ne_ioctl_sample.c, are not affected.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +1 -1
@@ -961,7 +961,7 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, } gup_rc =get_user_pages_unlocked (mem_region.userspace_addr + memory_size,1 , - ne_mem_region->pages + i,FOLL_GET ); + ne_mem_region->pages + i,FOLL_GET |FOLL_WRITE );if (gup_rc !=1 ) { rc = gup_rc <0 ? gup_rc : -EFAULT ;
Signature · 1 line
2.47.1
PATCH v2 4/23 nitro_enclaves: Split freeing the slot from freeing the enclave
In reply to Alexander GrafUnder rare circumstances, the nitro_enclaves device may turn down an ENCLAVE_STOP or SLOT_FREE command. In preparation to handle that case, split ne_enclave_release() into ne_enclave_free_slot() and ne_enclave_free().
ne_enclave_free_slot() asks the device to stop the enclave and to free its slot. ne_enclave_free() drops the references on the memory region pages, puts the enclave's threads back into the NE CPU pool and frees the enclave. It only runs once the device has confirmed that it no longer owns what the enclave was given, which is the default behavior today.
With the two apart, all donated Enclave resources like memory and CPUs can stay tracked as in-use when the device turns a command down, for as long as the device may still use them.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 111 ++++++++++++++-------- 1 file changed, 74 insertions(+), 37 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +74 -37
@@ -1386,7 +1386,8 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long* from the enclave data structure. * @ne_enclave : Private data associated with the current enclave. * -* Context: Process context. This function is called with the ne_enclave mutex held. +* Context: Process context. This function is called on an enclave that is on +* no list and has no file left. */ static void ne_enclave_remove_all_mem_region_entries (struct ne_enclave *ne_enclave) {
@@ -1413,7 +1414,8 @@ static void ne_enclave_remove_all_mem_region_entries(struct ne_enclave *ne_encla* the enclave data structure. * @ne_enclave : Private data associated with the current enclave. * -* Context: Process context. This function is called with the ne_enclave mutex held. +* Context: Process context. This function is called on an enclave that is on +* no list and has no file left. */ static void ne_enclave_remove_all_vcpu_id_entries (struct ne_enclave *ne_enclave) {
@@ -1464,41 +1466,24 @@ static void ne_pci_dev_remove_enclave_entry(struct ne_enclave *ne_enclave, }/** -* ne_enclave_release() - Release function provided by the enclave file. -* @inode: Inode associated with this file release function. -* @file: File associated with this release function. +* ne_enclave_free_slot() - Stop an enclave and free its slot in the device. +* @ne_pci_dev : Private data associated with the PCI device. +* @ne_enclave : Private data associated with the current enclave. * -* Context: Process context. +* Context: Process context. This function is called with the ne_pci_dev enclave +* mutex and the ne_enclave mutex held. * Return: -* * 0 on success. +* * 0 once the device has freed the slot. * * Negative return value on failure. */ -static int ne_enclave_release (struct inode *inode,struct file *file) +static int ne_enclave_free_slot (struct ne_pci_dev *ne_pci_dev, +struct ne_enclave *ne_enclave) {struct ne_pci_dev_cmd_reply cmd_reply = {};struct enclave_stop_req enclave_stop_request = {}; -struct ne_enclave *ne_enclave = file->private_data; -struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev; -struct pci_dev *pdev = ne_pci_dev->pdev; -int rc = -EINVAL ;struct slot_free_req slot_free_req = {}; - -if (!ne_enclave) -return 0 ; - -/* -* Early exit in case there is an error in the enclave creation logic -* and fput() is called on the cleanup path. -*/ -if (!ne_enclave->slot_uid) -return 0 ; - -/* -* Acquire the enclave list mutex before the enclave mutex -* in order to avoid deadlocks with @ref ne_event_work_handler. -*/ -mutex_lock (&ne_pci_dev->enclaves_list_mutex); -mutex_lock (&ne_enclave->enclave_info_mutex); +struct pci_dev *pdev = ne_pci_dev->pdev; +int rc;if (ne_enclave->state !=NE_STATE_INIT && ne_enclave->state !=NE_STATE_STOPPED ) { enclave_stop_request.slot_uid = ne_enclave->slot_uid;
@@ -1510,7 +1495,7 @@ static int ne_enclave_release(struct inode *inode, struct file *file)dev_err_ratelimited (ne_misc_dev.this_device,"Error in enclave stop [rc=%d]\n" , rc); - goto unlock_mutex; +return rc; }memset (&cmd_reply,0 ,sizeof (cmd_reply));
@@ -1525,22 +1510,74 @@ static int ne_enclave_release(struct inode *inode, struct file *file)dev_err_ratelimited (ne_misc_dev.this_device,"Error in slot free [rc=%d]\n" , rc); - goto unlock_mutex; +return rc; } -ne_pci_dev_remove_enclave_entry (ne_enclave, ne_pci_dev); +return 0 ; +} + +/** +* ne_enclave_free() - Give back what an enclave was given and free it. +* @ne_enclave : Private data associated with the current enclave. +* +* Context: Process context. This function is called once the device has freed +* the slot of the enclave, on an enclave that is on no list and has no +* file left. +*/ +static void ne_enclave_free (struct ne_enclave *ne_enclave) +{ne_enclave_remove_all_mem_region_entries (ne_enclave);ne_enclave_remove_all_vcpu_id_entries (ne_enclave); -mutex_unlock (&ne_enclave->enclave_info_mutex); -mutex_unlock (&ne_pci_dev->enclaves_list_mutex); -kfree (ne_enclave); +} -return 0 ; +/** +* ne_enclave_release() - Release function provided by the enclave file. +* @inode: Inode associated with this file release function. +* @file: File associated with this release function. +* +* Context: Process context. +* Return: +* * 0 on success. +* * Negative return value on failure. +*/ +static int ne_enclave_release (struct inode *inode,struct file *file) +{ +struct ne_enclave *ne_enclave = file->private_data; +struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev; +int rc; + +if (!ne_enclave) +return 0 ; + +/* +* Early exit in case there is an error in the enclave creation logic +* and fput() is called on the cleanup path. +*/ +if (!ne_enclave->slot_uid) +return 0 ; + +/* +* Acquire the enclave list mutex before the enclave mutex +* in order to avoid deadlocks with @ref ne_event_work_handler. +*/ +mutex_lock (&ne_pci_dev->enclaves_list_mutex); +mutex_lock (&ne_enclave->enclave_info_mutex); + +/* +* A slot the device did not free may still run on the memory and the +* CPUs of the enclave, so the enclave keeps them. +*/ + rc =ne_enclave_free_slot (ne_pci_dev, ne_enclave); +if (!rc) +ne_pci_dev_remove_enclave_entry (ne_enclave, ne_pci_dev); -unlock_mutex:mutex_unlock (&ne_enclave->enclave_info_mutex); + +if (!rc) +ne_enclave_free (ne_enclave); +mutex_unlock (&ne_pci_dev->enclaves_list_mutex);return rc;
Signature · 1 line
2.47.1
PATCH v2 5/23 nitro_enclaves: Ignore the reply of a failed slot info command
In reply to Alexander GrafWhen the nitro_enclaves device fails a SLOT_INFO command, ne_event_work_handler() logs the failure and still compares the enclave's state against the reply, which then holds zero or the reply for the previous enclave. Zero is NE_STATE_INIT, so a running enclave is recorded as not started: its owner is woken for a state change that did not happen, and NE_ADD_VCPU, NE_SET_USER_MEMORY_REGION and NE_START_ENCLAVE become callable on it again.
Skip the enclave when SLOT_INFO fails, so that it stays in NE_STATE_RUNNING and a later event can examine it.
--- drivers/virt/nitro_enclaves/ne_pci_dev.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/virt/nitro_enclaves/ne_pci_dev.c +4 -1
@@ -243,9 +243,12 @@ static void ne_event_work_handler(struct work_struct *work) rc =ne_do_request (pdev,SLOT_INFO , &slot_info_req,sizeof (slot_info_req), &cmd_reply,sizeof (cmd_reply)); -if (rc <0 ) +if (rc <0 ) {dev_err (&pdev->dev,"Error in slot info [rc=%d]\n" , rc); + goto unlock; + } +/* Notify enclave process that the enclave state changed. */ if (ne_enclave->state != cmd_reply.state) { ne_enclave->state = cmd_reply.state;
Signature · 1 line
2.47.1
PATCH v2 6/23 nitro_enclaves: Retry a command the device asks us to retry
In reply to Alexander GrafThe nitro_enclaves device may return -EAGAIN under rare circumstances, which asks the system to perform the command again. Handle it as gracefully as possible inside the kernel driver.
Most commands finish almost instantly; give them a 40s timeout. But commands like free may indicate through -EAGAIN that they are still actively working on freeling a slot which can take significantly longer. Use 10 minutes for those.
To ensure that user space still reacts to events during such long timeout periods, rework some of the locking ordering and use killable locks.
With this patch applied, a launch or a release that runs into such a window waits for it to pass. When the device still asks for a retry once the time is up, the caller gets -EAGAIN.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 28 +++++--- drivers/virt/nitro_enclaves/ne_pci_dev.c | 83 ++++++++++++++++++++++- 2 files changed, 101 insertions(+), 10 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +20 -8
@@ -1155,7 +1155,9 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned longif (copy_from_user (&vcpu_id, (void __user *)arg,sizeof (vcpu_id)))return -EFAULT ; -mutex_lock (&ne_enclave->enclave_info_mutex); + rc =mutex_lock_killable (&ne_enclave->enclave_info_mutex); +if (rc) +return rc;if (ne_enclave->state !=NE_STATE_INIT ) { dev_err_ratelimited(ne_misc_dev.this_device,
@@ -1219,11 +1221,14 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned longcase NE_GET_IMAGE_LOAD_INFO : {struct ne_image_load_info image_load_info = {}; +int rc;if (copy_from_user (&image_load_info, (void __user *)arg,sizeof (image_load_info)))return -EFAULT ; -mutex_lock (&ne_enclave->enclave_info_mutex); + rc =mutex_lock_killable (&ne_enclave->enclave_info_mutex); +if (rc) +return rc;if (ne_enclave->state !=NE_STATE_INIT ) { dev_err_ratelimited(ne_misc_dev.this_device,
@@ -1267,7 +1272,9 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned longreturn -NE_ERR_INVALID_FLAG_VALUE ; } -mutex_lock (&ne_enclave->enclave_info_mutex); + rc =mutex_lock_killable (&ne_enclave->enclave_info_mutex); +if (rc) +return rc;if (ne_enclave->state !=NE_STATE_INIT ) { dev_err_ratelimited(ne_misc_dev.this_device,
@@ -1347,7 +1354,9 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned longreturn -NE_ERR_INVALID_ENCLAVE_CID ; } -mutex_lock (&ne_enclave->enclave_info_mutex); + rc =mutex_lock_killable (&ne_enclave->enclave_info_mutex); +if (rc) +return rc;if (ne_enclave->state !=NE_STATE_INIT ) { dev_err_ratelimited(ne_misc_dev.this_device,
@@ -1777,15 +1786,18 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {switch (cmd) {case NE_CREATE_VM : { -int enclave_fd =-1 ;struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev;u64 __user *slot_uid = (void __user *)arg; +int rc; -mutex_lock (&ne_pci_dev->enclaves_list_mutex); - enclave_fd =ne_create_vm_ioctl (ne_pci_dev, slot_uid); + rc =mutex_lock_killable (&ne_pci_dev->enclaves_list_mutex); +if (rc) +return rc; + + rc =ne_create_vm_ioctl (ne_pci_dev, slot_uid);mutex_unlock (&ne_pci_dev->enclaves_list_mutex); -return enclave_fd; +return rc; }default :
drivers/virt/nitro_enclaves/ne_pci_dev.c +81 -2
130 lines not on this page · raw message
Signature · 1 line
2.47.1
[email protected]
AI reviewRe: [PATCH v2 06/23] nitro_enclaves: Retry a command the device asks us to retry
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] nitro_enclaves: enclaves_list_mutex held across retry in ne_enclave_release() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] nitro_enclaves: enclaves_list_mutex held across retry in ne_enclave_release()
--
commit 6fed9d92931fd77de6660031a8280950cb317c53
Author: Alexander Graf <[email protected]>
nitro_enclaves: Retry a command the device asks us to retry
This commit adds retry handling with exponential backoff in ne_do_request() when the Nitro Enclaves PCI device replies with -EAGAIN, and converts enclave and list mutex acquisitions in the ioctl and event work paths to mutex_lock_killable().
Quoted 4 lines
drivers/virt/nitro_enclaves/ne_pci_dev.c +0
-0
[ ... ]
Quoted 45 lines
drivers/virt/nitro_enclaves/ne_pci_dev.c +29
-1@@ -159,6 +209,27 @@ int ne_do_request(struct pci_dev *pdev, enum ne_pci_dev_cmd_type cmd_type,atomic_set (&ne_pci_dev->cmd_reply_avail,0 ); +if (cmd_reply->rc == -EAGAIN ) { +/* +* The device answered, so nothing is in flight and another +* command may go out while this one waits to be sent again. +*/ +mutex_unlock (&ne_pci_dev->pci_dev_mutex); + +if (!deadline) { +unsigned int budget_msecs =ne_cmd_frees_slot (cmd_type) ? +NE_FREE_TIMEOUT_MSECS : +NE_CMD_RETRY_TOTAL_MSECS ; + + deadline = jiffies +msecs_to_jiffies (budget_msecs); + } + +msleep (delay_msecs); + delay_msecs =min (delay_msecs *2 ,NE_CMD_RETRY_MAX_MSECS ); + + goto retry; + } +if (cmd_reply->rc <0 ) { rc = cmd_reply->rc;@@ -221,7 +292,14 @@ static void ne_event_work_handler(struct work_struct *work)int rc = -EINVAL ;struct slot_info_req slot_info_req = {}; -mutex_lock (&ne_pci_dev->enclaves_list_mutex); +/* +* A release holds these locks for as long as the device takes to free +* the slot, which can be minutes, so wait killable: that is not +* reported as a hung task. The worker gets no signal, so the locks do +* not fail. +*/ +if (mutex_lock_killable (&ne_pci_dev->enclaves_list_mutex)) +return ;/* * Iterate over all enclaves registered for the Nitro Enclaves
drivers/virt/nitro_enclaves/ne_pci_dev.c static void ne_event_work_handler(struct work_struct *work) · line 311
Quoted 6 lines
drivers/virt/nitro_enclaves/ne_pci_dev.c +2
-1@@ -229,7 +307,8 @@ static void ne_event_work_handler(struct work_struct *work)* is corresponding to. */ list_for_each_entry (ne_enclave, &ne_pci_dev->enclaves_list, enclave_list_entry) { -mutex_lock (&ne_enclave->enclave_info_mutex); +if (mutex_lock_killable (&ne_enclave->enclave_info_mutex)) +break ;
[Severity: Medium]
Does holding ne_pci_dev->enclaves_list_mutex across ne_enclave_free_slot() in ne_enclave_release() block out-of-band event delivery for all other running enclaves and still trigger hung-task warnings during a slow slot release?
drivers/virt/nitro_enclaves/ne_misc_dev.c:ne_enclave_release() {
...
mutex_lock(&ne_pci_dev->enclaves_list_mutex);
mutex_lock(&ne_enclave->enclave_info_mutex);
/*
* A slot the device did not free may still run on the memory and the
* CPUs of the enclave, so the enclave keeps them.
*/
rc = ne_enclave_free_slot(ne_pci_dev, ne_enclave);
if (!rc)
ne_pci_dev_remove_enclave_entry(ne_enclave, ne_pci_dev);
...
}Although ne_do_request() drops ne_pci_dev->pci_dev_mutex between -EAGAIN retries so other commands can proceed, ne_enclave_release() holds ne_pci_dev->enclaves_list_mutex across the entire ENCLAVE_STOP and SLOT_FREE retry loop (up to 10 minutes per command). While one enclave is waiting for its slot to be scrubbed, ne_event_work_handler() blocks on ne_pci_dev->enclaves_list_mutex and cannot deliver out-of-band events to user space for any other running enclave, and NE_CREATE_VM in ne_ioctl() is similarly blocked before it can issue SLOT_ALLOC.
In addition, if a second enclave file descriptor is closed while the first release is retrying for longer than the hung-task timeout, the second ne_enclave_release() call blocks in uninterruptible mutex_lock() on ne_pci_dev->enclaves_list_mutex (and ne_enclave_release() cannot simply use mutex_lock_killable() because it may run from do_exit() with a fatal signal already pending).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v2 7/23 nitro_enclaves: Stop sending commands to a broken device
In reply to Alexander GrafThe Nitro Enclaves device always answers quickly to its commands. Consider it broken if it does not reply to a command within 120s. Except for STOP/FREE commands which legitimately may take longer because the hypervisor performs cleanup work that may take some time. For those, use a 10 minute timeout.
Rework the request handler logic to enforce these limits. But still keep processes killable during that time. Except for stop commands, which are used as part of the slot shutdown sequence and have to block.
If a STOP/FREE command failed to respond in time (10 minutes), the device is clearly broken. Switch to a mode then where it no longer accepts any new commands.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 20 ++----- drivers/virt/nitro_enclaves/ne_pci_dev.c | 63 ++++++++++++++++++----- drivers/virt/nitro_enclaves/ne_pci_dev.h | 4 ++ 3 files changed, 59 insertions(+), 28 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +4 -16
@@ -1500,29 +1500,17 @@ static int ne_enclave_free_slot(struct ne_pci_dev *ne_pci_dev, rc =ne_do_request (pdev,ENCLAVE_STOP , &enclave_stop_request,sizeof (enclave_stop_request), &cmd_reply,sizeof (cmd_reply)); -if (rc <0 ) { -dev_err_ratelimited (ne_misc_dev.this_device, -"Error in enclave stop [rc=%d]\n" , rc); - +if (rc <0 )return rc; - }memset (&cmd_reply,0 ,sizeof (cmd_reply)); } slot_free_req.slot_uid = ne_enclave->slot_uid; - rc =ne_do_request (pdev,SLOT_FREE , - &slot_free_req,sizeof (slot_free_req), - &cmd_reply,sizeof (cmd_reply)); -if (rc <0 ) { -dev_err_ratelimited (ne_misc_dev.this_device, -"Error in slot free [rc=%d]\n" , rc); - -return rc; - } - -return 0 ; +return ne_do_request (pdev,SLOT_FREE , + &slot_free_req,sizeof (slot_free_req), + &cmd_reply,sizeof (cmd_reply)); }/**
drivers/virt/nitro_enclaves/ne_pci_dev.c +51 -12
108 lines not on this page · raw message
drivers/virt/nitro_enclaves/ne_pci_dev.h +4 -0
@@ -279,6 +279,9 @@ struct ne_pci_dev_cmd_reply {* struct ne_pci_dev - Nitro Enclaves (NE) PCI device. * @cmd_reply_avail: Variable set if a reply has been sent by the * PCI device. +* @failed: A command got no reply in time, or the device did +* not stop or free a slot. No command is sent to +* the device after that. * @cmd_reply_wait_q: Wait queue for handling command reply from the * PCI device. * @enclaves_list: List of the enclaves managed by the PCI device.
@@ -294,6 +297,7 @@ struct ne_pci_dev_cmd_reply {*/ struct ne_pci_dev {atomic_t cmd_reply_avail; +bool failed;wait_queue_head_t cmd_reply_wait_q;struct list_head enclaves_list;struct mutex enclaves_list_mutex;
Signature · 1 line
2.47.1
PATCH v2 8/23 nitro_enclaves: Set up the device state before requesting its interrupts
In reply to Alexander GrafA nitro_enclaves device left enabled by a previous kernel, for example before a kdump capture kernel binds it, may raise an interrupt as soon as the driver requests its MSI-X vectors. ne_pci_probe() requests them before it initialises the reply wait queue, the enclave list and the mutexes the handlers use, so such an interrupt oopses the kernel while it binds the device.
Initialise those fields ahead of ne_setup_msix(). ne_devs.ne_pci_dev stays beside misc_register(), because setting it publishes the device to the ioctl paths.
---
v1 -> v2:
- Add the Fixes: tag and Cc: stable (Sashiko)
drivers/virt/nitro_enclaves/ne_pci_dev.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/virt/nitro_enclaves/ne_pci_dev.c +7 -7
@@ -620,6 +620,13 @@ static int ne_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)pci_set_drvdata (pdev, ne_pci_dev); +atomic_set (&ne_pci_dev->cmd_reply_avail,0 ); +init_waitqueue_head (&ne_pci_dev->cmd_reply_wait_q); +INIT_LIST_HEAD (&ne_pci_dev->enclaves_list); +mutex_init (&ne_pci_dev->enclaves_list_mutex); +mutex_init (&ne_pci_dev->pci_dev_mutex); + ne_pci_dev->pdev = pdev; + rc =ne_setup_msix (pdev);if (rc <0 ) {dev_err (&pdev->dev,"Error in pci dev msix setup [rc=%d]\n" , rc);
@@ -636,13 +643,6 @@ static int ne_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) goto teardown_msix; } -atomic_set (&ne_pci_dev->cmd_reply_avail,0 ); -init_waitqueue_head (&ne_pci_dev->cmd_reply_wait_q); -INIT_LIST_HEAD (&ne_pci_dev->enclaves_list); -mutex_init (&ne_pci_dev->enclaves_list_mutex); -mutex_init (&ne_pci_dev->pci_dev_mutex); - ne_pci_dev->pdev = pdev; - ne_devs.ne_pci_dev = ne_pci_dev; rc =misc_register (ne_devs.ne_misc_dev);
Signature · 1 line
2.47.1
PATCH v2 9/23 nitro_enclaves: Online and offline pool CPUs with no pool lock held
In reply to Alexander Grafremove_cpu() and add_cpu() take device_hotplug_lock, and the driver calls both with the CPU pool mutex held. ACPI hotplug holds device_hotplug_lock around a device's probe and remove callbacks, and enclave creation takes the pool mutex with the enclave list mutex held. In preparation to make remove wait for the driver's file operations, rework pool setup and teardown so that no CPU changes state under the pool mutex: a remove that waits on a lock an ioctl holds, while the ioctl waits for the pool mutex and a pool change waits for device_hotplug_lock, would deadlock.
Build a new pool in local memory, offline its CPUs with no locks held, and publish the finished pool as the last step. Tear a pool down by taking it out of the driver's view first, and online its CPUs once no reader can see them. The pool mutex then only guards the switch of the pool fields, so a reader sees the whole pool or none of it. Two pool changes cannot interleave: the kernel holds the parameter lock across the ne_cpus setter.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 148 ++++++++++++++-------- 1 file changed, 98 insertions(+), 50 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +98 -50
@@ -163,6 +163,75 @@ static bool ne_check_enclaves_created(void)return ret; } +/** +* ne_free_core_slots() - Free an array of cpumasks that holds the threads of +* full CPU cores. +* @threads_per_core: The array with one cpumask per full CPU core. +* @nr_cores: The size of the array. +* +* Context: Process context. +*/ +static void ne_free_core_slots (cpumask_var_t *threads_per_core,unsigned int nr_cores) +{ +unsigned int i =0 ; + +if (!threads_per_core) +return ; + +for (i =0 ; i < nr_cores; i++) +free_cpumask_var (threads_per_core[i]); + +kfree (threads_per_core); +} + +/** +* ne_online_core_slots() - Online the CPUs of an array of full CPU cores. +* @threads_per_core: The array with one cpumask per full CPU core. +* @nr_cores: The size of the array. +* +* Context: Process context. No driver lock may be held: add_cpu() takes +* device_hotplug_lock. +*/ +static void ne_online_core_slots (cpumask_var_t *threads_per_core,unsigned int nr_cores) +{ +unsigned int cpu =0 ; +unsigned int i =0 ; +int rc = -EINVAL ; + +for (i =0 ; i < nr_cores; i++) +for_each_cpu (cpu, threads_per_core[i]) { + rc =add_cpu (cpu); +if (rc !=0 ) +pr_err ("%s: CPU %d is not onlined [rc=%d]\n" , + ne_misc_dev.name, cpu, rc); + } +} + +/** +* ne_detach_cpu_pool() - Take the CPU pool out of the driver's view. +* @threads_per_core: Returns the array with one cpumask per core of the pool. +* @nr_cores: Returns the size of the array. +* +* The caller owns the returned array: it onlines the CPUs and frees the array +* once no reader can see the pool anymore. +* +* Context: Process context. +*/ +static void ne_detach_cpu_pool (cpumask_var_t **threads_per_core,unsigned int *nr_cores) +{ +mutex_lock (&ne_cpu_pool.mutex); + + *threads_per_core = ne_cpu_pool.avail_threads_per_core; + *nr_cores = ne_cpu_pool.nr_parent_vm_cores; + + ne_cpu_pool.avail_threads_per_core =NULL ; + ne_cpu_pool.nr_parent_vm_cores =0 ; + ne_cpu_pool.nr_threads_per_core =0 ; + ne_cpu_pool.numa_node =-1 ; + +mutex_unlock (&ne_cpu_pool.mutex); +} +/** * ne_setup_cpu_pool() - Set the NE CPU pool after handling sanity checks such * as not sharing CPU cores with the primary / parent VM
@@ -171,7 +240,9 @@ static bool ne_check_enclaves_created(void)* pool after the checks passed. * @ne_cpu_list: The CPU list used for setting NE CPU pool. * -* Context: Process context. +* Context: Process context. No driver lock may be held: remove_cpu() and +* add_cpu() take device_hotplug_lock. The pool mutex only guards the +* publish of the finished pool. * Return: * * 0 on success. * * Negative return value on failure.
@@ -183,14 +254,15 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list)cpumask_var_t cpu_pool;unsigned int cpu_sibling =0 ;unsigned int i =0 ; +unsigned int nr_parent_vm_cores =0 ; +unsigned int nr_threads_per_core =0 ;int numa_node =-1 ;int rc = -EINVAL ; +cpumask_var_t *threads_per_core =NULL ;if (!zalloc_cpumask_var (&cpu_pool,GFP_KERNEL ))return -ENOMEM ; -mutex_lock (&ne_cpu_pool.mutex); - rc =cpulist_parse (ne_cpu_list, cpu_pool);if (rc <0 ) {pr_err ("%s: Error in cpulist parse [rc=%d]\n" , ne_misc_dev.name, rc);
@@ -290,20 +362,19 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list)/* Calculate the number of threads from a full CPU core. */ cpu =cpumask_any (cpu_pool);for_each_cpu (cpu_sibling,topology_sibling_cpumask (cpu)) - ne_cpu_pool.nr_threads_per_core++; + nr_threads_per_core++; - ne_cpu_pool.nr_parent_vm_cores = nr_cpu_ids / ne_cpu_pool.nr_threads_per_core; + nr_parent_vm_cores = nr_cpu_ids / nr_threads_per_core; - ne_cpu_pool.avail_threads_per_core =kzalloc_objs (*ne_cpu_pool.avail_threads_per_core, - ne_cpu_pool.nr_parent_vm_cores); -if (!ne_cpu_pool.avail_threads_per_core) { + threads_per_core =kzalloc_objs (*threads_per_core, nr_parent_vm_cores); +if (!threads_per_core) { rc = -ENOMEM ; goto free_pool_cpumask; } -for (i =0 ; i < ne_cpu_pool.nr_parent_vm_cores; i++) -if (!zalloc_cpumask_var (&ne_cpu_pool.avail_threads_per_core[i],GFP_KERNEL )) { +for (i =0 ; i < nr_parent_vm_cores; i++) +if (!zalloc_cpumask_var (&threads_per_core[i],GFP_KERNEL )) { rc = -ENOMEM ; goto free_cores_cpumask;
@@ -315,16 +386,16 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list)*/ for_each_cpu (cpu, cpu_pool) { core_id =topology_core_id (cpu); -if (core_id <0 || core_id >= ne_cpu_pool.nr_parent_vm_cores) { +if (core_id <0 || core_id >= nr_parent_vm_cores) {pr_err ("%s: Invalid core id %d for CPU %d\n" , ne_misc_dev.name, core_id, cpu); rc = -EINVAL ; - goto clear_cpumask; + goto free_cores_cpumask; } -cpumask_set_cpu (cpu, ne_cpu_pool.avail_threads_per_core[core_id]); +cpumask_set_cpu (cpu, threads_per_core[core_id]); }/*
@@ -349,6 +420,11 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list)free_cpumask_var (cpu_pool); +mutex_lock (&ne_cpu_pool.mutex); + + ne_cpu_pool.avail_threads_per_core = threads_per_core; + ne_cpu_pool.nr_parent_vm_cores = nr_parent_vm_cores; + ne_cpu_pool.nr_threads_per_core = nr_threads_per_core; ne_cpu_pool.numa_node = numa_node;mutex_unlock (&ne_cpu_pool.mutex);
@@ -358,19 +434,10 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) online_cpus:for_each_cpu (cpu, cpu_pool)add_cpu (cpu); -clear_cpumask: -for (i =0 ; i < ne_cpu_pool.nr_parent_vm_cores; i++) -cpumask_clear (ne_cpu_pool.avail_threads_per_core[i]); free_cores_cpumask: -for (i =0 ; i < ne_cpu_pool.nr_parent_vm_cores; i++) -free_cpumask_var (ne_cpu_pool.avail_threads_per_core[i]); -kfree (ne_cpu_pool.avail_threads_per_core); +ne_free_core_slots (threads_per_core, nr_parent_vm_cores); free_pool_cpumask:free_cpumask_var (cpu_pool); - ne_cpu_pool.nr_parent_vm_cores =0 ; - ne_cpu_pool.nr_threads_per_core =0 ; - ne_cpu_pool.numa_node =-1 ; -mutex_unlock (&ne_cpu_pool.mutex);return rc; }
@@ -380,41 +447,22 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list)* CPU pool. * @void: No parameters provided. * -* Context: Process context. +* Context: Process context. No driver lock may be held: add_cpu() takes +* device_hotplug_lock. */ static void ne_teardown_cpu_pool (void ) { -unsigned int cpu =0 ; -unsigned int i =0 ; -int rc = -EINVAL ; +cpumask_var_t *threads_per_core =NULL ; +unsigned int nr_cores =0 ; -mutex_lock (&ne_cpu_pool.mutex); - -if (!ne_cpu_pool.nr_parent_vm_cores) { -mutex_unlock (&ne_cpu_pool.mutex); +ne_detach_cpu_pool (&threads_per_core, &nr_cores); +if (!threads_per_core)return ; - } -for (i =0 ; i < ne_cpu_pool.nr_parent_vm_cores; i++) { -for_each_cpu (cpu, ne_cpu_pool.avail_threads_per_core[i]) { - rc =add_cpu (cpu); -if (rc !=0 ) -pr_err ("%s: CPU %d is not onlined [rc=%d]\n" , - ne_misc_dev.name, cpu, rc); - } - -cpumask_clear (ne_cpu_pool.avail_threads_per_core[i]); +ne_online_core_slots (threads_per_core, nr_cores); -free_cpumask_var (ne_cpu_pool.avail_threads_per_core[i]); - } - -kfree (ne_cpu_pool.avail_threads_per_core); - ne_cpu_pool.nr_parent_vm_cores =0 ; - ne_cpu_pool.nr_threads_per_core =0 ; - ne_cpu_pool.numa_node =-1 ; - -mutex_unlock (&ne_cpu_pool.mutex); +ne_free_core_slots (threads_per_core, nr_cores); }/**
Signature · 1 line
2.47.1
PATCH v2 10/23 nitro_enclaves: Wait for file operations before unbinding the PCI device
In reply to Alexander GrafUser space may hold enclave fds and /dev/nitro_enclaves fds across a sysfs unbind or a hot-unplug of the NE PCI device. Their ioctls and their release then use ne_devs.ne_pci_dev and ne_misc_dev.this_device after ne_pci_remove() freed both, or oops on the NULL pointer it leaves behind.
Add ne_devs.lock, a rw_semaphore that probe and remove hold for writing and the file operations hold for reading, so that remove waits for the ones in flight. A release or a remove can hold it for minutes while the device frees a slot, so the ioctls take it with down_read_killable(). Remove then marks every enclave detached, which wakes its poll like an out-of-band event, and an ioctl on a detached enclave, or on the misc device while no device is bound, fails with -ENODEV. A detached enclave keeps its memory and CPUs on release, because the device that could confirm that they are free is gone.
A second NE PCI function would overwrite ne_devs.ne_pci_dev at probe, so refuse it with -EBUSY.
Kiro, an AI coding assistant, found this while checking device removal with enclave fds still open.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 93 +++++++++++++++++++---- drivers/virt/nitro_enclaves/ne_misc_dev.h | 8 ++ drivers/virt/nitro_enclaves/ne_pci_dev.c | 62 +++++++++++++++ 3 files changed, 148 insertions(+), 15 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +78 -15
@@ -78,6 +78,7 @@ static struct miscdevice ne_misc_dev = {struct ne_devs ne_devs = { .ne_misc_dev = &ne_misc_dev, + .lock =__RWSEM_INITIALIZER (ne_devs.lock), };/*
@@ -147,18 +148,22 @@ struct ne_phys_contig_mem_regions {*/ static bool ne_check_enclaves_created (void ) { -struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev; +struct ne_pci_dev *ne_pci_dev =NULL ;bool ret = false; -if (!ne_pci_dev) -return ret; +down_read (&ne_devs.lock); -mutex_lock (&ne_pci_dev->enclaves_list_mutex); + ne_pci_dev = ne_devs.ne_pci_dev; +if (ne_pci_dev) { +mutex_lock (&ne_pci_dev->enclaves_list_mutex); -if (!list_empty (&ne_pci_dev->enclaves_list)) - ret = true; +if (!list_empty (&ne_pci_dev->enclaves_list)) + ret = true; -mutex_unlock (&ne_pci_dev->enclaves_list_mutex); +mutex_unlock (&ne_pci_dev->enclaves_list_mutex); + } + +up_read (&ne_devs.lock);return ret; }
@@ -1181,17 +1186,18 @@ static int ne_start_enclave_ioctl(struct ne_enclave *ne_enclave, }/** -* ne_enclave_ioctl() - Ioctl function provided by the enclave file. +* ne_enclave_do_ioctl() - Ioctl function provided by the enclave file. * @file: File associated with this ioctl function. * @cmd: The command that is set for the ioctl call. * @arg: The argument that is provided for the ioctl call. * -* Context: Process context. +* Context: Process context. This function is called with ne_devs.lock held +* for reading, on an enclave that is not detached. * Return: * * 0 on success. * * Negative return value on failure. */ -static long ne_enclave_ioctl (struct file *file,unsigned int cmd,unsigned long arg) +static long ne_enclave_do_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {struct ne_enclave *ne_enclave = file->private_data;
@@ -1438,6 +1444,36 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned longreturn 0 ; } +/** +* ne_enclave_ioctl() - Ioctl function provided by the enclave file. +* @file: File associated with this ioctl function. +* @cmd: The command that is set for the ioctl call. +* @arg: The argument that is provided for the ioctl call. +* +* Context: Process context. +* Return: +* * 0 on success. +* * Negative return value on failure. +*/ +static long ne_enclave_ioctl (struct file *file,unsigned int cmd,unsigned long arg) +{ +struct ne_enclave *ne_enclave = file->private_data; +long rc; + + rc =down_read_killable (&ne_devs.lock); +if (rc) +return rc; + +if (ne_enclave->detached) + rc = -ENODEV ; +else + rc =ne_enclave_do_ioctl (file, cmd, arg); + +up_read (&ne_devs.lock); + +return rc; +} +/** * ne_enclave_remove_all_mem_region_entries() - Remove all memory region entries * from the enclave data structure.
@@ -1590,7 +1626,7 @@ static void ne_enclave_free(struct ne_enclave *ne_enclave)static int ne_enclave_release (struct inode *inode,struct file *file) {struct ne_enclave *ne_enclave = file->private_data; -struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev; +struct ne_pci_dev *ne_pci_dev =NULL ;int rc;if (!ne_enclave)
@@ -1603,6 +1639,21 @@ static int ne_enclave_release(struct inode *inode, struct file *file)if (!ne_enclave->slot_uid)return 0 ; +down_read (&ne_devs.lock); + +/* +* The device the enclave was created on is gone, and with it the only +* party that can confirm that the slot no longer uses its memory and +* its CPUs, so the enclave keeps both. +*/ +if (ne_enclave->detached) { + rc = -ENODEV ; + + goto unlock_devs; + } + + ne_pci_dev = ne_devs.ne_pci_dev; +/* * Acquire the enclave list mutex before the enclave mutex * in order to avoid deadlocks with @ref ne_event_work_handler.
@@ -1625,6 +1676,9 @@ static int ne_enclave_release(struct inode *inode, struct file *file)mutex_unlock (&ne_pci_dev->enclaves_list_mutex); +unlock_devs: +up_read (&ne_devs.lock); +return rc; }
@@ -1822,16 +1876,25 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {switch (cmd) {case NE_CREATE_VM : { -struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev; +struct ne_pci_dev *ne_pci_dev =NULL ;u64 __user *slot_uid = (void __user *)arg;int rc; - rc =mutex_lock_killable (&ne_pci_dev->enclaves_list_mutex); + rc =down_read_killable (&ne_devs.lock);if (rc)return rc; - rc =ne_create_vm_ioctl (ne_pci_dev, slot_uid); -mutex_unlock (&ne_pci_dev->enclaves_list_mutex); + rc = -ENODEV ; + ne_pci_dev = ne_devs.ne_pci_dev; +if (ne_pci_dev) { + rc =mutex_lock_killable (&ne_pci_dev->enclaves_list_mutex); +if (!rc) { + rc =ne_create_vm_ioctl (ne_pci_dev, slot_uid); +mutex_unlock (&ne_pci_dev->enclaves_list_mutex); + } + } + +up_read (&ne_devs.lock);return rc; }
drivers/virt/nitro_enclaves/ne_misc_dev.h +8 -0
@@ -12,6 +12,7 @@#include <linux/mm.h> #include <linux/mutex.h> #include <linux/pci.h> +#include <linux/rwsem.h> #include <linux/wait.h> #include "ne_pci_dev.h"
@@ -35,6 +36,8 @@ struct ne_mem_region {/** * struct ne_enclave - Per-enclave data used for enclave lifetime management. * @enclave_info_mutex : Mutex for accessing this internal state. +* @detached: The NE PCI device the enclave was created on is +* gone. Written with ne_devs.lock held for writing. * @enclave_list_entry : Entry in the list of created enclaves. * @eventq: Wait queue used for out-of-band event notifications * triggered from the PCI device event handler to
@@ -63,6 +66,7 @@ struct ne_mem_region {*/ struct ne_enclave {struct mutex enclave_info_mutex; +bool detached;struct list_head enclave_list_entry;wait_queue_head_t eventq;bool has_event;
@@ -97,10 +101,14 @@ enum ne_state {* struct ne_devs - Data structure to keep refs to the NE misc and PCI devices. * @ne_misc_dev: Nitro Enclaves misc device. * @ne_pci_dev : Nitro Enclaves PCI device. +* @lock: Held for writing while the NE PCI device binds or unbinds, and for +* reading by the file operations that reach it or log through the +* misc device, which goes away with it. */ struct ne_devs {struct miscdevice *ne_misc_dev;struct ne_pci_dev *ne_pci_dev; +struct rw_semaphore lock; };/* Nitro Enclaves (NE) data structure for keeping refs to the NE misc and PCI devices. */
drivers/virt/nitro_enclaves/ne_pci_dev.c +62 -0
108 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v2 11/23 nitro_enclaves: Let the region tracking arrays fall back to vmalloc
In reply to Alexander GrafNE_SET_USER_MEMORY_REGION allocates two tracking arrays that grow with the region, and for a large region kzalloc_objs() fails with a WARN. Neither needs physically contiguous memory, so allocate both with kvzalloc_objs(), with __GFP_NOWARN since a region can push even kvmalloc() past its limit.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +10 -8
@@ -986,15 +986,17 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, 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); + ne_mem_region->pages =kvzalloc_objs (*ne_mem_region->pages, max_nr_pages, +GFP_KERNEL | __GFP_NOWARN);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); + phys_contig_mem_regions.regions =kvzalloc_objs (*phys_contig_mem_regions.regions, + max_nr_pages, +GFP_KERNEL | __GFP_NOWARN);if (!phys_contig_mem_regions.regions) { rc = -ENOMEM ;
@@ -1084,7 +1086,7 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,dev_err_ratelimited (ne_misc_dev.this_device,"Error in slot add mem [rc=%d]\n" , rc); -kfree (phys_contig_mem_regions.regions); +kvfree (phys_contig_mem_regions.regions);/* * Exit here without put pages as memory regions may
@@ -1097,7 +1099,7 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, ne_enclave->nr_mem_regions++; } -kfree (phys_contig_mem_regions.regions); +kvfree (phys_contig_mem_regions.regions);return 0 ;
@@ -1105,8 +1107,8 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,for (i =0 ; i < ne_mem_region->nr_pages; i++)put_page (ne_mem_region->pages[i]); free_mem_region: -kfree (phys_contig_mem_regions.regions); -kfree (ne_mem_region->pages); +kvfree (phys_contig_mem_regions.regions); +kvfree (ne_mem_region->pages);kfree (ne_mem_region);return rc;
@@ -1496,7 +1498,7 @@ static void ne_enclave_remove_all_mem_region_entries(struct ne_enclave *ne_enclafor (i =0 ; i < ne_mem_region->nr_pages; i++)put_page (ne_mem_region->pages[i]); -kfree (ne_mem_region->pages); +kvfree (ne_mem_region->pages);kfree (ne_mem_region); }
Signature · 1 line
2.47.1
PATCH v2 12/23 nitro_enclaves: Do not return enclave threads to a replaced CPU pool
In reply to Alexander GrafA write to ne_cpus checks that no enclave exists, drops enclaves_list_mutex and then replaces the CPU pool, so an NE_CREATE_VM in between gets an enclave whose release writes into the freed pool. Hold the mutex across the check and take the pool out of the driver's view before dropping it, and take it and ne_devs.lock killable, as the ioctls do. An NE_CREATE_VM that runs while the pool is being replaced then sees no pool and is refused, instead of caching the one that is going away. The CPUs of the old and the new pool change state only once the locks drop: remove_cpu() and add_cpu() take device_hotplug_lock, which ACPI hotplug holds around the probe and remove callbacks that take ne_devs.lock for writing.
---
v1 -> v2:
- Drop the driver locks before onlining or offlining CPUs (Sashiko)
- Refuse an NE_CREATE_VM that races a replace instead of blocking it
drivers/virt/nitro_enclaves/ne_misc_dev.c | 89 ++++++++++++++--------- 1 file changed, 55 insertions(+), 34 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +55 -34
@@ -137,37 +137,6 @@ struct ne_phys_contig_mem_regions {struct range *regions; }; -/** -* ne_check_enclaves_created() - Verify if at least one enclave has been created. -* @void: No parameters provided. -* -* Context: Process context. -* Return: -* * True if at least one enclave is created. -* * False otherwise. -*/ -static bool ne_check_enclaves_created (void ) -{ -struct ne_pci_dev *ne_pci_dev =NULL ; -bool ret = false; - -down_read (&ne_devs.lock); - - ne_pci_dev = ne_devs.ne_pci_dev; -if (ne_pci_dev) { -mutex_lock (&ne_pci_dev->enclaves_list_mutex); - -if (!list_empty (&ne_pci_dev->enclaves_list)) - ret = true; - -mutex_unlock (&ne_pci_dev->enclaves_list_mutex); - } - -up_read (&ne_devs.lock); - -return ret; -} -/** * ne_free_core_slots() - Free an array of cpumasks that holds the threads of * full CPU cores.
@@ -483,18 +452,61 @@ static void ne_teardown_cpu_pool(void)static int ne_set_kernel_param (const char *val,const struct kernel_param *kp) {char error_val[] ="" ; +struct ne_pci_dev *ne_pci_dev =NULL ; +cpumask_var_t *threads_per_core =NULL ; +unsigned int nr_cores =0 ;int rc = -EINVAL ;if (!capable (CAP_SYS_ADMIN ))return -EPERM ; -if (ne_check_enclaves_created ()) { + rc =down_read_killable (&ne_devs.lock); +if (rc) +return rc; + + ne_pci_dev = ne_devs.ne_pci_dev; + +/* +* Keep the enclave list from gaining an entry while the pool is being +* replaced, so that no enclave ends up holding a core count that +* describes a pool that is already gone. The list lives in the PCI +* device, which is not bound yet when this runs for a value given on +* the kernel command line; nothing can create an enclave then either. +*/ +if (ne_pci_dev) { + rc =mutex_lock_killable (&ne_pci_dev->enclaves_list_mutex); +if (rc) + goto unlock_devs; + } + +if (ne_pci_dev && !list_empty (&ne_pci_dev->enclaves_list)) {pr_err ("%s: The CPU pool is used by enclave(s)\n" , ne_misc_dev.name); -return -EPERM ; + rc = -EPERM ; + + goto unlock; } -ne_teardown_cpu_pool (); +/* +* Take the pool out of the driver's view while the enclave list is +* still locked, so an enclave created once the locks drop sees no pool +* instead of the one that is going away. The CPUs change state only +* after the locks drop: remove_cpu() and add_cpu() take +* device_hotplug_lock, which ACPI hotplug holds around the probe and +* remove callbacks, and those take ne_devs.lock. +*/ +ne_detach_cpu_pool (&threads_per_core, &nr_cores); + +if (ne_pci_dev) +mutex_unlock (&ne_pci_dev->enclaves_list_mutex); + +up_read (&ne_devs.lock); + +if (threads_per_core) { +ne_online_core_slots (threads_per_core, nr_cores); + +ne_free_core_slots (threads_per_core, nr_cores); + } rc =ne_setup_cpu_pool (val);if (rc <0 ) {
@@ -517,6 +529,15 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) }return 0 ; + +unlock: +if (ne_pci_dev) +mutex_unlock (&ne_pci_dev->enclaves_list_mutex); + +unlock_devs: +up_read (&ne_devs.lock); + +return rc; }/**
Signature · 1 line
2.47.1
PATCH v2 13/23 nitro_enclaves: Do not report a torn down CPU pool as set
In reply to Alexander GrafAn admin who writes a CPU list to ne_cpus can be told that the write succeeded while the driver is left with no CPU pool: remove_cpu() returns 1 for a CPU that went offline after the list was checked, and ne_set_kernel_param() only tests for a negative value. Return -EINVAL, as for a list that names an offline CPU.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/virt/nitro_enclaves/ne_misc_dev.c +3 -0
@@ -388,6 +388,9 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list)pr_err ("%s: CPU %d is not offlined [rc=%d]\n" , ne_misc_dev.name, cpu, rc); +if (rc >0 ) + rc = -EINVAL ; + goto online_cpus; } }
Signature · 1 line
2.47.1
PATCH v2 14/23 nitro_enclaves: Keep the CPU pool when the new CPU list does not parse
In reply to Alexander GrafAn admin who mistypes a CPU list into ne_cpus loses the pool that was there, because ne_set_kernel_param() tears the pool down before ne_setup_cpu_pool() parses the new value. The write fails, but the pool's CPUs come back online and ne_cpus reads empty.
A list that parses but does not fit the 512 byte ne_cpus buffer fails the same way, only later: param_set_copystring() refuses it at the end of the replace, after the new pool is already in place, and the teardown that follows destroys a pool a new enclave may already have drawn threads from. A sysfs write takes a page, and a list of single-CPU entries for 128 CPUs is over 600 bytes, so a valid list can reach this.
Parse the list and check its length before the teardown and pass the mask down. The other checks stay after the teardown, because they want every CPU of the list online, and a list that extends the current pool names offline CPUs.
---
v1 -> v2:
- Refuse a list that does not fit ne_cpus before replacing the pool
drivers/virt/nitro_enclaves/ne_misc_dev.c | 77 ++++++++++++++--------- 1 file changed, 46 insertions(+), 31 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +46 -31
@@ -212,7 +212,7 @@ static void ne_detach_cpu_pool(cpumask_var_t **threads_per_core, unsigned int *n* or not using CPU 0, which should remain available for * the primary / parent VM. Offline the CPUs from the * pool after the checks passed. -* @ne_cpu_list: The CPU list used for setting NE CPU pool. +* @cpu_pool: The CPU mask used for setting NE CPU pool. * * Context: Process context. No driver lock may be held: remove_cpu() and * add_cpu() take device_hotplug_lock. The pool mutex only guards the
@@ -221,11 +221,10 @@ static void ne_detach_cpu_pool(cpumask_var_t **threads_per_core, unsigned int *n* * 0 on success. * * Negative return value on failure. */ -static int ne_setup_cpu_pool (const char *ne_cpu_list) +static int ne_setup_cpu_pool (const struct cpumask *cpu_pool) {int core_id =-1 ;unsigned int cpu =0 ; -cpumask_var_t cpu_pool;unsigned int cpu_sibling =0 ;unsigned int i =0 ;unsigned int nr_parent_vm_cores =0 ;
@@ -234,23 +233,13 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list)int rc = -EINVAL ;cpumask_var_t *threads_per_core =NULL ; -if (!zalloc_cpumask_var (&cpu_pool,GFP_KERNEL )) -return -ENOMEM ; - - rc =cpulist_parse (ne_cpu_list, cpu_pool); -if (rc <0 ) { -pr_err ("%s: Error in cpulist parse [rc=%d]\n" , ne_misc_dev.name, rc); - - goto free_pool_cpumask; - } - cpu =cpumask_any (cpu_pool);if (cpu >= nr_cpu_ids) {pr_err ("%s: No CPUs available in CPU pool\n" , ne_misc_dev.name); rc = -EINVAL ; - goto free_pool_cpumask; + goto free_cores_cpumask; }/*
@@ -264,7 +253,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL ; - goto free_pool_cpumask; + goto free_cores_cpumask; }/*
@@ -279,7 +268,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL ; - goto free_pool_cpumask; + goto free_cores_cpumask; } }else {if (numa_node !=cpu_to_node (cpu)) {
@@ -288,7 +277,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL ; - goto free_pool_cpumask; + goto free_cores_cpumask; } }
@@ -301,7 +290,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL ; - goto free_pool_cpumask; + goto free_cores_cpumask; }for_each_cpu (cpu_sibling,topology_sibling_cpumask (0 )) {
@@ -311,7 +300,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL ; - goto free_pool_cpumask; + goto free_cores_cpumask; } }
@@ -328,7 +317,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) rc = -EINVAL ; - goto free_pool_cpumask; + goto free_cores_cpumask; } } }
@@ -344,7 +333,7 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list)if (!threads_per_core) { rc = -ENOMEM ; - goto free_pool_cpumask; + goto free_cores_cpumask; }for (i =0 ; i < nr_parent_vm_cores; i++)
@@ -395,8 +384,6 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) } } -free_cpumask_var (cpu_pool); -mutex_lock (&ne_cpu_pool.mutex); ne_cpu_pool.avail_threads_per_core = threads_per_core;
@@ -413,8 +400,6 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list)add_cpu (cpu); free_cores_cpumask:ne_free_core_slots (threads_per_core, nr_parent_vm_cores); -free_pool_cpumask: -free_cpumask_var (cpu_pool);return rc; }
@@ -455,6 +440,7 @@ static void ne_teardown_cpu_pool(void)static int ne_set_kernel_param (const char *val,const struct kernel_param *kp) {char error_val[] ="" ; +cpumask_var_t cpu_pool;struct ne_pci_dev *ne_pci_dev =NULL ;cpumask_var_t *threads_per_core =NULL ;unsigned int nr_cores =0 ;
@@ -463,9 +449,12 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)if (!capable (CAP_SYS_ADMIN ))return -EPERM ; +if (!zalloc_cpumask_var (&cpu_pool,GFP_KERNEL )) +return -ENOMEM ; + rc =down_read_killable (&ne_devs.lock);if (rc) -return rc; + goto free_cpumask; ne_pci_dev = ne_devs.ne_pci_dev;
@@ -490,6 +479,31 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) goto unlock; } +/* +* Refuse a list the parameter buffer cannot hold before the detach +* below, so that the copy at the end of the replace cannot fail +* once the new pool is in place. +*/ +if (strlen (val) >=NE_CPUS_SIZE ) { +pr_err ("%s: The CPU list does not fit ne_cpus\n" , ne_misc_dev.name); + + rc = -ENOSPC ; + + goto unlock; + } + +/* +* Parse the list before the detach below takes the pool out of the +* driver's view, so that a value that does not parse leaves it in +* place. +*/ + rc =cpulist_parse (val, cpu_pool); +if (rc <0 ) { +pr_err ("%s: Error in cpulist parse [rc=%d]\n" , ne_misc_dev.name, rc); + + goto unlock; + } +/* * Take the pool out of the driver's view while the enclave list is * still locked, so an enclave created once the locks drop sees no pool
@@ -511,13 +525,13 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)ne_free_core_slots (threads_per_core, nr_cores); } - rc =ne_setup_cpu_pool (val); + rc =ne_setup_cpu_pool (cpu_pool);if (rc <0 ) {pr_err ("%s: Error in setup CPU pool [rc=%d]\n" , ne_misc_dev.name, rc);param_set_copystring (error_val, kp); -return rc; + goto free_cpumask; } rc =param_set_copystring (val, kp);
@@ -527,11 +541,9 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)ne_teardown_cpu_pool ();param_set_copystring (error_val, kp); - -return rc; } -return 0 ; + goto free_cpumask; unlock:if (ne_pci_dev)
@@ -540,6 +552,9 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) unlock_devs:up_read (&ne_devs.lock); +free_cpumask: +free_cpumask_var (cpu_pool); +return rc; }
Signature · 1 line
2.47.1
PATCH v2 15/23 nitro_enclaves: Put the CPU pool back when the new CPU list is refused
In reply to Alexander GrafAn admin who writes a CPU list that the driver refuses, for example one that names CPU 0, leaves out a sibling or spans two NUMA nodes, loses the pool that was there. The checks need every CPU of the list online and a CPU the pool holds is offline, so they can only run after the old pool is torn down.
Snapshot the CPUs of the pool before the teardown, and set the pool up again from the snapshot when the new list is refused. Where that fails as well, clear ne_cpus so that it does not name a pool that is gone.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 43 +++++++++++++++++++---- 1 file changed, 37 insertions(+), 6 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +37 -6
@@ -427,6 +427,24 @@ static void ne_teardown_cpu_pool(void)ne_free_core_slots (threads_per_core, nr_cores); } +/** +* ne_snapshot_cpu_pool() - Copy out the CPUs the NE CPU pool holds. +* @cpu_pool: CPU mask to fill in with the CPUs from the NE CPU pool. +* +* Context: Process context. +*/ +static void ne_snapshot_cpu_pool (struct cpumask *cpu_pool) +{ +unsigned int i =0 ; + +mutex_lock (&ne_cpu_pool.mutex); + +for (i =0 ; i < ne_cpu_pool.nr_parent_vm_cores; i++) +cpumask_or (cpu_pool, cpu_pool, ne_cpu_pool.avail_threads_per_core[i]); + +mutex_unlock (&ne_cpu_pool.mutex); +} +/** * ne_set_kernel_param() - Set the NE CPU pool value via the NE kernel parameter. * @val: NE CPU pool string value.
@@ -441,6 +459,7 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) {char error_val[] ="" ;cpumask_var_t cpu_pool; +cpumask_var_t old_pool;struct ne_pci_dev *ne_pci_dev =NULL ;cpumask_var_t *threads_per_core =NULL ;unsigned int nr_cores =0 ;
@@ -452,9 +471,15 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)if (!zalloc_cpumask_var (&cpu_pool,GFP_KERNEL ))return -ENOMEM ; +if (!zalloc_cpumask_var (&old_pool,GFP_KERNEL )) { +free_cpumask_var (cpu_pool); + +return -ENOMEM ; + } + rc =down_read_killable (&ne_devs.lock);if (rc) - goto free_cpumask; + goto free_cpumasks; ne_pci_dev = ne_devs.ne_pci_dev;
@@ -512,6 +537,8 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)* device_hotplug_lock, which ACPI hotplug holds around the probe and * remove callbacks, and those take ne_devs.lock. */ +ne_snapshot_cpu_pool (old_pool); +ne_detach_cpu_pool (&threads_per_core, &nr_cores);if (ne_pci_dev)
@@ -529,9 +556,10 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)if (rc <0 ) {pr_err ("%s: Error in setup CPU pool [rc=%d]\n" , ne_misc_dev.name, rc); -param_set_copystring (error_val, kp); +if (cpumask_empty (old_pool) ||ne_setup_cpu_pool (old_pool) <0 ) +param_set_copystring (error_val, kp); - goto free_cpumask; + goto free_cpumasks; } rc =param_set_copystring (val, kp);
@@ -540,10 +568,11 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)ne_teardown_cpu_pool (); -param_set_copystring (error_val, kp); +if (cpumask_empty (old_pool) ||ne_setup_cpu_pool (old_pool) <0 ) +param_set_copystring (error_val, kp); } - goto free_cpumask; + goto free_cpumasks; unlock:if (ne_pci_dev)
@@ -552,7 +581,9 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) unlock_devs:up_read (&ne_devs.lock); -free_cpumask: +free_cpumasks: +free_cpumask_var (old_pool); +free_cpumask_var (cpu_pool);return rc;
Signature · 1 line
2.47.1
PATCH v2 16/23 nitro_enclaves: Initialise the CPU pool mutex statically
In reply to Alexander GrafThe ne_cpus setter can run before ne_init(), when the parameter is given at boot or to modprobe, and then takes ne_cpu_pool.mutex before mutex_init() ran on it. Initialise the mutex in its definition instead.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +3 -3
@@ -125,7 +125,9 @@ struct ne_cpu_pool {int numa_node; }; -static struct ne_cpu_pool ne_cpu_pool; +static struct ne_cpu_pool ne_cpu_pool = { + .mutex =__MUTEX_INITIALIZER (ne_cpu_pool.mutex), +};/** * struct ne_phys_contig_mem_regions - Contiguous physical memory regions.
@@ -1984,8 +1986,6 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg)static int __init ne_init (void ) { -mutex_init (&ne_cpu_pool.mutex); -return pci_register_driver (&ne_pci_driver); }
Signature · 1 line
2.47.1
PATCH v2 17/23 nitro_enclaves: Free the CPU pool when driver registration fails
In reply to Alexander GrafWhen the module loads with a ne_cpus= parameter, the setter builds the CPU pool and offlines its CPUs before ne_init() runs. A pci_register_driver() failure then unloads the module with the pool still allocated and its CPUs offline, and only onlining them by hand brings them back.
Tear down the pool when the driver cannot register.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +7 -1
@@ -1986,7 +1986,13 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg)static int __init ne_init (void ) { -return pci_register_driver (&ne_pci_driver); +int rc; + + rc =pci_register_driver (&ne_pci_driver); +if (rc <0 ) +ne_teardown_cpu_pool (); + +return rc; } static void __exit ne_exit(void)
Signature · 1 line
2.47.1
PATCH v2 18/23 nitro_enclaves: Free the CPU pool that ne_exit() does not see
In reply to Alexander GrafWhen the kernel command line carries nitro_enclaves.ne_cpus=, modprobe passes that value to the module ahead of its own arguments. A "modprobe nitro_enclaves ne_cpus=banana" then builds the pool from the command line value and offlines its CPUs. The second value fails to parse, which keeps that pool and aborts the load. ne_exit() does not run for a failed load, so the pool stays allocated with its CPUs offline. Every later modprobe fails with "CPU 2 is offline, has to be online to get its metadata" until someone onlines the CPUs by hand. Any other load failure after a pool was built ends the same way.
rmmod can leave a pool behind too. A write to the ne_cpus parameter takes no reference on the module, so ne_exit() can tear down the pool while the write is still offlining the CPUs of a new one. The write then publishes its pool after the module tore down the old one.
The module loader calls the free callback of every parameter when a load fails at or after the point where it parses the arguments. It also calls it on unload, after removing the parameter files, which waits for any write in progress and refuses new ones. Tear down the pool there.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
drivers/virt/nitro_enclaves/ne_misc_dev.c +19 -0
@@ -86,10 +86,12 @@ struct ne_devs ne_devs = {* a kernel parameter e.g. if multiple sysfs files needed. */ static int ne_set_kernel_param (const char *val,const struct kernel_param *kp); +static void ne_free_kernel_param (void *arg);static const struct kernel_param_ops ne_cpu_pool_ops = { .get = param_get_string, .set = ne_set_kernel_param, + .free = ne_free_kernel_param, };static char ne_cpus[NE_CPUS_SIZE ];
@@ -591,6 +593,23 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)return rc; } +/** +* ne_free_kernel_param() - Tear down the NE CPU pool when the module goes away. +* @arg: The NE kernel parameter argument, unused. +* +* The module loader calls this when a load fails at or after argument +* parsing, and on unload once the parameter files are gone, so no write to +* the parameter can still be running. ne_exit() does not run for a failed +* load and can run while a write is still building a pool, so tear the pool +* down here too. +* +* Context: Process context. No driver lock may be held. +*/ +static void ne_free_kernel_param (void *arg) +{ +ne_teardown_cpu_pool (); +} +/** * ne_donated_cpu() - Check if the provided CPU is already used by the enclave. * @ne_enclave : Private data associated with the current enclave.
Signature · 1 line
2.47.1
PATCH v2 19/23 nitro_enclaves: Build the boot-time CPU pool once the CPUs are online
In reply to Alexander GrafAn admin who passes nitro_enclaves.ne_cpus= to a kernel with the driver built in gets no CPU pool, and this not very informative error message:
Booting kernel: 2-5' invalid for parameter nitro_enclaves.ne_cpus'
The parameter setter runs from parse_args() in start_kernel(), before the page allocator and the secondary CPUs are up. Building a pool needs both.
Keep a value that arrives while system_state is SYSTEM_BOOTING in ne_cpus, note its parameter in ne_cpus_kp, and build the pool from it in ne_init(), which runs once both are up. The pool building moves into ne_replace_cpu_pool() for that. That way a valid list on the command line gets its pool, and an invalid one reports why. A list that ne_init() rejects is cleared out of ne_cpus, so the parameter does not keep naming a pool that was never built.
---
v1 -> v2:
- Clear ne_cpus when ne_init() rejects the boot-time list (Sashiko)
drivers/virt/nitro_enclaves/ne_misc_dev.c | 67 +++++++++++++++++++++-- 1 file changed, 62 insertions(+), 5 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +62 -5
@@ -14,6 +14,7 @@#include <linux/device.h> #include <linux/file.h> #include <linux/hugetlb.h> +#include <linux/kernel.h> #include <linux/limits.h> #include <linux/list.h> #include <linux/miscdevice.h>
@@ -104,6 +105,13 @@ module_param_cb(ne_cpus, &ne_cpu_pool_ops, &ne_cpus_arg, 0644);/* https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html#cpu-lists */ MODULE_PARM_DESC (ne_cpus,"<cpu-list> - CPU pool used for Nitro Enclaves" ); +/* +* The parameter a value from the kernel command line is kept in until ne_init() +* builds its pool. Nothing else writes ne_cpus, so it is set whenever ne_cpus +* holds a value, and no sysfs write can reach the setter before ne_init() runs. +*/ +static const struct kernel_param *ne_cpus_kp; +/** * struct ne_cpu_pool - CPU pool used for Nitro Enclaves. * @avail_threads_per_core: Available full CPU cores to be dedicated to
@@ -450,7 +458,7 @@ static void ne_snapshot_cpu_pool(struct cpumask *cpu_pool) }/** -* ne_set_kernel_param() - Set the NE CPU pool value via the NE kernel parameter. +* ne_replace_cpu_pool() - Replace the NE CPU pool with the one a CPU list names. * @val: NE CPU pool string value. * @kp : NE kernel parameter associated with the NE CPU pool. *
@@ -459,7 +467,7 @@ static void ne_snapshot_cpu_pool(struct cpumask *cpu_pool)* * 0 on success. * * Negative return value on failure. */ -static int ne_set_kernel_param (const char *val,const struct kernel_param *kp) +static int ne_replace_cpu_pool (const char *val,const struct kernel_param *kp) {char error_val[] ="" ;cpumask_var_t cpu_pool;
@@ -469,9 +477,6 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)unsigned int nr_cores =0 ;int rc = -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) -return -EPERM ; -if (!zalloc_cpumask_var (&cpu_pool,GFP_KERNEL ))return -ENOMEM ;
@@ -593,6 +598,39 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)return rc; } +/** +* ne_set_kernel_param() - Set the NE CPU pool value via the NE kernel parameter. +* @val: NE CPU pool string value. +* @kp : NE kernel parameter associated with the NE CPU pool. +* +* Context: Process context. +* Return: +* * 0 on success. +* * Negative return value on failure. +*/ +static int ne_set_kernel_param (const char *val,const struct kernel_param *kp) +{ +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; + +/* +* A value on the kernel command line reaches this from the parse_args() +* call in start_kernel(), which runs before mm_core_init() has brought +* up the allocator a cpumask comes from and long before smp_init() has +* brought online the CPUs a pool is built out of. Keep the value and +* leave the pool to ne_init(), which runs from do_basic_setup() with +* both of those behind it. A value from modprobe or from sysfs arrives +* with the system already up and is applied here. +*/ +if (system_state ==SYSTEM_BOOTING ) { + ne_cpus_kp = kp; + +return param_set_copystring (val, kp); + } + +return ne_replace_cpu_pool (val, kp); +} +/** * ne_free_kernel_param() - Tear down the NE CPU pool when the module goes away. * @arg: The NE kernel parameter argument, unused.
@@ -2005,8 +2043,27 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg)static int __init ne_init (void ) { +static char cpu_list[NE_CPUS_SIZE ]__initdata ;int rc; +/* +* Build the pool of a CPU list that came from the kernel command line, +* which the setter only kept. The value is taken out of the parameter +* string first, because building the pool writes that string. A list +* the build rejects is taken out of the parameter too, so ne_cpus does +* not name a pool that was never built. +*/ +if (ne_cpus_kp && ne_cpus[0 ]) { +memcpy (cpu_list, ne_cpus,sizeof (cpu_list)); + +if (ne_replace_cpu_pool (cpu_list, ne_cpus_kp) <0 ) { +param_set_copystring ("" , ne_cpus_kp); + +pr_err ("%s: No CPU pool from nitro_enclaves.ne_cpus=%s\n" , + ne_misc_dev.name, cpu_list); + } + } + rc =pci_register_driver (&ne_pci_driver);if (rc <0 )ne_teardown_cpu_pool ();
Signature · 1 line
2.47.1
PATCH v2 20/23 nitro_enclaves: Set up the CPU pool under the CPU hotplug read lock
In reply to Alexander GrafAn admin who writes a CPU list to ne_cpus while a CPU goes offline can get a pool built from the topology before and after that CPU went. ne_setup_cpu_pool() reads cpu_is_offline(), cpu_to_node() and topology_sibling_cpumask() for the list without the CPU hotplug lock.
Take cpus_read_lock() over those reads, and drop it before the CPUs are offlined, because remove_cpu() takes the write side. The failure paths before the drop share one cpus_read_unlock().
---
v1 -> v2:
- Adapt to the pool setup that offlines the CPUs with no lock held
- Unwind failures before the lock drop with one cpus_read_unlock()
drivers/virt/nitro_enclaves/ne_misc_dev.c | 31 +++++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +23 -8
@@ -226,6 +226,11 @@ static void ne_detach_cpu_pool(cpumask_var_t **threads_per_core, unsigned int *n* pool after the checks passed. * @cpu_pool: The CPU mask used for setting NE CPU pool. * +* The topology of the pool - online state, sibling masks, core ids - is read +* under the CPU hotplug read lock. The lock is dropped before the pool CPUs are +* offlined, because remove_cpu() takes the write side of the same percpu-rwsem. +* Nothing reads topology after the drop. +* * Context: Process context. No driver lock may be held: remove_cpu() and * add_cpu() take device_hotplug_lock. The pool mutex only guards the * publish of the finished pool.
@@ -245,13 +250,15 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)int rc = -EINVAL ;cpumask_var_t *threads_per_core =NULL ; +cpus_read_lock (); + cpu =cpumask_any (cpu_pool);if (cpu >= nr_cpu_ids) {pr_err ("%s: No CPUs available in CPU pool\n" , ne_misc_dev.name); rc = -EINVAL ; - goto free_cores_cpumask; + goto unlock_hotplug; }/*
@@ -265,7 +272,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL ; - goto free_cores_cpumask; + goto unlock_hotplug; }/*
@@ -280,7 +287,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL ; - goto free_cores_cpumask; + goto unlock_hotplug; } }else {if (numa_node !=cpu_to_node (cpu)) {
@@ -289,7 +296,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL ; - goto free_cores_cpumask; + goto unlock_hotplug; } }
@@ -302,7 +309,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL ; - goto free_cores_cpumask; + goto unlock_hotplug; }for_each_cpu (cpu_sibling,topology_sibling_cpumask (0 )) {
@@ -312,7 +319,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL ; - goto free_cores_cpumask; + goto unlock_hotplug; } }
@@ -329,7 +336,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL ; - goto free_cores_cpumask; + goto unlock_hotplug; } } }
@@ -345,7 +352,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)if (!threads_per_core) { rc = -ENOMEM ; - goto free_cores_cpumask; + goto unlock_hotplug; }for (i =0 ; i < nr_parent_vm_cores; i++)
@@ -373,6 +380,8 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)cpumask_set_cpu (cpu, threads_per_core[core_id]); } +cpus_read_unlock (); +/* * CPUs that are given to enclave(s) should not be considered online * by Linux anymore, as the hypervisor will degrade them to floating.
@@ -410,8 +419,14 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) online_cpus:for_each_cpu (cpu, cpu_pool)add_cpu (cpu); +ne_free_core_slots (threads_per_core, nr_parent_vm_cores); + +return rc; + free_cores_cpumask:ne_free_core_slots (threads_per_core, nr_parent_vm_cores); +unlock_hotplug: +cpus_read_unlock ();return rc; }
Signature · 1 line
2.47.1
PATCH v2 21/23 nitro_enclaves: Let 32-bit processes use the ioctl interface
In reply to Alexander GrafA 32-bit process on a 64-bit kernel gets -ENOTTY for every NE ioctl, because neither the misc device nor an enclave fd sets .compat_ioctl. The ioctl arguments don't require translation, so just add the default compat_ioctl pointer to them and hence enable 32bit user space to drive the device as well.
--- drivers/virt/nitro_enclaves/ne_misc_dev.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/virt/nitro_enclaves/ne_misc_dev.c +2 -0
@@ -68,6 +68,7 @@ static const struct file_operations ne_fops = { .owner = THIS_MODULE, .llseek = noop_llseek, .unlocked_ioctl = ne_ioctl, + .compat_ioctl = compat_ptr_ioctl, };static struct miscdevice ne_misc_dev = {
@@ -1855,6 +1856,7 @@ static const struct file_operations ne_enclave_fops = { .llseek = noop_llseek, .poll = ne_enclave_poll, .unlocked_ioctl = ne_enclave_ioctl, + .compat_ioctl = compat_ptr_ioctl, .release = ne_enclave_release, };
Signature · 1 line
2.47.1
PATCH v2 22/23 nitro_enclaves: Index CPU pool cores by sibling mask
In reply to Alexander GrafAn enclave on a host whose CPU pool spans two packages can get the threads of a core on a different package. The pool indexes avail_threads_per_core[] by topology_core_id(), which is package-local on x86, so two cores in different packages share one entry.
Walk the sibling mask of each pool CPU instead, in ne_build_core_slots(), which gives each core its own entry and counts the cores of the pool. An enclave copies that count at NE_CREATE_VM. NE_ADD_VCPU now bounds vcpu_id by nr_cpu_ids instead of the thread count of the pool, so an id above the old bound that the pool does not hold answers NE_ERR_VCPU_NOT_IN_CPU_POOL instead of NE_ERR_INVALID_VCPU.
---
v1 -> v2:
- Return the slots in local arrays for the reworked pool setup
- Build the slots under the CPU hotplug read lock, not the pool mutex
drivers/virt/nitro_enclaves/ne_misc_dev.c | 171 ++++++++++++++-------- drivers/virt/nitro_enclaves/ne_misc_dev.h | 16 +- 2 files changed, 116 insertions(+), 71 deletions(-)
drivers/virt/nitro_enclaves/ne_misc_dev.c +109 -62
@@ -18,6 +18,7 @@#include <linux/limits.h> #include <linux/list.h> #include <linux/miscdevice.h> +#include <linux/minmax.h> #include <linux/mm.h> #include <linux/mman.h> #include <linux/module.h>
@@ -116,23 +117,23 @@ static const struct kernel_param *ne_cpus_kp;/** * struct ne_cpu_pool - CPU pool used for Nitro Enclaves. * @avail_threads_per_core: Available full CPU cores to be dedicated to -* enclave(s). The cpumasks from the array, indexed -* by core id, contain all the threads from the -* available cores, that are not set for created -* enclave(s). The full CPU cores are part of the -* NE CPU pool. +* enclave(s). Each cpumask in the array holds the +* threads of one core in the pool that are not set +* for created enclave(s). Slots are handed out as +* cores are discovered and carry no meaning +* outside the driver. * @mutex: Mutex for the access to the NE CPU pool. -* @nr_parent_vm_cores : The size of the available threads per core array. -* The total number of CPU cores available on the -* primary / parent VM. -* @nr_threads_per_core: The number of threads that a full CPU core has. +* @nr_parent_vm_cores : The size of the available threads per core array, +* one slot per full CPU core in the pool. Counted +* from the pool rather than derived from a thread +* count, so a machine whose cores do not all have +* the same number of threads is sized correctly. * @numa_node: NUMA node of the CPUs in the pool. */ struct ne_cpu_pool {cpumask_var_t *avail_threads_per_core;struct mutex mutex;unsigned int nr_parent_vm_cores; -unsigned int nr_threads_per_core;int numa_node; };
@@ -213,12 +214,96 @@ static void ne_detach_cpu_pool(cpumask_var_t **threads_per_core, unsigned int *n ne_cpu_pool.avail_threads_per_core =NULL ; ne_cpu_pool.nr_parent_vm_cores =0 ; - ne_cpu_pool.nr_threads_per_core =0 ; ne_cpu_pool.numa_node =-1 ;mutex_unlock (&ne_cpu_pool.mutex); } +/** +* ne_build_core_slots() - Group the CPU pool into full cores and give each one +* a slot of the NE CPU pool arrays. +* @cpu_pool: The CPU pool as parsed out of the ne_cpus parameter. +* @slots: Returns the array with one cpumask per core of the pool. +* @nr_cores: Returns the size of the array. +* +* The slots preserve the CPU topology of the pool once its CPUs are offlined. +* topology_core_id() is package-local on x86, so two cores in different +* packages report the same id and would share a slot. The per-CPU sibling +* cpumask identifies a core on the architectures this driver builds for, so +* walk that instead: once to count the cores the pool holds, and once to hand +* each of them the next free slot. Counting first sizes the arrays to the pool, +* so the walk has a slot for every core it finds. +* +* Context: Process context. This function is called with the CPU hotplug read +* lock held. +* Return: +* * 0 on success. +* * Negative return value on failure. +*/ +static int ne_build_core_slots (const struct cpumask *cpu_pool, +cpumask_var_t **slots,unsigned int *nr_cores) +{ +unsigned int cpu =0 ; +unsigned int cpu_sibling =0 ; +unsigned int i =0 ; +unsigned int next_core_idx =0 ; +unsigned int nr =0 ; +cpumask_var_t *threads_per_core =NULL ; +cpumask_var_t processed; +int rc = -ENOMEM ; + +if (!zalloc_cpumask_var (&processed,GFP_KERNEL )) +return -ENOMEM ; + +for_each_cpu (cpu, cpu_pool) { +if (cpumask_test_cpu (cpu, processed)) +continue ; + +cpumask_or (processed, processed,topology_sibling_cpumask (cpu)); + + nr++; + } + + threads_per_core =kzalloc_objs (*threads_per_core, nr); +if (!threads_per_core) + goto free_processed; + +for (i =0 ; i < nr; i++) +if (!zalloc_cpumask_var (&threads_per_core[i],GFP_KERNEL )) + goto free_slots; + +cpumask_clear (processed); + +for_each_cpu (cpu, cpu_pool) { +if (cpumask_test_cpu (cpu, processed)) +continue ; + +for_each_cpu (cpu_sibling,topology_sibling_cpumask (cpu)) { +if (!cpumask_test_cpu (cpu_sibling, cpu_pool)) +continue ; + +cpumask_set_cpu (cpu_sibling, threads_per_core[next_core_idx]); +cpumask_set_cpu (cpu_sibling, processed); + } + + next_core_idx++; + } + +free_cpumask_var (processed); + + *slots = threads_per_core; + *nr_cores = nr; + +return 0 ; + +free_slots: +ne_free_core_slots (threads_per_core, nr); +free_processed: +free_cpumask_var (processed); + +return rc; +} +/** * ne_setup_cpu_pool() - Set the NE CPU pool after handling sanity checks such * as not sharing CPU cores with the primary / parent VM
@@ -227,7 +312,7 @@ static void ne_detach_cpu_pool(cpumask_var_t **threads_per_core, unsigned int *n* pool after the checks passed. * @cpu_pool: The CPU mask used for setting NE CPU pool. * -* The topology of the pool - online state, sibling masks, core ids - is read +* The topology of the pool - online state, sibling masks, core slots - is read * under the CPU hotplug read lock. The lock is dropped before the pool CPUs are * offlined, because remove_cpu() takes the write side of the same percpu-rwsem. * Nothing reads topology after the drop.
@@ -241,12 +326,9 @@ static void ne_detach_cpu_pool(cpumask_var_t **threads_per_core, unsigned int *n*/ static int ne_setup_cpu_pool (const struct cpumask *cpu_pool) { -int core_id =-1 ;unsigned int cpu =0 ;unsigned int cpu_sibling =0 ; -unsigned int i =0 ;unsigned int nr_parent_vm_cores =0 ; -unsigned int nr_threads_per_core =0 ;int numa_node =-1 ;int rc = -EINVAL ;cpumask_var_t *threads_per_core =NULL ;
@@ -264,7 +346,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)/* * Check if the CPUs are online, to further get info about them -* e.g. numa node, core id, siblings. +* e.g. numa node, siblings. */ for_each_cpu (cpu, cpu_pool)if (cpu_is_offline (cpu)) {
@@ -342,44 +424,9 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) } } -/* Calculate the number of threads from a full CPU core. */ - cpu =cpumask_any (cpu_pool); -for_each_cpu (cpu_sibling,topology_sibling_cpumask (cpu)) - nr_threads_per_core++; - - nr_parent_vm_cores = nr_cpu_ids / nr_threads_per_core; - - threads_per_core =kzalloc_objs (*threads_per_core, nr_parent_vm_cores); -if (!threads_per_core) { - rc = -ENOMEM ; - + rc =ne_build_core_slots (cpu_pool, &threads_per_core, &nr_parent_vm_cores); +if (rc <0 ) goto unlock_hotplug; - } - -for (i =0 ; i < nr_parent_vm_cores; i++) -if (!zalloc_cpumask_var (&threads_per_core[i],GFP_KERNEL )) { - rc = -ENOMEM ; - - goto free_cores_cpumask; - } - -/* -* Split the NE CPU pool in threads per core to keep the CPU topology -* after offlining the CPUs. -*/ -for_each_cpu (cpu, cpu_pool) { - core_id =topology_core_id (cpu); -if (core_id <0 || core_id >= nr_parent_vm_cores) { -pr_err ("%s: Invalid core id %d for CPU %d\n" , - ne_misc_dev.name, core_id, cpu); - - rc = -EINVAL ; - - goto free_cores_cpumask; - } - -cpumask_set_cpu (cpu, threads_per_core[core_id]); - }cpus_read_unlock ();
@@ -410,7 +457,6 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) ne_cpu_pool.avail_threads_per_core = threads_per_core; ne_cpu_pool.nr_parent_vm_cores = nr_parent_vm_cores; - ne_cpu_pool.nr_threads_per_core = nr_threads_per_core; ne_cpu_pool.numa_node = numa_node;mutex_unlock (&ne_cpu_pool.mutex);
@@ -424,8 +470,6 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)return rc; -free_cores_cpumask: -ne_free_core_slots (threads_per_core, nr_parent_vm_cores); unlock_hotplug:cpus_read_unlock ();
@@ -740,7 +784,8 @@ static int ne_set_enclave_threads_per_core(struct ne_enclave *ne_enclave,return -NE_ERR_VCPU_NOT_IN_CPU_POOL ; } -if (core_id >= ne_enclave->nr_parent_vm_cores) { +if (core_id >=min (ne_enclave->nr_parent_vm_cores, + ne_cpu_pool.nr_parent_vm_cores)) {dev_err_ratelimited (ne_misc_dev.this_device,"Invalid core id %d - ne_enclave\n" , core_id);
@@ -1369,8 +1414,7 @@ static long ne_enclave_do_ioctl(struct file *file, unsigned int cmd, unsigned loreturn -NE_ERR_NOT_IN_INIT_STATE ; } -if (vcpu_id >= (ne_enclave->nr_parent_vm_cores * - ne_enclave->nr_threads_per_core)) { +if (vcpu_id >= nr_cpu_ids) {dev_err_ratelimited (ne_misc_dev.this_device,"vCPU id higher than max CPU id\n" );
@@ -1659,18 +1703,22 @@ static void ne_enclave_remove_all_mem_region_entries(struct ne_enclave *ne_encla*/ static void ne_enclave_remove_all_vcpu_id_entries (struct ne_enclave *ne_enclave) { +unsigned int nr_cores =0 ;unsigned int cpu =0 ;unsigned int i =0 ;mutex_lock (&ne_cpu_pool.mutex); -for (i =0 ; i < ne_enclave->nr_parent_vm_cores; i++) { + nr_cores =min (ne_enclave->nr_parent_vm_cores, + ne_cpu_pool.nr_parent_vm_cores); + +for (i =0 ; i < nr_cores; i++)for_each_cpu (cpu, ne_enclave->threads_per_core[i])/* Update the available NE CPU pool. */ cpumask_set_cpu (cpu, ne_cpu_pool.avail_threads_per_core[i]); +for (i =0 ; i < ne_enclave->nr_parent_vm_cores; i++)free_cpumask_var (ne_enclave->threads_per_core[i]); - }mutex_unlock (&ne_cpu_pool.mutex);
@@ -1909,7 +1957,6 @@ static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_uimutex_lock (&ne_cpu_pool.mutex); ne_enclave->nr_parent_vm_cores = ne_cpu_pool.nr_parent_vm_cores; - ne_enclave->nr_threads_per_core = ne_cpu_pool.nr_threads_per_core; ne_enclave->numa_node = ne_cpu_pool.numa_node;mutex_unlock (&ne_cpu_pool.mutex);
drivers/virt/nitro_enclaves/ne_misc_dev.h +7 -9
@@ -50,18 +50,17 @@ struct ne_mem_region {* @mem_size: Enclave memory size. * @mm : Enclave process abstraction mm data struct. * @nr_mem_regions: Number of memory regions associated with the enclave. -* @nr_parent_vm_cores : The size of the threads per core array. The -* total number of CPU cores available on the -* parent / primary VM. -* @nr_threads_per_core: The number of threads that a full CPU core has. +* @nr_parent_vm_cores : The size of the threads per core array, one slot +* per full CPU core in the NE CPU pool the enclave +* was created from. * @nr_vcpus: Number of vcpus associated with the enclave. * @numa_node: NUMA node of the enclave memory and CPUs. * @slot_uid: Slot unique id mapped to the enclave. * @state: Enclave state, updated during enclave lifetime. -* @threads_per_core: Enclave full CPU cores array, indexed by core id, -* consisting of cpumasks with all their threads. -* Full CPU cores are taken from the NE CPU pool -* and are available to the enclave. +* @threads_per_core: Enclave full CPU cores array. Each cpumask in the +* array holds the threads of one core taken from +* the NE CPU pool and available to the enclave, +* at the index of that core's slot in the pool. * @vcpu_ids: Cpumask of the vCPUs that are set for the enclave. */ struct ne_enclave {
@@ -76,7 +75,6 @@ struct ne_enclave {struct mm_struct *mm;unsigned int nr_mem_regions;unsigned int nr_parent_vm_cores; -unsigned int nr_threads_per_core;unsigned int nr_vcpus;int numa_node;u64 slot_uid;
Signature · 1 line
2.47.1
PATCH v2 23/23 nitro_enclaves: Time the wait for the device to turn off by the clock
In reply to Alexander GrafUnbind, shutdown and the next probe give the nitro_enclaves device NE_DEFAULT_TIMEOUT_MSECS, 120 s, to turn off. The device turns off only once it has stopped every enclave and scrubbed its memory, which can take as long as a single ENCLAVE_STOP. ne_pci_dev_disable() also counts the wait in 10 ms steps of msleep_interruptible(). At HZ=100 each step sleeps two jiffies, so the wait takes 240 s, and with a signal pending it does not sleep at all.
Allow the device NE_FREE_TIMEOUT_MSECS, 10 minutes, as for ENCLAVE_STOP, and poll the register with readx_poll_timeout(), which keeps a ktime deadline.
Kiro, an AI coding assistant, found this while timing an unbind on hardware.
--- drivers/virt/nitro_enclaves/ne_pci_dev.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
drivers/virt/nitro_enclaves/ne_pci_dev.c +8 -13
@@ -9,6 +9,7 @@#include <linux/delay.h> #include <linux/device.h> +#include <linux/iopoll.h> #include <linux/list.h> #include <linux/module.h> #include <linux/mutex.h>
@@ -550,8 +551,7 @@ static void ne_pci_dev_disable(struct pci_dev *pdev) {u8 dev_disable_reply =0 ;struct ne_pci_dev *ne_pci_dev =pci_get_drvdata (pdev); -const unsigned int sleep_time =10 ;/* 10 ms */ -unsigned int sleep_time_count =0 ; +int rc;iowrite8 (NE_ENABLE_OFF , ne_pci_dev->iomem_base +NE_ENABLE );
@@ -560,17 +560,12 @@ static void ne_pci_dev_disable(struct pci_dev *pdev)* state is not immediately set to disabled and going through a * transitory state of disabling. */ -while (sleep_time_count <NE_DEFAULT_TIMEOUT_MSECS ) { - dev_disable_reply =ioread8 (ne_pci_dev->iomem_base +NE_ENABLE ); -if (dev_disable_reply ==NE_ENABLE_OFF ) -return ; - -msleep_interruptible (sleep_time); - sleep_time_count += sleep_time; - } - - dev_disable_reply =ioread8 (ne_pci_dev->iomem_base +NE_ENABLE ); -if (dev_disable_reply !=NE_ENABLE_OFF ) + rc =readx_poll_timeout (ioread8, ne_pci_dev->iomem_base +NE_ENABLE , + dev_disable_reply, + dev_disable_reply ==NE_ENABLE_OFF , +10 *USEC_PER_MSEC , +NE_FREE_TIMEOUT_MSECS *USEC_PER_MSEC ); +if (rc <0 )dev_err (&pdev->dev,"Error in pci dev disable cmd\n" ); }
Signature · 1 line
2.47.1
[email protected]
AI reviewSashiko has finished reviewing this 23-patch series:
Sashiko has finished reviewing this 23-patch series:
- Patches 2-5 and 7-23: no issues found.
- Patch 6 ("nitro_enclaves: Retry a command the device asks us t..."): 1 potential issue (1 Medium)
- Patch 1: skipped (not reviewed).
A detailed review has been sent as a separate reply to patch 6.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]