[CRIU] [PATCH 2/4] sysctl: Pass number of requests in argument

Cyrill Gorcunov gorcunov at openvz.org
Thu Apr 30 13:07:56 PDT 2015


This saves memory.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/sysctl.h |  2 +-
 ipc_ns.c         | 18 ++++++------------
 kerndat.c        |  6 ++----
 net.c            |  2 +-
 sysctl.c         |  4 ++--
 uts_ns.c         |  5 ++---
 6 files changed, 14 insertions(+), 23 deletions(-)

diff --git a/include/sysctl.h b/include/sysctl.h
index 4a3d551ec710..3d7b182e862f 100644
--- a/include/sysctl.h
+++ b/include/sysctl.h
@@ -7,7 +7,7 @@ struct sysctl_req {
 	int	type;
 };
 
-extern int sysctl_op(struct sysctl_req *req, int op);
+extern int sysctl_op(struct sysctl_req *req, size_t nr_req, int op);
 
 enum {
 	CTL_READ,
diff --git a/ipc_ns.c b/ipc_ns.c
index 118cc684dd88..c6e2eeb2391a 100644
--- a/ipc_ns.c
+++ b/ipc_ns.c
@@ -179,10 +179,9 @@ static int dump_ipc_msg_queue_messages(struct cr_img *img, const IpcMsgEntry *ms
 	int ret, msg_cnt = 0;
 	struct sysctl_req req[] = {
 		{ "kernel/msgmax", &msgmax, CTL_U32 },
-		{ },
 	};
 
-	ret = sysctl_op(req, CTL_READ);
+	ret = sysctl_op(req, ARRAY_SIZE(req), CTL_READ);
 	if (ret < 0) {
 		pr_err("Failed to read max IPC message size\n");
 		goto err;
@@ -304,19 +303,17 @@ static int ipc_sysctl_req(IpcVarEntry *e, int op)
 		{ "kernel/shmall",		&e->shm_ctlall,		CTL_U64 },
 		{ "kernel/shmmni",		&e->shm_ctlmni,		CTL_U32 },
 		{ "kernel/shm_rmid_forced",	&e->shm_rmid_forced,	CTL_U32 },
-		{ },
 	};
 
 	struct sysctl_req req_mq[] = {
 		{ "fs/mqueue/queues_max",	&e->mq_queues_max,	CTL_U32 },
 		{ "fs/mqueue/msg_max",		&e->mq_msg_max,		CTL_U32 },
 		{ "fs/mqueue/msgsize_max",	&e->mq_msgsize_max,	CTL_U32 },
-		{ },
 	};
 
 	int ret;
 
-	ret = sysctl_op(req, op);
+	ret = sysctl_op(req, ARRAY_SIZE(req), op);
 	if (ret)
 		return ret;
 
@@ -325,7 +322,7 @@ static int ipc_sysctl_req(IpcVarEntry *e, int op)
 		return 0;
 	}
 
-	return sysctl_op(req_mq, op);
+	return sysctl_op(req_mq, ARRAY_SIZE(req_mq), op);
 }
 
 /*
@@ -555,11 +552,10 @@ static int prepare_ipc_sem_desc(struct cr_img *img, const IpcSemEntry *sem)
 	int ret, id;
 	struct sysctl_req req[] = {
 		{ "kernel/sem_next_id", &sem->desc->id, CTL_U32 },
-		{ },
 	};
 	struct semid_ds semid;
 
-	ret = sysctl_op(req, CTL_WRITE);
+	ret = sysctl_op(req, ARRAY_SIZE(req), CTL_WRITE);
 	if (ret < 0) {
 		pr_err("Failed to set desired IPC sem ID\n");
 		return ret;
@@ -692,11 +688,10 @@ static int prepare_ipc_msg_queue(struct cr_img *img, const IpcMsgEntry *msq)
 	int ret, id;
 	struct sysctl_req req[] = {
 		{ "kernel/msg_next_id", &msq->desc->id, CTL_U32 },
-		{ },
 	};
 	struct msqid_ds msqid;
 
-	ret = sysctl_op(req, CTL_WRITE);
+	ret = sysctl_op(req, ARRAY_SIZE(req), CTL_WRITE);
 	if (ret < 0) {
 		pr_err("Failed to set desired IPC msg ID\n");
 		return ret;
@@ -804,11 +799,10 @@ static int prepare_ipc_shm_seg(struct cr_img *img, const IpcShmEntry *shm)
 	int ret, id;
 	struct sysctl_req req[] = {
 		{ "kernel/shm_next_id", &shm->desc->id, CTL_U32 },
-		{ },
 	};
 	struct shmid_ds shmid;
 
-	ret = sysctl_op(req, CTL_WRITE);
+	ret = sysctl_op(req, ARRAY_SIZE(req), CTL_WRITE);
 	if (ret < 0) {
 		pr_err("Failed to set desired IPC shm ID\n");
 		return ret;
diff --git a/kerndat.c b/kerndat.c
index 6745dbb0296f..bea828695e6a 100644
--- a/kerndat.c
+++ b/kerndat.c
@@ -198,14 +198,13 @@ static int tcp_read_sysctl_limits(void)
 
 	struct sysctl_req req[] = {
 		{ "net/ipv4/tcp_rmem", &vect[1], CTL_U32A(ARRAY_SIZE(vect[1])) },
-		{ },
 	};
 
 	/*
 	 * Lets figure out which exactly amount of memory is
 	 * availabe for send/read queues on restore.
 	 */
