All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] Xen: grant table related adjustments following recent XSAs
@ 2021-03-10 10:44 Jan Beulich
  2021-03-10 10:45 ` [PATCH 1/3] Xen/gntdev: don't needlessly allocate k{,un}map_ops[] Jan Beulich
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Jan Beulich @ 2021-03-10 10:44 UTC (permalink / raw)
  To: Juergen Gross, Boris Ostrovsky, Stefano Stabellini; +Cc: xen-devel

While addressing the XSAs a number of further oddities were noticed.
Try to take care of them.

1: gntdev: don't needlessly allocate k{,un}map_ops[]
2: gnttab: introduce common INVALID_GRANT_{HANDLE,REF}
3: gntdev: don't needlessly use kvcalloc()

Jan


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/3] Xen/gntdev: don't needlessly allocate k{,un}map_ops[]
  2021-03-10 10:44 [PATCH 0/3] Xen: grant table related adjustments following recent XSAs Jan Beulich
@ 2021-03-10 10:45 ` Jan Beulich
  2021-03-10 11:05   ` Jürgen Groß
  2021-03-10 10:45 ` [PATCH 2/3] Xen/gnttab: introduce common INVALID_GRANT_{HANDLE,REF} Jan Beulich
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Jan Beulich @ 2021-03-10 10:45 UTC (permalink / raw)
  To: Juergen Gross, Boris Ostrovsky, Stefano Stabellini; +Cc: xen-devel

They're needed only in the not-auto-translate (i.e. PV) case; there's no
point in allocating memory that's never going to get accessed.

Signed-off-by: Jan Beulich <jbeulich@suse.com>

--- a/drivers/xen/gntdev.c
+++ b/drivers/xen/gntdev.c
@@ -136,17 +136,20 @@ struct gntdev_grant_map *gntdev_alloc_ma
 	add->grants    = kvcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
 	add->map_ops   = kvcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
 	add->unmap_ops = kvcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
-	add->kmap_ops  = kvcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
-	add->kunmap_ops = kvcalloc(count,
-				   sizeof(add->kunmap_ops[0]), GFP_KERNEL);
 	add->pages     = kvcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
 	if (NULL == add->grants    ||
 	    NULL == add->map_ops   ||
 	    NULL == add->unmap_ops ||
-	    NULL == add->kmap_ops  ||
-	    NULL == add->kunmap_ops ||
 	    NULL == add->pages)
 		goto err;
+	if (use_ptemod) {
+		add->kmap_ops   = kvcalloc(count, sizeof(add->kmap_ops[0]),
+					   GFP_KERNEL);
+		add->kunmap_ops = kvcalloc(count, sizeof(add->kunmap_ops[0]),
+					   GFP_KERNEL);
+		if (NULL == add->kmap_ops || NULL == add->kunmap_ops)
+			goto err;
+	}
 
 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
 	add->dma_flags = dma_flags;
@@ -185,8 +188,10 @@ struct gntdev_grant_map *gntdev_alloc_ma
 	for (i = 0; i < count; i++) {
 		add->map_ops[i].handle = -1;
 		add->unmap_ops[i].handle = -1;
-		add->kmap_ops[i].handle = -1;
-		add->kunmap_ops[i].handle = -1;
+		if (use_ptemod) {
+			add->kmap_ops[i].handle = -1;
+			add->kunmap_ops[i].handle = -1;
+		}
 	}
 
 	add->index = 0;
@@ -332,8 +337,8 @@ int gntdev_map_grant_pages(struct gntdev
 	}
 
 	pr_debug("map %d+%d\n", map->index, map->count);
