Skip to contentSkip to conversation

libceph: fix osd request encoding regression

PATCH In review Yan, Zheng 2 participants 6 messages Jul 24, 2017 – Jul 26, 2017 Ceph common code (libceph)

lore
Share & tools
lore

Yan, Zheng

author
#0 lore raw

PATCH 1/1 libceph: fix osd request encoding regression

The new BUG_ON in encode_request_partial() verifies that space used by encoding request front is exactly equal to request message size. This is wrong because request messages allocated from mempool always have size PAGE_SIZE.

---
 net/ceph/osd_client.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
net/ceph/osd_client.c +9 -8
index 5c9d696..81f6199 100644
@@ -1913,10 +1913,11 @@ static void encode_request_partial(struct ceph_osd_request *req,
 	}
 
 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
-	BUG_ON(p != end - 8); /* space for features */
+	BUG_ON(p + 8 > end); /* space for features */
 
 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
-	/* front_len is finalized in encode_request_finish() */
+	msg->front.iov_len = p + 8 - msg->front.iov_base;
+	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
 	msg->hdr.data_len = cpu_to_le32(data_len);
 	/*
 	 * The header "data_off" is a hint to the receiver allowing it
@@ -1932,7 +1933,7 @@ static void encode_request_partial(struct ceph_osd_request *req,
 static void encode_request_finish(struct ceph_msg *msg)
 {
 	void *p = msg->front.iov_base;
-	void *const end = p + msg->front_alloc_len;
+	void *const end = p + msg->front.iov_len;
 
 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
 		/* luminous OSD -- encode features and be done */
@@ -2008,11 +2009,11 @@ static void encode_request_finish(struct ceph_msg *msg)
 		p += tail_len;
 
 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
-	}
 
-	BUG_ON(p > end);
-	msg->front.iov_len = p - msg->front.iov_base;
-	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+		BUG_ON(p > end);
+		msg->front.iov_len = p - msg->front.iov_base;
+		msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+	}
 
 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
@@ -3981,7 +3982,7 @@ static struct ceph_msg *create_backoff_message(
 		return NULL;
 
 	p = msg->front.iov_base;
-	end = p + msg->front_alloc_len;
+	end = p + msg->front.iov_len;
 
 	encode_spgid(&p, &backoff->spgid);
 	ceph_encode_32(&p, map_epoch);
Signature · 1 line
2.9.4
maintainer
#1 lore raw
Quoted 54 lines · Zheng

The new BUG_ON in encode_request_partial() verifies that space used by encoding request front is exactly equal to request message size. This is wrong because request messages allocated from mempool always have size PAGE_SIZE.

---
 net/ceph/osd_client.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
net/ceph/osd_client.c +8 -7
index 5c9d696..81f6199 100644
@@ -1913,10 +1913,11 @@ static void encode_request_partial(struct ceph_osd_request *req,
        }
 
        ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
-       BUG_ON(p != end - 8); /* space for features */
+       BUG_ON(p + 8 > end); /* space for features */
 
        msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
-       /* front_len is finalized in encode_request_finish() */
+       msg->front.iov_len = p + 8 - msg->front.iov_base;
+       msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
        msg->hdr.data_len = cpu_to_le32(data_len);
        /*
         * The header "data_off" is a hint to the receiver allowing it
@@ -1932,7 +1933,7 @@ static void encode_request_partial(struct ceph_osd_request *req,
 static void encode_request_finish(struct ceph_msg *msg)
 {
        void *p = msg->front.iov_base;
-       void *const end = p + msg->front_alloc_len;
+       void *const end = p + msg->front.iov_len;
 
        if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
                /* luminous OSD -- encode features and be done */
@@ -2008,11 +2009,11 @@ static void encode_request_finish(struct ceph_msg *msg)
                p += tail_len;
 
                msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
-       }
 
-       BUG_ON(p > end);
-       msg->front.iov_len = p - msg->front.iov_base;
-       msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+               BUG_ON(p > end);
+               msg->front.iov_len = p - msg->front.iov_base;
+               msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+       }
 
        dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
             le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),

