From: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
To: Ric Li <ming3.li@intel.com>
Cc: dev@dpdk.org, Tyler Retzlaff <roretzla@linux.microsoft.com>
Subject: Re: [PATCH v2] windows/virt2phys: fix block MDL not updated
Date: Tue, 12 Sep 2023 00:50:41 +0300 [thread overview]
Message-ID: <20230912005041.0e09671c@sovereign> (raw)
In-Reply-To: <20230911130936.1485584-1-ming3.li@intel.com>
Hi Ric,
2023-09-11 21:09 (UTC+0800), Ric Li:
> The virt2phys_translate function previously scanned existing blocks,
> returning the physical address from the stored MDL info if present.
> This method was problematic when a virtual address pointed to a freed
> and reallocated memory segment, potentially changing the physical
> address mapping. Yet, virt2phys_translate would consistently return
> the originally stored physical address, which could be invalid.
I missed this case completely :(
Is any if these bugs are related?
If so, please mention "Bugzilla ID: xxxx" in the commit message.
https://bugs.dpdk.org/show_bug.cgi?id=1201
https://bugs.dpdk.org/show_bug.cgi?id=1213
>
> This issue surfaced when allocating a memory region larger than 2MB
> using rte_malloc. This action would allocate a new memory segment
> and use virt2phy to set the iova. The driver would store the MDL
Typo: "iova" -> "IOVA" here and below.
> and lock the pages initially. When this region was freed, the memory
> segment used as a whole page could be freed, invalidating the virtual
> to physical mapping. It then needed to be deleted from the driver's
> block MDL info. Before this fix, the driver would only return the
> initial physical address, leading to illegal iova for some pages when
> allocating a new memory region of the same size.
>
> To address this, a refresh function has been added. If a block with the
> same base address is detected in the driver's context, the MDL's
> physical address is compared with the real physical address.
> If they don't match, the MDL within the block is released and rebuilt
> to store the correct mapping.
What if the size is different?
Should it be updated for the refreshed block along with the MDL?
[...]
> +static NTSTATUS
> +virt2phys_block_refresh(struct virt2phys_block *block, PVOID base, size_t size)
> +{
> + NTSTATUS status;
> + PMDL mdl = block->mdl;
> +
> + /*
> + * Check if we need to refresh MDL in block.
> + * The virtual to physical memory mapping may be changed when the
> + * virtual memory region is freed by the user process and malloc again,
> + * then we need to unlock the physical memory and lock again to
> + * refresh the MDL information stored in block.
> + */
> + PHYSICAL_ADDRESS block_phys, real_phys;
> +
> + block_phys = virt2phys_block_translate(block, base);
> + real_phys = MmGetPhysicalAddress(base);
> +
> + if (block_phys.QuadPart == real_phys.QuadPart)
> + /* No need to refresh block. */
> + return STATUS_SUCCESS;
> +
> + virt2phys_unlock_memory(mdl);
After this call the block's MDL is a dangling pointer.
If an error occurs below, the block with a dangling pointer
will remain in the list and will probably cause a crash later.
If a block can't be refreshed, it must be freed (it's invalid anyway).
> + mdl = NULL;
> +
> + status = virt2phys_lock_memory(base, size, &mdl);
> + if (!NT_SUCCESS(status))
> + return status;
> +
> + status = virt2phys_check_memory(mdl);
> + if (!NT_SUCCESS(status)) {
> + virt2phys_unlock_memory(mdl);
> + return status;
> + }
> + block->mdl = mdl;
> +
> + TraceInfo("Block refreshed from %llx to %llx", block_phys.QuadPart, real_phys.QuadPart);
Please add process ID, block VA, and block size.
If refreshing fails, there is not way to tell what happened and why.
What do you think about logging like this?
ID=... VA=... size=... requires physical address refresh
ID=... VA=... size=... physical address refreshed from ... to ...
> +
> + return STATUS_SUCCESS;
> +}
> +
> NTSTATUS
> virt2phys_translate(PVOID virt, PHYSICAL_ADDRESS *phys)
> {
> PMDL mdl;
> HANDLE process_id;
> - void *base;
> + PVOID base;
> size_t size;
> struct virt2phys_process *process;
> struct virt2phys_block *block;
> @@ -371,6 +412,11 @@ virt2phys_translate(PVOID virt, PHYSICAL_ADDRESS *phys)
>
> /* Don't lock the same memory twice. */
> if (block != NULL) {
> + KeAcquireSpinLock(g_lock, &irql);
> + status = virt2phys_block_refresh(block, base, size);
> + KeReleaseSpinLock(g_lock, irql);
Is it safe to do all the external calls holding this spinlock?
I can't confirm from the doc that ZwQueryVirtualMemory(), for example,
does not access pageable data.
And virt2phys_lock_memory() raises exceptions, although it handles them.
Other stuff seems safe.
The rest of the code only takes the lock to access block and process lists,
which are allocated from the non-paged pool.
Now that I think of it, this may be insufficient because the code and the
static variables are not marked as non-paged.
The translation IOCTL performance is not critical,
so maybe it is worth replacing the spinlock with just a global mutex,
WDYT?
next prev parent reply other threads:[~2023-09-11 21:50 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-11 8:22 [PATCH] " Ric Li
2023-09-11 13:09 ` [PATCH v2] " Ric Li
2023-09-11 21:50 ` Dmitry Kozlyuk [this message]
2023-09-12 11:13 ` Li, Ming3
2023-09-12 11:17 ` [PATCH v3] " Ric Li
2023-11-27 1:31 ` Li, Ming3
2023-11-30 4:10 ` Dmitry Kozlyuk
2023-12-04 10:22 ` [PATCH v4] " Ric Li
2023-12-04 10:32 ` Ric Li
2024-10-15 23:04 ` Stephen Hemminger
2024-10-16 6:32 ` Dmitry Kozlyuk
2024-11-20 12:03 ` Thomas Monjalon
2023-09-12 12:18 ` [PATCH v2] " Dmitry Kozlyuk
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=20230912005041.0e09671c@sovereign \
--to=dmitry.kozliuk@gmail.com \
--cc=dev@dpdk.org \
--cc=ming3.li@intel.com \
--cc=roretzla@linux.microsoft.com \
/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;
as well as URLs for NNTP newsgroup(s).