-	err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
-			map->pages, map->count);
+	err = gnttab_map_refs(map->map_ops, map->kmap_ops, map->pages,
+			map->count);
 
 	for (i = 0; i < map->count; i++) {
 		if (map->map_ops[i].status == GNTST_okay)



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/3] Xen/gnttab: introduce common INVALID_GRANT_{HANDLE,REF}
  2021-03-10 10:44 [PATCH 0/3] Xen: grant table related adjustments following recent XSAs Jan Beulich
  2021-03-10 10:45 ` [PATCH 1/3] Xen/gntdev: don't needlessly allocate k{,un}map_ops[] Jan Beulich
@ 2021-03-10 10:45 ` Jan Beulich
  2021-03-10 11:09   ` Jürgen Groß
  2021-03-10 10:46 ` [PATCH 3/3] Xen/gntdev: don't needlessly use kvcalloc() Jan Beulich
  2021-03-11 14:38 ` [PATCH 0/3] Xen: grant table related adjustments following recent XSAs Boris Ostrovsky
  3 siblings, 1 reply; 8+ messages in thread
From: Jan Beulich @ 2021-03-10 10:45 UTC (permalink / raw)
  To: Juergen Gross, Boris Ostrovsky, Stefano Stabellini; +Cc: xen-devel

It's not helpful if every driver has to cook its own. Generalize
xenbus'es INVALID_GRANT_HANDLE and pcifront's INVALID_GRANT_REF (which
shouldn't have expanded to zero to begin with). Use the constants in
p2m.c and gntdev.c right away, and update field types where necessary so
they would match with the constants' types (albeit without touching
struct ioctl_gntdev_grant_ref's ref field, as that's part of the public
interface of the kernel and would require introducing a dependency on
Xen's grant_table.h public header).

Signed-off-by: Jan Beulich <jbeulich@suse.com>

--- a/arch/arm/xen/p2m.c
+++ b/arch/arm/xen/p2m.c
@@ -11,6 +11,7 @@
 
 #include <xen/xen.h>
 #include <xen/interface/memory.h>
+#include <xen/grant_table.h>
 #include <xen/page.h>
 #include <xen/swiotlb-xen.h>
 
@@ -109,7 +110,7 @@ int set_foreign_p2m_mapping(struct gntta
 		map_ops[i].status = GNTST_general_error;
 		unmap.host_addr = map_ops[i].host_addr,
 		unmap.handle = map_ops[i].handle;
-		map_ops[i].handle = ~0;
+		map_ops[i].handle = INVALID_GRANT_HANDLE;
 		if (map_ops[i].flags & GNTMAP_device_map)
 			unmap.dev_bus_addr = map_ops[i].dev_bus_addr;
 		else
--- a/arch/x86/xen/p2m.c
+++ b/arch/x86/xen/p2m.c
@@ -741,7 +741,7 @@ int set_foreign_p2m_mapping(struct gntta
 		map_ops[i].status = GNTST_general_error;
 		unmap[0].host_addr = map_ops[i].host_addr,
 		unmap[0].handle = map_ops[i].handle;
-		map_ops[i].handle = ~0;
+		map_ops[i].handle = INVALID_GRANT_HANDLE;
 		if (map_ops[i].flags & GNTMAP_device_map)
 			unmap[0].dev_bus_addr = map_ops[i].dev_bus_addr;
 		else
@@ -751,7 +751,7 @@ int set_foreign_p2m_mapping(struct gntta
 			kmap_ops[i].status = GNTST_general_error;
 			unmap[1].host_addr = kmap_ops[i].host_addr,
 			unmap[1].handle = kmap_ops[i].handle;
-			kmap_ops[i].handle = ~0;
+			kmap_ops[i].handle = INVALID_GRANT_HANDLE;
 			if (kmap_ops[i].flags & GNTMAP_device_map)
 				unmap[1].dev_bus_addr = kmap_ops[i].dev_bus_addr;
 			else
--- a/drivers/pci/xen-pcifront.c
+++ b/drivers/pci/xen-pcifront.c
@@ -26,7 +26,7 @@
 #include <xen/platform_pci.h>
 
 #include <asm/xen/swiotlb-xen.h>
-#define INVALID_GRANT_REF (0)
+
 #define INVALID_EVTCHN    (-1)
 
 struct pci_bus_entry {
@@ -42,7 +42,7 @@ struct pcifront_device {
 	struct list_head root_buses;
 
 	int evtchn;
-	int gnt_ref;
+	grant_ref_t gnt_ref;
 
 	int irq;
 
--- a/drivers/xen/gntdev.c
+++ b/drivers/xen/gntdev.c
@@ -186,11 +186,11 @@ struct gntdev_grant_map *gntdev_alloc_ma
 		goto err;
 
 	for (i = 0; i < count; i++) {
-		add->map_ops[i].handle = -1;
-		add->unmap_ops[i].handle = -1;
+		add->map_ops[i].handle = INVALID_GRANT_HANDLE;
+		add->unmap_ops[i].handle = INVALID_GRANT_HANDLE;
 		if (use_ptemod) {
-			add->kmap_ops[i].handle = -1;
-			add->kunmap_ops[i].handle = -1;
+			add->kmap_ops[i].handle = INVALID_GRANT_HANDLE;
+			add->kunmap_ops[i].handle = INVALID_GRANT_HANDLE;
 		}
 	}
 
@@ -279,7 +279,7 @@ static int find_grant_ptes(pte_t *pte, u
 			  map->grants[pgnr].ref,
 			  map->grants[pgnr].domid);
 	gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
-			    -1 /* handle */);
+			    INVALID_GRANT_HANDLE);
 	return 0;
 }
 
@@ -297,7 +297,7 @@ int gntdev_map_grant_pages(struct gntdev
 
 	if (!use_ptemod) {
 		/* Note: it could already be mapped */
-		if (map->map_ops[0].handle != -1)
+		if (map->map_ops[0].handle != INVALID_GRANT_HANDLE)
 			return 0;
 		for (i = 0; i < map->count; i++) {
 			unsigned long addr = (unsigned long)
@@ -306,7 +306,7 @@ int gntdev_map_grant_pages(struct gntdev
 				map->grants[i].ref,
 				map->grants[i].domid);
 			gnttab_set_unmap_op(&map->unmap_ops[i], addr,
-				map->flags, -1 /* handle */);
+				map->flags, INVALID_GRANT_HANDLE);
 		}
 	} else {
 		/*
@@ -332,7 +332,7 @@ int gntdev_map_grant_pages(struct gntdev
 				map->grants[i].ref,
 				map->grants[i].domid);
 			gnttab_set_unmap_op(&map->kunmap_ops[i], address,
-				flags, -1);
+				flags, INVALID_GRANT_HANDLE);
 		}
 	}
 
@@ -390,7 +390,7 @@ static int __unmap_grant_pages(struct gn
 		pr_debug("unmap handle=%d st=%d\n",
 			map->unmap_ops[offset+i].handle,
 			map->unmap_ops[offset+i].status);
-		map->unmap_ops[offset+i].handle = -1;
+		map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
 	}
 	return err;
 }
@@ -406,13 +406,15 @@ static int unmap_grant_pages(struct gntd
 	 * already unmapped some of the grants. Only unmap valid ranges.
 	 */
 	while (pages && !err) {
-		while (pages && map->unmap_ops[offset].handle == -1) {
+		while (pages &&
+		       map->unmap_ops[offset].handle == INVALID_GRANT_HANDLE) {
 			offset++;
 			pages--;
 		}
 		range = 0;
 		while (range < pages) {
-			if (map->unmap_ops[offset+range].handle == -1)
+			if (map->unmap_ops[offset + range].handle ==
+			    INVALID_GRANT_HANDLE)
 				break;
 			range++;
 		}
--- a/include/xen/grant_table.h
+++ b/include/xen/grant_table.h
@@ -50,6 +50,13 @@
 #include <linux/page-flags.h>
 #include <linux/kernel.h>
 
+/*
+ * Technically there's no reliably invalid grant reference or grant handle,
+ * so pick the value that is the most unlikely one to be observed valid.
+ */
+#define INVALID_GRANT_REF          ((grant_ref_t)-1)
+#define INVALID_GRANT_HANDLE       ((grant_handle_t)-1)
+
 #define GNTTAB_RESERVED_XENSTORE 1
 
 /* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
--- a/include/xen/xenbus.h
+++ b/include/xen/xenbus.h
@@ -51,7 +51,6 @@
 
 #define XENBUS_MAX_RING_GRANT_ORDER 4
 #define XENBUS_MAX_RING_GRANTS      (1U << XENBUS_MAX_RING_GRANT_ORDER)
-#define INVALID_GRANT_HANDLE       (~0U)
 
 /* Register callback to watch this node. */
 struct xenbus_watch



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 3/3] Xen/gntdev: don't needlessly use kvcalloc()
  2021-03-10 10:44 [PATCH 0/3] Xen: grant table related adjustments following recent XSAs Jan Beulich
  2021-03-10 10:45 ` [PATCH 1/3] Xen/gntdev: don't needlessly allocate k{,un}map_ops[] Jan Beulich
  2021-03-10 10:45 ` [PATCH 2/3] Xen/gnttab: introduce common INVALID_GRANT_{HANDLE,REF} Jan Beulich
@ 2021-03-10 10:46 ` Jan Beulich
  2021-03-10 11:11   ` Jürgen Groß
  2021-03-11 14:38 ` [PATCH 0/3] Xen: grant table related adjustments following recent XSAs Boris Ostrovsky
  3 siblings, 1 reply; 8+ messages in thread