net/ceph/osd_client.c static struct ceph_msg *create_backoff_message( · line 3988

Quoted 8 lines
net/ceph/osd_client.c +1 -1
@@ -3981,7 +3982,7 @@ static struct ceph_msg *create_backoff_message(
                return NULL;
 
        p = msg->front.iov_base;
-       end = p + msg->front_alloc_len;
+       end = p + msg->front.iov_len;
 
        encode_spgid(&p, &backoff->spgid);
        ceph_encode_32(&p, map_epoch);

Hi Zheng,

How about the attached patch instead? It's shorter and more importantly preserves the existing structure.

What is create_backoff_message() hunk for? Backoff messages are never allocated out of ceph_msgpool.

Thanks,

Ilya

From ca074c13f159e10f0b0cc3f2af74647925e600f1 Mon Sep 17 00:00:00 2001
From: Ilya Dryomov <[email protected]>
Date: Tue, 25 Jul 2017 14:40:03 +0200
Subject: [PATCH] libceph: make encode_request_*() work with r_mempool requests

Messages allocated out of ceph_msgpool have a fixed front length (pool->front_len). Asserting that the entire front has been filled while encoding is thus wrong.

Fixes 8cb441c0545d ("libceph: MOSDOp v8 encoding (actual spgid + full hash)")
---
 net/ceph/osd_client.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
net/ceph/osd_client.c +6 -3
index 901bb8221366..b5f016cb9569 100644
@@ -1918,10 +1918,12 @@ static void encode_request_partial(struct ceph_osd_request *req,
 	}
 
 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
-	BUG_ON(p != end - 8); /* space for features */
+	BUG_ON(p > end - 8); /* space for features */
 
 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
 	/* front_len is finalized in encode_request_finish() */
+	msg->front.iov_len = p - msg->front.iov_base;
+	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
 	msg->hdr.data_len = cpu_to_le32(data_len);
 	/*
 	 * The header "data_off" is a hint to the receiver allowing it
@@ -1937,11 +1939,12 @@ static void encode_request_partial(struct ceph_osd_request *req,
 static void encode_request_finish(struct ceph_msg *msg)
 {
 	void *p = msg->front.iov_base;
+	void *const partial_end = p + msg->front.iov_len;
 	void *const end = p + msg->front_alloc_len;
 
 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
 		/* luminous OSD -- encode features and be done */
-		p = end - 8;
+		p = partial_end;
 		ceph_encode_64(&p, msg->con->peer_features);
 	} else {
 		struct {
@@ -1984,7 +1987,7 @@ static void encode_request_finish(struct ceph_msg *msg)
 		oid_len = p - oid;
 
 		tail = p;
-		tail_len = (end - p) - 8;
+		tail_len = partial_end - p;
 
 		p = msg->front.iov_base;
 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
Signature · 1 line
2.4.3
author
#2 lore raw
Quoted 64 lines · Ilya Dryomov
Quoted 60 lines · Zheng

The new BUG_ON in encode_request_partial() verifies that space used by encoding request front is exactly equal to request message size. This is wrong because request messages allocated from mempool always have size PAGE_SIZE.

---
net/ceph/osd_client.c | 17 +++++++++--------

1 file changed, 9 insertions(+), 8 deletions(-)
net/ceph/osd_client.c +9 -8
index 5c9d696..81f6199 100644
@@ -1913,10 +1913,11 @@ static void encode_request_partial(struct ceph_osd_request *req,
       }
 
       ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
-       BUG_ON(p != end - 8); /* space for features */
+       BUG_ON(p + 8 > end); /* space for features */
 
       msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
-       /* front_len is finalized in encode_request_finish() */
+       msg->front.iov_len = p + 8 - msg->front.iov_base;
+       msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
       msg->hdr.data_len = cpu_to_le32(data_len);
       /*
        * The header "data_off" is a hint to the receiver allowing it
@@ -1932,7 +1933,7 @@ static void encode_request_partial(struct ceph_osd_request *req,
 static void encode_request_finish(struct ceph_msg *msg)
 {
       void *p = msg->front.iov_base;
-       void *const end = p + msg->front_alloc_len;
+       void *const end = p + msg->front.iov_len;
 
       if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
               /* luminous OSD -- encode features and be done */
@@ -2008,11 +2009,11 @@ static void encode_request_finish(struct ceph_msg *msg)
               p += tail_len;
 
               msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
-       }
 
-       BUG_ON(p > end);
-       msg->front.iov_len = p - msg->front.iov_base;
-       msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+               BUG_ON(p > end);
+               msg->front.iov_len = p - msg->front.iov_base;
+               msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+       }
 
       dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
            le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
@@ -3981,7 +3982,7 @@ static struct ceph_msg *create_backoff_message(
               return NULL;
 
       p = msg->front.iov_base;
-       end = p + msg->front_alloc_len;
+       end = p + msg->front.iov_len;
 
Quoted 7 lines

net/ceph/osd_client.c static struct ceph_msg *create_backoff_message( · line 3988

Quoted 2 lines
net/ceph/osd_client.c +0 -0
       encode_spgid(&p, &backoff->spgid);
       ceph_encode_32(&p, map_epoch);

Hi Zheng,

How about the attached patch instead? It's shorter and more importantly preserves the existing structure.

does encode_request_finish() get called each time we re-send a message? If it does, your patch seems incorrect. encode_request_finish() appends extra 8 bytes to the message each time it get called.

Quoted 3 lines

What is create_backoff_message() hunk for? Backoff messages are never allocated out of ceph_msgpool.

For unifying the code.

Regards
Yan, Zheng

Quoted 5 lines

Thanks,

Ilya
<0001-libceph-make-encode_request_-work-with-r_mempool-req.patch>

maintainer
#3 lore raw
Quoted 69 lines · Zheng
Quoted 69 lines · Ilya Dryomov
Quoted 54 lines · Zheng

The new BUG_ON in encode_request_partial() verifies that space used by encoding request front is exactly equal to request message size. This is wrong because request messages allocated from mempool always have size PAGE_SIZE.

---
net/ceph/osd_client.c | 17 +++++++++--------

1 file changed, 9 insertions(+), 8 deletions(-)
net/ceph/osd_client.c +8 -7
index 5c9d696..81f6199 100644
@@ -1913,10 +1913,11 @@ static void encode_request_partial(struct ceph_osd_request *req,
       }
 
       ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
-       BUG_ON(p != end - 8); /* space for features */
+       BUG_ON(p + 8 > end); /* space for features */
 
       msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
-       /* front_len is finalized in encode_request_finish() */
+       msg->front.iov_len = p + 8 - msg->front.iov_base;
+       msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
       msg->hdr.data_len = cpu_to_le32(data_len);
       /*
        * The header "data_off" is a hint to the receiver allowing it
@@ -1932,7 +1933,7 @@ static void encode_request_partial(struct ceph_osd_request *req,
 static void encode_request_finish(struct ceph_msg *msg)
 {
       void *p = msg->front.iov_base;
-       void *const end = p + msg->front_alloc_len;
+       void *const end = p + msg->front.iov_len;
 
       if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
               /* luminous OSD -- encode features and be done */
@@ -2008,11 +2009,11 @@ static void encode_request_finish(struct ceph_msg *msg)
               p += tail_len;
 
               msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
-       }
 
-       BUG_ON(p > end);
-       msg->front.iov_len = p - msg->front.iov_base;
-       msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+               BUG_ON(p > end);
+               msg->front.iov_len = p - msg->front.iov_base;
+               msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+       }
 
       dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
            le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),

