Skip to contentSkip to conversation

ceph-client: some refactoring patches

PATCH New Alex Elder 1 participant 8 messages Apr 27, 2015 Ceph common code (libceph)

lore
Share & tools
lore

Alex Elder

author
#0 lore raw

cover ceph-client: some refactoring patches

Most of this series is just comment changes and a few minor naming tweaks. The last two patches actually change code, but the resulting behavior should be identical to before.

These have been compile tested only. They (especially the last two) need to run through teuthology. The series is based on the current testing branch:
5eeb14f ceph: check OSD caps before read/write

-Alex

Alex Elder (7):
messenger: update state diagram
messenger: add some clarifying comments
messenger: rename "out_kvec_left" field
osd_client: don't supply connection to handle_reply()
osd_client: improve safety of osd_req_op_data() macro
messenger: encapsulate grabbing incoming message
messenger: con_work() is not really a loop

 include/linux/ceph/messenger.h |   2 +-
 net/ceph/messenger.c           | 254 +++++++++++++++++++++++------------------
 net/ceph/osd_client.c          |  16 +--
 3 files changed, 156 insertions(+), 116 deletions(-)
Signature · 1 line
2.1.0
author
#1 lore raw

PATCH 1/7 messenger: update state diagram

Update the fabulous state diagram in "messenger.c" to reflect the way the code operates. Two new transitions are added:

    CLOSED -> CLOSED
    CLOSING -> CLOSING
---
 net/ceph/messenger.c | 51 +++++++++++++++++++++++++--------------------------
 1 file changed, 25 insertions(+), 26 deletions(-)
net/ceph/messenger.c +25 -26
index 967080a..9dfdb20 100644
@@ -46,32 +46,31 @@
  *          | con_sock_state_init()
  *          v
  *      ----------
- *      | CLOSED |  initialized, but no socket (and no
- *      ----------  TCP connection)
- *       ^      \
- *       |       \ con_sock_state_connecting()
- *       |        ----------------------
- *       |                              \
- *       + con_sock_state_closed()       \
- *       |+---------------------------    \
- *       | \                          \    \
- *       |  -----------                \    \
- *       |  | CLOSING |  socket event;  \    \
- *       |  -----------  await close     \    \
- *       |       ^                        \   |
- *       |       |                         \  |
- *       |       + con_sock_state_closing() \ |
- *       |      / \                         | |
- *       |     /   ---------------          | |
- *       |    /                   \         v v
- *       |   /                    --------------
- *       |  /    -----------------| CONNECTING |  socket created, TCP
- *       |  |   /                 --------------  connect initiated
- *       |  |   | con_sock_state_connected()
- *       |  |   v
- *      -------------
- *      | CONNECTED |  TCP connection established
- *      -------------
+ *      | CLOSED |  initialized, but no socket (and no TCP connection)
+ *      ----------
+ *       ^   \  \  con_sock_state_connecting()
+ *       |   /   ------------------------
+ *       +<--                            \
+ *       |\  cen_sock_state_closed()      \
+ *       | +<--------------------------    \
+ *       |  \                          \    \
+ *       | -----------                  \    \
+ *       | | CLOSING |  socket event;    \    \
+ *       | -----------  await close       \    \
+ *       |    /   ^                        \    \
+ *       |    \   |                         \   |
+ *       |     -->+ con_sock_state_closing() \  |
+ *       |       / \                         |  |
+ *       |      /   ----------------------   |  |
+ *       |     /                          \  |  v
+ *       |    /                        --------------
+ *       |   /    ---------------------| CONNECTING |  socket created, TCP
+ *       |   |   /                     --------------  connect initiated
+ *       |   |   | con_sock_state_connected()
+ *       |   |   v
+ *     -------------
+ *     | CONNECTED |  TCP connection established
+ *     -------------
  *
  * State values for ceph_connection->sock_state; NEW is assumed to be 0.
  */
Signature · 1 line
2.1.0
author
#2 lore raw

PATCH 2/7 messenger: add some clarifying comments

In reply to Alex Elder

In con_sock_closed() we test *and* clear the socket closed connection flag. It's not immediately obvious why the flag needs to be cleared. Add a comment so that's a little clearer.

Similarly, we only want to do backoff processing once each time the backoff flag is set. Add a comment about that as well.