From: Jan Beulich @ 2021-03-10 10:46 UTC (permalink / raw)
  To: Juergen Gross, Boris Ostrovsky, Stefano Stabellini; +Cc: xen-devel

Requesting zeroed memory when all of it will be overwritten subsequently
by all ones is a waste of processing bandwidth. In fact, rather than
recording zeroed ->grants[], fill that array too with more appropriate
"invalid" indicators.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
v2: Use (now generalized) INVALID_GRANT_REF.

--- a/drivers/xen/gntdev.c
+++ b/drivers/xen/gntdev.c
@@ -133,9 +133,12 @@ struct gntdev_grant_map *gntdev_alloc_ma
 	if (NULL == add)
 		return NULL;
 
-	add->grants    = kvcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
-	add->map_ops   = kvcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
-	add->unmap_ops = kvcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
+	add->grants    = kvmalloc_array(count, sizeof(add->grants[0]),
+					GFP_KERNEL);
+	add->map_ops   = kvmalloc_array(count, sizeof(add->map_ops[0]),
+					GFP_KERNEL);
+	add->unmap_ops = kvmalloc_array(count, sizeof(add->unmap_ops[0]),
+					GFP_KERNEL);
 	add->pages     = kvcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
 	if (NULL == add->grants    ||
 	    NULL == add->map_ops   ||
@@ -143,10 +146,10 @@ struct gntdev_grant_map *gntdev_alloc_ma
 	    NULL == add->pages)
 		goto err;
 	if (use_ptemod) {
-		add->kmap_ops   = kvcalloc(count, sizeof(add->kmap_ops[0]),
-					   GFP_KERNEL);
-		add->kunmap_ops = kvcalloc(count, sizeof(add->kunmap_ops[0]),
-					   GFP_KERNEL);
+		add->kmap_ops   = kvmalloc_array(count, sizeof(add->kmap_ops[0]),
+						 GFP_KERNEL);
+		add->kunmap_ops = kvmalloc_array(count, sizeof(add->kunmap_ops[0]),
+						 GFP_KERNEL);
 		if (NULL == add->kmap_ops || NULL == add->kunmap_ops)
 			goto err;
 	}
