Skip to contentSkip to conversation

Add a ckpt_read_string() function (v3)

PATCH In review Dan Smith 3 participants 4 messages Aug 4, 2009 2 messages not in the archive

lore
Share & tools
lore

Dan Smith

author
#0 lore raw

PATCH 1/1 Add a ckpt_read_string() function (v3)

Add a ckpt_read_string() function to allow reading of a variable-length (but length-capped) string from the checkpoint stream.

Changes in v3:
- Return immediately on allocation failure instead of falling through to the inevitable crash

Changes in v2:
- Avoid memcpy() by reading into the allocated buffer directly

---
 checkpoint/restart.c       |   36 ++++++++++++++++++++++++++++++++++++
 include/linux/checkpoint.h |    1 +
 2 files changed, 37 insertions(+), 0 deletions(-)
checkpoint/restart.c +36 -0
index 65cafd9..b1ffc54 100644
@@ -285,6 +285,42 @@ int ckpt_read_consume(struct ckpt_ctx *ctx, int len, int type)
 	return ret;
 }
 
+/**
+ * ckpt_read_string - read a string (variable length)
+ * @ctx: checkpoint context
+ * @str: pointer to buffer to store allocated string (caller must kfree())
+ * @max: maximum acceptable length
+ *
+ * This can be used to read a variable-length string from the checkpoint
+ * stream. @max limits the size of the resulting buffer.  Returns zero on
+ * success, negative on failure.
+ */
+int ckpt_read_string(struct ckpt_ctx *ctx, char **str, int max)
+{
+	int len;
+	int ret = 0;
+
+	*str = NULL;
+
+	len = _ckpt_read_obj_type(ctx, NULL, 0, CKPT_HDR_STRING);
+	if (len < 0)
+		return len;
+	else if (len > max)
+		return -EINVAL;
+
+	*str = kzalloc(len + 1, GFP_KERNEL);
+	if (!*str)
+		return -ENOMEM;
+
+	ret = ckpt_kread(ctx, *str, len);
+	if (ret < 0) {
+		kfree(*str);
+		*str = NULL;
+	}
+
+	return ret;
+}
+
 /***********************************************************************
  * Restart
  */
include/linux/checkpoint.h +1 -0
index 87b683b..a6935b3 100644
@@ -68,6 +68,7 @@ extern int _ckpt_read_obj_type(struct ckpt_ctx *ctx,
 extern int _ckpt_read_nbuffer(struct ckpt_ctx *ctx, void *ptr, int len);
 extern int _ckpt_read_buffer(struct ckpt_ctx *ctx, void *ptr, int len);
 extern int _ckpt_read_string(struct ckpt_ctx *ctx, void *ptr, int len);
+extern int ckpt_read_string(struct ckpt_ctx *ctx, char **str, int max);
 extern void *ckpt_read_obj_type(struct ckpt_ctx *ctx, int len, int type);
 extern void *ckpt_read_buf_type(struct ckpt_ctx *ctx, int len, int type);
 extern int ckpt_read_consume(struct ckpt_ctx *ctx, int len, int type);
Signature · 1 line
1.6.0.4

A message that is not in the archive (sent off-list or not yet received): 1249418420-807-1-git-send-email-danms-r/[email protected]

#2 lore raw
Quoted 11 lines · Dan Smith

Add a ckpt_read_string() function to allow reading of a variable-length (but length-capped) string from the checkpoint stream.

Changes in v3:
- Return immediately on allocation failure instead of falling through to the inevitable crash

Changes in v2:
- Avoid memcpy() by reading into the allocated buffer directly

Quoted 71 lines
---
 checkpoint/restart.c       |   36 ++++++++++++++++++++++++++++++++++++
 include/linux/checkpoint.h |    1 +
 2 files changed, 37 insertions(+), 0 deletions(-)
checkpoint/restart.c +36 -0
index 65cafd9..b1ffc54 100644
@@ -285,6 +285,42 @@ int ckpt_read_consume(struct ckpt_ctx *ctx, int len, int type)
 	return ret;
 }
 