---
 net/ceph/messenger.c | 9 +++++++++
 1 file changed, 9 insertions(+)
net/ceph/messenger.c +9 -0
index 9dfdb20..37b0fa7 100644
@@ -2733,6 +2733,10 @@ static void cancel_con(struct ceph_connection *con)
 
 static bool con_sock_closed(struct ceph_connection *con)
 {
+	/*
+	 * We want to handle the event of the socket being
+	 * closed once, so clear the flag in case it is set.
+	 */
 	if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
 		return false;
 
@@ -2764,6 +2768,11 @@ static bool con_backoff(struct ceph_connection *con)
 {
 	int ret;
 
+	/*
+	 * See if we need to back off.  We only want to do this
+	 * processing once each time the flag is set, so clear the
+	 * flag after it's tested.
+	 */
 	if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
 		return false;
 
Signature · 1 line
2.1.0
author
#3 lore raw

PATCH 3/7 messenger: rename "out_kvec_left" field

In reply to Alex Elder

The name "out_kvec_left" in the ceph_connection structure is sort of ambiguous. It represents the number of the entries of the out_kvec[] array that are filled with data to send. The name can easily be misinterpreted to mean the number of such entries that are available.

Change the name to "out_kvec_used" to avoid misunderstanding.

---
 include/linux/ceph/messenger.h |  2 +-
 net/ceph/messenger.c           | 22 +++++++++++-----------
 2 files changed, 12 insertions(+), 12 deletions(-)
include/linux/ceph/messenger.h +1 -1
index e154994..c457f13 100644
@@ -230,7 +230,7 @@ struct ceph_connection {
 
 	struct kvec out_kvec[8],         /* sending header/footer data */
 		*out_kvec_cur;
-	int out_kvec_left;   /* kvec's left in out_kvec */
+	int out_kvec_used;   /* kvec's used in out_kvec */
 	int out_skip;        /* skip this many bytes */
 	int out_kvec_bytes;  /* total bytes left */
 	bool out_kvec_is_msg; /* kvec refers to out_msg */
net/ceph/messenger.c +11 -11
index 37b0fa7..a0d2673 100644
@@ -773,7 +773,7 @@ static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
 
 static void con_out_kvec_reset(struct ceph_connection *con)
 {
-	con->out_kvec_left = 0;
+	con->out_kvec_used = 0;
 	con->out_kvec_bytes = 0;
 	con->out_kvec_cur = &con->out_kvec[0];
 }
@@ -783,12 +783,12 @@ static void con_out_kvec_add(struct ceph_connection *con,
 {
 	int index;
 
-	index = con->out_kvec_left;
+	index = con->out_kvec_used;
 	BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
 
 	con->out_kvec[index].iov_len = size;
 	con->out_kvec[index].iov_base = data;
-	con->out_kvec_left++;
+	con->out_kvec_used++;
 	con->out_kvec_bytes += size;
 }
 
@@ -1194,7 +1194,7 @@ static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
 static void prepare_write_message_footer(struct ceph_connection *con)
 {
 	struct ceph_msg *m = con->out_msg;
-	int v = con->out_kvec_left;
+	int v = con->out_kvec_used;
 
 	m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
 
@@ -1213,7 +1213,7 @@ static void prepare_write_message_footer(struct ceph_connection *con)
 		con->out_kvec[v].iov_len = sizeof(m->old_footer);
 		con->out_kvec_bytes += sizeof(m->old_footer);
 	}
-	con->out_kvec_left++;
+	con->out_kvec_used++;
 	con->out_more = m->more_to_follow;
 	con->out_msg_done = true;
 }
@@ -1462,7 +1462,7 @@ static int write_partial_kvec(struct ceph_connection *con)
 	dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
 	while (con->out_kvec_bytes > 0) {
 		ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
-				       con->out_kvec_left, con->out_kvec_bytes,
+				       con->out_kvec_used, con->out_kvec_bytes,
 				       con->out_more);
 		if (ret <= 0)
 			goto out;
@@ -1472,10 +1472,10 @@ static int write_partial_kvec(struct ceph_connection *con)
 
 		/* account for full iov entries consumed */
 		while (ret >= con->out_kvec_cur->iov_len) {
-			BUG_ON(!con->out_kvec_left);
+			BUG_ON(!con->out_kvec_used);
 			ret -= con->out_kvec_cur->iov_len;
 			con->out_kvec_cur++;
-			con->out_kvec_left--;
+			con->out_kvec_used--;
 		}
 		/* and for a partially-consumed entry */
 		if (ret) {
@@ -1483,12 +1483,12 @@ static int write_partial_kvec(struct ceph_connection *con)
 			con->out_kvec_cur->iov_base += ret;
 		}
 	}
-	con->out_kvec_left = 0;
+	con->out_kvec_used = 0;
 	con->out_kvec_is_msg = false;
 	ret = 1;
 out:
 	dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
-	     con->out_kvec_bytes, con->out_kvec_left, ret);
+	     con->out_kvec_bytes, con->out_kvec_used, ret);
 	return ret;  /* done! */
 }
 
