All of lore.kernel.org
 help / color / mirror / Atom feed
* [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros
@ 2014-07-16 14:32 Andrew Cooper
  2014-07-16 14:32 ` [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact() Andrew Cooper
  2014-07-17 14:15 ` [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros Ian Campbell
  0 siblings, 2 replies; 10+ messages in thread
From: Andrew Cooper @ 2014-07-16 14:32 UTC (permalink / raw)
  To: Xen-devel; +Cc: Andrew Cooper, Ian Jackson, Ian Campbell

Move the typesafe min() macro from xc_dom_decompress_unsafe_xz.c to
xc_private.h, and complement it with an equivalent max() macro.

Replace current users of type unsafe MIN()/MAX() macros, and remove their
scattered definitions.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
CC: Ian Campbell <Ian.Campbell@citrix.com>
CC: Ian Jackson <Ian.Jackson@eu.citrix.com>

---
v3: Rebase on top of min_t()/max_t() changes.
---
 tools/libxc/xc_core_x86.c                 |    4 ----
 tools/libxc/xc_dom_decompress_unsafe_xz.c |    6 ------
 tools/libxc/xc_domain_restore.c           |    2 +-
 tools/libxc/xc_private.h                  |   11 +++++++++++
 tools/libxc/xg_private.h                  |    2 +-
 tools/libxc/xg_save_restore.h             |    7 -------
 6 files changed, 13 insertions(+), 19 deletions(-)

diff --git a/tools/libxc/xc_core_x86.c b/tools/libxc/xc_core_x86.c
index e328dcf..f05060a 100644
--- a/tools/libxc/xc_core_x86.c
+++ b/tools/libxc/xc_core_x86.c
@@ -24,10 +24,6 @@
 
 #define GET_FIELD(_p, _f) ((dinfo->guest_width==8) ? ((_p)->x64._f) : ((_p)->x32._f))
 
-#ifndef MAX
-#define MAX(_a, _b) ((_a) >= (_b) ? (_a) : (_b))
-#endif
-
 int
 xc_core_arch_gpfn_may_present(struct xc_core_arch_context *arch_ctxt,
                               unsigned long pfn)
diff --git a/tools/libxc/xc_dom_decompress_unsafe_xz.c b/tools/libxc/xc_dom_decompress_unsafe_xz.c
index 6be1f89..fe7a7f4 100644
--- a/tools/libxc/xc_dom_decompress_unsafe_xz.c
+++ b/tools/libxc/xc_dom_decompress_unsafe_xz.c
@@ -34,12 +34,6 @@ static inline u32 le32_to_cpup(const u32 *p)
 	return cpu_to_le32(*p);
 }
 
-#define min(x,y) ({ \
-        const typeof(x) _x = (x);       \
-        const typeof(y) _y = (y);       \
-        (void) (&_x == &_y);            \
-        _x < _y ? _x : _y; })
-
 #define __force
 #define always_inline
 
diff --git a/tools/libxc/xc_domain_restore.c b/tools/libxc/xc_domain_restore.c
index 071ab6a..e73e0a2 100644
--- a/tools/libxc/xc_domain_restore.c
+++ b/tools/libxc/xc_domain_restore.c
@@ -339,7 +339,7 @@ static xen_pfn_t *load_p2m_frame_list(
             /* Any remaining bytes of this chunk: read and discard. */
             while ( chunk_bytes )
             {
-                unsigned long sz = MIN(chunk_bytes, sizeof(xen_pfn_t));
+                unsigned long sz = min_t(unsigned long, chunk_bytes, sizeof(xen_pfn_t));
                 if ( RDEXACT(io_fd, &p2m_fl_zero, sz) )
                 {
                     PERROR("read-and-discard extended-info chunk bytes failed");
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index c240a7c..ce7e81e 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -349,6 +349,17 @@ int xc_ffs16(uint16_t x);
 int xc_ffs32(uint32_t x);
 int xc_ffs64(uint64_t x);
 
+#define min(X, Y) ({                             \
+            const typeof (X) _x = (X);           \
+            const typeof (Y) _y = (Y);           \
+            (void) (&_x == &_y);                 \
+            (_x < _y) ? _x : _y; })
+#define max(X, Y) ({                             \
+            const typeof (X) _x = (X);           \
+            const typeof (Y) _y = (Y);           \
+            (void) (&_x == &_y);                 \
+            (_x > _y) ? _x : _y; })
+
 #define min_t(type,x,y) \
         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
 #define max_t(type,x,y) \
