Skip to content

Commit bdc612d

Browse files
dgdegraafkonradwilk
authored andcommitted
xen/gntalloc,gntdev: Add unmap notify ioctl
This ioctl allows the users of a shared page to be notified when the other end exits abnormally. [v2: updated description in structs] Signed-off-by: Daniel De Graaf <dgdegra@tycho.nsa.gov> Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
1 parent dd31405 commit bdc612d

4 files changed

Lines changed: 182 additions & 1 deletion

File tree

‎drivers/xen/gntalloc.c‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@
6060
#include <linux/uaccess.h>
6161
#include <linux/types.h>
6262
#include <linux/list.h>
63+
#include <linux/highmem.h>
6364

6465
#include <xen/xen.h>
6566
#include <xen/page.h>
6667
#include <xen/grant_table.h>
6768
#include <xen/gntalloc.h>
69+
#include <xen/events.h>
6870

6971
static int limit = 1024;
7072
module_param(limit, int, 0644);
@@ -75,6 +77,12 @@ static LIST_HEAD(gref_list);
7577
static DEFINE_SPINLOCK(gref_lock);
7678
static int gref_size;
7779

80+
struct notify_info {
81+
uint16_t pgoff:12; /* Bits 0-11: Offset of the byte to clear */
82+
uint16_t flags:2; /* Bits 12-13: Unmap notification flags */
83+
int event; /* Port (event channel) to notify */
84+
};
85+
7886
/* Metadata on a grant reference. */
7987
struct gntalloc_gref {
8088
struct list_head next_gref; /* list entry gref_list */
@@ -83,6 +91,7 @@ struct gntalloc_gref {
8391
uint64_t file_index; /* File offset for mmap() */
8492
unsigned int users; /* Use count - when zero, waiting on Xen */
8593
grant_ref_t gref_id; /* The grant reference number */
94+
struct notify_info notify; /* Unmap notification */
8695
};
8796

8897
struct gntalloc_file_private_data {
@@ -164,6 +173,16 @@ static int add_grefs(struct ioctl_gntalloc_alloc_gref *op,
164173

165174
static void __del_gref(struct gntalloc_gref *gref)
166175
{
176+
if (gref->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
177+
uint8_t *tmp = kmap(gref->page);
178+
tmp[gref->notify.pgoff] = 0;
179+
kunmap(gref->page);
180+
}
181+
if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT)
182+
notify_remote_via_evtchn(gref->notify.event);
183+
184+
gref->notify.flags = 0;
185+
167186
if (gref->gref_id > 0) {
168187
if (gnttab_query_foreign_access(gref->gref_id))
169188
return;
@@ -349,6 +368,43 @@ static long gntalloc_ioctl_dealloc(struct gntalloc_file_private_data *priv,
349368
return rc;
350369
}
351370

371+
static long gntalloc_ioctl_unmap_notify(struct gntalloc_file_private_data *priv,
372+
void __user *arg)
373+
{
374+
struct ioctl_gntalloc_unmap_notify op;
375+
struct gntalloc_gref *gref;
376+
uint64_t index;
377+
int pgoff;
378+
int rc;
379+
380+
if (copy_from_user(&op, arg, sizeof(op)))
381+
return -EFAULT;
382+
383+
index = op.index & ~(PAGE_SIZE - 1);
384+
pgoff = op.index & (PAGE_SIZE - 1);
385+
386+
spin_lock(&gref_lock);
387+
388+
gref = find_grefs(priv, index, 1);
389+
if (!gref) {
390+
rc = -ENOENT;
391+
goto unlock_out;
392+
}
393+
394+
if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) {
395+
rc = -EINVAL;
396+
goto unlock_out;
397+
}
398+
399+
gref->notify.flags = op.action;
400+
gref->notify.pgoff = pgoff;
401+
gref->notify.event = op.event_channel_port;
402+
rc = 0;
403+
unlock_out:
404+
spin_unlock(&gref_lock);
405+
return rc;
406+
}
407+
352408
static long gntalloc_ioctl(struct file *filp, unsigned int cmd,
353409
unsigned long arg)
354410
{
@@ -361,6 +417,9 @@ static long gntalloc_ioctl(struct file *filp, unsigned int cmd,
361417
case IOCTL_GNTALLOC_DEALLOC_GREF:
362418
return gntalloc_ioctl_dealloc(priv, (void __user *)arg);
363419

420+
case IOCTL_GNTALLOC_SET_UNMAP_NOTIFY:
421+
return gntalloc_ioctl_unmap_notify(priv, (void __user *)arg);
422+
364423
default:
365424
return -ENOIOCTLCMD;
366425
}

‎drivers/xen/gntdev.c‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <xen/xen.h>
3838
#include <xen/grant_table.h>
3939
#include <xen/gntdev.h>
40+
#include <xen/events.h>
4041
#include <asm/xen/hypervisor.h>
4142
#include <asm/xen/hypercall.h>
4243
#include <asm/xen/page.h>
@@ -63,6 +64,13 @@ struct gntdev_priv {
6364
struct mmu_notifier mn;
6465
};
6566

67+
struct unmap_notify {
68+
int flags;
69+
/* Address relative to the start of the grant_map */
70+
int addr;
71+
int event;
72+
};
73+
6674
struct grant_map {
6775
struct list_head next;
6876
struct vm_area_struct *vma;
@@ -71,6 +79,7 @@ struct grant_map {
7179
int flags;
7280
int is_mapped;
7381
atomic_t users;
82+
struct unmap_notify notify;
7483
struct ioctl_gntdev_grant_ref *grants;
7584
struct gnttab_map_grant_ref *map_ops;
7685
struct gnttab_unmap_grant_ref *unmap_ops;
@@ -165,7 +174,7 @@ static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
165174
list_for_each_entry(map, &priv->maps, next) {
166175
if (map->index != index)
167176
continue;
168-
if (map->count != count)
177+
if (count && map->count != count)
169178
continue;
170179
return map;
171180
}
@@ -184,6 +193,10 @@ static void gntdev_put_map(struct grant_map *map)
184193

185194
atomic_sub(map->count, &pages_mapped);
186195

196+
if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
197+
notify_remote_via_evtchn(map->notify.event);
198+
}
199+
187200
if (map->pages) {
188201
if (!use_ptemod)
189202
unmap_grant_pages(map, 0, map->count);
@@ -274,6 +287,16 @@ static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
274287
{
275288
int i, err = 0;
276289

290+
if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
291+
int pgno = (map->notify.addr >> PAGE_SHIFT);
292+
if (pgno >= offset && pgno < offset + pages) {
293+
uint8_t *tmp = kmap(map->pages[pgno]);
294+
tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
295+
kunmap(map->pages[pgno]);
296+
map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
297+
}
298+
}
299+
277300
pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
278301
err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages, pages);
279302
if (err)
@@ -519,6 +542,39 @@ static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
519542
return 0;
520543
}
521544

545+
static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
546+
{
547+
struct ioctl_gntdev_unmap_notify op;
548+
struct grant_map *map;
549+
int rc;
550+
551+
if (copy_from_user(&op, u, sizeof(op)))
552+
return -EFAULT;
553+
554+
if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
555+
return -EINVAL;
556+
557+
spin_lock(&priv->lock);
558+
559+
list_for_each_entry(map, &priv->maps, next) {
560+
uint64_t begin = map->index << PAGE_SHIFT;
561+
uint64_t end = (map->index + map->count) << PAGE_SHIFT;
562+
if (op.index >= begin && op.index < end)
563+
goto found;
564+
}
565+
rc = -ENOENT;
566+
goto unlock_out;
567+
568+
found:
569+
map->notify.flags = op.action;
570+
map->notify.addr = op.index - (map->index << PAGE_SHIFT);
571+
map->notify.event = op.event_channel_port;
572+
rc = 0;
573+
unlock_out:
574+
spin_unlock(&priv->lock);
575+
return rc;
576+
}
577+
522578
static long gntdev_ioctl(struct file *flip,
523579
unsigned int cmd, unsigned long arg)
524580
{
@@ -535,6 +591,9 @@ static long gntdev_ioctl(struct file *flip,
535591
case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
536592
return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
537593

594+
case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
595+
return gntdev_ioctl_notify(priv, ptr);
596+
538597
default:
539598
pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
540599
return -ENOIOCTLCMD;

‎include/xen/gntalloc.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,36 @@ struct ioctl_gntalloc_dealloc_gref {
4747
/* Number of references to unmap */
4848
uint32_t count;
4949
};
50+
51+
/*
52+
* Sets up an unmap notification within the page, so that the other side can do
53+
* cleanup if this side crashes. Required to implement cross-domain robust
54+
* mutexes or close notification on communication channels.
55+
*
56+
* Each mapped page only supports one notification; multiple calls referring to
57+
* the same page overwrite the previous notification. You must clear the
58+
* notification prior to the IOCTL_GNTALLOC_DEALLOC_GREF if you do not want it
59+
* to occur.
60+
*/
61+
#define IOCTL_GNTALLOC_SET_UNMAP_NOTIFY \
62+
_IOC(_IOC_NONE, 'G', 7, sizeof(struct ioctl_gntalloc_unmap_notify))
63+
struct ioctl_gntalloc_unmap_notify {
64+
/* IN parameters */
65+
/* Offset in the file descriptor for a byte within the page (same as
66+
* used in mmap). If using UNMAP_NOTIFY_CLEAR_BYTE, this is the byte to
67+
* be cleared. Otherwise, it can be any byte in the page whose
68+
* notification we are adjusting.
69+
*/
70+
uint64_t index;
71+
/* Action(s) to take on unmap */
72+
uint32_t action;
73+
/* Event channel to notify */
74+
uint32_t event_channel_port;
75+
};
76+
77+
/* Clear (set to zero) the byte specified by index */
78+
#define UNMAP_NOTIFY_CLEAR_BYTE 0x1
79+
/* Send an interrupt on the indicated event channel */
80+
#define UNMAP_NOTIFY_SEND_EVENT 0x2
81+
5082
#endif /* __LINUX_PUBLIC_GNTALLOC_H__ */

‎include/xen/gntdev.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,35 @@ struct ioctl_gntdev_set_max_grants {
116116
uint32_t count;
117117
};
118118

119+
/*
120+
* Sets up an unmap notification within the page, so that the other side can do
121+
* cleanup if this side crashes. Required to implement cross-domain robust
122+
* mutexes or close notification on communication channels.
123+
*
124+
* Each mapped page only supports one notification; multiple calls referring to
125+
* the same page overwrite the previous notification. You must clear the
126+
* notification prior to the IOCTL_GNTALLOC_DEALLOC_GREF if you do not want it
127+
* to occur.
128+
*/
129+
#define IOCTL_GNTDEV_SET_UNMAP_NOTIFY \
130+
_IOC(_IOC_NONE, 'G', 7, sizeof(struct ioctl_gntdev_unmap_notify))
131+
struct ioctl_gntdev_unmap_notify {
132+
/* IN parameters */
133+
/* Offset in the file descriptor for a byte within the page (same as
134+
* used in mmap). If using UNMAP_NOTIFY_CLEAR_BYTE, this is the byte to
135+
* be cleared. Otherwise, it can be any byte in the page whose
136+
* notification we are adjusting.
137+
*/
138+
uint64_t index;
139+
/* Action(s) to take on unmap */
140+
uint32_t action;
141+
/* Event channel to notify */
142+
uint32_t event_channel_port;
143+
};
144+
145+
/* Clear (set to zero) the byte specified by index */
146+
#define UNMAP_NOTIFY_CLEAR_BYTE 0x1
147+
/* Send an interrupt on the indicated event channel */
148+
#define UNMAP_NOTIFY_SEND_EVENT 0x2
149+
119150
#endif /* __LINUX_PUBLIC_GNTDEV_H__ */

0 commit comments

Comments
 (0)