net/ceph/osd_client.c static struct ceph_msg *create_backoff_message( · line 3988

Quoted 8 lines
net/ceph/osd_client.c +1 -1
@@ -3981,7 +3982,7 @@ static struct ceph_msg *create_backoff_message(
               return NULL;
 
       p = msg->front.iov_base;
-       end = p + msg->front_alloc_len;
+       end = p + msg->front.iov_len;
 
       encode_spgid(&p, &backoff->spgid);
       ceph_encode_32(&p, map_epoch);

Hi Zheng,

Quoted 6 lines
Quoted 2 lines

How about the attached patch instead? It's shorter and more importantly preserves the existing structure.

does encode_request_finish() get called each time we re-send a message? If it does, your patch seems incorrect. encode_request_finish() appends extra 8 bytes to the message each time it get called.

Hrm, it is. But then your patch doesn't fix it either because the problem is the ->reencode_message() call itself -- I didn't intend it to be called on every resend. Let me run some tests...

Thanks,

Ilya

maintainer
#4 lore raw
Quoted 73 lines · Ilya Dryomov
Quoted 72 lines · Zheng
Quoted 71 lines · Ilya Dryomov
Quoted 54 lines · Zheng

The new BUG_ON in encode_request_partial() verifies that space used by encoding request front is exactly equal to request message size. This is wrong because request messages allocated from mempool always have size PAGE_SIZE.

---
net/ceph/osd_client.c | 17 +++++++++--------

1 file changed, 9 insertions(+), 8 deletions(-)
net/ceph/osd_client.c +8 -7
index 5c9d696..81f6199 100644
@@ -1913,10 +1913,11 @@ static void encode_request_partial(struct ceph_osd_request *req,
       }
 
       ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
-       BUG_ON(p != end - 8); /* space for features */
+       BUG_ON(p + 8 > end); /* space for features */
 
       msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
-       /* front_len is finalized in encode_request_finish() */
+       msg->front.iov_len = p + 8 - msg->front.iov_base;
+       msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
       msg->hdr.data_len = cpu_to_le32(data_len);
       /*
        * The header "data_off" is a hint to the receiver allowing it
@@ -1932,7 +1933,7 @@ static void encode_request_partial(struct ceph_osd_request *req,
 static void encode_request_finish(struct ceph_msg *msg)
 {
       void *p = msg->front.iov_base;
-       void *const end = p + msg->front_alloc_len;
+       void *const end = p + msg->front.iov_len;
 
       if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
               /* luminous OSD -- encode features and be done */
@@ -2008,11 +2009,11 @@ static void encode_request_finish(struct ceph_msg *msg)
               p += tail_len;
 
               msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
-       }
 
-       BUG_ON(p > end);
-       msg->front.iov_len = p - msg->front.iov_base;
-       msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+               BUG_ON(p > end);
+               msg->front.iov_len = p - msg->front.iov_base;
+               msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+       }
 
       dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
            le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),