@@ -186,6 +189,8 @@ struct gntdev_grant_map *gntdev_alloc_ma
 		goto err;
 
 	for (i = 0; i < count; i++) {
+		add->grants[i].domid = DOMID_INVALID;
+		add->grants[i].ref = INVALID_GRANT_REF;
 		add->map_ops[i].handle = INVALID_GRANT_HANDLE;
 		add->unmap_ops[i].handle = INVALID_GRANT_HANDLE;
 		if (use_ptemod) {



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] Xen/gntdev: don't needlessly allocate k{,un}map_ops[]
  2021-03-10 10:45 ` [PATCH 1/3] Xen/gntdev: don't needlessly allocate k{,un}map_ops[] Jan Beulich
@ 2021-03-10 11:05   ` Jürgen Groß
  0 siblings, 0 replies; 8+ messages in thread
From: Jürgen Groß @ 2021-03-10 11:05 UTC (permalink / raw)
  To: Jan Beulich, Boris Ostrovsky, Stefano Stabellini; +Cc: xen-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 296 bytes --]

On 10.03.21 11:45, Jan Beulich wrote:
> They're needed only in the not-auto-translate (i.e. PV) case; there's no
> point in allocating memory that's never going to get accessed.
> 
> Signed-off-by: Jan Beulich <jbeulich@suse.com>

Reviewed-by: Juergen Gross <jgross@suse.com>


Juergen

[-- Attachment #1.1.2: OpenPGP_0xB0DE9DD628BF132F.asc --]
[-- Type: application/pgp-keys, Size: 3135 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] Xen/gnttab: introduce common INVALID_GRANT_{HANDLE,REF}
  2021-03-10 10:45 ` [PATCH 2/3] Xen/gnttab: introduce common INVALID_GRANT_{HANDLE,REF} Jan Beulich
@ 2021-03-10 11:09   ` Jürgen Groß
  0 siblings, 0 replies; 8+ messages in thread
From: Jürgen Groß @ 2021-03-10 11:09 UTC (permalink / raw)
  To: Jan Beulich, Boris Ostrovsky, Stefano Stabellini; +Cc: xen-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 702 bytes --]