@@ -2497,7 +2497,7 @@ more_kvec:
 		if (ret <= 0)
 			goto out;
 	}
-	if (con->out_kvec_left) {
+	if (con->out_kvec_used) {
 		ret = write_partial_kvec(con);
 		if (ret <= 0)
 			goto out;
Signature · 1 line
2.1.0
author
#4 lore raw

PATCH 4/7 osd_client: don't supply connection to handle_reply()

In reply to Alex Elder

The connection parameter in handle_reply() is never used, so get rid of it.

---
 net/ceph/osd_client.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/ceph/osd_client.c +2 -3
index e3c2e8b..6539dfc 100644
@@ -1748,8 +1748,7 @@ static void complete_request(struct ceph_osd_request *req)
  * handle osd op reply.  either call the callback if it is specified,
  * or do the completion to wake up the waiting thread.
  */
-static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
-			 struct ceph_connection *con)
+static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
 {
 	void *p, *end;
 	struct ceph_osd_request *req;
@@ -2796,7 +2795,7 @@ static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
 		ceph_osdc_handle_map(osdc, msg);
 		break;
 	case CEPH_MSG_OSD_OPREPLY:
-		handle_reply(osdc, msg, con);
+		handle_reply(osdc, msg);
 		break;
 	case CEPH_MSG_WATCH_NOTIFY:
 		handle_watch_notify(osdc, msg);
Signature · 1 line
2.1.0
author
#5 lore raw

PATCH 5/7 osd_client: improve safety of osd_req_op_data() macro

In reply to Alex Elder

The osd_req_op_data() macro uses (and thus evaluates) two of its parameters more than once. This isn't guaranteed safe. Define and use some local variables to fix that.

---
 net/ceph/osd_client.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/ceph/osd_client.c +7 -4
index 6539dfc..4602442 100644
@@ -120,10 +120,13 @@ static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
 }
 #endif /* CONFIG_BLOCK */
 
-#define osd_req_op_data(oreq, whch, typ, fld)	\
-	({						\
-		BUG_ON(whch >= (oreq)->r_num_ops);	\
-		&(oreq)->r_ops[whch].typ.fld;		\
+#define osd_req_op_data(oreq, whch, typ, fld)				\
+	({								\
+		struct ceph_osd_request *__oreq = (oreq);		\
+		unsigned int __whch = (whch);				\
+									\
+		BUG_ON(__whch >= __oreq->r_num_ops);			\
+		&__oreq->r_ops[__whch].typ.fld;				\
 	})
 
 static struct ceph_osd_data *
Signature · 1 line
2.1.0
author
#6 lore raw

PATCH 6/7 messenger: encapsulate grabbing incoming message

In reply to Alex Elder

There are currently three places in the messenger code that grab a connection's incoming message (i.e., get the message--if any--and and replace the connection's incoming message with a null pointer).

Create a new function ceph_con_in_msg_grab() to encapsulate this operation, returning what had been the connection's incoming message. The connection's reference to the message is transferred to the caller. In the new function, test for a few null pointers before using them, in case of pathological errors.

Do not call BUG_ON() if the message's connection pointer is bad. Instead, report an error, then drop and ignore the message. Take care to use the right connection's "put" operation.

Establish the naming convention that "in_msg" is a pointer to a message that was a connection's incoming message. (This affects some additional code in process_message().)

---
 net/ceph/messenger.c | 88 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 60 insertions(+), 28 deletions(-)
net/ceph/messenger.c +60 -28
index a0d2673..ec60c23 100644
@@ -650,21 +650,55 @@ static void ceph_msg_remove_list(struct list_head *head)
 	}
 }
 