net/ceph/osd_client.c static struct ceph_msg *create_backoff_message( · line 3988

Quoted 8 lines
net/ceph/osd_client.c +1 -1
@@ -3981,7 +3982,7 @@ static struct ceph_msg *create_backoff_message(
               return NULL;
 
       p = msg->front.iov_base;
-       end = p + msg->front_alloc_len;
+       end = p + msg->front.iov_len;
 
       encode_spgid(&p, &backoff->spgid);
       ceph_encode_32(&p, map_epoch);

Hi Zheng,

How about the attached patch instead? It's shorter and more importantly preserves the existing structure.

Quoted 7 lines
Quoted 3 lines

does encode_request_finish() get called each time we re-send a message? If it does, your patch seems incorrect. encode_request_finish() appends extra 8 bytes to the message each time it get called.

Hrm, it is. But then your patch doesn't fix it either because the problem is the ->reencode_message() call itself -- I didn't intend it to be called on every resend. Let me run some tests...

OK, so ->reencode_message() being called more than once could affect the MDS client, but since only the OSD client is using it, there are no immediate issues. I'll post the attached patch for the sake of robustness though.

Thanks,

Ilya

From 8524b57a5881b548ecb17b150aaf599e647b8982 Mon Sep 17 00:00:00 2001
From: Ilya Dryomov <[email protected]>
Date: Wed, 26 Jul 2017 09:59:15 +0200
Subject: [PATCH] libceph: don't call ->reencode_message() more than once per
message

Reencoding an already reencoded message is a bad idea. This could happen on Policy::stateful_server connections (!CEPH_MSG_CONNECT_LOSSY), such as MDS sessions.

This didn't pop up in testing because currently only OSD requests are reencoded and OSD sessions are always lossy.

Fixes 98ad5ebd1505 ("libceph: ceph_connection_operations::reencode_message() method")
---
 net/ceph/messenger.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/ceph/messenger.c +3 -3
index b7cc615d42ef..a67298c7e0cd 100644
@@ -1287,10 +1287,10 @@ static void prepare_write_message(struct ceph_connection *con)
 	if (m->needs_out_seq) {
 		m->hdr.seq = cpu_to_le64(++con->out_seq);
 		m->needs_out_seq = false;
-	}
 
-	if (con->ops->reencode_message)
-		con->ops->reencode_message(m);
+		if (con->ops->reencode_message)
+			con->ops->reencode_message(m);
+	}
 
 	dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
 	     m, con->out_seq, le16_to_cpu(m->hdr.type),
Signature · 1 line
2.4.3
author
#5 lore raw
Quoted 81 lines · Ilya Dryomov
Quoted 77 lines · Ilya Dryomov
Quoted 75 lines · Zheng
Quoted 64 lines · Ilya Dryomov
Quoted 60 lines · Zheng

The new BUG_ON in encode_request_partial() verifies that space used by encoding request front is exactly equal to request message size. This is wrong because request messages allocated from mempool always have size PAGE_SIZE.

---
net/ceph/osd_client.c | 17 +++++++++--------

1 file changed, 9 insertions(+), 8 deletions(-)
net/ceph/osd_client.c +9 -8
index 5c9d696..81f6199 100644
@@ -1913,10 +1913,11 @@ static void encode_request_partial(struct ceph_osd_request *req,
      }
 
      ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
-       BUG_ON(p != end - 8); /* space for features */
+       BUG_ON(p + 8 > end); /* space for features */
 
      msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
-       /* front_len is finalized in encode_request_finish() */
+       msg->front.iov_len = p + 8 - msg->front.iov_base;
+       msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
      msg->hdr.data_len = cpu_to_le32(data_len);
      /*
       * The header "data_off" is a hint to the receiver allowing it
@@ -1932,7 +1933,7 @@ static void encode_request_partial(struct ceph_osd_request *req,
 static void encode_request_finish(struct ceph_msg *msg)
 {
      void *p = msg->front.iov_base;
-       void *const end = p + msg->front_alloc_len;
+       void *const end = p + msg->front.iov_len;
 
      if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
              /* luminous OSD -- encode features and be done */
@@ -2008,11 +2009,11 @@ static void encode_request_finish(struct ceph_msg *msg)
              p += tail_len;
 
              msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
-       }
 