On 10.03.21 11:45, Jan Beulich wrote:
> It's not helpful if every driver has to cook its own. Generalize
> xenbus'es INVALID_GRANT_HANDLE and pcifront's INVALID_GRANT_REF (which
> shouldn't have expanded to zero to begin with). Use the constants in
> p2m.c and gntdev.c right away, and update field types where necessary so
> they would match with the constants' types (albeit without touching
> struct ioctl_gntdev_grant_ref's ref field, as that's part of the public
> interface of the kernel and would require introducing a dependency on
> Xen's grant_table.h public header).
> 
> Signed-off-by: Jan Beulich <jbeulich@suse.com>

Reviewed-by: Juergen Gross <jgross@suse.com>


Juergen

[-- Attachment #1.1.2: OpenPGP_0xB0DE9DD628BF132F.asc --]
[-- Type: application/pgp-keys, Size: 3135 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/3] Xen/gntdev: don't needlessly use kvcalloc()
  2021-03-10 10:46 ` [PATCH 3/3] Xen/gntdev: don't needlessly use kvcalloc() Jan Beulich
@ 2021-03-10 11:11   ` Jürgen Groß
  0 siblings, 0 replies; 8+ messages in thread
From: Jürgen Groß @ 2021-03-10 11:11 UTC (permalink / raw)
  To: Jan Beulich, Boris Ostrovsky, Stefano Stabellini; +Cc: xen-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 401 bytes --]

On 10.03.21 11:46, Jan Beulich wrote:
> Requesting zeroed memory when all of it will be overwritten subsequently
> by all ones is a waste of processing bandwidth. In fact, rather than
> recording zeroed ->grants[], fill that array too with more appropriate
> "invalid" indicators.
> 
> Signed-off-by: Jan Beulich <jbeulich@suse.com>

Reviewed-by: Juergen Gross <jgross@suse.com>


Juergen

[-- Attachment #1.1.2: OpenPGP_0xB0DE9DD628BF132F.asc --]
[-- Type: application/pgp-keys, Size: 3135 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 0/3] Xen: grant table related adjustments following recent XSAs
  2021-03-10 10:44 [PATCH 0/3] Xen: grant table related adjustments following recent XSAs Jan Beulich
                   ` (2 preceding siblings ...)
  2021-03-10 10:46 ` [PATCH 3/3] Xen/gntdev: don't needlessly use kvcalloc() Jan Beulich
@ 2021-03-11 14:38 ` Boris Ostrovsky
  3 siblings, 0 replies; 8+ messages in thread
From: Boris Ostrovsky @ 2021-03-11 14:38 UTC (permalink / raw)
  To: Jan Beulich, Juergen Gross, Stefano Stabellini; +Cc: xen-devel


On 3/10/21 5:44 AM, Jan Beulich wrote:
> While addressing the XSAs a number of further oddities were noticed.
> Try to take care of them.
>
> 1: gntdev: don't needlessly allocate k{,un}map_ops[]
> 2: gnttab: introduce common INVALID_GRANT_{HANDLE,REF}
> 3: gntdev: don't needlessly use kvcalloc()


Applied to for-linus-5.12b



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-03-11 14:38 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-10 10:44 [PATCH 0/3] Xen: grant table related adjustments following recent XSAs Jan Beulich
2021-03-10 10:45 ` [PATCH 1/3] Xen/gntdev: don't needlessly allocate k{,un}map_ops[] Jan Beulich
2021-03-10 11:05   ` Jürgen Groß
2021-03-10 10:45 ` [PATCH 2/3] Xen/gnttab: introduce common INVALID_GRANT_{HANDLE,REF} Jan Beulich
2021-03-10 11:09   ` Jürgen Groß
2021-03-10 10:46 ` [PATCH 3/3] Xen/gntdev: don't needlessly use kvcalloc() Jan Beulich
2021-03-10 11:11   ` Jürgen Groß
2021-03-11 14:38 ` [PATCH 0/3] Xen: grant table related adjustments following recent XSAs Boris Ostrovsky

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.