Skip to contentSkip to conversation

Add two macros for build-time testing of time compatibility

PATCH New Dan Smith 1 participant 2 messages Aug 5, 2010 1 message not in the archive

lore
Share & tools
lore

Dan Smith

author
#0 lore raw

PATCH 2/3 Add two macros for build-time testing of time compatibility

We often canonicalize data from internal kernel data structures that use ambiguous types (int, short, long, etc) and write them to the checkpoint stream with unambiguous types (__u16, __u32, __u64, etc). If an internal structure changes a type somewhere down the road, we may not notice and write invalid data to the checkpoint stream as a result.

This patch introduces two helper macros to make it easier to generate a BUILD_BUG when this happens. Examples of usage are included in the comment before the macros in the code.

---
 include/linux/checkpoint_hdr.h |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
include/linux/checkpoint_hdr.h +15 -0
index f4f9577..75d863d 100644
@@ -42,6 +42,21 @@
 #define CHECKPOINT_LSM_NAME_MAX 10
 
 /*
+ * Macros to help generate a build break if an ambiguous type changes
+ * in such a way that the size differs from the unambiguous type we
+ * actually write to the checkpoint stream.
+ *
+ * Examples:
+ *
+ * CKPT_BUILD_BUG_ON_MISMATCH(short, __u16);
+ * CKPT_BUILD_BUG_ON_MISMATCH(STRUCT_MEMBER(mystruct, mymember),
+ *                            STRUCT_MEMBER(ckpt_hdr_foo, bar));
+ *
+ */
+#define CKPT_STRUCT_MEMBER(type, member) (((struct type *)(NULL))->member)
+#define CKPT_BUILD_BUG_ON_MISMATCH(a, b) (BUILD_BUG_ON(sizeof(a) != sizeof(b)))
+
+/*
  * To maintain compatibility between 32-bit and 64-bit architecture flavors,
  * keep data 64-bit aligned: use padding for structure members, and use
  * __attribute__((aligned (8))) for the entire structure.
Signature · 1 line
1.7.1.1
author
#2 lore raw

PATCH 3/3 Add support for the per-task sem_undo list (v3.01)

In reply to a message not in the archive

The semaphore undo list is a set of adjustments to be made to semaphores held by a task on exit. Right now, we do not checkpoint or restore this list which could cause undesirable behavior by a restarted process on exit.

Changes in v3:
- Move taking of the refcount for the first process to restore_sem_undo() and make restore_obj_sem_undo() take the reference for second-and- later tasks
- Fix uses of __u16 to represent a short
- Fix potential for overrunning the un->semadj buffer in restore
- Move the checkpoint object and init functions to the bottom of sem.c
- Use ckpt_read_payload() instead of allocating our own semadj buffer
- Change the build bug macro to use the new one introduced in the previous patch

Changes in v2:
- Remove collect operation
- Add a BUILD_BUG_ON() to ensure sizeof(short) == sizeof(__u16)
- Use sizeof(__u16) when copying to/from checkpoint header
- Fix a couple of leaked hdr objects
- Avoid reading the semadj buffer with rcu_read_lock() held
- Set the sem_undo pointer on tasks other than the first to restore a list
- Fix refcounting on restart
- Pull out the guts of exit_sem() into put_undo_list() and call that from our drop() function in case we're the last one.

---
 include/linux/checkpoint.h     |    4 +
 include/linux/checkpoint_hdr.h |   18 ++
 ipc/sem.c                      |  344 ++++++++++++++++++++++++++++++++++++++--
 kernel/checkpoint/process.c    |   13 ++
 4 files changed, 363 insertions(+), 16 deletions(-)
include/linux/checkpoint.h +4 -0
index 4e25042..a11d40e 100644
@@ -271,6 +271,10 @@ extern int ckpt_collect_fs(struct ckpt_ctx *ctx, struct task_struct *t);
 extern int checkpoint_obj_fs(struct ckpt_ctx *ctx, struct task_struct *t);
 extern int restore_obj_fs(struct ckpt_ctx *ctx, int fs_objref);
 
+/* per-task semaphore undo */
+extern int checkpoint_obj_sem_undo(struct ckpt_ctx *ctx, struct task_struct *t);
+extern int restore_obj_sem_undo(struct ckpt_ctx *ctx, int sem_undo_objref);
+
 /* memory */
 extern void ckpt_pgarr_free(struct ckpt_ctx *ctx);
 
