Skip to contentSkip to conversation

linux: add new (replacement) mmap-batch ioctl

RFC PATCH In review Jan Beulich 3 participants 9 messages Jan 11, 2010 – Jan 12, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/4 linux: add new (replacement) mmap-batch ioctl

While the error indicator of IOCTL_PRIVCMD_MMAPBATCH should be in the top nibble (it is documented that way in include/xen/public/privcmd.h and include/xen/compat_ioctl.h), it really wasn't for 64-bit implementations. With MFNs now possibly being 32 or more bits wide on x86-64, using bits 28-31 as failure indicator (and bit 31 as paged-out indicator) is not longer acceptable. Instead, a new ioctl with a separate error indication array is being introduced.

As usual, written against 2.6.32.3 and made apply to the 2.6.18 tree without further testing.

drivers/xen/privcmd/compat_privcmd.c +50 -0
@@ -96,6 +96,56 @@ int privcmd_ioctl_32(int fd, unsigned in
 #endif
 	}
 		break;
+	case IOCTL_PRIVCMD_MMAP_BATCH_32: {
+		struct privcmd_mmap_batch *p;
+		struct privcmd_mmap_batch_32 *p32;
+		struct privcmd_mmap_batch_32 n32;
+#ifdef xen_pfn32_t
+		xen_pfn_t *__user arr;
+		xen_pfn32_t *__user arr32;
+		unsigned int i;
+#endif
+
+		p32 = compat_ptr(arg);
+		p = compat_alloc_user_space(sizeof(*p));
+		if (copy_from_user(&n32, p32, sizeof(n32)) ||
+		    put_user(n32.num, &p->num) ||
+		    put_user(n32.dom, &p->dom) ||
+		    put_user(n32.addr, &p->addr) ||
+		    put_user(compat_ptr(n32.err), &p->err))
+			return -EFAULT;
+#ifdef xen_pfn32_t
+		arr = compat_alloc_user_space(n32.num * sizeof(*arr)
+					      + sizeof(*p));
+		arr32 = compat_ptr(n32.arr);
+		for (i = 0; i < n32.num; ++i) {
+			xen_pfn32_t mfn;
+
+			if (get_user(mfn, arr32 + i) || put_user(mfn, arr + i))
+				return -EFAULT;
+		}
+
+		if (put_user(arr, &p->arr))
+			return -EFAULT;
+#else
+		if (put_user(compat_ptr(n32.arr), &p->arr))
+			return -EFAULT;
+#endif
+
+		ret = sys_ioctl(fd, IOCTL_PRIVCMD_MMAP_BATCH, (unsigned long)p);
+
+#ifdef xen_pfn32_t
+		for (i = 0; !ret && i < n32.num; ++i) {
+			xen_pfn_t mfn;
+
+			if (get_user(mfn, arr + i) || put_user(mfn, arr32 + i))
+				ret = -EFAULT;
+			else if (mfn != (xen_pfn32_t)mfn)
+				ret = -ERANGE;
+		}
+#endif
+	}
+		break;
 	default:
 		ret = -EINVAL;
 		break;
drivers/xen/privcmd/privcmd.c +102 -0

108 lines not on this page · raw message

fs/compat_ioctl.c +1 -0
@@ -2937,6 +2937,7 @@ IGNORE_IOCTL(FBIOGCURSOR32)
 #ifdef CONFIG_XEN
 HANDLE_IOCTL(IOCTL_PRIVCMD_MMAP_32, privcmd_ioctl_32)
 HANDLE_IOCTL(IOCTL_PRIVCMD_MMAPBATCH_32, privcmd_ioctl_32)
+HANDLE_IOCTL(IOCTL_PRIVCMD_MMAP_BATCH_32, privcmd_ioctl_32)
 COMPATIBLE_IOCTL(IOCTL_PRIVCMD_HYPERCALL)
 COMPATIBLE_IOCTL(IOCTL_EVTCHN_BIND_VIRQ)
 COMPATIBLE_IOCTL(IOCTL_EVTCHN_BIND_INTERDOMAIN)
include/xen/compat_ioctl.h +19 -1
@@ -49,9 +49,27 @@ struct privcmd_mmapbatch_32 {
 #endif
 	compat_uptr_t arr; /* array of mfns - top nibble set on err */
 };
+
+struct privcmd_mmap_batch_32 {
+	unsigned int num; /* number of pages to populate */
+	domid_t dom;      /* target domain */
+#if defined(CONFIG_X86) || defined(CONFIG_IA64)
+	union {      /* virtual address */
+		__u64 addr __packed;
+		__u32 va;
+	};
+#else
+	__u64 addr;  /* virtual address */
+#endif
+	compat_uptr_t arr; /* array of mfns */
+	compat_uptr_t err; /* array of error codes */
+};
+
 #define IOCTL_PRIVCMD_MMAP_32                   \
 	_IOC(_IOC_NONE, 'P', 2, sizeof(struct privcmd_mmap_32))