+/**
+ * ckpt_read_string - read a string (variable length)
+ * @ctx: checkpoint context
+ * @str: pointer to buffer to store allocated string (caller must kfree())
+ * @max: maximum acceptable length
+ *
+ * This can be used to read a variable-length string from the checkpoint
+ * stream. @max limits the size of the resulting buffer.  Returns zero on
+ * success, negative on failure.
+ */
+int ckpt_read_string(struct ckpt_ctx *ctx, char **str, int max)
+{
+	int len;
+	int ret = 0;
+
+	*str = NULL;
+
+	len = _ckpt_read_obj_type(ctx, NULL, 0, CKPT_HDR_STRING);
+	if (len < 0)
+		return len;
+	else if (len > max)
+		return -EINVAL;
+
+	*str = kzalloc(len + 1, GFP_KERNEL);
+	if (!*str)
+		return -ENOMEM;
+
+	ret = ckpt_kread(ctx, *str, len);
+	if (ret < 0) {
+		kfree(*str);
+		*str = NULL;
+	}
+
+	return ret;
+}
+
 /***********************************************************************
  * Restart
  */
include/linux/checkpoint.h +1 -0
index 87b683b..a6935b3 100644
@@ -68,6 +68,7 @@ extern int _ckpt_read_obj_type(struct ckpt_ctx *ctx,
 extern int _ckpt_read_nbuffer(struct ckpt_ctx *ctx, void *ptr, int len);
 extern int _ckpt_read_buffer(struct ckpt_ctx *ctx, void *ptr, int len);
 extern int _ckpt_read_string(struct ckpt_ctx *ctx, void *ptr, int len);
+extern int ckpt_read_string(struct ckpt_ctx *ctx, char **str, int max);
 extern void *ckpt_read_obj_type(struct ckpt_ctx *ctx, int len, int type);
 extern void *ckpt_read_buf_type(struct ckpt_ctx *ctx, int len, int type);
 extern int ckpt_read_consume(struct ckpt_ctx *ctx, int len, int type);
Signature · 6 lines
1.6.0.4

_______________________________________________
Containers mailing list
Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linux-foundation.org/mailman/listinfo/containers
#3 lore raw
In reply to a message not in the archive

Hi Dan,

Quoted 25 lines · Dan Smith
checkpoint/restart.c +25 -0
+/**
+ * ckpt_read_string - read a string (variable length)
+ * @ctx: checkpoint context
+ * @str: pointer to buffer to store allocated string (caller must kfree())
+ * @max: maximum acceptable length
+ *
+ * This can be used to read a variable-length string from the checkpoint
+ * stream. @max limits the size of the resulting buffer.  Returns zero on
+ * success, negative on failure.
+ */
+int ckpt_read_string(struct ckpt_ctx *ctx, char **str, int max)
+{
+	int len;
+	int ret = 0;
+
+	*str = NULL;
+
+	len = _ckpt_read_obj_type(ctx, NULL, 0, CKPT_HDR_STRING);
+	if (len < 0)
+		return len;
+	else if (len > max)
+		return -EINVAL;
+
+	*str = kzalloc(len + 1, GFP_KERNEL);
+	if (!*str)

checkpoint/restart.c int ckpt_read_consume(struct ckpt_ctx *ctx, int len, int type) · line 322

Quoted 10 lines
checkpoint/restart.c +10 -0
+		return -ENOMEM;
+
+	ret = ckpt_kread(ctx, *str, len);
+	if (ret < 0) {
+		kfree(*str);
+		*str = NULL;
+	}
+
+	return ret;
+}

Maybe I'm missing the utility this provides, but I think this helper is trying to do too much. It's often more straightforward to allow (or force, depending on your POV) the caller to handle the allocation if it's going to have to free it anyway. It also allows the caller to choose its own allocation method, e.g. on-stack for small things, or maybe some filesystem-related code will need to use GFP_NOFS.

So I imagine a simple wrapper to probe the length of the string would suffice? Something like

ssize_t ckpt_next_string_length(struct ckpt_ctx *ctx)

{
        return _ckpt_read_obj_type(ctx, NULL, 0, CKPT_HDR_STRING);
}

...

len = ckpt_next_string_length(ctx);
if (len < 1 || len > max)
return -EINVAL;

buf = kzalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;

rc = ckpt_kread(ctx, buf, len);
if (rc < 0) {
kfree(buf)
return -EINVAL;
}

kfree(buf);

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#5 lore raw

NL> So I imagine a simple wrapper to probe the length of the string NL> would suffice? Something like

We've been there already, a few iterations ago, right? It's been revised so many times, I've lost count, but I believe that's what I started with.

I'd really like Oren to own this helper function along with all the rest, and I'll use whatever API is available at any given point :)

Signature · 3 lines
Dan Smith
IBM Linux Technology Center
email: danms-r/[email protected]