Skip to contentSkip to conversation

preadv & pwritev syscalls.

PATCH Superseded Gerd Hoffmann 6 participants 14 messages Dec 15, 2008 – Dec 16, 2008

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover preadv & pwritev syscalls.

Hi,

Next revision of the patch series, with the alignment issue fixed by swapping the last two arguments as suggested by arch maintainers.

I've dropped the now-obsolete wrappers for mips. Ralf, please double-check.

thanks,
Gerd

---
 fs/compat.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
fs/compat.c +6 -0
index e5f49f5..aab2234 100644
@@ -1187,6 +1187,9 @@ compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsign
 	ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
 
 out:
+	if (ret > 0)
+		add_rchar(current, ret);
+	inc_syscr(current);
 	fput(file);
 	return ret;
 }
@@ -1210,6 +1213,9 @@ compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsig
 	ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
 
 out:
+	if (ret > 0)
+		add_wchar(current, ret);
+	inc_syscw(current);
 	fput(file);
 	return ret;
 }
Signature · 6 lines
1.5.6.5

--
To unsubscribe from this list: send the line "unsubscribe linux-api" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw

PATCH v3 1/3 Add missing accounting calls to compat_sys_{readv,writev}.

---
 fs/compat.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
fs/compat.c +6 -0
index e5f49f5..aab2234 100644
@@ -1187,6 +1187,9 @@ compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsign
 	ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
 
 out:
+	if (ret > 0)
+		add_rchar(current, ret);
+	inc_syscr(current);
 	fput(file);
 	return ret;
 }
@@ -1210,6 +1213,9 @@ compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsig
 	ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
 
 out:
+	if (ret > 0)
+		add_wchar(current, ret);
+	inc_syscw(current);
 	fput(file);
 	return ret;
 }
Signature · 1 line
1.5.6.5
#2 lore raw

PATCH v3 2/3 Add preadv and pwritev system calls.

In reply to Gerd Hoffmann

This patch adds preadv and pwritev system calls. These syscalls are a pretty straightforward combination of pread and readv (same for write). They are quite useful for doing vectored I/O in threaded applications. Using lseek+readv instead opens race windows you'll have to plug with locking.

Other systems have such system calls too, for example NetBSD, check
here: http://www.daemon-systems.org/man/preadv.2.html

The application-visible interface provided by glibc should look like this to be compatible to the existing implementations in the *BSD family:

ssize_t preadv(int d, const struct iovec *iov, int iovcnt, off_t offset); ssize_t pwritev(int d, const struct iovec *iov, int iovcnt, off_t offset);

Note that the system call uses another argument ordering (last two args swapped) to avoid unaligned 64bit arguments on the stack.

The patch sports the actual system call implementation and the windup in the x86 system call tables. Other archs follow as separate patches.

---
 arch/x86/ia32/ia32entry.S          |    2 +
 arch/x86/include/asm/unistd_32.h   |    2 +
 arch/x86/include/asm/unistd_64.h   |    4 ++
 arch/x86/kernel/syscall_table_32.S |    2 +
 fs/compat.c                        |   61 ++++++++++++++++++++++++++++++++++++
 fs/read_write.c                    |   48 ++++++++++++++++++++++++++++
 6 files changed, 119 insertions(+), 0 deletions(-)
arch/x86/ia32/ia32entry.S +2 -0
index 256b00b..9a8501b 100644
@@ -826,4 +826,6 @@ ia32_sys_call_table:
 	.quad sys_dup3			/* 330 */
 	.quad sys_pipe2
 	.quad sys_inotify_init1
+	.quad compat_sys_preadv
+	.quad compat_sys_pwritev
 ia32_syscall_end:
arch/x86/include/asm/unistd_32.h +2 -0
index f2bba78..6e72d74 100644
@@ -338,6 +338,8 @@
 #define __NR_dup3		330
 #define __NR_pipe2		331
 #define __NR_inotify_init1	332