diff --git a/tools/libxc/xg_private.h b/tools/libxc/xg_private.h
index e593364..1910361 100644
--- a/tools/libxc/xg_private.h
+++ b/tools/libxc/xg_private.h
@@ -159,7 +159,7 @@ static inline xen_pfn_t xc_pfn_to_mfn(xen_pfn_t pfn, xen_pfn_t *p2m,
 /* Size in bytes of the pfn_to_mfn_frame_list     */
 #define P2M_GUEST_FL_SIZE ((P2M_FL_ENTRIES) * (dinfo->guest_width))
 #define P2M_TOOLS_FL_SIZE ((P2M_FL_ENTRIES) *                           \
-                           MAX((sizeof (xen_pfn_t)), dinfo->guest_width))
+                           max_t(size_t, sizeof(xen_pfn_t), dinfo->guest_width))
 
 /* Masks for PTE<->PFN conversions */
 #define MADDR_BITS_X86  ((dinfo->guest_width == 8) ? 52 : 44)
diff --git a/tools/libxc/xg_save_restore.h b/tools/libxc/xg_save_restore.h
index aa93c13..bdd9009 100644
--- a/tools/libxc/xg_save_restore.h
+++ b/tools/libxc/xg_save_restore.h
@@ -392,10 +392,3 @@ static inline int get_platform_info(xc_interface *xch, uint32_t dom,
     else                                                           \
         memset(&(_p)->x32._f[0], (_v), sizeof((_p)->x32._f));      \
 } while (0)