include/linux/checkpoint_hdr.h +18 -0
index 75d863d..005cd8d 100644
@@ -182,6 +182,10 @@ enum {
 #define CKPT_HDR_IPC_MSG_MSG CKPT_HDR_IPC_MSG_MSG
 	CKPT_HDR_IPC_SEM,
 #define CKPT_HDR_IPC_SEM CKPT_HDR_IPC_SEM
+	CKPT_HDR_TASK_SEM_UNDO_LIST,
+#define CKPT_HDR_TASK_SEM_UNDO_LIST CKPT_HDR_TASK_SEM_UNDO_LIST
+	CKPT_HDR_TASK_SEM_UNDO,
+#define CKPT_HDR_TASK_SEM_UNDO CKPT_HDR_TASK_SEM_UNDO
 
 	CKPT_HDR_SIGHAND = 601,
 #define CKPT_HDR_SIGHAND CKPT_HDR_SIGHAND
@@ -288,6 +292,8 @@ enum obj_type {
 #define CKPT_OBJ_NET_NS CKPT_OBJ_NET_NS
 	CKPT_OBJ_NETDEV,
 #define CKPT_OBJ_NETDEV CKPT_OBJ_NETDEV
+	CKPT_OBJ_SEM_UNDO,
+#define CKPT_OBJ_SEM_UNDO CKPT_OBJ_SEM_UNDO
 	CKPT_OBJ_MAX
 #define CKPT_OBJ_MAX CKPT_OBJ_MAX
 };
@@ -476,6 +482,17 @@ struct ckpt_hdr_ns {
 	__s32 net_objref;
 } __attribute__((aligned(8)));
 
+struct ckpt_hdr_task_sem_undo_list {
+	struct ckpt_hdr h;
+	__u32 count;
+};
+
+struct ckpt_hdr_task_sem_undo {
+	struct ckpt_hdr h;
+	__u32 semid;
+	__u32 semadj_count;
+};
+
 /* cannot include <linux/tty.h> from userspace, so define: */
 #define CKPT_NEW_UTS_LEN  64
 #ifdef __KERNEL__
@@ -502,6 +519,7 @@ struct ckpt_hdr_task_objs {
 	__s32 files_objref;
 	__s32 mm_objref;
 	__s32 fs_objref;
+	__s32 sem_undo_objref;
 	__s32 sighand_objref;
 	__s32 signal_objref;
 } __attribute__((aligned(8)));
ipc/sem.c +328 -16

372 lines not on this page · raw message

kernel/checkpoint/process.c +13 -0
index 936675a..4ec9cdd 100644
@@ -236,6 +236,7 @@ static int checkpoint_task_objs(struct ckpt_ctx *ctx, struct task_struct *t)
 	int files_objref;
 	int mm_objref;
 	int fs_objref;
+	int sem_undo_objref;
 	int sighand_objref;
 	int signal_objref;
 	int first, ret;
@@ -283,6 +284,12 @@ static int checkpoint_task_objs(struct ckpt_ctx *ctx, struct task_struct *t)
 		return fs_objref;
 	}
 
+	sem_undo_objref = checkpoint_obj_sem_undo(ctx, t);
+	if (sem_undo_objref < 0) {
+		ckpt_err(ctx, sem_undo_objref, "%(T)process sem_undo\n");
+		return sem_undo_objref;
+	}
+
 	sighand_objref = checkpoint_obj_sighand(ctx, t);
 	ckpt_debug("sighand: objref %d\n", sighand_objref);
 	if (sighand_objref < 0) {
@@ -311,6 +318,7 @@ static int checkpoint_task_objs(struct ckpt_ctx *ctx, struct task_struct *t)
 	h->files_objref = files_objref;
 	h->mm_objref = mm_objref;
 	h->fs_objref = fs_objref;
+	h->sem_undo_objref = sem_undo_objref;
 	h->sighand_objref = sighand_objref;
 	h->signal_objref = signal_objref;
 	ret = ckpt_write_obj(ctx, &h->h);
@@ -679,6 +687,11 @@ static int restore_task_objs(struct ckpt_ctx *ctx)
 	if (ret < 0)
 		return ret;
 
+	ret = restore_obj_sem_undo(ctx, h->sem_undo_objref);
+	ckpt_debug("sem_undo: ret %d\n", ret);
+	if (ret < 0)
+		return ret;
+
 	ret = restore_obj_sighand(ctx, h->sighand_objref);
 	ckpt_debug("sighand: ret %d (%p)\n", ret, current->sighand);
 	if (ret < 0)
Signature · 1 line
1.7.1.1