+/*
+ * Grab the given connection's incoming message.
+ */
+static struct ceph_msg *ceph_con_in_msg_grab(struct ceph_connection *con)
+{
+	struct ceph_msg *in_msg;
+	struct ceph_connection *in_msg_con;
+
+	if (!con->in_msg)
+		return NULL;
+
+	/*
+	 * Grab the incoming message and its connection, then update
+	 * their pointers to each other.  The connection's reference
+	 * to the message is transferred to the caller unless there
+	 * is an error.
+	 */
+	in_msg = con->in_msg;
+	con->in_msg = NULL;
+	in_msg_con = in_msg->con;
+	in_msg->con = NULL;
+
+	if (in_msg_con && in_msg->con->ops)
+		in_msg_con->ops->put(in_msg_con);
+
+	if (in_msg_con == con)
+		return in_msg;
+
+	pr_err("incoming message %p with bad connection (%p != %p)\n",
+		in_msg, in_msg_con, con);
+	ceph_msg_put(in_msg);
+
+	return NULL;
+}
+
 static void reset_connection(struct ceph_connection *con)
 {
+	struct ceph_msg *in_msg;
+
 	/* reset connection, out_queue, msg_ and connect_seq */
 	/* discard existing out_queue and msg_seq */
 	dout("reset_connection %p\n", con);
 	ceph_msg_remove_list(&con->out_queue);
 	ceph_msg_remove_list(&con->out_sent);
 
-	if (con->in_msg) {
-		BUG_ON(con->in_msg->con != con);
-		con->in_msg->con = NULL;
-		ceph_msg_put(con->in_msg);
-		con->in_msg = NULL;
-		con->ops->put(con);
-	}
+	/* If there is an incoming message, grab it and release it */
+	in_msg = ceph_con_in_msg_grab(con);
+	if (in_msg)
+		ceph_msg_put(in_msg);
 
 	con->connect_seq = 0;
 	con->out_seq = 0;
@@ -2428,30 +2462,29 @@ static int read_partial_message(struct ceph_connection *con)
  */
 static void process_message(struct ceph_connection *con)
 {
-	struct ceph_msg *msg;
+	struct ceph_msg *in_msg = ceph_con_in_msg_grab(con);
 
-	BUG_ON(con->in_msg->con != con);
-	con->in_msg->con = NULL;
-	msg = con->in_msg;
-	con->in_msg = NULL;
-	con->ops->put(con);
+	if (!in_msg) {
+		pr_err("no message to process");
+		return;
+	}
 
 	/* if first message, set peer_name */
 	if (con->peer_name.type == 0)
-		con->peer_name = msg->hdr.src;
+		con->peer_name = in_msg->hdr.src;
 
 	con->in_seq++;
 	mutex_unlock(&con->mutex);
 
 	dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
-	     msg, le64_to_cpu(msg->hdr.seq),
-	     ENTITY_NAME(msg->hdr.src),
-	     le16_to_cpu(msg->hdr.type),
-	     ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
-	     le32_to_cpu(msg->hdr.front_len),
-	     le32_to_cpu(msg->hdr.data_len),
+	     in_msg, le64_to_cpu(in_msg->hdr.seq),
+	     ENTITY_NAME(in_msg->hdr.src),
+	     le16_to_cpu(in_msg->hdr.type),
+	     ceph_msg_type_name(le16_to_cpu(in_msg->hdr.type)),
+	     le32_to_cpu(in_msg->hdr.front_len),
+	     le32_to_cpu(in_msg->hdr.data_len),
 	     con->in_front_crc, con->in_middle_crc, con->in_data_crc);
-	con->ops->dispatch(con, msg);
+	con->ops->dispatch(con, in_msg);
 
 	mutex_lock(&con->mutex);
 }
