Skip to contentSkip to conversation

libceph: fix sparse-read failure bug

PATCH Superseded [email protected] 3 participants 11 messages Dec 15, 2023 – Jan 22, 2024 3 messages not in the archive Ceph common code (libceph)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover libceph: fix sparse-read failure bug

From: Xiubo Li <[email protected]>

The debug logs:

2725523 <7>[ 8771.114348] libceph:  [0] got 1 extents
2725524 <7>[ 8771.114353] libceph:  [0] ext 0 off 0x66000 len 0x4000
2725525 <7>[ 8771.114370] libceph:  prep_next_sparse_read: [0] completed extent array len 1 cursor->resid 0
2725526 <7>[ 8771.114374] libceph:  read_partial have 0, left 21, con->v1.in_base_pos 53
2725527 <7>[ 8771.114379] libceph:  read_partial have 14, left 7, con->v1.in_base_pos 67         ====> there were still 7 bytes not received
2725528 <7>[ 8771.114382] libceph:  read_partial return 0
2725529 <7>[ 8771.114384] libceph:  try_read done on 0000000094d53202 ret 0
2725530 <7>[ 8771.114387] libceph:  try_write start 0000000094d53202 state 12
2725531 <7>[ 8771.114389] libceph:  try_write out_kvec_bytes 0
2725532 <7>[ 8771.114391] libceph:  try_write nothing else to write.
2725533 <7>[ 8771.114393] libceph:  try_write done on 0000000094d53202 ret 0
2725534 <7>[ 8771.114396] libceph:  put_osd 000000009b11f20c 5 -> 4
2725535 <7>[ 8771.114450] libceph:  ceph_sock_data_ready 0000000094d53202 state = 12, queueing work
2725536 <7>[ 8771.114454] libceph:  get_osd 000000009b11f20c 4 -> 5
2725537 <7>[ 8771.114457] libceph:  queue_con_delay 0000000094d53202 0
2725538 <7>[ 8771.114651] libceph:  try_read start 0000000094d53202 state 12
2725539 <7>[ 8771.114655] libceph:  try_read tag 7 in_base_pos 67
2725540 <7>[ 8771.114657] libceph:  read_partial_message con 0000000094d53202 msg 0000000060b8a473
2725541 <7>[ 8771.114660] libceph:  read_partial return 1
2725542 <7>[ 8771.114663] libceph:  read_partial have 14, left 7, con->v1.in_base_pos 67         ====> the rest 7 bytes came
2725543 <7>[ 8771.114669] libceph:  read_partial return 1
2725544 <7>[ 8771.114671] libceph:  read_partial_message got msg 0000000060b8a473 164 (4271800174) + 0 (0) + 16408 (2739232014)
2725545 <7>[ 8771.114677] libceph:  ===== 0000000060b8a473 73 from osd0 43=osd_opreply len 164+0+16408 (4271800174 0 2739232014) =====
2725546 <7>[ 8771.114683] libceph:  handle_reply msg 0000000060b8a473 tid 99
2725547 <7>[ 8771.114687] libceph:  handle_reply req 000000006ba179f6 tid 99 flags 0x400015 pgid 3.2b epoch 53 attempt 0 v 0'0 uv 5984

2725548 <7>[ 8771.114693] libceph: req 000000006ba179f6 tid 99 op 0 rval 0 len 16408
2725549 <7>[ 8771.114697] libceph: handle_reply req 000000006ba179f6 tid 99 result 0 data_len 16408
2725550 <7>[ 8771.114701] libceph: finish_request req 000000006ba179f6 tid 99 ====> the request was successfully finished

V3:
- rename read_sparse_msg_XX to read_partial_sparse_msg_XX
- fix the sparse-read bug in the messager v1 code.

V2:
- fix the sparse-read bug in the sparse-read code instead

Xiubo Li (3):
libceph: fail the sparse-read if there still has data in socket
libceph: rename read_sparse_msg_XX to read_partial_sparse_msg_XX
libceph: just wait for more data to be available on the socket

 include/linux/ceph/messenger.h |  2 ++
 net/ceph/messenger.c           |  1 +
 net/ceph/messenger_v1.c        | 29 ++++++++++++++++++++---------
 net/ceph/osd_client.c          |  5 ++++-
 4 files changed, 27 insertions(+), 10 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/3 libceph: fail the sparse-read if there still has data in socket

From: Xiubo Li <[email protected]>

Once this happens that means there have bugs.

URL: https://tracker.ceph.com/issues/63586

---
 net/ceph/osd_client.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/ceph/osd_client.c +3 -1