-       BUG_ON(p > end);
-       msg->front.iov_len = p - msg->front.iov_base;
-       msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+               BUG_ON(p > end);
+               msg->front.iov_len = p - msg->front.iov_base;
+               msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
+       }
 
      dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
           le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
@@ -3981,7 +3982,7 @@ static struct ceph_msg *create_backoff_message(
              return NULL;
 
      p = msg->front.iov_base;
-       end = p + msg->front_alloc_len;
+       end = p + msg->front.iov_len;
 
Quoted 7 lines

net/ceph/osd_client.c static struct ceph_msg *create_backoff_message( · line 3988

Quoted 2 lines
net/ceph/osd_client.c +0 -0
      encode_spgid(&p, &backoff->spgid);
      ceph_encode_32(&p, map_epoch);

Hi Zheng,

How about the attached patch instead? It's shorter and more importantly preserves the existing structure.

does encode_request_finish() get called each time we re-send a message? If it does, your patch seems incorrect. encode_request_finish() appends extra 8 bytes to the message each time it get called.

Quoted 9 lines
Quoted 3 lines

Hrm, it is. But then your patch doesn't fix it either because the problem is the ->reencode_message() call itself -- I didn't intend it to be called on every resend. Let me run some tests...

OK, so ->reencode_message() being called more than once could affect the MDS client, but since only the OSD client is using it, there are no immediate issues. I'll post the attached patch for the sake of robustness though.

With this new patch, your previous patch looks good.

Regards
Yan, Zheng

Quoted 4 lines

Thanks,

Ilya
<0001-libceph-don-t-call-reencode_message-more-than-once-p.patch>