Skip to contentSkip to conversation

libceph: odds and ends

PATCH In review Alex Elder 4 participants 11 messages Apr 21, 2013 – Apr 22, 2013 Ceph common code (libceph)

lore
Share & tools
lore

Alex Elder

author
#0 lore raw

cover libceph: odds and ends

(These patches, along with the patches I'll be posting in the next few minutes, are available in the "review/wip-stripe-v2" branch of the ceph-client git repository.)

This series just fleshes out some code in libceph. The first completes the set of definitions for limit values (like U8_MAX) to include the signed minimum and maximum values. The second just adds some error checking and type casting to timespec encoding and decoding routines.

-Alex

[PATCH 1/2] libceph: add signed type limits [PATCH 2/2] libceph: validate timespec conversions

author
#1 lore raw

PATCH 1/2 libceph: add signed type limits

A ceph timespec contains 32-bit unsigned values for its seconds and nanoseconds components. For a standard timespec, both fields are signed, and the seconds field is almost surely 64 bits.

Add some explicit casts so the fact that this conversion is taking place is obvious. Also trip a bug if we ever try to put out of range (negative or too big) values into a ceph timespec.

---
 include/linux/ceph/decode.h |   13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
include/linux/ceph/decode.h +9 -4
index 9575a52..379f715 100644
@@ -154,14 +154,19 @@ bad:
 static inline void ceph_decode_timespec(struct timespec *ts,
 					const struct ceph_timespec *tv)
 {
-	ts->tv_sec = le32_to_cpu(tv->tv_sec);
-	ts->tv_nsec = le32_to_cpu(tv->tv_nsec);
+	ts->tv_sec = (__kernel_time_t)le32_to_cpu(tv->tv_sec);
+	ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec);
 }
 static inline void ceph_encode_timespec(struct ceph_timespec *tv,
 					const struct timespec *ts)
 {
-	tv->tv_sec = cpu_to_le32(ts->tv_sec);
-	tv->tv_nsec = cpu_to_le32(ts->tv_nsec);
+	BUG_ON(ts->tv_sec < 0);
+	BUG_ON(ts->tv_sec > (__kernel_time_t)U32_MAX);
+	BUG_ON(ts->tv_nsec < 0);
+	BUG_ON(ts->tv_nsec > (long)U32_MAX);
+
+	tv->tv_sec = cpu_to_le32((u32)ts->tv_sec);
+	tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec);
 }
 
 /*
Signature · 1 line
1.7.9.5
author
#2 lore raw

Re: [PATCH 1/2] libceph: add signed type limits

(Sorry, the subject line didn't match the content. Please ignore the previous one--this is the right patch.)

Flesh out the limits defined in <linux/ceph/decode.h> to include the maximum and minimum values for signed type S8, S16, S32, and S64.

---
 include/linux/ceph/decode.h |   18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
include/linux/ceph/decode.h +14 -4
index 689f1df..9575a52 100644
@@ -10,10 +10,20 @@
 
 /* This seemed to be the easiest place to define these */
 
-#define	U8_MAX	((u8)  (~0U))
-#define	U16_MAX	((u16) (~0U))
-#define	U32_MAX	((u32) (~0U))
-#define	U64_MAX	((u64) (~0ULL))
+#define	U8_MAX	((u8)(~0U))
+#define	U16_MAX	((u16)(~0U))
+#define	U32_MAX	((u32)(~0U))
+#define	U64_MAX	((u64)(~0ULL))
+
+#define	S8_MAX	((s8)(U8_MAX >> 1))
+#define	S16_MAX	((s16)(U16_MAX >> 1))
+#define	S32_MAX	((s32)(U32_MAX >> 1))
+#define	S64_MAX	((s64)(U64_MAX >> 1LL))
+
+#define	S8_MIN	((s8)(-S8_MAX - 1))
+#define	S16_MIN	((s16)(-S16_MAX - 1))
+#define	S32_MIN	((s32)(-S32_MAX - 1))
+#define	S64_MIN	((s64)(-S64_MAX - 1LL))
 
 /*
  * in all cases,
Signature · 1 line
1.7.9.5
Reviewed-by
#3 lore raw

Re: [PATCH 1/2] libceph: add signed type limits

Quoted 43 lines · Alex Elder

(Sorry, the subject line didn't match the content. Please ignore the previous one--this is the right patch.)

Flesh out the limits defined in <linux/ceph/decode.h> to include the maximum and minimum values for signed type S8, S16, S32, and S64.

---
  include/linux/ceph/decode.h |   18 ++++++++++++++----
  1 file changed, 14 insertions(+), 4 deletions(-)
include/linux/ceph/decode.h +14 -4
index 689f1df..9575a52 100644
@@ -10,10 +10,20 @@
 
  /* This seemed to be the easiest place to define these */
 
