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