+#define __NR_preadv		333
+#define __NR_pwritev		334
 
 #ifdef __KERNEL__
 
arch/x86/include/asm/unistd_64.h +4 -0
index d2e415e..f818294 100644
@@ -653,6 +653,10 @@ __SYSCALL(__NR_dup3, sys_dup3)
 __SYSCALL(__NR_pipe2, sys_pipe2)
 #define __NR_inotify_init1			294
 __SYSCALL(__NR_inotify_init1, sys_inotify_init1)
+#define __NR_preadv				295
+__SYSCALL(__NR_preadv, sys_preadv)
+#define __NR_pwritev				296
+__SYSCALL(__NR_pwritev, sys_pwritev)
 
 
 #ifndef __NO_STUBS
arch/x86/kernel/syscall_table_32.S +2 -0
index d44395f..a1a5506 100644
@@ -332,3 +332,5 @@ ENTRY(sys_call_table)
 	.long sys_dup3			/* 330 */
 	.long sys_pipe2
 	.long sys_inotify_init1
+	.long sys_preadv
+	.long sys_pwritev
fs/compat.c +61 -0

67 lines not on this page · raw message

fs/read_write.c +48 -0

54 lines not on this page · raw message

Signature · 1 line
1.5.6.5
#3 lore raw

PATCH v3 3/3 MIPS: Add preadv(2) and pwritev(2) syscalls.

In reply to Gerd Hoffmann

From: Ralf Baechle <ralf-6z/[email protected]>

From: Ralf Baechle <ralf-6z/[email protected]>

---
 arch/mips/include/asm/unistd.h |   18 ++++++++++++------
 arch/mips/kernel/scall32-o32.S |    2 ++
 arch/mips/kernel/scall64-64.S  |    2 ++
 arch/mips/kernel/scall64-n32.S |    2 ++
 arch/mips/kernel/scall64-o32.S |    2 ++
 5 files changed, 20 insertions(+), 6 deletions(-)
arch/mips/include/asm/unistd.h +12 -6
index a73e153..4000501 100644
@@ -350,16 +350,18 @@
 #define __NR_dup3			(__NR_Linux + 327)
 #define __NR_pipe2			(__NR_Linux + 328)
 #define __NR_inotify_init1		(__NR_Linux + 329)
+#define __NR_preadv			(__NR_Linux + 330)
+#define __NR_pwritev			(__NR_Linux + 331)
 
 /*
  * Offset of the last Linux o32 flavoured syscall
  */
-#define __NR_Linux_syscalls		329
+#define __NR_Linux_syscalls		331
 
 #endif /* _MIPS_SIM == _MIPS_SIM_ABI32 */
 
 #define __NR_O32_Linux			4000
-#define __NR_O32_Linux_syscalls		329
+#define __NR_O32_Linux_syscalls		331
 
 #if _MIPS_SIM == _MIPS_SIM_ABI64
 
@@ -656,16 +658,18 @@
 #define __NR_dup3			(__NR_Linux + 286)
 #define __NR_pipe2			(__NR_Linux + 287)
 #define __NR_inotify_init1		(__NR_Linux + 288)
+#define __NR_preadv			(__NR_Linux + 289)
+#define __NR_pwritev			(__NR_Linux + 290)
 
 /*
  * Offset of the last Linux 64-bit flavoured syscall
  */
-#define __NR_Linux_syscalls		288
+#define __NR_Linux_syscalls		290
 
 #endif /* _MIPS_SIM == _MIPS_SIM_ABI64 */
 
 #define __NR_64_Linux			5000
-#define __NR_64_Linux_syscalls		288
+#define __NR_64_Linux_syscalls		290
 
 #if _MIPS_SIM == _MIPS_SIM_NABI32
 