-#define	U8_MAX	((u8)  (~0U))
-#define	U16_MAX	((u16) (~0U))
-#define	U32_MAX	((u32) (~0U))
-#define	U64_MAX	((u64) (~0ULL))
+#define	U8_MAX	((u8)(~0U))
+#define	U16_MAX	((u16)(~0U))
+#define	U32_MAX	((u32)(~0U))
+#define	U64_MAX	((u64)(~0ULL))
+
+#define	S8_MAX	((s8)(U8_MAX >> 1))
+#define	S16_MAX	((s16)(U16_MAX >> 1))
+#define	S32_MAX	((s32)(U32_MAX >> 1))
+#define	S64_MAX	((s64)(U64_MAX >> 1LL))
+
+#define	S8_MIN	((s8)(-S8_MAX - 1))
+#define	S16_MIN	((s16)(-S16_MAX - 1))
+#define	S32_MIN	((s32)(-S32_MAX - 1))
+#define	S64_MIN	((s64)(-S64_MAX - 1LL))
 
  /*
   * in all cases,
author
#4 lore raw

PATCH 2/2 libceph: validate timespec conversions

In reply to Alex Elder

A ceph timespec contains 32-bit unsigned values for its seconds and nanoseconds components. For a standard timespec, both fields are signed, and the seconds field is almost surely 64 bits.

Add some explicit casts so the fact that this conversion is taking place is obvious. Also trip a bug if we ever try to put out of range (negative or too big) values into a ceph timespec.

---
 include/linux/ceph/decode.h |   13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
include/linux/ceph/decode.h +9 -4
index 9575a52..379f715 100644
@@ -154,14 +154,19 @@ bad:
 static inline void ceph_decode_timespec(struct timespec *ts,
 					const struct ceph_timespec *tv)
 {
-	ts->tv_sec = le32_to_cpu(tv->tv_sec);
-	ts->tv_nsec = le32_to_cpu(tv->tv_nsec);
+	ts->tv_sec = (__kernel_time_t)le32_to_cpu(tv->tv_sec);
+	ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec);
 }
 static inline void ceph_encode_timespec(struct ceph_timespec *tv,
 					const struct timespec *ts)
 {
-	tv->tv_sec = cpu_to_le32(ts->tv_sec);
-	tv->tv_nsec = cpu_to_le32(ts->tv_nsec);
+	BUG_ON(ts->tv_sec < 0);
+	BUG_ON(ts->tv_sec > (__kernel_time_t)U32_MAX);
+	BUG_ON(ts->tv_nsec < 0);
+	BUG_ON(ts->tv_nsec > (long)U32_MAX);
+
+	tv->tv_sec = cpu_to_le32((u32)ts->tv_sec);
+	tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec);
 }
 
 /*
Signature · 1 line
1.7.9.5
#5 lore raw

Re: [PATCH 2/2] libceph: validate timespec conversions

Quoted 3 lines · ----- "Alex Elder

A ceph timespec contains 32-bit unsigned values for its seconds and nanoseconds components. For a standard timespec, both fields are signed, and the seconds field is almost surely 64 bits.

Is the Ceph timespec going to change at some point?

Quoted 5 lines

Add some explicit casts so the fact that this conversion is taking place is obvious. Also trip a bug if we ever try to put out of range (negative or too big) values into a ceph timespec.

Signature · 10 lines
Matt Benjamin
The Linux Box
206 South Fifth Ave. Suite 150
Ann Arbor, MI  48104

http://linuxbox.com

tel.  734-761-4689 
fax.  734-769-8938 
cel.  734-216-5309 
#6 lore raw

Re: [PATCH 2/2] libceph: validate timespec conversions

Quoted 8 lines · Matt W. Benjamin
Quoted 3 lines · ----- "Alex Elder

A ceph timespec contains 32-bit unsigned values for its seconds and nanoseconds components. For a standard timespec, both fields are signed, and the seconds field is almost surely 64 bits.

Is the Ceph timespec going to change at some point?

I don't think so. 32-bits is enough for the billion nanoseconds in a second. And I'm not sure if the signedness is used/useful... the ceph utime_t code always normalizes the ns result to be in [0, 1 billion).

sage

Quoted 24 lines
Quoted 6 lines

Add some explicit casts so the fact that this conversion is taking place is obvious. Also trip a bug if we ever try to put out of range (negative or too big) values into a ceph timespec.

Signature · 14 lines
Matt Benjamin
The Linux Box
206 South Fifth Ave. Suite 150
Ann Arbor, MI  48104

http://linuxbox.com

tel.  734-761-4689 
fax.  734-769-8938 
cel.  734-216-5309 
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#7 lore raw

Re: [PATCH 2/2] libceph: validate timespec conversions

I was thinking about the seconds component.

Quoted 18 lines · ----- "Sage Weil
Quoted 4 lines · Matt W. Benjamin
Quoted 1 line · ----- "Alex Elder

A ceph timespec contains 32-bit unsigned values for its seconds

and

Quoted 4 lines
Quoted 2 lines

nanoseconds components. For a standard timespec, both fields are signed, and the seconds field is almost surely 64 bits.

Is the Ceph timespec going to change at some point?

I don't think so. 32-bits is enough for the billion nanoseconds in a

second. And I'm not sure if the signedness is used/useful... the ceph

utime_t code always normalizes the ns result to be in [0, 1 billion).

sage

Signature · 10 lines
Matt Benjamin
The Linux Box
206 South Fifth Ave. Suite 150
Ann Arbor, MI  48104

http://linuxbox.com

tel.  734-761-4689 
fax.  734-769-8938 
cel.  734-216-5309 
#8 lore raw

Re: [PATCH 2/2] libceph: validate timespec conversions

Quoted 1 line · Matt W. Benjamin

I was thinking about the seconds component.

Oh, right.. that's the unix epoch(alypse) in 2038 or something?

That we should probably fix. :)

s

Quoted 40 lines
Quoted 18 lines · ----- "Sage Weil
Quoted 4 lines · Matt W. Benjamin
Quoted 1 line · ----- "Alex Elder

A ceph timespec contains 32-bit unsigned values for its seconds

and

Quoted 4 lines
Quoted 2 lines

nanoseconds components. For a standard timespec, both fields are signed, and the seconds field is almost surely 64 bits.

Is the Ceph timespec going to change at some point?

I don't think so. 32-bits is enough for the billion nanoseconds in a

second. And I'm not sure if the signedness is used/useful... the ceph

utime_t code always normalizes the ns result to be in [0, 1 billion).

sage

Signature · 14 lines
Matt Benjamin
The Linux Box
206 South Fifth Ave. Suite 150
Ann Arbor, MI  48104

http://linuxbox.com

tel.  734-761-4689 
fax.  734-769-8938 
cel.  734-216-5309 
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#9 lore raw

Re: [PATCH 2/2] libceph: validate timespec conversions

In reply to Matt W. Benjamin
Quoted 1 line · Matt W. Benjamin

I was thinking about the seconds component.

I wondered the same thing. It will most likely
have to some time in the next 25 years or so.

-Alex

Quoted 22 lines
Quoted 18 lines · ----- "Sage Weil
Quoted 4 lines · Matt W. Benjamin
Quoted 1 line · ----- "Alex Elder

A ceph timespec contains 32-bit unsigned values for its seconds

and

Quoted 4 lines
Quoted 2 lines

nanoseconds components. For a standard timespec, both fields are signed, and the seconds field is almost surely 64 bits.

Is the Ceph timespec going to change at some point?

I don't think so. 32-bits is enough for the billion nanoseconds in a

second. And I'm not sure if the signedness is used/useful... the ceph

utime_t code always normalizes the ns result to be in [0, 1 billion).

sage

Reviewed-by on v1 2/2
#10 lore raw

Re: [PATCH 2/2] libceph: validate timespec conversions

In reply to Alex Elder
Quoted 42 lines · Alex Elder

A ceph timespec contains 32-bit unsigned values for its seconds and nanoseconds components. For a standard timespec, both fields are signed, and the seconds field is almost surely 64 bits.

Add some explicit casts so the fact that this conversion is taking place is obvious. Also trip a bug if we ever try to put out of range (negative or too big) values into a ceph timespec.

---
  include/linux/ceph/decode.h |   13 +++++++++----
  1 file changed, 9 insertions(+), 4 deletions(-)
include/linux/ceph/decode.h +9 -4
index 9575a52..379f715 100644
@@ -154,14 +154,19 @@ bad:
  static inline void ceph_decode_timespec(struct timespec *ts,
  					const struct ceph_timespec *tv)
  {
-	ts->tv_sec = le32_to_cpu(tv->tv_sec);
-	ts->tv_nsec = le32_to_cpu(tv->tv_nsec);
+	ts->tv_sec = (__kernel_time_t)le32_to_cpu(tv->tv_sec);
+	ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec);
  }
  static inline void ceph_encode_timespec(struct ceph_timespec *tv,
  					const struct timespec *ts)
  {
-	tv->tv_sec = cpu_to_le32(ts->tv_sec);
-	tv->tv_nsec = cpu_to_le32(ts->tv_nsec);
+	BUG_ON(ts->tv_sec < 0);
+	BUG_ON(ts->tv_sec > (__kernel_time_t)U32_MAX);
+	BUG_ON(ts->tv_nsec < 0);
+	BUG_ON(ts->tv_nsec > (long)U32_MAX);
+
+	tv->tv_sec = cpu_to_le32((u32)ts->tv_sec);
+	tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec);
  }
 
  /*