index 5753036d1957..848ef19055a0 100644
@@ -5912,10 +5912,12 @@ static int osd_sparse_read(struct ceph_connection *con,
 		fallthrough;
 	case CEPH_SPARSE_READ_DATA:
 		if (sr->sr_index >= count) {
-			if (sr->sr_datalen && count)
+			if (sr->sr_datalen) {
 				pr_warn_ratelimited("sr_datalen %u sr_index %d count %u\n",
 						    sr->sr_datalen, sr->sr_index,
 						    count);
+				return -EREMOTEIO;
+			}
 
 			sr->sr_state = CEPH_SPARSE_READ_HDR;
 			goto next_op;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 1/3] libceph: fail the sparse-read if there still has data in socket

Quoted 18 lines · On Fri, 2023-12-15 at 08:20 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

Once this happens that means there have bugs.

URL: https://tracker.ceph.com/issues/63586

---
 net/ceph/osd_client.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/ceph/osd_client.c +0 -0
index 5753036d1957..848ef19055a0 100644
@@ -5912,10 +5912,12 @@ static int osd_sparse_read(struct ceph_connection *con,
 		fallthrough;
 	case CEPH_SPARSE_READ_DATA:
 		if (sr->sr_index >= count) {

net/ceph/osd_client.c static int osd_sparse_read(struct ceph_connection *con, · line 5923

Quoted 10 lines
net/ceph/osd_client.c +3 -1
-			if (sr->sr_datalen && count)
+			if (sr->sr_datalen) {
 				pr_warn_ratelimited("sr_datalen %u sr_index %d count %u\n",
 						    sr->sr_datalen, sr->sr_index,
 						    count);
+				return -EREMOTEIO;
+			}
 
 			sr->sr_state = CEPH_SPARSE_READ_HDR;
 			goto next_op;

Do you really need to fail the read in this case? Would it not be better to just advance past the extra junk? Or is this problem more indicative of a malformed frame?

It'd be nice to have some specific explanation of the problem this is fixing and how it was triggered. If this due to a misbehaving server? Bad hw?

Signature · 1 line
Jeff Layton <[email protected]>
author
#3 lore raw

Re: [PATCH v3 1/3] libceph: fail the sparse-read if there still has data in socket

Quoted 33 lines · Jeff Layton
Quoted 18 lines · On Fri, 2023-12-15 at 08:20 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

Once this happens that means there have bugs.

URL: https://tracker.ceph.com/issues/63586

---
  net/ceph/osd_client.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
net/ceph/osd_client.c +0 -0
index 5753036d1957..848ef19055a0 100644
@@ -5912,10 +5912,12 @@ static int osd_sparse_read(struct ceph_connection *con,
  		fallthrough;
  	case CEPH_SPARSE_READ_DATA:
  		if (sr->sr_index >= count) {

net/ceph/osd_client.c static int osd_sparse_read(struct ceph_connection *con, · line 5923

Quoted 10 lines
net/ceph/osd_client.c +3 -1
-			if (sr->sr_datalen && count)
+			if (sr->sr_datalen) {
  				pr_warn_ratelimited("sr_datalen %u sr_index %d count %u\n",
  						    sr->sr_datalen, sr->sr_index,
  						    count);
+				return -EREMOTEIO;
+			}
 
  			sr->sr_state = CEPH_SPARSE_READ_HDR;
  			goto next_op;

Do you really need to fail the read in this case? Would it not be better to just advance past the extra junk? Or is this problem more indicative of a malformed frame?

Quoted 3 lines

It'd be nice to have some specific explanation of the problem this is fixing and how it was triggered. If this due to a misbehaving server? Bad hw?

Hi Jeff,

Why I added this check before was that when I was debugging the sparse-read issue last time I found even when the extent array 'count == 0', sometimes the 'sr_datalen != 0'. I just thought the ceph side's logic allowed it.

But this time from going through and debugging more in the ceph side code, I didn't get where was doing this. So I just suspected it should be an issue somewhere in kclient side and it also possibly would trigger the sparse-read bug.  So I just removed the 'count' check and finally found the root case for both these two issues.

IMO normally this shouldn't happen anyway. Once this happens in kclient side it will 100% corrupt the following msgs and reset the connection from my tests. Actually when the 'count ==0' the 'sr_datalen' will be a random number, sometimes very large, so just advancing past the extra junk makes no any sense.

Thanks

- Xiubo

#4 lore raw

PATCH v3 2/3 libceph: rename read_sparse_msg_XX to read_partial_sparse_msg_XX

In reply to [email protected]

From: Xiubo Li <[email protected]>

Actually the read_sparse_msg_XX functions allow to continue reading and parsing the socket buffer when handling of short receives.

Just rename it with _partial_ prefixed.

URL: https://tracker.ceph.com/issues/63586

---
 net/ceph/messenger_v1.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/ceph/messenger_v1.c +4 -4
index f9a50d7f0d20..4cb60bacf5f5 100644
@@ -991,7 +991,7 @@ static inline int read_partial_message_section(struct ceph_connection *con,
 	return read_partial_message_chunk(con, section, sec_len, crc);
 }
 
-static int read_sparse_msg_extent(struct ceph_connection *con, u32 *crc)
+static int read_partial_sparse_msg_extent(struct ceph_connection *con, u32 *crc)
 {
 	struct ceph_msg_data_cursor *cursor = &con->in_msg->cursor;
 	bool do_bounce = ceph_test_opt(from_msgr(con->msgr), RXBOUNCE);
@@ -1026,7 +1026,7 @@ static int read_sparse_msg_extent(struct ceph_connection *con, u32 *crc)
 	return 1;
 }
 
-static int read_sparse_msg_data(struct ceph_connection *con)
+static int read_partial_sparse_msg_data(struct ceph_connection *con)
 {
 	struct ceph_msg_data_cursor *cursor = &con->in_msg->cursor;
 	bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
@@ -1043,7 +1043,7 @@ static int read_sparse_msg_data(struct ceph_connection *con)
 							 con->v1.in_sr_len,
 							 &crc);
 		else if (cursor->sr_resid > 0)
-			ret = read_sparse_msg_extent(con, &crc);
+			ret = read_partial_sparse_msg_extent(con, &crc);
 
 		if (ret <= 0) {
 			if (do_datacrc)
@@ -1254,7 +1254,7 @@ static int read_partial_message(struct ceph_connection *con)
 			return -EIO;
 
 		if (m->sparse_read)
-			ret = read_sparse_msg_data(con);
+			ret = read_partial_sparse_msg_data(con);
 		else if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE))
 			ret = read_partial_msg_data_bounce(con);
 		else
Signature · 1 line
2.43.0
#5 lore raw

PATCH v3 3/3 libceph: just wait for more data to be available on the socket

In reply to [email protected]

From: Xiubo Li <[email protected]>

The messages from ceph maybe split into multiple socket packages and we just need to wait for all the data to be availiable on the sokcet.

This will add 'sr_total_resid' to record the total length for all data items for sparse-read message and 'sr_resid_elen' to record the current extent total length.

URL: https://tracker.ceph.com/issues/63586

---
 include/linux/ceph/messenger.h |  2 ++
 net/ceph/messenger.c           |  1 +
 net/ceph/messenger_v1.c        | 21 ++++++++++++++++-----
 net/ceph/osd_client.c          |  1 +
 4 files changed, 20 insertions(+), 5 deletions(-)
include/linux/ceph/messenger.h +2 -0
index 2eaaabbe98cb..05e9b39a58f8 100644
@@ -231,10 +231,12 @@ struct ceph_msg_data {
 
 struct ceph_msg_data_cursor {
 	size_t			total_resid;	/* across all data items */
+	size_t			sr_total_resid;	/* across all data items for sparse-read */
 
 	struct ceph_msg_data	*data;		/* current data item */
 	size_t			resid;		/* bytes not yet consumed */
 	int			sr_resid;	/* residual sparse_read len */
+	int			sr_resid_elen;	/* total sparse_read elen */
 	bool			need_crc;	/* crc update needed */
 	union {
 #ifdef CONFIG_BLOCK
net/ceph/messenger.c +1 -0
index 3c8b78d9c4d1..eafd592af382 100644
@@ -1073,6 +1073,7 @@ void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
 	cursor->total_resid = length;
 	cursor->data = msg->data;
 	cursor->sr_resid = 0;
+	cursor->sr_resid_elen = 0;
 
 	__ceph_msg_data_cursor_init(cursor);
 }
net/ceph/messenger_v1.c +16 -5
index 4cb60bacf5f5..7425fa26e4c3 100644
@@ -160,7 +160,9 @@ static size_t sizeof_footer(struct ceph_connection *con)
 static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
 {
 	/* Initialize data cursor if it's not a sparse read */
-	if (!msg->sparse_read)
+	if (msg->sparse_read)
+		msg->cursor.sr_total_resid = data_len;
+	else
 		ceph_msg_data_cursor_init(&msg->cursor, msg, data_len);
 }
 
@@ -1032,18 +1034,25 @@ static int read_partial_sparse_msg_data(struct ceph_connection *con)
 	bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
 	u32 crc = 0;
 	int ret = 1;
+	int len;
 
 	if (do_datacrc)
 		crc = con->in_data_crc;
 
-	do {
-		if (con->v1.in_sr_kvec.iov_base)
+	while (cursor->sr_total_resid && ret > 0) {
+		len = 0;
+		if (con->v1.in_sr_kvec.iov_base) {
 			ret = read_partial_message_chunk(con,
 							 &con->v1.in_sr_kvec,
 							 con->v1.in_sr_len,
 							 &crc);
-		else if (cursor->sr_resid > 0)
+			if (ret == 1)
+				len = con->v1.in_sr_len;
+		} else if (cursor->sr_resid > 0) {
 			ret = read_partial_sparse_msg_extent(con, &crc);
+			if (ret == 1)
+				len = cursor->sr_resid_elen;
+		}
 
 		if (ret <= 0) {
 			if (do_datacrc)
@@ -1051,11 +1060,13 @@ static int read_partial_sparse_msg_data(struct ceph_connection *con)
 			return ret;
 		}
 
+		cursor->sr_total_resid -= len;
+
 		memset(&con->v1.in_sr_kvec, 0, sizeof(con->v1.in_sr_kvec));
 		ret = con->ops->sparse_read(con, cursor,
 				(char **)&con->v1.in_sr_kvec.iov_base);
 		con->v1.in_sr_len = ret;
-	} while (ret > 0);
+	}
 
 	if (do_datacrc)
 		con->in_data_crc = crc;
net/ceph/osd_client.c +1 -0
index 848ef19055a0..b53b017afc0a 100644
@@ -5946,6 +5946,7 @@ static int osd_sparse_read(struct ceph_connection *con,
 
 		/* send back the new length and nullify the ptr */
 		cursor->sr_resid = elen;
+		cursor->sr_resid_elen = elen;
 		ret = elen;
 		*pbuf = NULL;
 
Signature · 1 line
2.43.0
maintainer
#6 lore raw

Re: [PATCH v3 3/3] libceph: just wait for more data to be available on the socket

Quoted 25 lines · On Fri, Dec 15, 2023 at 1:22 AM <[email protected]> wrote:

From: Xiubo Li <[email protected]>

The messages from ceph maybe split into multiple socket packages and we just need to wait for all the data to be availiable on the sokcet.

This will add 'sr_total_resid' to record the total length for all data items for sparse-read message and 'sr_resid_elen' to record the current extent total length.

URL: https://tracker.ceph.com/issues/63586

---
 include/linux/ceph/messenger.h |  2 ++
 net/ceph/messenger.c           |  1 +
 net/ceph/messenger_v1.c        | 21 ++++++++++++++++-----
 net/ceph/osd_client.c          |  1 +
 4 files changed, 20 insertions(+), 5 deletions(-)
include/linux/ceph/messenger.h +0 -0
index 2eaaabbe98cb..05e9b39a58f8 100644

include/linux/ceph/messenger.h struct ceph_msg_data { · line 239

Quoted 9 lines
include/linux/ceph/messenger.h +2 -0
@@ -231,10 +231,12 @@ struct ceph_msg_data {
 
 struct ceph_msg_data_cursor {
        size_t                  total_resid;    /* across all data items */
+       size_t                  sr_total_resid; /* across all data items for sparse-read */
 
        struct ceph_msg_data    *data;          /* current data item */
        size_t                  resid;          /* bytes not yet consumed */
        int                     sr_resid;       /* residual sparse_read len */
+       int                     sr_resid_elen;  /* total sparse_read elen */

Hi Xiubo,

Is adding yet another sparse-read field to the cursor really needed? Would making read_partial_sparse_msg_extent() decrement sr_total_resid as it goes just like sr_resid is decremented work?

Quoted 46 lines
include/linux/ceph/messenger.h +0 -0
        bool                    need_crc;       /* crc update needed */
        union {
 #ifdef CONFIG_BLOCK
net/ceph/messenger.c +1 -0
index 3c8b78d9c4d1..eafd592af382 100644
@@ -1073,6 +1073,7 @@ void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
        cursor->total_resid = length;
        cursor->data = msg->data;
        cursor->sr_resid = 0;
+       cursor->sr_resid_elen = 0;
 
        __ceph_msg_data_cursor_init(cursor);
 }
net/ceph/messenger_v1.c +7 -3
index 4cb60bacf5f5..7425fa26e4c3 100644
@@ -160,7 +160,9 @@ static size_t sizeof_footer(struct ceph_connection *con)
 static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
 {
        /* Initialize data cursor if it's not a sparse read */
-       if (!msg->sparse_read)
+       if (msg->sparse_read)
+               msg->cursor.sr_total_resid = data_len;
+       else
                ceph_msg_data_cursor_init(&msg->cursor, msg, data_len);
 }
 
@@ -1032,18 +1034,25 @@ static int read_partial_sparse_msg_data(struct ceph_connection *con)
        bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
        u32 crc = 0;
        int ret = 1;
+       int len;
 
        if (do_datacrc)
                crc = con->in_data_crc;
 
-       do {
-               if (con->v1.in_sr_kvec.iov_base)
+       while (cursor->sr_total_resid && ret > 0) {
+               len = 0;
+               if (con->v1.in_sr_kvec.iov_base) {
                        ret = read_partial_message_chunk(con,
                                                         &con->v1.in_sr_kvec,

net/ceph/messenger_v1.c static int read_partial_sparse_msg_data(struct ceph_connection *con) · line 1055

Quoted 10 lines
net/ceph/messenger_v1.c +6 -1
                                                         con->v1.in_sr_len,
                                                         &crc);
-               else if (cursor->sr_resid > 0)
+                       if (ret == 1)
+                               len = con->v1.in_sr_len;
+               } else if (cursor->sr_resid > 0) {
                        ret = read_partial_sparse_msg_extent(con, &crc);
+                       if (ret == 1)
+                               len = cursor->sr_resid_elen;
+               }

I was waiting for Jeff's review since this is his code, but it's been a while so I'll just comment.

To me, it's a sign of suboptimal structure that you needed to add new fields to the cursor just to be able tell whether this function is done with the message, because it's something that is already tracked but gets lost between the loops here.

Currently this function is structured as:

do {
if (set up for kvec)
read as much as possible into kvec
else if (set up for pages)
read as much as possible into pages

if (short read)
bail, will be called again later

invoke con->ops->sparse_read() for processing the thing what was read and setting up the next read OR setting up the initial read
} until (con->ops->sparse_read() returns "done")

If it was me, I would pursue refactoring this to:

do {
if (set up for kvec)
read as much as possible into kvec
else if (set up for pages)
read as much as possible into pages
else
bail

if (short read)
bail, will be called again later

invoke con->ops->sparse_read() for processing the thing that was read and setting up the next read
} until (con->ops->sparse_read() returns "done")

... and invoke con->ops->sparse_read() for the first time elsewhere, likely in prepare_message_data(). The rationale is that the state machine inside con->ops->sparse_read() is conceptually a cursor and prepare_message_data() is where the cursor is initialized for normal reads. This way no additional state should be needed.

Thanks,

Ilya

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

maintainer
#8 lore raw

Re: [PATCH v3 3/3] libceph: just wait for more data to be available on the socket

Quoted 139 lines · Xiubo Li

On 1/16/24 06:38, Ilya Dryomov wrote:

On Fri, Dec 15, 2023 at 1:22 AM <[email protected]> wrote:

From: Xiubo Li <[email protected]>

The messages from ceph maybe split into multiple socket packages and we just need to wait for all the data to be availiable on the sokcet.

This will add 'sr_total_resid' to record the total length for all data items for sparse-read message and 'sr_resid_elen' to record the current extent total length.

URL: https://tracker.ceph.com/issues/63586

---
 include/linux/ceph/messenger.h |  2 ++
 net/ceph/messenger.c           |  1 +
 net/ceph/messenger_v1.c        | 21 ++++++++++++++++-----
 net/ceph/osd_client.c          |  1 +
 4 files changed, 20 insertions(+), 5 deletions(-)
include/linux/ceph/messenger.h +2 -0
index 2eaaabbe98cb..05e9b39a58f8 100644
@@ -231,10 +231,12 @@ struct ceph_msg_data {
 
 struct ceph_msg_data_cursor {
        size_t                  total_resid;    /* across all data items */
+       size_t                  sr_total_resid; /* across all data items for sparse-read */
 
        struct ceph_msg_data    *data;          /* current data item */
        size_t                  resid;          /* bytes not yet consumed */
        int                     sr_resid;       /* residual sparse_read len */
+       int                     sr_resid_elen;  /* total sparse_read elen */
 
 Hi Xiubo,
 

Is adding yet another sparse-read field to the cursor really needed? Would making read_partial_sparse_msg_extent() decrement sr_total_resid as it goes just like sr_resid is decremented work?

This can be improved by removing it. Let me fix it.

bool need_crc; /* crc update needed */
union {
#ifdef CONFIG_BLOCK

net/ceph/messenger.c +1 -0
index 3c8b78d9c4d1..eafd592af382 100644
@@ -1073,6 +1073,7 @@ void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
        cursor->total_resid = length;
        cursor->data = msg->data;
        cursor->sr_resid = 0;
+       cursor->sr_resid_elen = 0;
 
        __ceph_msg_data_cursor_init(cursor);
 }
net/ceph/messenger_v1.c +13 -4
index 4cb60bacf5f5..7425fa26e4c3 100644
@@ -160,7 +160,9 @@ static size_t sizeof_footer(struct ceph_connection *con)
 static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
 {
        /* Initialize data cursor if it's not a sparse read */
-       if (!msg->sparse_read)
+       if (msg->sparse_read)
+               msg->cursor.sr_total_resid = data_len;
+       else
                ceph_msg_data_cursor_init(&msg->cursor, msg, data_len);
 }
 
@@ -1032,18 +1034,25 @@ static int read_partial_sparse_msg_data(struct ceph_connection *con)
        bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
        u32 crc = 0;
        int ret = 1;
+       int len;
 
        if (do_datacrc)
                crc = con->in_data_crc;
 
-       do {
-               if (con->v1.in_sr_kvec.iov_base)
+       while (cursor->sr_total_resid && ret > 0) {
+               len = 0;
+               if (con->v1.in_sr_kvec.iov_base) {
                        ret = read_partial_message_chunk(con,
                                                         &con->v1.in_sr_kvec,
                                                         con->v1.in_sr_len,
                                                         &crc);
-               else if (cursor->sr_resid > 0)
+                       if (ret == 1)
+                               len = con->v1.in_sr_len;
+               } else if (cursor->sr_resid > 0) {
                        ret = read_partial_sparse_msg_extent(con, &crc);
+                       if (ret == 1)
+                               len = cursor->sr_resid_elen;
+               }
 

I was waiting for Jeff's review since this is his code, but it's been a while so I'll just comment.

To me, it's a sign of suboptimal structure that you needed to add new fields to the cursor just to be able tell whether this function is done with the message, because it's something that is already tracked but gets lost between the loops here.

Currently this function is structured as:

do {
if (set up for kvec)
read as much as possible into kvec
else if (set up for pages)
read as much as possible into pages

if (short read)
bail, will be called again later

invoke con->ops->sparse_read() for processing the thing what was read and setting up the next read OR setting up the initial read
} until (con->ops->sparse_read() returns "done")

If it was me, I would pursue refactoring this to:

do {
if (set up for kvec)
read as much as possible into kvec
else if (set up for pages)
read as much as possible into pages
else
bail

Quoted 3 lines

Why bail here ? For the new sparse read both the 'kvec' and 'pages' shouldn't be set, so the following '->sparse_read()' will set up them and continue.

Or you just want the 'read_partial_sparse_msg_data()' to read data but not the first time to trigger the '->sparse_read()' ? Before I tried a similar approach and it was not as optional as this one as I do.

Hi Xiubo,

I addressed that in the previous message:

... and invoke con->ops->sparse_read() for the first time elsewhere, likely in prepare_message_data(). The rationale is that the state machine inside con->ops->sparse_read() is conceptually a cursor and prepare_message_data() is where the cursor is initialized for normal reads.

The benefit is that no additional state would be needed.

Quoted 1 line

The 'cursor->sr_total_resid', which is similar with 'cursor->total_resid', will just record the total data length for current sparse-read request and will determine whether should we skip parsing the "(page) data" in 'read_partial_message()'.

I understand the intent behind cursor->sr_total_resid, but it would be nice to do without it because of its inherent redundancy.

Did you try calling con->ops->sparse_read() for the first time exactly where cursor->sr_total_resid is initialized in your patch?

Thanks,

Ilya

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

maintainer
#10 lore raw

Re: [PATCH v3 3/3] libceph: just wait for more data to be available on the socket

Quoted 157 lines · Xiubo Li

On 1/16/24 18:00, Ilya Dryomov wrote:

On Tue, Jan 16, 2024 at 5:09 AM Xiubo Li <[email protected]> wrote:

On 1/16/24 06:38, Ilya Dryomov wrote:

On Fri, Dec 15, 2023 at 1:22 AM <[email protected]> wrote:

From: Xiubo Li <[email protected]>

The messages from ceph maybe split into multiple socket packages and we just need to wait for all the data to be availiable on the sokcet.

This will add 'sr_total_resid' to record the total length for all data items for sparse-read message and 'sr_resid_elen' to record the current extent total length.

URL: https://tracker.ceph.com/issues/63586

---
 include/linux/ceph/messenger.h |  2 ++
 net/ceph/messenger.c           |  1 +
 net/ceph/messenger_v1.c        | 21 ++++++++++++++++-----
 net/ceph/osd_client.c          |  1 +
 4 files changed, 20 insertions(+), 5 deletions(-)
include/linux/ceph/messenger.h +2 -0
index 2eaaabbe98cb..05e9b39a58f8 100644
@@ -231,10 +231,12 @@ struct ceph_msg_data {
 
 struct ceph_msg_data_cursor {
        size_t                  total_resid;    /* across all data items */
+       size_t                  sr_total_resid; /* across all data items for sparse-read */
 
        struct ceph_msg_data    *data;          /* current data item */
        size_t                  resid;          /* bytes not yet consumed */
        int                     sr_resid;       /* residual sparse_read len */
+       int                     sr_resid_elen;  /* total sparse_read elen */
 
 Hi Xiubo,
 

Is adding yet another sparse-read field to the cursor really needed? Would making read_partial_sparse_msg_extent() decrement sr_total_resid as it goes just like sr_resid is decremented work?

This can be improved by removing it. Let me fix it.

bool need_crc; /* crc update needed */
union {
#ifdef CONFIG_BLOCK

net/ceph/messenger.c +1 -0
index 3c8b78d9c4d1..eafd592af382 100644
@@ -1073,6 +1073,7 @@ void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
        cursor->total_resid = length;
        cursor->data = msg->data;
        cursor->sr_resid = 0;
+       cursor->sr_resid_elen = 0;
 
        __ceph_msg_data_cursor_init(cursor);
 }
net/ceph/messenger_v1.c +13 -4
index 4cb60bacf5f5..7425fa26e4c3 100644
@@ -160,7 +160,9 @@ static size_t sizeof_footer(struct ceph_connection *con)
 static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
 {
        /* Initialize data cursor if it's not a sparse read */
-       if (!msg->sparse_read)
+       if (msg->sparse_read)
+               msg->cursor.sr_total_resid = data_len;
+       else
                ceph_msg_data_cursor_init(&msg->cursor, msg, data_len);
 }
 
@@ -1032,18 +1034,25 @@ static int read_partial_sparse_msg_data(struct ceph_connection *con)
        bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
        u32 crc = 0;
        int ret = 1;
+       int len;
 
        if (do_datacrc)
                crc = con->in_data_crc;
 
-       do {
-               if (con->v1.in_sr_kvec.iov_base)
+       while (cursor->sr_total_resid && ret > 0) {
+               len = 0;
+               if (con->v1.in_sr_kvec.iov_base) {
                        ret = read_partial_message_chunk(con,
                                                         &con->v1.in_sr_kvec,
                                                         con->v1.in_sr_len,
                                                         &crc);
-               else if (cursor->sr_resid > 0)
+                       if (ret == 1)
+                               len = con->v1.in_sr_len;
+               } else if (cursor->sr_resid > 0) {
                        ret = read_partial_sparse_msg_extent(con, &crc);
+                       if (ret == 1)
+                               len = cursor->sr_resid_elen;
+               }
 

I was waiting for Jeff's review since this is his code, but it's been a while so I'll just comment.

To me, it's a sign of suboptimal structure that you needed to add new fields to the cursor just to be able tell whether this function is done with the message, because it's something that is already tracked but gets lost between the loops here.

Currently this function is structured as:

do {
if (set up for kvec)
read as much as possible into kvec
else if (set up for pages)
read as much as possible into pages

if (short read)
bail, will be called again later

invoke con->ops->sparse_read() for processing the thing what was read and setting up the next read OR setting up the initial read
} until (con->ops->sparse_read() returns "done")

If it was me, I would pursue refactoring this to:

do {
if (set up for kvec)
read as much as possible into kvec
else if (set up for pages)
read as much as possible into pages
else
bail

Why bail here ? For the new sparse read both the 'kvec' and 'pages' shouldn't be set, so the following '->sparse_read()' will set up them and continue.

Or you just want the 'read_partial_sparse_msg_data()' to read data but not the first time to trigger the '->sparse_read()' ? Before I tried a similar approach and it was not as optional as this one as I do.

Hi Xiubo,

I addressed that in the previous message:

... and invoke con->ops->sparse_read() for the first time elsewhere, likely in prepare_message_data(). The rationale is that the state machine inside con->ops->sparse_read() is conceptually a cursor and prepare_message_data() is where the cursor is initialized for normal reads.

Quoted 3 lines

The benefit is that no additional state would be needed.

Sorry, which state do you mean here ? Is the 'sr_total_resid' ?

Yes.

Ilya

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

maintainer
#12 lore raw

Re: [PATCH v3 3/3] libceph: just wait for more data to be available on the socket

Quoted 61 lines · Xiubo Li

On 1/16/24 18:00, Ilya Dryomov wrote:

On Tue, Jan 16, 2024 at 5:09 AM Xiubo Li <[email protected]> wrote:

[...]
I was waiting for Jeff's review since this is his code, but it's been a while so I'll just comment.

To me, it's a sign of suboptimal structure that you needed to add new fields to the cursor just to be able tell whether this function is done with the message, because it's something that is already tracked but gets lost between the loops here.

Currently this function is structured as:

do {
if (set up for kvec)
read as much as possible into kvec
else if (set up for pages)
read as much as possible into pages

if (short read)
bail, will be called again later

invoke con->ops->sparse_read() for processing the thing what was read and setting up the next read OR setting up the initial read
} until (con->ops->sparse_read() returns "done")

If it was me, I would pursue refactoring this to:

do {
if (set up for kvec)
read as much as possible into kvec
else if (set up for pages)
read as much as possible into pages
else
bail

Why bail here ? For the new sparse read both the 'kvec' and 'pages' shouldn't be set, so the following '->sparse_read()' will set up them and continue.

Or you just want the 'read_partial_sparse_msg_data()' to read data but not the first time to trigger the '->sparse_read()' ? Before I tried a similar approach and it was not as optional as this one as I do.

Hi Xiubo,

I addressed that in the previous message:

... and invoke con->ops->sparse_read() for the first time elsewhere, likely in prepare_message_data(). The rationale is that the state machine inside con->ops->sparse_read() is conceptually a cursor and prepare_message_data() is where the cursor is initialized for normal reads.

The benefit is that no additional state would be needed.

Hi Ilya,

I am afraid this won't work if my understanding is correct.

Quoted 7 lines

I think you want to call the first 'con->ops->sparse_read()' earlier somewhere such as in 'prepare_message_data()' to parse and skip the extra spare-read extent array info and then we could reuse the 'cursor->total_resid' as others, right ?

As we know the 'cursor->total_resid' is for pure data only, while for sparse-read it introduce some extra extent array info.

For sparse-read we need to call 'con->ops->sparse_read()' several times for each sparse-read OP and each time it will only parse part of the extra info. That means we need to call 'con->ops->sparse_read()' many times in 'prepare_message_data()' in this approach.

The the most important thing is that we couldn't do this in 'prepare_message_data()', because the 'prepare_message_data()' will be called just before parsing the "front" and "middle" of the msg.

Hi Xiubo,

I see, I missed that in my suggestion.

Quoted 20 lines

Else if we did this in somewhere place out of 'prepare_message_data()', then we must do it in the following code just before Line#1267:

1262 /* (page) data */ 1263 if (data_len) { 1264 if (!m->num_data_items) 1265 return -EIO; 1266 1267 if (m->sparse_read) 1268 ret = read_partial_sparse_msg_data(con); 1269 else if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) 1270 ret = read_partial_msg_data_bounce(con); 1271 else 1272 ret = read_partial_msg_data(con); 1273 if (ret <= 0) 1274 return ret; 1275 }

But this still couldn't resolve the multiple sparse-read OPs case, once the first OP parsing finishes we couldn't jump back to call 'prepare_message_data()'.

The sparse-read data in the socket buffer will be:

OP1: <sparse-read header> <sparse-read extents> <sparse-read real data length> <real data 1>; OP2: <sparse-read header> <sparse-read extents> <sparse-read real data length> <real data 2>; OP3:....

Currently the 'cursor->total_resid' will record the total length of <real data 1> + <real data 2> + ...

And the 'cursor->sr_total_resid' will record all the above.

The 'cursor->sr_total_resid', which is similar with 'cursor->total_resid', will just record the total data length for current sparse-read request and will determine whether should we skip parsing the "(page) data" in 'read_partial_message()'.

I understand the intent behind cursor->sr_total_resid, but it would be nice to do without it because of its inherent redundancy.

Quoted 6 lines

The above is why I added ''cursor->sr_total_resid'.

Did you try calling con->ops->sparse_read() for the first time exactly where cursor->sr_total_resid is initialized in your patch?

Yeah, I did but it didn't work as I mentioned above. If I am wrong, please correct me.

I think you are right, and also in that introducing a separate field makes for an easier fix. I continue to claim that a separate field is _inherently_ redundant though -- it's just that due to the structure of the current code, it's not obvious.

There is definitely a way to make this (to be precise: allow read_partial_sparse_msg_data() to be invoked by the messenger on short reads of the footer without causing any damage like attempting to parse random data as sparse-read header and/or extents) happen without adding additional fields to the cursor or elsewhere. But it might involve refactoring the entire state machine such that information isn't lost between the messenger and the OSD client, so I'm not asking that you take that on here.

I wouldn't object to cursor->sr_total_resid being added, I just don't like it ;)

Thanks,

Ilya

maintainer
#13 lore raw

Re: [PATCH v3 3/3] libceph: just wait for more data to be available on the socket

Quoted 2 lines · Ilya Dryomov

I wouldn't object to cursor->sr_total_resid being added, I just don't like it ;)

Actually, how about just reusing cursor->sr_resid, which happens to be an int? Set it to -1 when con->ops->sparse_read() returns 0 and check for that at the top:

    struct ceph_msg_data_cursor *cursor = &con->in_msg->cursor;
    bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
    u32 crc = 0;
    int ret = 1;

    if (cursor->sr_resid < 0)
            return 1;

    if (do_datacrc)
            crc = con->in_data_crc;

    [ ... no changes ... ]

    if (do_datacrc)
            con->in_data_crc = crc;

    if (ret < 0)
            return ret;

    cursor->sr_resid = -1;
    return 1;  /* must return > 0 to indicate success */

Thanks,

Ilya