@@ -966,16 +970,18 @@
 #define __NR_dup3			(__NR_Linux + 290)
 #define __NR_pipe2			(__NR_Linux + 291)
 #define __NR_inotify_init1		(__NR_Linux + 292)
+#define __NR_preadv			(__NR_Linux + 293)
+#define __NR_pwritev			(__NR_Linux + 294)
 
 /*
  * Offset of the last N32 flavoured syscall
  */
-#define __NR_Linux_syscalls		292
+#define __NR_Linux_syscalls		294
 
 #endif /* _MIPS_SIM == _MIPS_SIM_NABI32 */
 
 #define __NR_N32_Linux			6000
-#define __NR_N32_Linux_syscalls		292
+#define __NR_N32_Linux_syscalls		294
 
 #ifdef __KERNEL__
 
arch/mips/kernel/scall32-o32.S +2 -0
index d0916a5..4a8b3e8 100644
@@ -650,6 +650,8 @@ einval:	li	v0, -ENOSYS
 	sys	sys_dup3		3
 	sys	sys_pipe2		2
 	sys	sys_inotify_init1	1
+	sys	sys_preadv		6	/* 4330 */
+	sys	sys_pwritev		6
 	.endm
 
 	/* We pre-compute the number of _instruction_ bytes needed to
arch/mips/kernel/scall64-64.S +2 -0
index a9e1716..217e3ce 100644
@@ -487,4 +487,6 @@ sys_call_table:
 	PTR	sys_dup3
 	PTR	sys_pipe2
 	PTR	sys_inotify_init1
+	PTR	sys_preadv
+	PTR	sys_pwritev			/* 5390 */
 	.size	sys_call_table,.-sys_call_table
arch/mips/kernel/scall64-n32.S +2 -0
index 30f3b63..f340963 100644
@@ -413,4 +413,6 @@ EXPORT(sysn32_call_table)
 	PTR	sys_dup3			/* 5290 */
 	PTR	sys_pipe2
 	PTR	sys_inotify_init1
+	PTR	sys_preadv
+	PTR	sys_pwritev
 	.size	sysn32_call_table,.-sysn32_call_table
arch/mips/kernel/scall64-o32.S +2 -0
index fefef4a..b1d281a 100644
@@ -533,4 +533,6 @@ sys_call_table:
 	PTR	sys_dup3
 	PTR	sys_pipe2
 	PTR	sys_inotify_init1
+	PTR	compat_sys_preadv		/* 4330 */
+	PTR	compat_sys_pwritev
 	.size	sys_call_table,.-sys_call_table
Signature · 6 lines
1.5.6.5

--
To unsubscribe from this list: send the line "unsubscribe linux-api" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw
In reply to Gerd Hoffmann
Quoted 5 lines · Gerd Hoffmann

Next revision of the patch series, with the alignment issue fixed by swapping the last two arguments as suggested by arch maintainers.

I've dropped the now-obsolete wrappers for mips. Ralf, please double-check.

It fixes the alignment issue but still won't work; on MIPS 32-bit userspace will pass the 64-bit argument in two registers but the 64-bit kernel code will assume it to be passed in a single registers. It'd be ugly but passing a pointer to a 64-bit argument would solve the issue; something like this:

sys_preadv(unsigned long fd, const struct iovec __user *vec,

                  unsigned long vlen, loff_t __user *pos);
compat_sys_preadv(unsigned long fd, const struct compat_iovec __user *vec,
                  unsigned long vlen, loff_t __user *pos);

sys_splice uses loff_t __user * arguments as well and that's why it's doesn't need any compat wrapper.

I'm surprised this works for x86; does x86-64 code really expect 64-bit arguments as 2 32-bit arguments?

Patch 1/3 looks like it's a bug fix and you may want to submit this separate from the remainder of the series for 2.6.28?

Ralf
--
To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

From: Ralf Baechle <[email protected]>
Date: Mon, 15 Dec 2008 16:03:11 +0000

Quoted 11 lines · Gerd Hoffmann
Quoted 6 lines

Next revision of the patch series, with the alignment issue fixed by swapping the last two arguments as suggested by arch maintainers.

I've dropped the now-obsolete wrappers for mips. Ralf, please double-check.

It fixes the alignment issue but still won't work; on MIPS 32-bit userspace will pass the 64-bit argument in two registers but the 64-bit kernel code will assume it to be passed in a single registers.

Same on 32-bit sparc, and I assume 32-bit powerpc as well.

Quoted 2 lines

I'm surprised this works for x86; does x86-64 code really expect 64-bit arguments as 2 32-bit arguments?

Args get pushed onto the stack, so perhaps it works by accident :)

#6 lore raw
In reply to Ralf Baechle
Quoted 13 lines · Ralf Baechle
Quoted 5 lines · Gerd Hoffmann

Next revision of the patch series, with the alignment issue fixed by swapping the last two arguments as suggested by arch maintainers.

I've dropped the now-obsolete wrappers for mips. Ralf, please double-check.

It fixes the alignment issue but still won't work; on MIPS 32-bit userspace will pass the 64-bit argument in two registers but the 64-bit kernel code will assume it to be passed in a single registers. It'd be ugly but passing a pointer to a 64-bit argument would solve the issue; something like this:

Quoted 4 lines

sys_preadv(unsigned long fd, const struct iovec __user *vec,

                  unsigned long vlen, loff_t __user *pos);
compat_sys_preadv(unsigned long fd, const struct compat_iovec __user *vec,
                  unsigned long vlen, loff_t __user *pos);

Suggestion from the s390 front was to explicitly pass high and low part of pos as two arguments. A bit ugly too, but should work fine as well and it avoids the user pointer dereference. What do you think about this?

Quoted 2 lines

sys_splice uses loff_t __user * arguments as well and that's why it's doesn't need any compat wrapper.

Well, avoiding the compat wrapper altogether unfortunately isn't going to work because struct iovec looks different in 32 and 64 bit.

Quoted 2 lines

I'm surprised this works for x86; does x86-64 code really expect 64-bit arguments as 2 32-bit arguments?

Args are passed on the stack, not in registers.

Quoted 2 lines

Patch 1/3 looks like it's a bug fix and you may want to submit this separate from the remainder of the series for 2.6.28?

Yep, it is a separate bugfix. Spotted by Christoph while reviewing v1, but there are no other dependencies to this patch series.

cheers,
Gerd

#7 lore raw
Quoted 5 lines · Gerd Hoffmann
Quoted 5 lines

It fixes the alignment issue but still won't work; on MIPS 32-bit userspace will pass the 64-bit argument in two registers but the 64-bit kernel code will assume it to be passed in a single registers. It'd be ugly but passing a pointer to a 64-bit argument would solve the issue; something like this:

Quoted 8 lines
Quoted 4 lines

sys_preadv(unsigned long fd, const struct iovec __user *vec,

                  unsigned long vlen, loff_t __user *pos);
compat_sys_preadv(unsigned long fd, const struct compat_iovec __user *vec,
                  unsigned long vlen, loff_t __user *pos);

Suggestion from the s390 front was to explicitly pass high and low part of pos as two arguments. A bit ugly too, but should work fine as well and it avoids the user pointer dereference. What do you think about this?

That's what the wrapper which you deleted, was doing ;-) So yes, I like it. It just raises one new problem, endianess - are arguments being passed as low/high or high/low? On MIPS we've been solving the issue with the merge_64() macro which is defined depending on the byte order:

#ifdef __MIPSEB__
#define merge_64(r1, r2) ((((r1) & 0xffffffffUL) << 32) + ((r2) & 0xffffffffUL)) #endif
#ifdef __MIPSEL__
#define merge_64(r1, r2) ((((r2) & 0xffffffffUL) << 32) + ((r1) & 0xffffffffUL)) #endif

The actual syscall wrapper could use it like:

asmlinkage int compat_sys_pwritev(unsigned long fd,
const struct compat_iovec __user *vec, unsigned a3, unsigned a4, unsigned long vlen)

{
	loff_t offset = merge_64(a3, a4);

...

If merge_64() was a standard macro in <asm/compat.h> on all architectures, compat_sys_pwrite() could invoke it directly and the need for the compat wrapper around the compat wrapper would go away.

Quoted 4 lines
Quoted 2 lines

I'm surprised this works for x86; does x86-64 code really expect 64-bit arguments as 2 32-bit arguments?

Args are passed on the stack, not in registers.

Same as 32-bit MIPS for argument #4 and up which will be passed on the stack - but the requirement for passing a long long is for it to be passed as an aligned pair of arguments and the original patch got that wrong.

Ralf

#8 lore raw
Quoted 4 lines · Ralf Baechle

If merge_64() was a standard macro in <asm/compat.h> on all architectures, compat_sys_pwrite() could invoke it directly and the need for the compat wrapper around the compat wrapper would go away.

+1 for this.

AFAIK, there's issues on s390 and sparc (at least) with sign extension, that they need to wrap anyway, we should really sort that out in the generic compat wrappers as well.

cheers, Kyle

#9 lore raw
In reply to Ralf Baechle
Quoted 11 lines · Ralf Baechle
Quoted 9 lines · Gerd Hoffmann
Quoted 9 lines

It fixes the alignment issue but still won't work; on MIPS 32-bit userspace will pass the 64-bit argument in two registers but the 64-bit kernel code will assume it to be passed in a single registers. It'd be ugly but passing a pointer to a 64-bit argument would solve the issue; something like this:

sys_preadv(unsigned long fd, const struct iovec __user *vec,

                  unsigned long vlen, loff_t __user *pos);
compat_sys_preadv(unsigned long fd, const struct compat_iovec __user *vec,
                  unsigned long vlen, loff_t __user *pos);
Quoted 6 lines
Quoted 3 lines

Suggestion from the s390 front was to explicitly pass high and low part of pos as two arguments. A bit ugly too, but should work fine as well and it avoids the user pointer dereference. What do you think about this?

That's what the wrapper which you deleted, was doing ;-) So yes, I like it.

Yep, but I'm trying to find a way to have it work without per-arch wrappers ...

Quoted 11 lines

It just raises one new problem, endianess - are arguments being passed as low/high or high/low? On MIPS we've been solving the issue with the merge_64() macro which is defined depending on the byte order:

#ifdef __MIPSEB__
#define merge_64(r1, r2) ((((r1) & 0xffffffffUL) << 32) + ((r2) & 0xffffffffUL)) #endif
#ifdef __MIPSEL__
#define merge_64(r1, r2) ((((r2) & 0xffffffffUL) << 32) + ((r1) & 0xffffffffUL)) #endif

Quoted 8 lines

The actual syscall wrapper could use it like:

asmlinkage int compat_sys_pwritev(unsigned long fd,
const struct compat_iovec __user *vec, unsigned a3, unsigned a4, unsigned long vlen)

{
	loff_t offset = merge_64(a3, a4);

...

i.e. the ordering of the splitted argument depends on the os endianness? What is the reason for this?

I'd prefer to have the ordering coded explicitly instead, like this:

asmlinkage int compat_sys_pwritev(unsigned long fd,
const struct compat_iovec __user *vec, unsigned long vlen, unsigned pos_low, unsigned pos_high)

{
	loff_t pos = pos_low | (loff_t)pos_high << 32;
        [ ... ]

cheers,
Gerd

--
To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw
Quoted 3 lines · Gerd Hoffmann

i.e. the ordering of the splitted argument depends on the os endianness? What is the reason for this?

Eh? The splitting will occur at the C ABI level, not as a result of glibc (though, it could be done that way if you really wanted, but then you're just moving the wrapper up the chain.)

A 64-bit value on a 32-bit machine will look like, in a register pair, like it looks in memory, with MSB/LSB ordering.

Quoted 9 lines

I'd prefer to have the ordering coded explicitly instead, like this:

asmlinkage int compat_sys_pwritev(unsigned long fd,
const struct compat_iovec __user *vec, unsigned long vlen, unsigned pos_low, unsigned pos_high)

{
	loff_t pos = pos_low | (loff_t)pos_high << 32;
        [ ... ]

Sadly this isn't possible without a wrapper unless you can guarantee it was passed low high instead of high low. Unless you munge it in userspace, you can't.

regards, Kyle

maintainer
#11 lore raw
Quoted 10 lines · Kyle McMartin
Quoted 9 lines

I'd prefer to have the ordering coded explicitly instead, like this:

asmlinkage int compat_sys_pwritev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen, unsigned pos_low, unsigned pos_high)
{
      loff_t pos = pos_low | (loff_t)pos_high << 32;
        [ ... ]

Quoted 4 lines

Sadly this isn't possible without a wrapper unless you can guarantee it was passed low high instead of high low. Unless you munge it in userspace, you can't.

Exactly, we have to munge it in userspace anyway because of the argument ordering requirement we get if we pass it as a single u64. I vote for the explicit split as well, this will give us a straightforward wrapper in user space, unlike the swapped arguments that are just asking for trouble, and will make it easy to have a common compat_sys_pwritev function.

	Arnd <><
#12 lore raw
In reply to Kyle McMartin
Quoted 7 lines · Kyle McMartin
Quoted 2 lines · Gerd Hoffmann

i.e. the ordering of the splitted argument depends on the os endianness? What is the reason for this?

Eh? The splitting will occur at the C ABI level, not as a result of glibc (though, it could be done that way if you really wanted, but then you're just moving the wrapper up the chain.)

Ah, ok. You'll just declare 64bit arg for userspace, gcc splits it into two 32bit in native byte order, and then the kernel picks up the two 32bit values and has to reassemble them correctly, right?

The application-visible API must be compatible to the existing implementations, i.e. this ...

pread(long fd, struct iovec *vec, long vlen, off_t pos);

... prototype with the unaligned 64bit pos argument (in 32compat case).

Looks like there is no way around wrapping stuff then for the archs wanting aligned 64bit values. The only choice we have is to do the wrap-o-magic in glibc or in the kernel.

I'd tend handle the wrapping in kernel space then because it is less confusing and we have to wrap only in case the ABI for $arch requires it.

Comments?

Gerd
--
To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#13 lore raw
Quoted 20 lines · Gerd Hoffmann
Quoted 7 lines · Kyle McMartin
Quoted 2 lines · Gerd Hoffmann

i.e. the ordering of the splitted argument depends on the os endianness? What is the reason for this?

Eh? The splitting will occur at the C ABI level, not as a result of glibc (though, it could be done that way if you really wanted, but then you're just moving the wrapper up the chain.)

Ah, ok. You'll just declare 64bit arg for userspace, gcc splits it into two 32bit in native byte order, and then the kernel picks up the two 32bit values and has to reassemble them correctly, right?

The application-visible API must be compatible to the existing implementations, i.e. this ...

   pread(long fd, struct iovec *vec, long vlen, off_t pos);

... prototype with the unaligned 64bit pos argument (in 32compat case).

Quoted 8 lines

Looks like there is no way around wrapping stuff then for the archs wanting aligned 64bit values. The only choice we have is to do the wrap-o-magic in glibc or in the kernel.

I'd tend handle the wrapping in kernel space then because it is less confusing and we have to wrap only in case the ABI for $arch requires it.

Comments?

Please do it just the way that Arnd suggested: explicitly pass the upper and lower part of loff_t as separate arguments.
It's the most simple approach.
--
To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html