-#define IOCTL_PRIVCMD_MMAPBATCH_32                  \
+#define IOCTL_PRIVCMD_MMAPBATCH_32              \
 	_IOC(_IOC_NONE, 'P', 3, sizeof(struct privcmd_mmapbatch_32))
+#define IOCTL_PRIVCMD_MMAP_BATCH_32             \
+	_IOC(_IOC_NONE, 'P', 4, sizeof(struct privcmd_mmap_batch_32))
 
 #endif /* __LINUX_XEN_COMPAT_H__ */
include/xen/public/privcmd.h +10 -0
@@ -60,6 +60,14 @@ typedef struct privcmd_mmapbatch {
 	xen_pfn_t __user *arr; /* array of mfns - top nibble set on err */
 } privcmd_mmapbatch_t; 
 
+typedef struct privcmd_mmap_batch {
+	unsigned int num; /* number of pages to populate */
+	domid_t dom;      /* target domain */
+	__u64 addr;       /* virtual address */
+	const xen_pfn_t __user *arr; /* array of mfns */
+	int __user *err;  /* array of error codes */
+} privcmd_mmap_batch_t;
+
 /*
  * @cmd: IOCTL_PRIVCMD_HYPERCALL
  * @arg: &privcmd_hypercall_t
@@ -71,5 +79,7 @@ typedef struct privcmd_mmapbatch {
 	_IOC(_IOC_NONE, 'P', 2, sizeof(privcmd_mmap_t))
 #define IOCTL_PRIVCMD_MMAPBATCH					\
 	_IOC(_IOC_NONE, 'P', 3, sizeof(privcmd_mmapbatch_t))
+#define IOCTL_PRIVCMD_MMAP_BATCH				\
+	_IOC(_IOC_NONE, 'P', 4, sizeof(privcmd_mmap_batch_t))
 
 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
#1 lore raw

include/xen/public/privcmd.h typedef struct privcmd_mmapbatch { · line 83

Quoted 4 lines · Jan Beulich
 #define IOCTL_PRIVCMD_MMAPBATCH                                        \
        _IOC(_IOC_NONE, 'P', 3, sizeof(privcmd_mmapbatch_t))
include/xen/public/privcmd.h +2 -0
+#define IOCTL_PRIVCMD_MMAP_BATCH                               \
+       _IOC(_IOC_NONE, 'P', 4, sizeof(privcmd_mmap_batch_t)) 

Distinguishing the old vs new ioctl by only a _ in the middle seems a bit horrid. MMAPBATCH2 would at least be less prone to confusion...

Why not take the opportunity to make the ioctl (and hypercall?) interface 32 vs 64 agnostic by unconditionally using 64 bit sizes?

Ian.

#2 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line

Ian Campbell <[email protected]> 11.01.10 16:12 >>>

include/xen/public/privcmd.h typedef struct privcmd_mmapbatch { · line 83

Quoted 4 lines · Jan Beulich
 #define IOCTL_PRIVCMD_MMAPBATCH                                        \
        _IOC(_IOC_NONE, 'P', 3, sizeof(privcmd_mmapbatch_t))
include/xen/public/privcmd.h +2 -0
+#define IOCTL_PRIVCMD_MMAP_BATCH                               \
+       _IOC(_IOC_NONE, 'P', 4, sizeof(privcmd_mmap_batch_t)) 

Distinguishing the old vs new ioctl by only a _ in the middle seems a bit horrid. MMAPBATCH2 would at least be less prone to confusion...

Otoh it clearly tells that the two are very similar...

Quoted 2 lines

Why not take the opportunity to make the ioctl (and hypercall?) interface 32 vs 64 agnostic by unconditionally using 64 bit sizes?

That's not common practice for structures containing pointers afaict, and wouldn't be nice for the pointer type of 'arr' (would double the array size on 32-bits for no reason, and would require extra checking anyway).

Jan

#3 lore raw
Quoted 9 lines · Jan Beulich
Quoted 7 lines

include/xen/public/privcmd.h typedef struct privcmd_mmapbatch { · line 83

Quoted 4 lines
 #define IOCTL_PRIVCMD_MMAPBATCH                                        \
        _IOC(_IOC_NONE, 'P', 3, sizeof(privcmd_mmapbatch_t))
include/xen/public/privcmd.h +2 -0
+#define IOCTL_PRIVCMD_MMAP_BATCH                               \
+       _IOC(_IOC_NONE, 'P', 4, sizeof(privcmd_mmap_batch_t))

Distinguishing the old vs new ioctl by only a _ in the middle seems a bit horrid. MMAPBATCH2 would at least be less prone to confusion...

Otoh it clearly tells that the two are very similar...

As does MMAPBATCH2. I actually strongly suggest IOCTL_PRIVCMD_MMAPBATCH_V2, which makes the situation even more crystal clear. Distinguishing based on a single underscore, to make the new name 'nice', is misplaced aestheticism. In particular, the tools end up referencing both names (for compatibility) and a clearer name makes the code more intelligible.

-- Keir

#4 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 11.01.10 20:24 >>>

Quoted 8 lines · Jan Beulich
Quoted 7 lines

include/xen/public/privcmd.h typedef struct privcmd_mmapbatch { · line 83

Quoted 4 lines
 #define IOCTL_PRIVCMD_MMAPBATCH                                        \
        _IOC(_IOC_NONE, 'P', 3, sizeof(privcmd_mmapbatch_t))
include/xen/public/privcmd.h +2 -0
+#define IOCTL_PRIVCMD_MMAP_BATCH                               \
+       _IOC(_IOC_NONE, 'P', 4, sizeof(privcmd_mmap_batch_t))

Distinguishing the old vs new ioctl by only a _ in the middle seems a bit horrid. MMAPBATCH2 would at least be less prone to confusion...

Quoted 7 lines
Quoted 1 line

Otoh it clearly tells that the two are very similar...

As does MMAPBATCH2. I actually strongly suggest IOCTL_PRIVCMD_MMAPBATCH_V2, which makes the situation even more crystal clear. Distinguishing based on a single underscore, to make the new name 'nice', is misplaced aestheticism. In particular, the tools end up referencing both names (for compatibility) and a clearer name makes the code more intelligible.

Okay, I'll change the name then. Before re-spinning the set, are there any other items that need fixing? Comments on the questions raised?

And are there chances this will still make 4.0? If not, can we at least add the interface definitions as place holders (similar to e.g. tmem before 3.4), allowing consuming code to be prepared for the addition?

Jan

#5 lore raw
Quoted 8 lines · Jan Beulich
Quoted 5 lines

As does MMAPBATCH2. I actually strongly suggest IOCTL_PRIVCMD_MMAPBATCH_V2, which makes the situation even more crystal clear. Distinguishing based on a single underscore, to make the new name 'nice', is misplaced aestheticism. In particular, the tools end up referencing both names (for compatibility) and a clearer name makes the code more intelligible.

Okay, I'll change the name then. Before re-spinning the set, are there any other items that need fixing? Comments on the questions raised?

Looked okay to me. The only question I saw was regarding
xc_get_pfn_type_xxx: I don't think they are used outside libxc, so you can do whatever makes internal sense for libxc (and remove the decls from xenctrl.h).

Quoted 3 lines

And are there chances this will still make 4.0? If not, can we at least add the interface definitions as place holders (similar to e.g. tmem before 3.4), allowing consuming code to be prepared for the addition?

It could go in for 4.0.0-rc2 I suppose.

-- Keir

#6 lore raw
Quoted 7 lines · Keir Fraser
Quoted 2 lines

Okay, I'll change the name then. Before re-spinning the set, are there any other items that need fixing? Comments on the questions raised?

Looked okay to me. The only question I saw was regarding
xc_get_pfn_type_xxx: I don't think they are used outside libxc, so you can do whatever makes internal sense for libxc (and remove the decls from xenctrl.h).

Oh, and libxc's version number should definitely be bumped.

-- Keir

#7 lore raw
In reply to Keir Fraser
Quoted 5 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 12.01.10 08:57 >>>

Looked okay to me. The only question I saw was regarding
xc_get_pfn_type_xxx: I don't think they are used outside libxc, so you can do whatever makes internal sense for libxc (and remove the decls from xenctrl.h).

The other question (of wider scope) was that on the save format currently used: Using bits 28-31 of the PFNs to encode their types (effectively wasting bits 32-63 on 64-bit) isn't forward compatible, and hence sooner or later will require a change. But of course there are other tools limitations (restricting guests to even smaller amounts of memory), but those are mostly internal to the tools (i.e. fixing them would not break compatibility).

Jan

#8 lore raw
Quoted 6 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 12.01.10 08:57 >>>

Looked okay to me. The only question I saw was regarding
xc_get_pfn_type_xxx: I don't think they are used outside libxc, so you can do whatever makes internal sense for libxc (and remove the decls from xenctrl.h).

Quoted 7 lines

The other question (of wider scope) was that on the save format currently used: Using bits 28-31 of the PFNs to encode their types (effectively wasting bits 32-63 on 64-bit) isn't forward compatible, and hence sooner or later will require a change. But of course there are other tools limitations (restricting guests to even smaller amounts of memory), but those are mostly internal to the tools (i.e. fixing them would not break compatibility).

We'll have to stick some kind of version-bump indicator in the save file, and change the way PFNs and their types are encoded. This is very doable, when the development window reopens.

This is definitely a 4.1 feature -- it only prevents us saving 1TB+ guests. It's quite a separate concern from our support for sparse-memory-map hosts.

-- Keir