Xen/gnttab: introduce common INVALID_GRANT_{HANDLE,REF}

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>
Link: https://lore.kernel.org/r/db7c38a5-0d75-d5d1-19de-e5fe9f0b9c48@suse.com
Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>

authored by Jan Beulich and committed by Boris Ostrovsky bce21a2b 36caa3fe

+26 -17
+2 -1
arch/arm/xen/p2m.c
··· 11 12 #include <xen/xen.h> 13 #include <xen/interface/memory.h> 14 #include <xen/page.h> 15 #include <xen/swiotlb-xen.h> 16 ··· 110 map_ops[i].status = GNTST_general_error; 111 unmap.host_addr = map_ops[i].host_addr, 112 unmap.handle = map_ops[i].handle; 113 - map_ops[i].handle = ~0; 114 if (map_ops[i].flags & GNTMAP_device_map) 115 unmap.dev_bus_addr = map_ops[i].dev_bus_addr; 116 else
··· 11 12 #include <xen/xen.h> 13 #include <xen/interface/memory.h> 14 + #include <xen/grant_table.h> 15 #include <xen/page.h> 16 #include <xen/swiotlb-xen.h> 17 ··· 109 map_ops[i].status = GNTST_general_error; 110 unmap.host_addr = map_ops[i].host_addr, 111 unmap.handle = map_ops[i].handle; 112 + map_ops[i].handle = INVALID_GRANT_HANDLE; 113 if (map_ops[i].flags & GNTMAP_device_map) 114 unmap.dev_bus_addr = map_ops[i].dev_bus_addr; 115 else
+2 -2
arch/x86/xen/p2m.c
··· 741 map_ops[i].status = GNTST_general_error; 742 unmap[0].host_addr = map_ops[i].host_addr, 743 unmap[0].handle = map_ops[i].handle; 744 - map_ops[i].handle = ~0; 745 if (map_ops[i].flags & GNTMAP_device_map) 746 unmap[0].dev_bus_addr = map_ops[i].dev_bus_addr; 747 else ··· 751 kmap_ops[i].status = GNTST_general_error; 752 unmap[1].host_addr = kmap_ops[i].host_addr, 753 unmap[1].handle = kmap_ops[i].handle; 754 - kmap_ops[i].handle = ~0; 755 if (kmap_ops[i].flags & GNTMAP_device_map) 756 unmap[1].dev_bus_addr = kmap_ops[i].dev_bus_addr; 757 else
··· 741 map_ops[i].status = GNTST_general_error; 742 unmap[0].host_addr = map_ops[i].host_addr, 743 unmap[0].handle = map_ops[i].handle; 744 + map_ops[i].handle = INVALID_GRANT_HANDLE; 745 if (map_ops[i].flags & GNTMAP_device_map) 746 unmap[0].dev_bus_addr = map_ops[i].dev_bus_addr; 747 else ··· 751 kmap_ops[i].status = GNTST_general_error; 752 unmap[1].host_addr = kmap_ops[i].host_addr, 753 unmap[1].handle = kmap_ops[i].handle; 754 + kmap_ops[i].handle = INVALID_GRANT_HANDLE; 755 if (kmap_ops[i].flags & GNTMAP_device_map) 756 unmap[1].dev_bus_addr = kmap_ops[i].dev_bus_addr; 757 else
+2 -2
drivers/pci/xen-pcifront.c
··· 26 #include <xen/platform_pci.h> 27 28 #include <asm/xen/swiotlb-xen.h> 29 - #define INVALID_GRANT_REF (0) 30 #define INVALID_EVTCHN (-1) 31 32 struct pci_bus_entry { ··· 42 struct list_head root_buses; 43 44 int evtchn; 45 - int gnt_ref; 46 47 int irq; 48
··· 26 #include <xen/platform_pci.h> 27 28 #include <asm/xen/swiotlb-xen.h> 29 + 30 #define INVALID_EVTCHN (-1) 31 32 struct pci_bus_entry { ··· 42 struct list_head root_buses; 43 44 int evtchn; 45 + grant_ref_t gnt_ref; 46 47 int irq; 48
+13 -11
drivers/xen/gntdev.c
··· 186 goto err; 187 188 for (i = 0; i < count; i++) { 189 - add->map_ops[i].handle = -1; 190 - add->unmap_ops[i].handle = -1; 191 if (use_ptemod) { 192 - add->kmap_ops[i].handle = -1; 193 - add->kunmap_ops[i].handle = -1; 194 } 195 } 196 ··· 279 map->grants[pgnr].ref, 280 map->grants[pgnr].domid); 281 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, 282 - -1 /* handle */); 283 return 0; 284 } 285 ··· 297 298 if (!use_ptemod) { 299 /* Note: it could already be mapped */ 300 - if (map->map_ops[0].handle != -1) 301 return 0; 302 for (i = 0; i < map->count; i++) { 303 unsigned long addr = (unsigned long) ··· 306 map->grants[i].ref, 307 map->grants[i].domid); 308 gnttab_set_unmap_op(&map->unmap_ops[i], addr, 309 - map->flags, -1 /* handle */); 310 } 311 } else { 312 /* ··· 332 map->grants[i].ref, 333 map->grants[i].domid); 334 gnttab_set_unmap_op(&map->kunmap_ops[i], address, 335 - flags, -1); 336 } 337 } 338 ··· 390 pr_debug("unmap handle=%d st=%d\n", 391 map->unmap_ops[offset+i].handle, 392 map->unmap_ops[offset+i].status); 393 - map->unmap_ops[offset+i].handle = -1; 394 } 395 return err; 396 } ··· 406 * already unmapped some of the grants. Only unmap valid ranges. 407 */ 408 while (pages && !err) { 409 - while (pages && map->unmap_ops[offset].handle == -1) { 410 offset++; 411 pages--; 412 } 413 range = 0; 414 while (range < pages) { 415 - if (map->unmap_ops[offset+range].handle == -1) 416 break; 417 range++; 418 }
··· 186 goto err; 187 188 for (i = 0; i < count; i++) { 189 + add->map_ops[i].handle = INVALID_GRANT_HANDLE; 190 + add->unmap_ops[i].handle = INVALID_GRANT_HANDLE; 191 if (use_ptemod) { 192 + add->kmap_ops[i].handle = INVALID_GRANT_HANDLE; 193 + add->kunmap_ops[i].handle = INVALID_GRANT_HANDLE; 194 } 195 } 196 ··· 279 map->grants[pgnr].ref, 280 map->grants[pgnr].domid); 281 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, 282 + INVALID_GRANT_HANDLE); 283 return 0; 284 } 285 ··· 297 298 if (!use_ptemod) { 299 /* Note: it could already be mapped */ 300 + if (map->map_ops[0].handle != INVALID_GRANT_HANDLE) 301 return 0; 302 for (i = 0; i < map->count; i++) { 303 unsigned long addr = (unsigned long) ··· 306 map->grants[i].ref, 307 map->grants[i].domid); 308 gnttab_set_unmap_op(&map->unmap_ops[i], addr, 309 + map->flags, INVALID_GRANT_HANDLE); 310 } 311 } else { 312 /* ··· 332 map->grants[i].ref, 333 map->grants[i].domid); 334 gnttab_set_unmap_op(&map->kunmap_ops[i], address, 335 + flags, INVALID_GRANT_HANDLE); 336 } 337 } 338 ··· 390 pr_debug("unmap handle=%d st=%d\n", 391 map->unmap_ops[offset+i].handle, 392 map->unmap_ops[offset+i].status); 393 + map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE; 394 } 395 return err; 396 } ··· 406 * already unmapped some of the grants. Only unmap valid ranges. 407 */ 408 while (pages && !err) { 409 + while (pages && 410 + map->unmap_ops[offset].handle == INVALID_GRANT_HANDLE) { 411 offset++; 412 pages--; 413 } 414 range = 0; 415 while (range < pages) { 416 + if (map->unmap_ops[offset + range].handle == 417 + INVALID_GRANT_HANDLE) 418 break; 419 range++; 420 }
+7
include/xen/grant_table.h
··· 50 #include <linux/page-flags.h> 51 #include <linux/kernel.h> 52 53 #define GNTTAB_RESERVED_XENSTORE 1 54 55 /* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
··· 50 #include <linux/page-flags.h> 51 #include <linux/kernel.h> 52 53 + /* 54 + * Technically there's no reliably invalid grant reference or grant handle, 55 + * so pick the value that is the most unlikely one to be observed valid. 56 + */ 57 + #define INVALID_GRANT_REF ((grant_ref_t)-1) 58 + #define INVALID_GRANT_HANDLE ((grant_handle_t)-1) 59 + 60 #define GNTTAB_RESERVED_XENSTORE 1 61 62 /* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
-1
include/xen/xenbus.h
··· 51 52 #define XENBUS_MAX_RING_GRANT_ORDER 4 53 #define XENBUS_MAX_RING_GRANTS (1U << XENBUS_MAX_RING_GRANT_ORDER) 54 - #define INVALID_GRANT_HANDLE (~0U) 55 56 /* Register callback to watch this node. */ 57 struct xenbus_watch
··· 51 52 #define XENBUS_MAX_RING_GRANT_ORDER 4 53 #define XENBUS_MAX_RING_GRANTS (1U << XENBUS_MAX_RING_GRANT_ORDER) 54 55 /* Register callback to watch this node. */ 56 struct xenbus_watch