Skip to contentSkip to conversation

xen/xenbus: Fix granting of vmalloc'd memory

PATCH In review Simon Leiner 5 participants 17 messages Aug 25, 2020 – Aug 27, 2020 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 xen/xenbus: Fix granting of vmalloc'd memory

On some architectures (like ARM), virt_to_gfn cannot be used for vmalloc'd memory because of its reliance on virt_to_phys. This patch introduces a check for vmalloc'd addresses and obtains the PFN using vmalloc_to_pfn in that case.

---
 drivers/xen/xenbus/xenbus_client.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/xen/xenbus/xenbus_client.c +8 -2
index 786fbb7d8be0..907bcbb93afb 100644
@@ -379,8 +379,14 @@ int xenbus_grant_ring(struct xenbus_device *dev, void *vaddr,
 	int i, j;
 
 	for (i = 0; i < nr_pages; i++) {
-		err = gnttab_grant_foreign_access(dev->otherend_id,
-						  virt_to_gfn(vaddr), 0);
+		unsigned long gfn;
+
+		if (is_vmalloc_addr(vaddr))
+			gfn = pfn_to_gfn(vmalloc_to_pfn(vaddr));
+		else
+			gfn = virt_to_gfn(vaddr);
+
+		err = gnttab_grant_foreign_access(dev->otherend_id, gfn, 0);
 		if (err < 0) {
 			xenbus_dev_fatal(dev, err,
 					 "granting access to ring page");
Signature · 1 line
2.24.3 (Apple Git-128)
#1 lore raw

PATCH 2/2 arm/xen: Add misuse warning to virt_to_gfn

As virt_to_gfn uses virt_to_phys, it will return invalid addresses when used with vmalloc'd addresses. This patch introduces a warning, when virt_to_gfn is used in this way.

---
 include/xen/arm/page.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
include/xen/arm/page.h +5 -1
index d7f6af50e200..b0d303b633d0 100644
@@ -76,7 +76,11 @@ static inline unsigned long bfn_to_pfn(unsigned long bfn)
 #define bfn_to_local_pfn(bfn)	bfn_to_pfn(bfn)
 
 /* VIRT <-> GUEST conversion */
-#define virt_to_gfn(v)		(pfn_to_gfn(virt_to_phys(v) >> XEN_PAGE_SHIFT))
+#define virt_to_gfn(v)                                                         \
+	({                                                                     \
+		WARN_ON_ONCE(is_vmalloc_addr(v));                              \
+		pfn_to_gfn(virt_to_phys(v) >> XEN_PAGE_SHIFT);                 \
+	})
 #define gfn_to_virt(m)		(__va(gfn_to_pfn(m) << XEN_PAGE_SHIFT))
 
 /* Only used in PV code. But ARM guests are always HVM. */
Signature · 1 line
2.24.3 (Apple Git-128)
#2 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

include/xen/arm/page.h static inline unsigned long bfn_to_pfn(unsigned long bfn) · line 83

Quoted 12 lines · Simon Leiner
include/xen/arm/page.h +5 -1
@@ -76,7 +76,11 @@ static inline unsigned long bfn_to_pfn(unsigned long bfn)
 #define bfn_to_local_pfn(bfn)	bfn_to_pfn(bfn)
 
 /* VIRT <-> GUEST conversion */
-#define virt_to_gfn(v)		(pfn_to_gfn(virt_to_phys(v) >> XEN_PAGE_SHIFT))
+#define virt_to_gfn(v)                                                         \
+	({                                                                     \
+		WARN_ON_ONCE(is_vmalloc_addr(v));                              \
+		pfn_to_gfn(virt_to_phys(v) >> XEN_PAGE_SHIFT);                 \
+	})

Shouldn't such a check cover more than just the vmalloc range, i.e. everything outside of what __va() can validly return?

Jan

#3 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

Quoted 6 lines · Jan Beulich
Quoted 5 lines · Simon Leiner
include/xen/arm/page.h +0 -0
@@ -76,7 +76,11 @@ static inline unsigned long bfn_to_pfn(unsigned long bfn)
 #define bfn_to_local_pfn(bfn)	bfn_to_pfn(bfn)
 
Quoted 10 lines

include/xen/arm/page.h static inline unsigned long bfn_to_pfn(unsigned long bfn) · line 83

Quoted 7 lines
include/xen/arm/page.h +5 -1
 /* VIRT <-> GUEST conversion */
-#define virt_to_gfn(v)		(pfn_to_gfn(virt_to_phys(v) >> XEN_PAGE_SHIFT))
+#define virt_to_gfn(v)                                                         \
+	({                                                                     \
+		WARN_ON_ONCE(is_vmalloc_addr(v));                              \
+		pfn_to_gfn(virt_to_phys(v) >> XEN_PAGE_SHIFT);                 \
+	})

Shouldn't such a check cover more than just the vmalloc range, i.e. everything outside of what __va() can validly return?

Keep in mind that this patch is meant as sister patch to
https://marc.info/?l=xen-devel&m=159834800203817
so it makes sense to check for vmalloc addresses specifically.

That said, I am not aware of a good way to implement the __va test you are suggesting.

#4 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

Quoted 18 lines · Stefano Stabellini
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Simon Leiner
include/xen/arm/page.h +0 -0
@@ -76,7 +76,11 @@ static inline unsigned long bfn_to_pfn(unsigned long bfn)
 #define bfn_to_local_pfn(bfn)	bfn_to_pfn(bfn)
 
Quoted 10 lines

include/xen/arm/page.h static inline unsigned long bfn_to_pfn(unsigned long bfn) · line 83

Quoted 7 lines
include/xen/arm/page.h +5 -1
 /* VIRT <-> GUEST conversion */
-#define virt_to_gfn(v)		(pfn_to_gfn(virt_to_phys(v) >> XEN_PAGE_SHIFT))
+#define virt_to_gfn(v)                                                         \
+	({                                                                     \
+		WARN_ON_ONCE(is_vmalloc_addr(v));                              \
+		pfn_to_gfn(virt_to_phys(v) >> XEN_PAGE_SHIFT);                 \
+	})

Shouldn't such a check cover more than just the vmalloc range, i.e. everything outside of what __va() can validly return?

Quoted 3 lines

Keep in mind that this patch is meant as sister patch to
https://marc.info/?l=xen-devel&m=159834800203817
so it makes sense to check for vmalloc addresses specifically.

I had seen that patch before writing the reply, and I had assumed precisely what you say. It was for this reason that I did point out the limitation: While there's a specific issue for the vmalloc range, any other addresses outside the direct mapping area are as problematic (aiui).

Quoted 2 lines

That said, I am not aware of a good way to implement the __va test you are suggesting.

Hmm, to me __phys_to_virt() and __virt_to_phys_nodebug() give the impression that they're just linear transformations of the address, which would seem to suggest there's a pre-determined address range used for the direct map.

Jan

#5 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

Quoted 4 lines · Jan Beulich

Hmm, to me __phys_to_virt() and __virt_to_phys_nodebug() give the impression that they're just linear transformations of the address, which would seem to suggest there's a pre-determined address range used for the direct map.

From eyeballing the kernel code, it seems like __is_lm_address() is suitable for such a check, especially since it is also used in __virt_to_phys() (the one in arch/arm64/mm/physaddr.c).

Greetings,
Simon

maintainer
#6 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

Quoted 9 lines · Simon Leiner
Quoted 4 lines · Jan Beulich

Hmm, to me __phys_to_virt() and __virt_to_phys_nodebug() give the impression that they're just linear transformations of the address, which would seem to suggest there's a pre-determined address range used for the direct map.

From eyeballing the kernel code, it seems like __is_lm_address() is
suitable for such a check, especially since it is also used in __virt_to_phys() (the one in arch/arm64/mm/physaddr.c).

This seems to be an Arm specific function.

virt_addr_valid() seems to be a good fit.

Juergen

#7 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

Quoted 1 line · Jürgen Groß

This seems to be an Arm specific function.

Is that a problem? The caller site is also ARM specific.

Quoted 1 line

virt_addr_valid() seems to be a good fit.

If you prefer that anyway, I will change it and resubmit that part of the patch.

Simon

maintainer
#8 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

Quoted 4 lines · Simon Leiner
Quoted 1 line · Jürgen Groß

This seems to be an Arm specific function.

Is that a problem? The caller site is also ARM specific.

The caller site is ARM specific, but __is_lm_address() is defined for arm64 only.

Quoted 5 lines
Quoted 2 lines

virt_addr_valid() seems to be a good fit.

If you prefer that anyway, I will change it and resubmit that part of the patch.

No need for resend, I can fix that when committing.

Juergen

#9 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

Quoted 8 lines · Jürgen Groß
Quoted 4 lines · Simon Leiner
Quoted 1 line · Jürgen Groß

This seems to be an Arm specific function.

Is that a problem? The caller site is also ARM specific.

The caller site is ARM specific, but __is_lm_address() is defined for arm64 only.

Oh, I did not catch that - you're right, thanks :-)

Quoted 1 line

No need for resend, I can fix that when committing.

Thanks!

Simon

maintainer Reviewed-by on v1 2/2
#10 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

In reply to Simon Leiner
Quoted 5 lines · Simon Leiner

As virt_to_gfn uses virt_to_phys, it will return invalid addresses when used with vmalloc'd addresses. This patch introduces a warning, when virt_to_gfn is used in this way.

Quoted 24 lines
---
 include/xen/arm/page.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
include/xen/arm/page.h +5 -1
index d7f6af50e200..b0d303b633d0 100644
@@ -76,7 +76,11 @@ static inline unsigned long bfn_to_pfn(unsigned long bfn)
 #define bfn_to_local_pfn(bfn)	bfn_to_pfn(bfn)
 
 /* VIRT <-> GUEST conversion */
-#define virt_to_gfn(v)		(pfn_to_gfn(virt_to_phys(v) >> XEN_PAGE_SHIFT))
+#define virt_to_gfn(v)                                                         \
+	({                                                                     \
+		WARN_ON_ONCE(is_vmalloc_addr(v));                              \
+		pfn_to_gfn(virt_to_phys(v) >> XEN_PAGE_SHIFT);                 \
+	})
 #define gfn_to_virt(m)		(__va(gfn_to_pfn(m) << XEN_PAGE_SHIFT))
 
 /* Only used in PV code. But ARM guests are always HVM. */
Signature · 1 line
2.24.3 (Apple Git-128)
#11 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

In reply to Simon Leiner

Hi Simon,

Quoted 14 lines · Simon Leiner

As virt_to_gfn uses virt_to_phys, it will return invalid addresses when used with vmalloc'd addresses. This patch introduces a warning, when virt_to_gfn is used in this way.

---
  include/xen/arm/page.h | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
include/xen/arm/page.h +0 -0
index d7f6af50e200..b0d303b633d0 100644

include/xen/arm/page.h static inline unsigned long bfn_to_pfn(unsigned long bfn) · line 81

Quoted 7 lines
include/xen/arm/page.h +3 -1
@@ -76,7 +76,11 @@ static inline unsigned long bfn_to_pfn(unsigned long bfn)
  #define bfn_to_local_pfn(bfn)	bfn_to_pfn(bfn)
 
  /* VIRT <-> GUEST conversion */
-#define virt_to_gfn(v)		(pfn_to_gfn(virt_to_phys(v) >> XEN_PAGE_SHIFT))
+#define virt_to_gfn(v)                                                         \
+	({                                                                     \
+		WARN_ON_ONCE(is_vmalloc_addr(v));                              \

virt_to_gfn() will usually be called from generic code. WARN_ON_ONCE() will only catch one instance and it means we would have to fix the first instance and then re-run to catch the others.

So I think we want to switch to WARN_ON() here.

Cheers,

Signature · 1 line
Julien Grall
maintainer
#12 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

Quoted 21 lines · Julien Grall

Hi Simon,

Quoted 18 lines · Simon Leiner

As virt_to_gfn uses virt_to_phys, it will return invalid addresses when used with vmalloc'd addresses. This patch introduces a warning, when virt_to_gfn is used in this way.

---
  include/xen/arm/page.h | 6 +++++-

  1 file changed, 5 insertions(+), 1 deletion(-)
include/xen/arm/page.h +0 -1
index d7f6af50e200..b0d303b633d0 100644
@@ -76,7 +76,11 @@ static inline unsigned long bfn_to_pfn(unsigned 
 long bfn)
   #define bfn_to_local_pfn(bfn)    bfn_to_pfn(bfn)
   /* VIRT <-> GUEST conversion */
-#define virt_to_gfn(v)        (pfn_to_gfn(virt_to_phys(v) >> 
Quoted 8 lines

include/xen/arm/page.h static inline unsigned long bfn_to_pfn(unsigned · line 84

Quoted 6 lines
include/xen/arm/page.h +3 -0
 XEN_PAGE_SHIFT))
+#define 
 virt_to_gfn(v)                                                         \
+    
 ({                                                                     \
+        WARN_ON_ONCE(is_vmalloc_addr(v));                              \

virt_to_gfn() will usually be called from generic code. WARN_ON_ONCE()

"Usually" is a bit gross here. The only generic call site I could find is xenbus_grant_ring(). All other instances (I counted 22) are not generic at all.

Quoted 3 lines

will only catch one instance and it means we would have to fix the first instance and then re-run to catch the others.

So I think we want to switch to WARN_ON() here.

No, please don't. In case there would be a frequent path the result would be a basically unusable system due to massive console clobbering.

Juergen

#13 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

Quoted 11 lines · Jürgen Groß

On 26.08.20 20:37, Julien Grall wrote:
"Usually" is a bit gross here. The only generic call site I could find is xenbus_grant_ring(). All other instances (I counted 22) are not generic at all.

Quoted 3 lines

will only catch one instance and it means we would have to fix the first instance and then re-run to catch the others.

So I think we want to switch to WARN_ON() here.

No, please don't. In case there would be a frequent path the result would be a basically unusable system due to massive console clobbering.

Right, but if that's really happenning then you have a much bigger problem on your platform because the address returned will be invalid.

So I still don't see the advantage of WARN_ON_ONCE() here.

Cheers,

Quoted 3 lines

Juergen

Signature · 1 line
Julien Grall
maintainer
#14 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

Quoted 10 lines · Julien Grall
Quoted 7 lines · Jürgen Groß

On 26.08.20 20:37, Julien Grall wrote:
"Usually" is a bit gross here. The only generic call site I could find is xenbus_grant_ring(). All other instances (I counted 22) are not generic at all.

Quoted 2 lines

will only catch one instance and it means we would have to fix the first instance and then re-run to catch the others.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

So I think we want to switch to WARN_ON() here.

No, please don't. In case there would be a frequent path the result would be a basically unusable system due to massive console clobbering.

Right, but if that's really happenning then you have a much bigger problem on your platform because the address returned will be invalid.

So I still don't see the advantage of WARN_ON_ONCE() here.

Depends of the (potential) source of the warnings. I think we can agree that e.g. a problem in the pv network stack is rather improbable, as it would have been detected long ago.

If, however, the problem is being introduced by one of the rather new pv-drivers (like sound, pvcalls, 9pfs) it is perfectly fine to assume the overall system is still functional even without those drivers working correctly. Having a message storm from those sources is still quite undesirable IMO and doesn't really help.

Juergen

#15 lore raw

Re: [PATCH 2/2] arm/xen: Add misuse warning to virt_to_gfn

Quoted 26 lines · Jürgen Groß
Quoted 13 lines · Julien Grall
Quoted 10 lines · Jürgen Groß

On 26.08.20 20:37, Julien Grall wrote:
"Usually" is a bit gross here. The only generic call site I could find is xenbus_grant_ring(). All other instances (I counted 22) are not generic at all.

Quoted 4 lines

will only catch one instance and it means we would have to fix the first instance and then re-run to catch the others.

So I think we want to switch to WARN_ON() here.

Quoted 7 lines
Quoted 2 lines

No, please don't. In case there would be a frequent path the result would be a basically unusable system due to massive console clobbering.

Right, but if that's really happenning then you have a much bigger problem on your platform because the address returned will be invalid.

So I still don't see the advantage of WARN_ON_ONCE() here.

Depends of the (potential) source of the warnings. I think we can agree that e.g. a problem in the pv network stack is rather improbable, as it would have been detected long ago.

Quoted 5 lines

If, however, the problem is being introduced by one of the rather new pv-drivers (like sound, pvcalls, 9pfs) it is perfectly fine to assume the overall system is still functional even without those drivers working correctly. Having a message storm from those sources is still quite undesirable IMO and doesn't really help.

I agree with this.

maintainer Reviewed-by on v1 1/2
#16 lore raw
In reply to Simon Leiner
Quoted 6 lines · Simon Leiner

On some architectures (like ARM), virt_to_gfn cannot be used for vmalloc'd memory because of its reliance on virt_to_phys. This patch introduces a check for vmalloc'd addresses and obtains the PFN using vmalloc_to_pfn in that case.

Quoted 28 lines
---
 drivers/xen/xenbus/xenbus_client.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/xen/xenbus/xenbus_client.c +8 -2
index 786fbb7d8be0..907bcbb93afb 100644
@@ -379,8 +379,14 @@ int xenbus_grant_ring(struct xenbus_device *dev, void *vaddr,
 	int i, j;
 
 	for (i = 0; i < nr_pages; i++) {
-		err = gnttab_grant_foreign_access(dev->otherend_id,
-						  virt_to_gfn(vaddr), 0);
+		unsigned long gfn;
+
+		if (is_vmalloc_addr(vaddr))
+			gfn = pfn_to_gfn(vmalloc_to_pfn(vaddr));
+		else
+			gfn = virt_to_gfn(vaddr);
+
+		err = gnttab_grant_foreign_access(dev->otherend_id, gfn, 0);
 		if (err < 0) {
 			xenbus_dev_fatal(dev, err,
 					 "granting access to ring page");
Signature · 1 line
2.24.3 (Apple Git-128)