@@ -2876,6 +2909,8 @@ static void con_work(struct work_struct *work)
  */
 static void con_fault(struct ceph_connection *con)
 {
+	struct ceph_msg *in_msg;
+
 	dout("fault %p state %lu to peer %s\n",
 	     con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
 
@@ -2895,13 +2930,10 @@ static void con_fault(struct ceph_connection *con)
 		return;
 	}
 
-	if (con->in_msg) {
-		BUG_ON(con->in_msg->con != con);
-		con->in_msg->con = NULL;
-		ceph_msg_put(con->in_msg);
-		con->in_msg = NULL;
-		con->ops->put(con);
-	}
+	/* If there is an incoming message, grab it and release it */
+	in_msg = ceph_con_in_msg_grab(con);
+	if (in_msg)
+		ceph_msg_put(in_msg);
 
 	/* Requeue anything that hasn't been acked */
 	list_splice_init(&con->out_sent, &con->out_queue);
Signature · 1 line
2.1.0
author
#7 lore raw

PATCH 7/7 messenger: con_work() is not really a loop

In reply to Alex Elder

In con_work(), a while(true) loop is used to contain a block of code. The normal path through that code does *not* actually loop. The only time it loops is if a socket read or write operation returns -EAGAIN. So the use of the loop control structure is a little misleading.

Restructure that block so it's *not* a loop, and use "goto" calls rather than a loop to implement the control flow.

---
 net/ceph/messenger.c | 84 +++++++++++++++++++++++++---------------------------
 1 file changed, 41 insertions(+), 43 deletions(-)
net/ceph/messenger.c +41 -43
index ec60c23..053c5f3 100644
@@ -2845,54 +2845,52 @@ static void con_work(struct work_struct *work)
 	struct ceph_connection *con = container_of(work, struct ceph_connection,
 						   work.work);
 	bool fault;
+	int ret;
 
 	mutex_lock(&con->mutex);
-	while (true) {
-		int ret;
-
-		if ((fault = con_sock_closed(con))) {
-			dout("%s: con %p SOCK_CLOSED\n", __func__, con);
-			break;
-		}
-		if (con_backoff(con)) {
-			dout("%s: con %p BACKOFF\n", __func__, con);
-			break;
-		}
-		if (con->state == CON_STATE_STANDBY) {
-			dout("%s: con %p STANDBY\n", __func__, con);
-			break;
-		}
-		if (con->state == CON_STATE_CLOSED) {
-			dout("%s: con %p CLOSED\n", __func__, con);
-			BUG_ON(con->sock);
-			break;
-		}
-		if (con->state == CON_STATE_PREOPEN) {
-			dout("%s: con %p PREOPEN\n", __func__, con);
-			BUG_ON(con->sock);
-		}
-
-		ret = try_read(con);
-		if (ret < 0) {
-			if (ret == -EAGAIN)
-				continue;
-			if (!con->error_msg)
-				con->error_msg = "socket error on read";
-			fault = true;
-			break;
-		}
+again:
+	fault = con_sock_closed(con);
+	if (fault) {
+		dout("%s: con %p SOCK_CLOSED\n", __func__, con);
+		goto done;
+	}
+	if (con_backoff(con)) {
+		dout("%s: con %p BACKOFF\n", __func__, con);
+		goto done;
+	}
+	if (con->state == CON_STATE_STANDBY) {
+		dout("%s: con %p STANDBY\n", __func__, con);
+		goto done;
+	}
+	if (con->state == CON_STATE_CLOSED) {
+		dout("%s: con %p CLOSED\n", __func__, con);
+		BUG_ON(con->sock);
+		goto done;
+	}
+	if (con->state == CON_STATE_PREOPEN) {
+		dout("%s: con %p PREOPEN\n", __func__, con);
+		BUG_ON(con->sock);
+	}
 
-		ret = try_write(con);
-		if (ret < 0) {
-			if (ret == -EAGAIN)
-				continue;
-			if (!con->error_msg)
-				con->error_msg = "socket error on write";
-			fault = true;
-		}
+	ret = try_read(con);
+	if (ret < 0) {
+		if (ret == -EAGAIN)
+			goto again;
+		if (!con->error_msg)
+			con->error_msg = "socket error on read";
+		fault = true;
+		goto done;
+	}
 
-		break;	/* If we make it to here, we're done */
+	ret = try_write(con);
+	if (ret < 0) {
+		if (ret == -EAGAIN)
+			goto again;
+		if (!con->error_msg)
+			con->error_msg = "socket error on write";
+		fault = true;
 	}
+done:
 	if (fault)
 		con_fault(con);
 	mutex_unlock(&con->mutex);
Signature · 1 line
2.1.0