-
-#ifndef MAX
-#define MAX(_a, _b) ((_a) >= (_b) ? (_a) : (_b))
-#endif
-#ifndef MIN
-#define MIN(_a, _b) ((_a) <= (_b) ? (_a) : (_b))
-#endif
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact()
  2014-07-16 14:32 [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros Andrew Cooper
@ 2014-07-16 14:32 ` Andrew Cooper
  2014-07-18  1:14   ` Wen Congyang
  2014-07-17 14:15 ` [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros Ian Campbell
  1 sibling, 1 reply; 10+ messages in thread
From: Andrew Cooper @ 2014-07-16 14:32 UTC (permalink / raw)
  To: Xen-devel; +Cc: Andrew Cooper, Ian Jackson, Ian Campbell

This implementation of writev_exact() will cope with an iovcnt greater than
IOV_MAX because glibc will actually let this work anyway, and it is very
useful not to have to work about this in the caller of writev_exact().  The
caller is still required to ensure that the sum of iov_len's doesn't overflow
a ssize_t.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
CC: Ian Campbell <Ian.Campbell@citrix.com>
CC: Ian Jackson <Ian.Jackson@eu.citrix.com>

---
v3:
 * Re-add adjustment for partial writes.
 * Split min/max adjustment into separate patch.

v2:
 * Remove adjustment for partial writes of a specific iov[] entry.
---
 tools/libxc/xc_private.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxc/xc_private.h |    2 ++
 2 files changed, 62 insertions(+)

diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
index 1c214dd..0941b06 100644
--- a/tools/libxc/xc_private.c
+++ b/tools/libxc/xc_private.c
@@ -858,6 +858,66 @@ int write_exact(int fd, const void *data, size_t size)
     return 0;
 }
 
+int writev_exact(int fd, const struct iovec *iov, int iovcnt)
+{
+    struct iovec *local_iov = NULL;
+    int rc = 0, iov_idx = 0, saved_errno = 0;
+    ssize_t len;
+
+    while ( iov_idx < iovcnt )
+    {
+        /* Skip over iov[] entries with 0 length. */
+        while ( iov[iov_idx].iov_len == 0 )
+            if ( ++iov_idx == iovcnt )
+                goto out;
+
+        len = writev(fd, &iov[iov_idx], min(iovcnt - iov_idx, IOV_MAX));
+        saved_errno = errno;
+
+        if ( (len == -1) && (errno == EINTR) )
+            continue;
+        if ( len <= 0 )
+        {
+            rc = -1;
+            goto out;
+        }
+
+        /* Check iov[] to see whether we had a partial or complete write. */
+        while ( len > 0 && (iov_idx < iovcnt) )
+        {
+            if ( len >= iov[iov_idx].iov_len )
+                len -= iov[iov_idx++].iov_len;
+            else
+            {
+                /* Partial write of iov[iov_idx]. Copy iov so we can adjust
+                 * element iov_idx and resubmit the rest. */
+                if ( !local_iov )
+                {
+                    local_iov = malloc(iovcnt * sizeof(*iov));
+                    if ( !local_iov )
+                    {
+                        saved_errno = ENOMEM;
+                        goto out;
+                    }
+
+                    iov = memcpy(local_iov, iov, iovcnt * sizeof(*iov));
+                }
+
+                local_iov[iov_idx].iov_base += len;
+                local_iov[iov_idx].iov_len  -= len;
+                break;
+            }
+        }
+    }
+
+    saved_errno = 0;
+
+ out:
+    free(local_iov);
+    errno = saved_errno;
+    return rc;
+}
+
 int xc_ffs8(uint8_t x)
 {
     int i;
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index ce7e81e..1f06166 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <sys/ioctl.h>
+#include <sys/uio.h>
 
 #include "xenctrl.h"
 #include "xenctrlosdep.h"
@@ -343,6 +344,7 @@ int xc_flush_mmu_updates(xc_interface *xch, struct xc_mmu *mmu);
 /* Return 0 on success; -1 on error setting errno. */
 int read_exact(int fd, void *data, size_t size); /* EOF => -1, errno=0 */
 int write_exact(int fd, const void *data, size_t size);
+int writev_exact(int fd, const struct iovec *iov, int iovcnt);
 
 int xc_ffs8(uint8_t x);
 int xc_ffs16(uint16_t x);
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros
  2014-07-16 14:32 [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros Andrew Cooper
  2014-07-16 14:32 ` [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact() Andrew Cooper
@ 2014-07-17 14:15 ` Ian Campbell
  2014-07-18 12:43   ` Ian Campbell
  1 sibling, 1 reply; 10+ messages in thread
From: Ian Campbell @ 2014-07-17 14:15 UTC (permalink / raw)
  To: Andrew Cooper; +Cc: Ian Jackson, Xen-devel

On Wed, 2014-07-16 at 15:32 +0100, Andrew Cooper wrote:
> Move the typesafe min() macro from xc_dom_decompress_unsafe_xz.c to
> xc_private.h, and complement it with an equivalent max() macro.
> 
> Replace current users of type unsafe MIN()/MAX() macros, and remove their
> scattered definitions.
> 
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>

Acked-by: Ian Campbell <Ian.Campbell@citrix.com>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact()
  2014-07-16 14:32 ` [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact() Andrew Cooper
@ 2014-07-18  1:14   ` Wen Congyang
  2014-07-18  9:20     ` Andrew Cooper
  0 siblings, 1 reply; 10+ messages in thread
From: Wen Congyang @ 2014-07-18  1:14 UTC (permalink / raw)
  To: Andrew Cooper, Xen-devel; +Cc: Ian Jackson, Ian Campbell

At 07/16/2014 10:32 PM, Andrew Cooper Wrote:
> This implementation of writev_exact() will cope with an iovcnt greater than
> IOV_MAX because glibc will actually let this work anyway, and it is very
> useful not to have to work about this in the caller of writev_exact().  The
> caller is still required to ensure that the sum of iov_len's doesn't overflow
> a ssize_t.
> 
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
> CC: Ian Campbell <Ian.Campbell@citrix.com>
> CC: Ian Jackson <Ian.Jackson@eu.citrix.com>
> 
> ---
> v3:
>  * Re-add adjustment for partial writes.
>  * Split min/max adjustment into separate patch.
> 
> v2:
>  * Remove adjustment for partial writes of a specific iov[] entry.
> ---
>  tools/libxc/xc_private.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++
>  tools/libxc/xc_private.h |    2 ++
>  2 files changed, 62 insertions(+)
> 
> diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
> index 1c214dd..0941b06 100644
> --- a/tools/libxc/xc_private.c
> +++ b/tools/libxc/xc_private.c
> @@ -858,6 +858,66 @@ int write_exact(int fd, const void *data, size_t size)
>      return 0;
>  }
>  
> +int writev_exact(int fd, const struct iovec *iov, int iovcnt)
> +{
> +    struct iovec *local_iov = NULL;
> +    int rc = 0, iov_idx = 0, saved_errno = 0;
> +    ssize_t len;
> +
> +    while ( iov_idx < iovcnt )
> +    {
> +        /* Skip over iov[] entries with 0 length. */
> +        while ( iov[iov_idx].iov_len == 0 )
> +            if ( ++iov_idx == iovcnt )
> +                goto out;

set saved_errn to 0 before goto out?

> +
> +        len = writev(fd, &iov[iov_idx], min(iovcnt - iov_idx, IOV_MAX));
> +        saved_errno = errno;
> +
> +        if ( (len == -1) && (errno == EINTR) )
> +            continue;
> +        if ( len <= 0 )
> +        {
> +            rc = -1;
> +            goto out;
> +        }
> +
> +        /* Check iov[] to see whether we had a partial or complete write. */
> +        while ( len > 0 && (iov_idx < iovcnt) )
> +        {
> +            if ( len >= iov[iov_idx].iov_len )
> +                len -= iov[iov_idx++].iov_len;
> +            else
> +            {
> +                /* Partial write of iov[iov_idx]. Copy iov so we can adjust
> +                 * element iov_idx and resubmit the rest. */
> +                if ( !local_iov )
> +                {
> +                    local_iov = malloc(iovcnt * sizeof(*iov));
> +                    if ( !local_iov )
> +                    {
> +                        saved_errno = ENOMEM;
> +                        goto out;
> +                    }
> +
> +                    iov = memcpy(local_iov, iov, iovcnt * sizeof(*iov));
> +                }
> +
> +                local_iov[iov_idx].iov_base += len;
> +                local_iov[iov_idx].iov_len  -= len;
> +                break;
> +            }
> +        }
> +    }
> +
> +    saved_errno = 0;
> +
> + out:
> +    free(local_iov);
> +    errno = saved_errno;
> +    return rc;
> +}
> +
>  int xc_ffs8(uint8_t x)
>  {
>      int i;
> diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
> index ce7e81e..1f06166 100644
> --- a/tools/libxc/xc_private.h
> +++ b/tools/libxc/xc_private.h
> @@ -28,6 +28,7 @@
>  #include <sys/stat.h>
>  #include <stdlib.h>
>  #include <sys/ioctl.h>
> +#include <sys/uio.h>
>  
>  #include "xenctrl.h"
>  #include "xenctrlosdep.h"
> @@ -343,6 +344,7 @@ int xc_flush_mmu_updates(xc_interface *xch, struct xc_mmu *mmu);
>  /* Return 0 on success; -1 on error setting errno. */
>  int read_exact(int fd, void *data, size_t size); /* EOF => -1, errno=0 */
>  int write_exact(int fd, const void *data, size_t size);
> +int writev_exact(int fd, const struct iovec *iov, int iovcnt);
>  
>  int xc_ffs8(uint8_t x);
>  int xc_ffs16(uint16_t x);
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact()
  2014-07-18  1:14   ` Wen Congyang
@ 2014-07-18  9:20     ` Andrew Cooper
  2014-07-18  9:31       ` [PATCH v4 " Andrew Cooper
  2014-07-18  9:53       ` [Patch v3 " Ian Campbell
  0 siblings, 2 replies; 10+ messages in thread
From: Andrew Cooper @ 2014-07-18  9:20 UTC (permalink / raw)
  To: Wen Congyang, Xen-devel; +Cc: Ian Jackson, Ian Campbell

On 18/07/14 02:14, Wen Congyang wrote:
> At 07/16/2014 10:32 PM, Andrew Cooper Wrote:
>> This implementation of writev_exact() will cope with an iovcnt greater than
>> IOV_MAX because glibc will actually let this work anyway, and it is very
>> useful not to have to work about this in the caller of writev_exact().  The
>> caller is still required to ensure that the sum of iov_len's doesn't overflow
>> a ssize_t.
>>
>> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
>> CC: Ian Campbell <Ian.Campbell@citrix.com>
>> CC: Ian Jackson <Ian.Jackson@eu.citrix.com>
>>
>> ---
>> v3:
>>  * Re-add adjustment for partial writes.
>>  * Split min/max adjustment into separate patch.
>>
>> v2:
>>  * Remove adjustment for partial writes of a specific iov[] entry.
>> ---
>>  tools/libxc/xc_private.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++
>>  tools/libxc/xc_private.h |    2 ++
>>  2 files changed, 62 insertions(+)
>>
>> diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
>> index 1c214dd..0941b06 100644
>> --- a/tools/libxc/xc_private.c
>> +++ b/tools/libxc/xc_private.c
>> @@ -858,6 +858,66 @@ int write_exact(int fd, const void *data, size_t size)
>>      return 0;
>>  }
>>  
>> +int writev_exact(int fd, const struct iovec *iov, int iovcnt)
>> +{
>> +    struct iovec *local_iov = NULL;
>> +    int rc = 0, iov_idx = 0, saved_errno = 0;
>> +    ssize_t len;
>> +
>> +    while ( iov_idx < iovcnt )
>> +    {
>> +        /* Skip over iov[] entries with 0 length. */
>> +        while ( iov[iov_idx].iov_len == 0 )
>> +            if ( ++iov_idx == iovcnt )
>> +                goto out;
> set saved_errn to 0 before goto out?

Good catch.

~Andrew

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v4 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact()
  2014-07-18  9:20     ` Andrew Cooper
@ 2014-07-18  9:31       ` Andrew Cooper
  2014-07-18  9:53       ` [Patch v3 " Ian Campbell
  1 sibling, 0 replies; 10+ messages in thread
From: Andrew Cooper @ 2014-07-18  9:31 UTC (permalink / raw)
  To: Xen-devel; +Cc: Andrew Cooper, Ian Jackson, Ian Campbell

This implementation of writev_exact() will cope with an iovcnt greater than
IOV_MAX because glibc will actually let this work anyway, and it is very
useful not to have to work about this in the caller of writev_exact().  The
caller is still required to ensure that the sum of iov_len's doesn't overflow
a ssize_t.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
CC: Ian Campbell <Ian.Campbell@citrix.com>
CC: Ian Jackson <Ian.Jackson@eu.citrix.com>

---
v4:
 * Set errno to 0 when exiting due to 0-length iov[]s.
v3:
 * Re-add adjustment for partial writes.
 * Split min/max adjustment into separate patch.
v2:
 * Remove adjustment for partial writes of a specific iov[] entry.
---
 tools/libxc/xc_private.c |   63 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxc/xc_private.h |    2 ++
 2 files changed, 65 insertions(+)

diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
index 1c214dd..338dc5a 100644
--- a/tools/libxc/xc_private.c
+++ b/tools/libxc/xc_private.c
@@ -858,6 +858,69 @@ int write_exact(int fd, const void *data, size_t size)
     return 0;
 }
 
+int writev_exact(int fd, const struct iovec *iov, int iovcnt)
+{
+    struct iovec *local_iov = NULL;
+    int rc = 0, iov_idx = 0, saved_errno = 0;
+    ssize_t len;
+
+    while ( iov_idx < iovcnt )
+    {
+        /* Skip over iov[] entries with 0 length. */
+        while ( iov[iov_idx].iov_len == 0 )
+            if ( ++iov_idx == iovcnt )
+            {
+                saved_errno = 0;
+                goto out;
+            }
+
+        len = writev(fd, &iov[iov_idx], min(iovcnt - iov_idx, IOV_MAX));
+        saved_errno = errno;
+
+        if ( (len == -1) && (errno == EINTR) )
+            continue;
+        if ( len <= 0 )
+        {
+            rc = -1;
+            goto out;
+        }
+
+        /* Check iov[] to see whether we had a partial or complete write. */
+        while ( len > 0 && (iov_idx < iovcnt) )
+        {
+            if ( len >= iov[iov_idx].iov_len )
+                len -= iov[iov_idx++].iov_len;
+            else
+            {
+                /* Partial write of iov[iov_idx]. Copy iov so we can adjust
+                 * element iov_idx and resubmit the rest. */
+                if ( !local_iov )
+                {
+                    local_iov = malloc(iovcnt * sizeof(*iov));
+                    if ( !local_iov )
+                    {
+                        saved_errno = ENOMEM;
+                        goto out;
+                    }
+
+                    iov = memcpy(local_iov, iov, iovcnt * sizeof(*iov));
+                }
+
+                local_iov[iov_idx].iov_base += len;
+                local_iov[iov_idx].iov_len  -= len;
+                break;
+            }
+        }
+    }
+
+    saved_errno = 0;
+
+ out:
+    free(local_iov);
+    errno = saved_errno;
+    return rc;
+}
+
 int xc_ffs8(uint8_t x)
 {
     int i;
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index ce7e81e..1f06166 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <sys/ioctl.h>
+#include <sys/uio.h>
 
 #include "xenctrl.h"
 #include "xenctrlosdep.h"
@@ -343,6 +344,7 @@ int xc_flush_mmu_updates(xc_interface *xch, struct xc_mmu *mmu);
 /* Return 0 on success; -1 on error setting errno. */
 int read_exact(int fd, void *data, size_t size); /* EOF => -1, errno=0 */
 int write_exact(int fd, const void *data, size_t size);
+int writev_exact(int fd, const struct iovec *iov, int iovcnt);
 
 int xc_ffs8(uint8_t x);
 int xc_ffs16(uint16_t x);
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact()
  2014-07-18  9:20     ` Andrew Cooper
  2014-07-18  9:31       ` [PATCH v4 " Andrew Cooper
@ 2014-07-18  9:53       ` Ian Campbell
  2014-07-18 10:05         ` Wen Congyang
  1 sibling, 1 reply; 10+ messages in thread
From: Ian Campbell @ 2014-07-18  9:53 UTC (permalink / raw)
  To: Andrew Cooper; +Cc: Ian Jackson, Wen Congyang, Xen-devel

On Fri, 2014-07-18 at 10:20 +0100, Andrew Cooper wrote:
> On 18/07/14 02:14, Wen Congyang wrote:
> > At 07/16/2014 10:32 PM, Andrew Cooper Wrote:
> >> This implementation of writev_exact() will cope with an iovcnt greater than
> >> IOV_MAX because glibc will actually let this work anyway, and it is very
> >> useful not to have to work about this in the caller of writev_exact().  The
> >> caller is still required to ensure that the sum of iov_len's doesn't overflow
> >> a ssize_t.
> >>
> >> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
> >> CC: Ian Campbell <Ian.Campbell@citrix.com>
> >> CC: Ian Jackson <Ian.Jackson@eu.citrix.com>
> >>
> >> ---
> >> v3:
> >>  * Re-add adjustment for partial writes.
> >>  * Split min/max adjustment into separate patch.
> >>
> >> v2:
> >>  * Remove adjustment for partial writes of a specific iov[] entry.
> >> ---
> >>  tools/libxc/xc_private.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++
> >>  tools/libxc/xc_private.h |    2 ++
> >>  2 files changed, 62 insertions(+)
> >>
> >> diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
> >> index 1c214dd..0941b06 100644
> >> --- a/tools/libxc/xc_private.c
> >> +++ b/tools/libxc/xc_private.c
> >> @@ -858,6 +858,66 @@ int write_exact(int fd, const void *data, size_t size)
> >>      return 0;
> >>  }
> >>  
> >> +int writev_exact(int fd, const struct iovec *iov, int iovcnt)
> >> +{
> >> +    struct iovec *local_iov = NULL;
> >> +    int rc = 0, iov_idx = 0, saved_errno = 0;
> >> +    ssize_t len;
> >> +
> >> +    while ( iov_idx < iovcnt )
> >> +    {
> >> +        /* Skip over iov[] entries with 0 length. */
> >> +        while ( iov[iov_idx].iov_len == 0 )
> >> +            if ( ++iov_idx == iovcnt )
> >> +                goto out;
> > set saved_errn to 0 before goto out?
> 
> Good catch.

Isn't this a success path? errno is generally undefined on success.

Ian.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact()
  2014-07-18  9:53       ` [Patch v3 " Ian Campbell
@ 2014-07-18 10:05         ` Wen Congyang
  2014-07-18 12:32           ` Andrew Cooper
  0 siblings, 1 reply; 10+ messages in thread
From: Wen Congyang @ 2014-07-18 10:05 UTC (permalink / raw)
  To: Ian Campbell, Andrew Cooper; +Cc: Ian Jackson, Xen-devel

At 07/18/2014 05:53 PM, Ian Campbell Wrote:
> On Fri, 2014-07-18 at 10:20 +0100, Andrew Cooper wrote:
>> On 18/07/14 02:14, Wen Congyang wrote:
>>> At 07/16/2014 10:32 PM, Andrew Cooper Wrote:
>>>> This implementation of writev_exact() will cope with an iovcnt greater than
>>>> IOV_MAX because glibc will actually let this work anyway, and it is very
>>>> useful not to have to work about this in the caller of writev_exact().  The
>>>> caller is still required to ensure that the sum of iov_len's doesn't overflow
>>>> a ssize_t.
>>>>
>>>> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
>>>> CC: Ian Campbell <Ian.Campbell@citrix.com>
>>>> CC: Ian Jackson <Ian.Jackson@eu.citrix.com>
>>>>
>>>> ---
>>>> v3:
>>>>  * Re-add adjustment for partial writes.
>>>>  * Split min/max adjustment into separate patch.
>>>>
>>>> v2:
>>>>  * Remove adjustment for partial writes of a specific iov[] entry.
>>>> ---
>>>>  tools/libxc/xc_private.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>  tools/libxc/xc_private.h |    2 ++
>>>>  2 files changed, 62 insertions(+)
>>>>
>>>> diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
>>>> index 1c214dd..0941b06 100644
>>>> --- a/tools/libxc/xc_private.c
>>>> +++ b/tools/libxc/xc_private.c
>>>> @@ -858,6 +858,66 @@ int write_exact(int fd, const void *data, size_t size)
>>>>      return 0;
>>>>  }
>>>>  
>>>> +int writev_exact(int fd, const struct iovec *iov, int iovcnt)
>>>> +{
>>>> +    struct iovec *local_iov = NULL;
>>>> +    int rc = 0, iov_idx = 0, saved_errno = 0;
>>>> +    ssize_t len;
>>>> +
>>>> +    while ( iov_idx < iovcnt )
>>>> +    {
>>>> +        /* Skip over iov[] entries with 0 length. */
>>>> +        while ( iov[iov_idx].iov_len == 0 )
>>>> +            if ( ++iov_idx == iovcnt )
>>>> +                goto out;
>>> set saved_errn to 0 before goto out?
>>
>> Good catch.
> 
> Isn't this a success path? errno is generally undefined on success.

Yes, but we set saved_errno to 0 here:
> +    saved_errno = 0;
> +
> + out:
> +    free(local_iov);
> +    errno = saved_errno;
> +    return rc;
> +}

I think there is no need to save errno in this function, because
we return -1 when writev()/malloc() fails.

Another problem:
> +                    local_iov = malloc(iovcnt * sizeof(*iov));
> +                    if ( !local_iov )
> +                    {
> +                        saved_errno = ENOMEM;
> +                        goto out;
> +                    }
> +
rc is not set to -1 before goto out.

Thanks
Wen Congyang

> 
> Ian.
> 
> .
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact()
  2014-07-18 10:05         ` Wen Congyang
@ 2014-07-18 12:32           ` Andrew Cooper
  0 siblings, 0 replies; 10+ messages in thread
From: Andrew Cooper @ 2014-07-18 12:32 UTC (permalink / raw)
  To: Wen Congyang, Ian Campbell; +Cc: Ian Jackson, Xen-devel

On 18/07/14 11:05, Wen Congyang wrote:
> At 07/18/2014 05:53 PM, Ian Campbell Wrote:
>> On Fri, 2014-07-18 at 10:20 +0100, Andrew Cooper wrote:
>>> On 18/07/14 02:14, Wen Congyang wrote:
>>>> At 07/16/2014 10:32 PM, Andrew Cooper Wrote:
>>>>> This implementation of writev_exact() will cope with an iovcnt greater than
>>>>> IOV_MAX because glibc will actually let this work anyway, and it is very
>>>>> useful not to have to work about this in the caller of writev_exact().  The
>>>>> caller is still required to ensure that the sum of iov_len's doesn't overflow
>>>>> a ssize_t.
>>>>>
>>>>> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
>>>>> CC: Ian Campbell <Ian.Campbell@citrix.com>
>>>>> CC: Ian Jackson <Ian.Jackson@eu.citrix.com>
>>>>>
>>>>> ---
>>>>> v3:
>>>>>  * Re-add adjustment for partial writes.
>>>>>  * Split min/max adjustment into separate patch.
>>>>>
>>>>> v2:
>>>>>  * Remove adjustment for partial writes of a specific iov[] entry.
>>>>> ---
>>>>>  tools/libxc/xc_private.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>  tools/libxc/xc_private.h |    2 ++
>>>>>  2 files changed, 62 insertions(+)
>>>>>
>>>>> diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
>>>>> index 1c214dd..0941b06 100644
>>>>> --- a/tools/libxc/xc_private.c
>>>>> +++ b/tools/libxc/xc_private.c
>>>>> @@ -858,6 +858,66 @@ int write_exact(int fd, const void *data, size_t size)
>>>>>      return 0;
>>>>>  }
>>>>>  
>>>>> +int writev_exact(int fd, const struct iovec *iov, int iovcnt)
>>>>> +{
>>>>> +    struct iovec *local_iov = NULL;
>>>>> +    int rc = 0, iov_idx = 0, saved_errno = 0;
>>>>> +    ssize_t len;
>>>>> +
>>>>> +    while ( iov_idx < iovcnt )
>>>>> +    {
>>>>> +        /* Skip over iov[] entries with 0 length. */
>>>>> +        while ( iov[iov_idx].iov_len == 0 )
>>>>> +            if ( ++iov_idx == iovcnt )
>>>>> +                goto out;
>>>> set saved_errn to 0 before goto out?
>>> Good catch.
>> Isn't this a success path? errno is generally undefined on success.
> Yes, but we set saved_errno to 0 here:
>> +    saved_errno = 0;
>> +
>> + out:
>> +    free(local_iov);
>> +    errno = saved_errno;
>> +    return rc;
>> +}
> I think there is no need to save errno in this function, because
> we return -1 when writev()/malloc() fails.
>
> Another problem:

I am deliberately creating the same (somewhat quirky) error semantics of
write_exact(), for consistency reasons.

The key point is that EOF is -1 and errno of 0.

>> +                    local_iov = malloc(iovcnt * sizeof(*iov));
>> +                    if ( !local_iov )
>> +                    {
>> +                        saved_errno = ENOMEM;
>> +                        goto out;
>> +                    }
>> +
> rc is not set to -1 before goto out.
>
> Thanks
> Wen Congyang

Good point.

~Andrew

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros
  2014-07-17 14:15 ` [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros Ian Campbell
@ 2014-07-18 12:43   ` Ian Campbell
  0 siblings, 0 replies; 10+ messages in thread
From: Ian Campbell @ 2014-07-18 12:43 UTC (permalink / raw)
  To: Andrew Cooper; +Cc: Ian Jackson, Xen-devel


On Thu, 2014-07-17 at 15:15 +0100, Ian Campbell wrote:
> On Wed, 2014-07-16 at 15:32 +0100, Andrew Cooper wrote:
> > Move the typesafe min() macro from xc_dom_decompress_unsafe_xz.c to
> > xc_private.h, and complement it with an equivalent max() macro.
> > 
> > Replace current users of type unsafe MIN()/MAX() macros, and remove their
> > scattered definitions.
> > 
> > Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
> 
> Acked-by: Ian Campbell <Ian.Campbell@citrix.com>

Applied, thanks.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2014-07-18 12:43 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-16 14:32 [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros Andrew Cooper
2014-07-16 14:32 ` [Patch v3 2/2] tools/libxc: Implement writev_exact() in the same style as write_exact() Andrew Cooper
2014-07-18  1:14   ` Wen Congyang
2014-07-18  9:20     ` Andrew Cooper
2014-07-18  9:31       ` [PATCH v4 " Andrew Cooper
2014-07-18  9:53       ` [Patch v3 " Ian Campbell
2014-07-18 10:05         ` Wen Congyang
2014-07-18 12:32           ` Andrew Cooper
2014-07-17 14:15 ` [Patch v3 1/2] tools/libxc: Shuffle definitions and uses of min()/max() macros Ian Campbell
2014-07-18 12:43   ` Ian Campbell

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.