-	ret = sysctl_op(req, CTL_READ);
+	ret = sysctl_op(req, ARRAY_SIZE(req), CTL_READ);
 	if (ret) {
 		pr_warn("TCP mem sysctls are not available. Using defaults.\n");
 		goto out;
@@ -250,10 +249,9 @@ static int get_last_cap(void)
 {
 	struct sysctl_req req[] = {
 		{ "kernel/cap_last_cap", &kdat.last_cap, CTL_U32 },
-		{ },
 	};
 
-	return sysctl_op(req, CTL_READ);
+	return sysctl_op(req, ARRAY_SIZE(req), CTL_READ);
 }
 
 static bool kerndat_has_memfd_create(void)
diff --git a/net.c b/net.c
index 5f378d496276..d4f762333c66 100644
--- a/net.c
+++ b/net.c
@@ -109,7 +109,7 @@ static int ipv4_conf_op(char *tgt, int *conf, int op, NetnsEntry **netns)
 	}
 	req[ri].name = NULL;
 
-	ret = sysctl_op(req, op);
+	ret = sysctl_op(req, ri ? ri - 1 : 0, op);
 	if (ret < 0) {
 		pr_err("Failed to %s %s/<confs>\n", (op == CTL_READ)?"read":"write", tgt);
 		return -1;
diff --git a/sysctl.c b/sysctl.c
index eaa1f58dab32..5c3a01f25ac8 100644
--- a/sysctl.c
+++ b/sysctl.c
@@ -166,7 +166,7 @@ static int __sysctl_op(int dir, struct sysctl_req *req, int op)
 	return ret;
 }
 
-int sysctl_op(struct sysctl_req *req, int op)
+int sysctl_op(struct sysctl_req *req, size_t nr_req, int op)
 {
 	int ret = 0;
 	int dir = -1;
@@ -177,7 +177,7 @@ int sysctl_op(struct sysctl_req *req, int op)
 		return -1;
 	}
 
-	while (req->name) {
+	while (nr_req--) {
 		ret = __sysctl_op(dir, req, op);
 		if (ret < 0)
 			break;
diff --git a/uts_ns.c b/uts_ns.c
index 766cae888bb7..e2c1f68d9668 100644
--- a/uts_ns.c
+++ b/uts_ns.c
@@ -43,10 +43,9 @@ int prepare_utsns(int pid)
 	int ret;
 	struct cr_img *img;
 	UtsnsEntry *ue;
-	struct sysctl_req req[3] = {
+	struct sysctl_req req[] = {
 		{ "kernel/hostname" },
 		{ "kernel/domainname" },
-		{ },
 	};
 
 	img = open_image(CR_FD_UTSNS, O_RSTR, pid);
@@ -62,7 +61,7 @@ int prepare_utsns(int pid)
 	req[1].arg = ue->domainname;
 	req[1].type = CTL_STR(strlen(ue->domainname));
 
-	ret = sysctl_op(req, CTL_WRITE);
+	ret = sysctl_op(req, ARRAY_SIZE(req), CTL_WRITE);
 	utsns_entry__free_unpacked(ue, NULL);
 out:
 	close_image(img);
-- 
2.1.0



More information about the CRIU mailing list