Skip to contentSkip to conversation

vunmap: let vunmap align virtual address by itself

PATCH In review Andrii Anisov 4 participants 15 messages Jul 18, 2019 – Jul 23, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vunmap: let vunmap align virtual address by itself

From: Andrii Anisov <[email protected]>

Let vunmap align passed virtual address by PAGE_SIZE. This also makes it consistent with how {,un}map_domain_page() currently works.

With the main change, also:
- strip all existing vunmap() calls from prior masking
- replace opencoded PAGE_MASK macro in vm_index()

---
 xen/arch/x86/mm/shadow/hvm.c | 2 +-
 xen/common/vmap.c            | 4 ++--
 xen/drivers/acpi/osl.c       | 2 +-
 xen/include/xen/vmap.h       | 4 +---
 4 files changed, 5 insertions(+), 7 deletions(-)
xen/arch/x86/mm/shadow/hvm.c +1 -1
index c6469c8..8561a11 100644
@@ -597,7 +597,7 @@ static void sh_emulate_unmap_dest(struct vcpu *v, void *addr,
     {
         paging_mark_dirty(v->domain, sh_ctxt->mfn[1]);
         put_page(mfn_to_page(sh_ctxt->mfn[1]));
-        vunmap((void *)((unsigned long)addr & PAGE_MASK));
+        vunmap(addr);
     }
     else
         unmap_domain_page(addr);
xen/common/vmap.c +2 -2
index faebc1d..e7bd6bf 100644
@@ -141,7 +141,7 @@ static void *vm_alloc(unsigned int nr, unsigned int align,
 
 static unsigned int vm_index(const void *va, enum vmap_region type)
 {
-    unsigned long addr = (unsigned long)va & ~(PAGE_SIZE - 1);
+    unsigned long addr = (unsigned long)va & PAGE_MASK;
     unsigned int idx;
     unsigned long start = (unsigned long)vm_base[type];
 
@@ -225,7 +225,7 @@ void *vmap(const mfn_t *mfn, unsigned int nr)
 
 void vunmap(const void *va)
 {
-    unsigned long addr = (unsigned long)va;
+    unsigned long addr = (unsigned long)va & PAGE_MASK;
     unsigned int pages = vm_size(va, VMAP_DEFAULT);
 
     if ( !pages )
xen/drivers/acpi/osl.c +1 -1
index 4c8bb78..1a91453 100644
@@ -115,7 +115,7 @@ void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
 	}
 
 	if (system_state >= SYS_STATE_boot)
-		vunmap((void *)((unsigned long)virt & PAGE_MASK));
+		vunmap(virt);
 }
 
 acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
xen/include/xen/vmap.h +1 -3
index 369560e..a556d13 100644
@@ -27,9 +27,7 @@ void __iomem *ioremap(paddr_t, size_t);
 
 static inline void iounmap(void __iomem *va)
 {
-    unsigned long addr = (unsigned long)(void __force *)va;
-
-    vunmap((void *)(addr & PAGE_MASK));
+    vunmap((void *)va);
 }
 
 void *arch_vmap_virt_end(void);
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Andrii Anisov

From: Andrii Anisov <[email protected]>

Let vunmap align passed virtual address by PAGE_SIZE.

You probably want to describe where this goes wrong currently on ARM here.

If you can come up with a suitable piece of text, I can fix up on commit.

Quoted 8 lines

This also makes it consistent with how {,un}map_domain_page() currently works.

With the main change, also:
- strip all existing vunmap() calls from prior masking
- replace opencoded PAGE_MASK macro in vm_index()

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw

Hello Andrew,

Quoted 1 line · Andrew Cooper

If you can come up with a suitable piece of text, I can fix up on commit.

Following text might have a better reference to the current problem:

Currently vunmap() is called from from xen/arch/arm/cpuerrata.c with an address potentially not page aligned. Instead of fixing that in ARM code, we let vunmap() making alignment by itself and stripping other existing vunmap() calls from prior masking. This makes it consistent with how {,un}map_domain_page() currently works.

With the main change, also:
- replace opencoded PAGE_MASK macro in vm_index()

Signature · 7 lines
Sincerely,
Andrii Anisov.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw
Quoted 8 lines · Andrii Anisov

Hello Andrew,

Quoted 1 line · Andrew Cooper

If you can come up with a suitable piece of text, I can fix up on commit.

Following text might have a better reference to the current problem:

Currently vunmap() is called from  from xen/arch/arm/cpuerrata.c with an

s/ from//

Quoted 1 line

address potentially not page aligned. Instead of fixing that in ARM code,

s/ARM/Arm/

Quoted 3 lines

we let vunmap() making alignment by itself and stripping other existing vunmap() calls from prior masking. This makes it consistent with how {,un}map_domain_page() currently works.

The commit message does not state what could goes wrong if the page is not aligned. So how about:

Since commit 9cc0618eb0 "xen/arm: mm: Sanity check any update of Xen page tables", the MM code requires the virtual address to be page-aligned. As the vunmap() helper is directly used the virtual address passed by its caller, this now implies the caller should pass a page-aligned virtual address.

One of the caller in xen/arch/arm/cpuerrata.c may actually pass an unaligned address resulting the vunmap() to silently fail and potentially making future user of vmap() to fail (the MM code does not allow to replace existing mapping).

In general, it would be better to have vunmap() more resilient to unaligned address. So the function is now page-aligning the virtual address.

Note that for multi-pages virtual mapping, the address should still point into the first page. Otherwise, vunmap() may only partially remove the mapping.

Quoted 3 lines

With the main change, also:
 - replace opencoded PAGE_MASK macro in vm_index()

Why did you remove the following line:
- strip all existing vunmap() calls from prior masking

Quoted 3 lines
Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw
Quoted 14 lines · Julien Grall
Quoted 1 line

Currently vunmap() is called from  from xen/arch/arm/cpuerrata.c with an

s/  from//

Quoted 1 line

address potentially not page aligned. Instead of fixing that in ARM code,

s/ARM/Arm/

Quoted 3 lines

we let vunmap() making alignment by itself and stripping other existing vunmap() calls from prior masking. This makes it consistent with how {,un}map_domain_page() currently works.

The commit message does not state what could goes wrong if the page is not aligned. So how about:

Quoted 7 lines

Since commit 9cc0618eb0 "xen/arm: mm: Sanity check any update of Xen page tables", the MM code requires the virtual address to be page-aligned. As the vunmap() helper is directly used the virtual address passed by its caller, this now implies the caller should pass a page-aligned virtual address.

One of the caller in xen/arch/arm/cpuerrata.c may actually pass an unaligned address resulting the vunmap() to silently fail and potentially making future user of vmap() to fail (the MM code does not allow to replace existing mapping).

In general, it would be better to have vunmap() more resilient to unaligned address. So the function is now page-aligning the virtual address.

Note that for multi-pages virtual mapping, the address should still point into the first page. Otherwise, vunmap() may only partially remove the mapping.

I'm ok with that.

Quoted 2 lines

Why did you remove the following line:
 - strip all existing vunmap() calls from prior masking

Because I already mentioned it in the main body of my message.

Quoted 3 lines

address potentially not page aligned. Instead of fixing that in ARM code, we let vunmap() making alignment by itself and stripping other existing vunmap() calls from prior masking.

Yet, with your text both notes are needed.

Signature · 7 lines
Sincerely,
Andrii Anisov.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw
In reply to Andrii Anisov
Quoted 1 line · Andrii Anisov

Let vunmap align passed virtual address by PAGE_SIZE.

Despite seeing Andrew's R-b you've already got I'd like to point out that this increases the risk of some code passing the wrong pointer into here. {,un}map_domain_page() act on single pages only, so there's no ambiguity. When talking about multi-page regions though, not undoing arithmetic previously done may mean actually pointing at other than the first page of the mapping.

Quoted 5 lines

This also makes it consistent with how {,un}map_domain_page() currently works.

With the main change, also:
- strip all existing vunmap() calls from prior masking

_If_ we are to go this route, then unmap_domain_page_global() callers should also be adjusted. There, as for unmap_domain_page(), I agree it would make sense to be more tolerant.

Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 9 lines · Jan Beulich
Quoted 1 line · Andrii Anisov

Let vunmap align passed virtual address by PAGE_SIZE.

Despite seeing Andrew's R-b you've already got I'd like to point out that this increases the risk of some code passing the wrong pointer into here. {,un}map_domain_page() act on single pages only, so there's no ambiguity. When talking about multi-page regions though, not undoing arithmetic previously done may mean actually pointing at other than the first page of the mapping.

Well, what we are moving into vunmap(), is a page alignment only. It can't save us from the wrong pointer, even if it is done outside the vunmap().

Quoted 6 lines
Quoted 2 lines

With the main change, also:
- strip all existing vunmap() calls from prior masking

_If_ we are to go this route, then unmap_domain_page_global() callers should also be adjusted. There, as for unmap_domain_page(), I agree it would make sense to be more tolerant.

I've found two page alignments prior to unmap_domain_page_global() call. Should I wipe them in this patch, or in separate?

Signature · 7 lines
Sincerely,
Andrii Anisov.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw
Quoted 15 lines · Andrii Anisov
Quoted 9 lines · Jan Beulich
Quoted 1 line · Andrii Anisov

Let vunmap align passed virtual address by PAGE_SIZE.

Despite seeing Andrew's R-b you've already got I'd like to point out that this increases the risk of some code passing the wrong pointer into here. {,un}map_domain_page() act on single pages only, so there's no ambiguity. When talking about multi-page regions though, not undoing arithmetic previously done may mean actually pointing at other than the first page of the mapping.

Well, what we are moving into vunmap(), is a page alignment only. It can't save us from the wrong pointer, even if it is done outside the vunmap().

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

With the main change, also:
   - strip all existing vunmap() calls from prior masking

_If_ we are to go this route, then unmap_domain_page_global() callers should also be adjusted. There, as for unmap_domain_page(), I agree it would make sense to be more tolerant.

I've found two page alignments prior to unmap_domain_page_global() call. Should I wipe them in this patch, or in separate?

If we're to go the suggested route then it might not matter much. As I'm not entirely certain yet that we will, making this a prereq one dealing with the alignment in unmap_domain_page_global() might be better. Your existing patch would then further shift this into vunmap().

Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Andrii Anisov
Quoted 9 lines · Jan Beulich
Quoted 1 line · Andrii Anisov

Let vunmap align passed virtual address by PAGE_SIZE.

Despite seeing Andrew's R-b you've already got I'd like to point out that this increases the risk of some code passing the wrong pointer into here. {,un}map_domain_page() act on single pages only, so there's no ambiguity. When talking about multi-page regions though, not undoing arithmetic previously done may mean actually pointing at other than the first page of the mapping.

Well, what we are moving into vunmap(), is a page alignment only. It can't save us from the wrong pointer, even if it is done outside the vunmap().

Quoted 6 lines
Quoted 2 lines

With the main change, also:
   - strip all existing vunmap() calls from prior masking

_If_ we are to go this route, then unmap_domain_page_global() callers should also be adjusted. There, as for unmap_domain_page(), I agree it would make sense to be more tolerant.

Quoted 7 lines
Quoted 1 line

I've found two page alignments prior to unmap_domain_page_global() call. Should I wipe them in this patch, or in separate?

If we're to go the suggested route then it might not matter much. As I'm not entirely certain yet that we will, making this a prereq one dealing with the alignment in unmap_domain_page_global() might be better. Your existing patch would then further shift this into vunmap().

I think allowing vunmap to deal with unaligned address could simplify some of the callers. Passing the wrong page-aligned pointer is less critical than passing an unaligned address.

The latter may result in misbehaving code. The vmap will end-up to not be sync with the page-table as we assume page-table update cannot fail (this probably should be changed). On Arm, this will result to any new vmap function to likely fail (we don't allow replace an existing valid page-table entry).

IIUC the code, the former will result to only unmapping some part of the vmap. While I agree this is not ideal, the vmap will still stay in-sync with the page-table.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#9 lore raw
Quoted 32 lines · Julien Grall
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Andrii Anisov
Quoted 9 lines · Jan Beulich
Quoted 1 line · Andrii Anisov

Let vunmap align passed virtual address by PAGE_SIZE.

Despite seeing Andrew's R-b you've already got I'd like to point out that this increases the risk of some code passing the wrong pointer into here. {,un}map_domain_page() act on single pages only, so there's no ambiguity. When talking about multi-page regions though, not undoing arithmetic previously done may mean actually pointing at other than the first page of the mapping.

Well, what we are moving into vunmap(), is a page alignment only. It can't save us from the wrong pointer, even if it is done outside the vunmap().

Quoted 6 lines
Quoted 2 lines

With the main change, also:
    - strip all existing vunmap() calls from prior masking

_If_ we are to go this route, then unmap_domain_page_global() callers should also be adjusted. There, as for unmap_domain_page(), I agree it would make sense to be more tolerant.

Quoted 7 lines
Quoted 1 line

I've found two page alignments prior to unmap_domain_page_global() call. Should I wipe them in this patch, or in separate?

If we're to go the suggested route then it might not matter much. As I'm not entirely certain yet that we will, making this a prereq one dealing with the alignment in unmap_domain_page_global() might be better. Your existing patch would then further shift this into vunmap().

Quoted 3 lines

I think allowing vunmap to deal with unaligned address could simplify some of the callers. Passing the wrong page-aligned pointer is less critical than passing an unaligned address.

The latter may result in misbehaving code.

Why only the latter?

Quoted 7 lines

The vmap will end-up to not be sync with the page-table as we assume page-table update cannot fail (this probably should be changed). On Arm, this will result to any new vmap function to likely fail (we don't allow replace an existing valid page-table entry).

IIUC the code, the former will result to only unmapping some part of the vmap.

AIUI the unmap request will simply fail: vm_index() and hence vm_size() will simply return 0. Hence, with vunmap() not itself returning any error, there'll be a silent leak of mappings.

Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

Hi,

Quoted 44 lines · Jan Beulich
Quoted 32 lines · Julien Grall
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Andrii Anisov
Quoted 9 lines · Jan Beulich
Quoted 1 line · Andrii Anisov

Let vunmap align passed virtual address by PAGE_SIZE.

Despite seeing Andrew's R-b you've already got I'd like to point out that this increases the risk of some code passing the wrong pointer into here. {,un}map_domain_page() act on single pages only, so there's no ambiguity. When talking about multi-page regions though, not undoing arithmetic previously done may mean actually pointing at other than the first page of the mapping.

Well, what we are moving into vunmap(), is a page alignment only. It can't save us from the wrong pointer, even if it is done outside the vunmap().

Quoted 6 lines
Quoted 2 lines

With the main change, also:
    - strip all existing vunmap() calls from prior masking

_If_ we are to go this route, then unmap_domain_page_global() callers should also be adjusted. There, as for unmap_domain_page(), I agree it would make sense to be more tolerant.

Quoted 7 lines
Quoted 1 line

I've found two page alignments prior to unmap_domain_page_global() call. Should I wipe them in this patch, or in separate?

If we're to go the suggested route then it might not matter much. As I'm not entirely certain yet that we will, making this a prereq one dealing with the alignment in unmap_domain_page_global() might be better. Your existing patch would then further shift this into vunmap().

Quoted 3 lines

I think allowing vunmap to deal with unaligned address could simplify some of the callers. Passing the wrong page-aligned pointer is less critical than passing an unaligned address.

The latter may result in misbehaving code.

Why only the latter?

Quoted 5 lines

The vmap will end-up to not be sync with the page-table as we assume page-table update cannot fail (this probably should be changed). On Arm, this will result to any new vmap function to likely fail (we don't allow replace an existing valid page-table entry).

Quoted 6 lines
Quoted 2 lines

IIUC the code, the former will result to only unmapping some part of the vmap.

AIUI the unmap request will simply fail: vm_index() and hence vm_size() will simply return 0. Hence, with vunmap() not itself returning any error, there'll be a silent leak of mappings.

Hmmm, I misread the read. vm_index() will indeed return 0 in that case. But, this will not silently leak the mappings thanks to the WARN_ON() in the code.

However, even if there are a leak, the vmap and page-table will stay in sync. So this is not as bad as the unaligned case.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#11 lore raw

Julien, Jan, Andrew,

The problem addressed by [1] causes random ARM64 boot fails dependent on hypervisor code changes. Yet more generic solution was requested by Andrew and supported by Julien [2].

How to proceed with this particular patch?
As I understand, Jan doubts we should move page alignment to vunmap(), while Julien and Andrew wanted the commit message clarification. Can we have an agreement on approach here?

[1] https://lists.xenproject.org/archives/html/xen-devel/2019-07/msg01167.html
[2] https://lists.xenproject.org/archives/html/xen-devel/2019-07/msg01129.html

Signature · 7 lines
Sincerely,
Andrii Anisov.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#12 lore raw
Quoted 11 lines · Andrii Anisov

Julien, Jan, Andrew,

The problem addressed by [1] causes random ARM64 boot fails dependent on hypervisor code changes. Yet more generic solution was requested by Andrew and supported by Julien [2].

How to proceed with this particular patch?
As I understand, Jan doubts we should move page alignment to vunmap(), while Julien and Andrew wanted the commit message clarification. Can we have an agreement on approach here?

[1] https://lists.xenproject.org/archives/html/xen-devel/2019-07/msg01167.html
[2] https://lists.xenproject.org/archives/html/xen-devel/2019-07/msg01129.html

First of all, let me quote Linux'es code:

static void __vunmap(const void *addr, int deallocate_pages)

{
	struct vm_struct *area;

	if (!addr)
		return;

	if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
			addr))
		return;

As long as we aim to have a reasonable level of compatibility of similar interfaces, we should not go the suggested route.

Beyond that I continue to be of the opinion that it should be
all-or-nothing: Any pointer pointing anywhere at or inside the region should be accepted, or just the one pointing precisely at the start.

Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#13 lore raw

Hello Jan,

Quoted 4 lines · Jan Beulich

Beyond that I continue to be of the opinion that it should be
all-or-nothing: Any pointer pointing anywhere at or inside the region should be accepted, or just the one pointing precisely at the start.

It's reasonable enough and I agree with that.
Sorry for missing this point.

Signature · 7 lines
Sincerely,
Andrii Anisov.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#14 lore raw
In reply to Jan Beulich

Hi Jan,

Quoted 26 lines · Jan Beulich
Quoted 11 lines · Andrii Anisov

Julien, Jan, Andrew,

The problem addressed by [1] causes random ARM64 boot fails dependent on hypervisor code changes. Yet more generic solution was requested by Andrew and supported by Julien [2].

How to proceed with this particular patch?
As I understand, Jan doubts we should move page alignment to vunmap(), while Julien and Andrew wanted the commit message clarification. Can we have an agreement on approach here?

[1] https://lists.xenproject.org/archives/html/xen-devel/2019-07/msg01167.html
[2] https://lists.xenproject.org/archives/html/xen-devel/2019-07/msg01129.html

First of all, let me quote Linux'es code:

static void __vunmap(const void *addr, int deallocate_pages)

{
	struct vm_struct *area;

	if (!addr)
		return;

	if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
			addr))
		return;
Quoted 2 lines

As long as we aim to have a reasonable level of compatibility of similar interfaces, we should not go the suggested route.

Well, it is more likely to have Linux code moving to Xen compare to the opposite. So the change suggested is still compatible as we don't restrict anything but just open more flexibility.

Quoted 5 lines

Beyond that I continue to be of the opinion that it should be
all-or-nothing: Any pointer pointing anywhere at or inside the region should be accepted, or just the one pointing precisely at the start.

That's fine, but we can still achieve this step by step... Handling unaligned address is quite easy.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel