Skip to contentSkip to conversation

read() MAX_IO_SIZE bytes, more than SSIZE_MAX?

DISCUSSION Joachim Schmitz 5 participants 20 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hi there

While investigating the problem with hung git-upload-pack we think to have found a bug in wrapper.c:

#define MAX_IO_SIZE (8*1024*1024)

This is then used in xread() to split read()s into suitable chunks. So far so good, but read() is only guaranteed to read as much as SSIZE_MAX bytes at a time. And on our platform that is way lower than those 8MB (only 52kB, POSIX allows it to be as small as 32k), and as a (rather strange) consequence mmap() (from compat/mmap.c) fails with EACCESS (why EACCESS?), because xpread() returns something > 0.

How large is SSIZE_MAX on other platforms? What happens there if you try to read() more? Should't we rather use SSIZE_MAX on all platforms? If I'm reading the header files right, on Linux it is LONG_MAX (2TB?), so I guess we should really go for MIN(8*1024*1024,SSIZE_MAX)?

bye, Jojo

#1 lore raw
Quoted 14 lines · Joachim Schmitz

Hi there

While investigating the problem with hung git-upload-pack we think to have found a bug in wrapper.c:

#define MAX_IO_SIZE (8*1024*1024)

This is then used in xread() to split read()s into suitable chunks. So far so good, but read() is only guaranteed to read as much as SSIZE_MAX bytes at a time. And on our platform that is way lower than those 8MB (only 52kB, POSIX allows it to be as small as 32k), and as a (rather strange) consequence mmap() (from compat/mmap.c) fails with EACCESS (why EACCESS?), because xpread() returns something > 0.

Quoted 4 lines

How large is SSIZE_MAX on other platforms? What happens there if you try to read() more? Should't we rather use SSIZE_MAX on all platforms? If I'm reading the header files right, on Linux it is LONG_MAX (2TB?), so I guess we should really go for MIN(8*1024*1024,SSIZE_MAX)?

How about changing wrapper.c like this:

#ifndef MAX_IO_SIZE
#define MAX_IO_SIZE (8*1024*1024)
#endif
---------------------
and to change config.mak.uname like this:

ifeq ($(uname_S),NONSTOP_KERNEL)

	BASIC_CFLAGS += -DMAX_IO_SIZE=(32*1024)

Does this work for you ?

#2 lore raw
Quoted 20 lines · On 2015-02-07 12:30PM Torsten Bögershausen wrote:
Quoted 14 lines · Joachim Schmitz

Hi there

While investigating the problem with hung git-upload-pack we think to have found a bug in wrapper.c:

#define MAX_IO_SIZE (8*1024*1024)

This is then used in xread() to split read()s into suitable chunks. So far so good, but read() is only guaranteed to read as much as SSIZE_MAX bytes at a time. And on our platform that is way lower than those 8MB (only 52kB, POSIX allows it to be as small as 32k), and as a (rather strange) consequence mmap() (from compat/mmap.c) fails with EACCESS (why EACCESS?), because xpread() returns something > 0.

Quoted 5 lines

How large is SSIZE_MAX on other platforms? What happens there if you try to
read() more? Should't we rather use SSIZE_MAX on all platforms? If I'm reading the header files right, on Linux it is LONG_MAX (2TB?), so I guess we should really go for MIN(8*1024*1024,SSIZE_MAX)?

Quoted 8 lines

How about changing wrapper.c like this:
#ifndef MAX_IO_SIZE
#define MAX_IO_SIZE (8*1024*1024)
#endif
---------------------
and to change config.mak.uname like this:
ifeq ($(uname_S),NONSTOP_KERNEL)

	BASIC_CFLAGS += -DMAX_IO_SIZE=(32*1024) Does this work for you ?

Yes, thank you Torsten. I have made this change in our branch (on behalf of Jojo). I think we can accept it. The (32*1024) does need to be properly quoted, however.

#3 lore raw
Quoted 21 lines · On 2015-02-07 13:07PM Randall S. Becker wrote:
Quoted 20 lines · On 2015-02-07 12:30PM Torsten Bögershausen wrote:
Quoted 19 lines · Joachim Schmitz

Hi there

While investigating the problem with hung git-upload-pack we think to have found a bug in wrapper.c:

#define MAX_IO_SIZE (8*1024*1024)

This is then used in xread() to split read()s into suitable chunks. So far so good, but read() is only guaranteed to read as much as SSIZE_MAX bytes at a time. And on our platform that is way lower than those 8MB (only 52kB, POSIX allows it to be as small as 32k), and as a (rather strange) consequence mmap() (from compat/mmap.c) fails with EACCESS (why EACCESS?), because xpread() returns something > 0.

How large is SSIZE_MAX on other platforms? What happens there if you try to
read() more? Should't we rather use SSIZE_MAX on all platforms? If I'm reading the header files right, on Linux it is LONG_MAX (2TB?), so I guess we should really go for MIN(8*1024*1024,SSIZE_MAX)?

Quoted 5 lines
Quoted 5 lines

How about changing wrapper.c like this:
#ifndef MAX_IO_SIZE
#define MAX_IO_SIZE (8*1024*1024)
#endif
---------------------

Although I do agree with Jojo, that MAX_IO_SIZE seems to be a platform constant and should be defined in terms of SSIZE_MAX. So something like:

#ifndef MAX_IO_SIZE

# ifdef SSIZE_MAX
#  define MAX_IO_SIZE (SSIZE_MAX)
# else
#  define MAX_IO_SIZE (8*1024*1024)
# endif
#endif

would be desirable.

Cheers, Randall

#4 lore raw
Quoted 4 lines · Randall S. Becker <rsbecker <at> nexbridge.com> writes:
Quoted 2 lines · On 2015-02-07 13:07PM Randall S. Becker wrote:
Quoted 1 line · On 2015-02-07 12:30PM Torsten Bögershausen wrote:

On 2015-02-07 17.45, Joachim Schmitz wrote:

<spip>

Quoted 4 lines

Although I do agree with Jojo, that MAX_IO_SIZE seems to be a platform constant and should be defined in terms of SSIZE_MAX. So something like:

#ifndef MAX_IO_SIZE

Quoted 8 lines
# ifdef SSIZE_MAX
#  define MAX_IO_SIZE (SSIZE_MAX)
# else
#  define MAX_IO_SIZE (8*1024*1024)
# endif
#endif

would be desirable.

It would be way too large on some platforms. those 8MB had been chosen for a good reason, I assume:

/*
* Limit size of IO chunks, because huge chunks only cause pain. OS X
* 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in
* the absence of bugs, large chunks can result in bad latencies when
* you decide to kill the process.
*/

However it should never be larger than SSIZE_MAX

#5 lore raw
In reply to Torsten Bögershausen
Quoted 2 lines · Torsten Bögershausen <tboegi <at> web.de> writes:

On 2015-02-07 17.45, Joachim Schmitz wrote:

<snip>

Quoted 9 lines

How about changing wrapper.c like this:

#ifndef MAX_IO_SIZE
#define MAX_IO_SIZE (8*1024*1024)
#endif
---------------------
and to change config.mak.uname like this:

Quoted 4 lines

ifeq ($(uname_S),NONSTOP_KERNEL)

	BASIC_CFLAGS += -DMAX_IO_SIZE=(32*1024)

Does this work for you ?

Of course it would, but,
a) 32k is smaller than we can go (and yes, we could make it 52k)
b) never ever should read() be asked to read more than SSIZE_MAX, this should be true for every platform on the planet? You may want to have is smaller than SSIZE_MAX (like the current 8MB vs. the possible 2TB on Linux), but surely never larger?

Bye, Jojo

#6 lore raw
Quoted 14 lines · Joachim Schmitz
Quoted 2 lines · Torsten Bögershausen <tboegi <at> web.de> writes:

On 2015-02-07 17.45, Joachim Schmitz wrote:

<snip>

Quoted 9 lines

How about changing wrapper.c like this:

#ifndef MAX_IO_SIZE
#define MAX_IO_SIZE (8*1024*1024)
#endif
---------------------
and to change config.mak.uname like this:

Quoted 7 lines
Quoted 4 lines

ifeq ($(uname_S),NONSTOP_KERNEL)

	BASIC_CFLAGS += -DMAX_IO_SIZE=(32*1024)

Does this work for you ?

Of course it would, but,
a) 32k is smaller than we can go (and yes, we could make it 52k)

Sorry, I missed that: (52*1024)

Quoted 5 lines

b) never ever should read() be asked to read more than SSIZE_MAX, this should be true for every platform on the planet? You may want to have is smaller than SSIZE_MAX (like the current 8MB vs. the possible 2TB on Linux), but surely never larger?

Good question.
I don't know every platform of the planet well enough to be helpful here, especially the ones which don't follow all the specifications.

In other words: As long as we can not guarantee that SSIZE_MAX is defined, (and is defined to somethong useful for xread()/xwrite() )
we should be more defensive here:

tweak only on platform where we know it is needed and we know that it works.

And leave the other ones alone, until someone finds another platform which needs the same or another tweak and sends a tested patch.

Thanks for the report, do you want to send a patch to the list ?

#7 lore raw
Quoted 8 lines · Torsten Bögershausen

I don't know every platform of the planet well enough to be helpful here, especially the ones which don't follow all the specifications.

In other words: As long as we can not guarantee that SSIZE_MAX is defined, (and is defined to somethong useful for xread()/xwrite() )
we should be more defensive here:

tweak only on platform where we know it is needed and we know that it works.

Yup, I agree that is a sensible way to go.

(1) if Makefile overrides the size, use it; otherwise
(2) if SSIZE_MAX is defined, and it is smaller than our internal
default, use it; otherwise
(3) use our internal default.

And leave our internal default to 8MB.

That way, nobody needs to do anything differently from his current build set-up, and I suspect that it would make step (1) optional.

#8 lore raw
Quoted 2 lines · Junio C Hamano <gitster <at> pobox.com> writes:

On Sat, Feb 7, 2015 at 12:32 PM, Torsten Bögershausen <tboegi <at>

Quoted 1 line · web.de> wrote:
Quoted 1 line

I don't know every platform of the planet well enough to be helpful

here,

Quoted 3 lines
Quoted 3 lines

especially the ones which don't follow all the specifications.

In other words: As long as we can not guarantee that SSIZE_MAX is

defined,

Quoted 4 lines
Quoted 4 lines

(and is defined to somethong useful for xread()/xwrite() ) we should be more defensive here:

tweak only on platform where we know it is needed and we know that it

works.

Quoted 11 lines

Yup, I agree that is a sensible way to go.

(1) if Makefile overrides the size, use it; otherwise
(2) if SSIZE_MAX is defined, and it is smaller than our internal
default, use it; otherwise
(3) use our internal default.

And leave our internal default to 8MB.

That way, nobody needs to do anything differently from his current build

set-up,

Quoted 2 lines

and I suspect that it would make step (1) optional.

something like this:

/* allow overwriting from e.g. Makefile */
#if !defined(MAX_IO_SIZE)

# define MAX_IO_SIZE (8*1024*1024)
#endif
/* for plattforms that have SSIZE and have it smaller */
#if defined(SSIZE_MAX && (SSIZE_MAX < MAX_IO_SIZE) 
# undef MAX_IO_SIZE /* avoid warning */
# define MAX_IO_SIZE SSIZE_MAX
#endif

Steps 2 and 3 only , indeed step 1 not needed...

Bye, Jojo

#9 lore raw
Quoted 19 lines · Joachim Schmitz
Quoted 11 lines · Junio C Hamano <gitster <at> pobox.com> writes:

Yup, I agree that is a sensible way to go.

(1) if Makefile overrides the size, use it; otherwise
(2) if SSIZE_MAX is defined, and it is smaller than our internal
default, use it; otherwise
(3) use our internal default.

And leave our internal default to 8MB.

That way, nobody needs to do anything differently from his current build

set-up,

Quoted 1 line

and I suspect that it would make step (1) optional.

something like this:

/* allow overwriting from e.g. Makefile */
#if !defined(MAX_IO_SIZE)

Quoted 7 lines
# define MAX_IO_SIZE (8*1024*1024)
#endif
/* for plattforms that have SSIZE and have it smaller */
#if defined(SSIZE_MAX && (SSIZE_MAX < MAX_IO_SIZE)
# undef MAX_IO_SIZE /* avoid warning */
# define MAX_IO_SIZE SSIZE_MAX
#endif

No, not like that. If you do (1), that is only so that the Makefile can override a broken definition a platform may give to SSIZE_MAX. So

(1) if Makefile gives one, use it without second-guessing with SSIZE_MAX.
(2) if SSIZE_MAX is defined, and if it is smaller than our internal
default, use it.
(3) all other cases, us our internal default.

#10 lore raw
Quoted 24 lines · Junio C Hamano
Quoted 16 lines · Joachim Schmitz
Quoted 8 lines · Junio C Hamano <gitster <at> pobox.com> writes:

Yup, I agree that is a sensible way to go.

(1) if Makefile overrides the size, use it; otherwise
(2) if SSIZE_MAX is defined, and it is smaller than our internal
default, use it; otherwise
(3) use our internal default.

Quoted 4 lines

And leave our internal default to 8MB.

That way, nobody needs to do anything differently from his current build

set-up,

Quoted 1 line

and I suspect that it would make step (1) optional.

Quoted 7 lines

something like this:

/* allow overwriting from e.g. Makefile */ #if !defined(MAX_IO_SIZE) # define MAX_IO_SIZE (8*1024*1024) #endif
/* for plattforms that have SSIZE and have it smaller */ #if defined(SSIZE_MAX && (SSIZE_MAX < MAX_IO_SIZE) # undef MAX_IO_SIZE /* avoid warning */ # define MAX_IO_SIZE SSIZE_MAX #endif

Quoted 4 lines

No, not like that. If you do (1), that is only so that the Makefile can override a broken definition a platform may give to SSIZE_MAX. So
(1) if Makefile gives one, use it without second-guessing with SSIZE_MAX.
(2) if SSIZE_MAX is defined, and if it is smaller than our internal default, use it.
(3) all other cases, us our internal default.

That is reasonable. I am more concerned about our git-upload-pak (separate thread) anyway :)

Cheers, Randall

#11 lore raw
In reply to Junio C Hamano

Junio C Hamano <gitster <at> pobox.com> writes:

<snip>

Quoted 13 lines
Quoted 5 lines

something like this:

/* allow overwriting from e.g. Makefile */
#if !defined(MAX_IO_SIZE)

Quoted 7 lines
# define MAX_IO_SIZE (8*1024*1024)
#endif
/* for plattforms that have SSIZE and have it smaller */
#if defined(SSIZE_MAX && (SSIZE_MAX < MAX_IO_SIZE)
# undef MAX_IO_SIZE /* avoid warning */
# define MAX_IO_SIZE SSIZE_MAX
#endif
Quoted 1 line

No, not like that. If you do (1), that is only so that the Makefile can

override

Quoted 6 lines

a broken definition a platform may give to SSIZE_MAX. So

(1) if Makefile gives one, use it without second-guessing with SSIZE_MAX.
(2) if SSIZE_MAX is defined, and if it is smaller than our internal
default, use it.
(3) all other cases, us our internal default.

oops, yes, of course

/* allow overwriting from e.g. Makefile */
#ifndef(MAX_IO_SIZE)

# define MAX_IO_SIZE (8*1024*1024)
  /* for plattforms that have SSIZE and have it smaller */
# if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE)
#  undef MAX_IO_SIZE /* avoid warning */
#  define MAX_IO_SIZE SSIZE_MAX
# endif
#endif
#12 lore raw
Quoted 10 lines · Joachim Schmitz
Quoted 4 lines · Junio C Hamano <gitster <at> pobox.com> writes:

(1) if Makefile gives one, use it without second-guessing with SSIZE_MAX.
(2) if SSIZE_MAX is defined, and if it is smaller than our internal
default, use it.
(3) all other cases, us our internal default.

oops, yes, of course

/* allow overwriting from e.g. Makefile */
#ifndef(MAX_IO_SIZE)

Quoted 7 lines
# define MAX_IO_SIZE (8*1024*1024)
  /* for plattforms that have SSIZE and have it smaller */
# if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE)
#  undef MAX_IO_SIZE /* avoid warning */
#  define MAX_IO_SIZE SSIZE_MAX
# endif
#endif

A bit cleaner:

#ifndef(MAX_IO_SIZE)

# define MAX_IO_SIZE_DEFAULT (8*1024*1024)
# if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE_DEFAULT)
#  define MAX_IO_SIZE SSIZE_MAX
# else
#  define MAX_IO_SIZE MAX_IO_SIZE_DEFAULT
# endif
#endif
#13 lore raw
Quoted 10 lines · Eric Sunshine

A bit cleaner:

#ifndef(MAX_IO_SIZE)

# define MAX_IO_SIZE_DEFAULT (8*1024*1024)
# if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE_DEFAULT)
#  define MAX_IO_SIZE SSIZE_MAX
# else
#  define MAX_IO_SIZE MAX_IO_SIZE_DEFAULT
# endif
#endif

OK, then let's do this.

-- >8 --
Subject: xread/xwrite: clip MAX_IO_SIZE to SSIZE_MAX

Since 0b6806b9 (xread, xwrite: limit size of IO to 8MB, 2013-08-20), we chomp our calls to read(2) and write(2) into chunks of MAX_IO_SIZE bytes (8 MiB), because a large IO results in a bad latency when the program needs to be killed. This also brought our IO below SSIZE_MAX, which is a limit POSIX allows read(2) and write(2) to fail when the IO size exceeds it, for OS X, where a problem was originally reported.

However, there are other systems that define SSIZE_MAX smaller than our default X-<. Make sure we clip our calls to this as well.

---
 wrapper.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
wrapper.c +14 -1
index 007ec0d..50e6697 100644
@@ -172,8 +172,21 @@ void *xcalloc(size_t nmemb, size_t size)
  * 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in
  * the absence of bugs, large chunks can result in bad latencies when
  * you decide to kill the process.
+ *
+ * We pick 8 MiB as our default, but if the platform defines SSIZE_MAX
+ * that is smaller than that, clip it to SSIZE_MAX, as a call to
+ * read(2) or write(2) larger than taht is allowed to fail.  As the last
+ * resort, we allow a port to pass via CFLAGS e.g. "-DMAX_IO_SIZE=value"
+ * to override this, if the definition of SSIZE_MAX platform is broken.
  */
-#define MAX_IO_SIZE (8*1024*1024)
+#ifndef(MAX_IO_SIZE)
+# define MAX_IO_SIZE_DEFAULT (8*1024*1024)
+# if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE_DEFAULT)
+#  define MAX_IO_SIZE SSIZE_MAX
+# else
+#  define MAX_IO_SIZE MAX_IO_SIZE_DEFAULT
+# endif
+#endif
 
 /*
  * xread() is the same a read(), but it automatically restarts read()
#14 lore raw

Junio C Hamano <gitster <at> pobox.com> writes:

<snip>

Quoted 2 lines

OK, then let's do this.

Yep, that'd do, thanks.

bye, Jojo

#15 lore raw
Quoted 6 lines · Joachim Schmitz <jojo <at> schmitz-digital.de> writes:

Junio C Hamano <gitster <at> pobox.com> writes:

<snip>

Quoted 2 lines

OK, then let's do this.

Except for the type "taht"

#16 lore raw
Quoted 11 lines · Joachim Schmitz
Quoted 6 lines · Joachim Schmitz <jojo <at> schmitz-digital.de> writes:

Junio C Hamano <gitster <at> pobox.com> writes:

<snip>

Quoted 2 lines

OK, then let's do this.

Except for the type "taht"

Also #ifndef part X-<

Here is what I queued for the day.

-- >8 --
Subject: xread/xwrite: clip MAX_IO_SIZE to SSIZE_MAX

Since 0b6806b9 (xread, xwrite: limit size of IO to 8MB, 2013-08-20), we chomp our calls to read(2) and write(2) into chunks of MAX_IO_SIZE bytes (8 MiB), because a large IO results in a bad latency when the program needs to be killed. This also brought our IO below SSIZE_MAX, which is a limit POSIX allows read(2) and write(2) to fail when the IO size exceeds it, for OS X, where a problem was originally reported.

However, there are other systems that define SSIZE_MAX smaller than our default, and feeding 8 MiB to underlying read(2)/write(2) would fail. Make sure we clip our calls to the lower limit as well.

---
 wrapper.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
wrapper.c +14 -1
index f92b147..c77c2eb 100644
@@ -135,8 +135,21 @@ void *xcalloc(size_t nmemb, size_t size)
  * 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in
  * the absense of bugs, large chunks can result in bad latencies when
  * you decide to kill the process.
+ *
+ * We pick 8 MiB as our default, but if the platform defines SSIZE_MAX
+ * that is smaller than that, clip it to SSIZE_MAX, as a call to
+ * read(2) or write(2) larger than that is allowed to fail.  As the last
+ * resort, we allow a port to pass via CFLAGS e.g. "-DMAX_IO_SIZE=value"
+ * to override this, if the definition of SSIZE_MAX platform is broken.
  */
-#define MAX_IO_SIZE (8*1024*1024)
+#ifndef MAX_IO_SIZE
+# define MAX_IO_SIZE_DEFAULT (8*1024*1024)
+# if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE_DEFAULT)
+#  define MAX_IO_SIZE SSIZE_MAX
+# else
+#  define MAX_IO_SIZE MAX_IO_SIZE_DEFAULT
+# endif
+#endif
 
 /*
  * xread() is the same a read(), but it automatically restarts read()
Signature · 1 line
2.3.0-186-g9f73ee1
#17 lore raw
In reply to Joachim Schmitz
Quoted 5 lines · Joachim Schmitz <jojo <at> schmitz-digital.de> writes:
Quoted 2 lines · Torsten Bögershausen <tboegi <at> web.de> writes:

On 2015-02-07 17.45, Joachim Schmitz wrote:

<snip>

Quoted 4 lines

b) never ever should read() be asked to read more than SSIZE_MAX, this should be true for every platform on the planet? You may want to have is smaller than SSIZE_MAX (like the current 8MB vs. the possible 2TB on Linux), but surely never larger?

Se also $gmane/232469, where that issue cropped up for MacOS X 64bit?

Bye, Jojo

#19 lore raw
In reply to Joachim Schmitz

Joachim Schmitz <jojo <at> schmitz-digital.de> writes:

<snip>

Quoted 2 lines

and as a (rather strange)
consequence mmap() (from compat/mmap.c) fails with EACCESS (why

EACCESS?),

Quoted 1 line

because xpread() returns something > 0.

Seems mmap() should either set errno to EINVAL or not set it at all an just 'forward' whatever xpread() has set.

As per http://man7.org/linux/man-pages/man2/mmap.2.html mmap() sets EINVAL if (amongst other things) it doesn't like the value of len, exactly the case here.

bye, Jojo