Skip to contentSkip to conversation

libceph: allow for multiple in-flight linger pings

PATCH In review Ilya Dryomov 3 participants 8 messages Nov 10 – Nov 12 Ceph common code (libceph)

lore
Share & tools
lore

Ilya Dryomov

author maintainer
#0 lore raw

cover libceph: allow for multiple in-flight linger pings

Hello,

This change significantly reduces the risk of erroneous blocklisting in environments with high latency or where low/moderate latency is coupled with huge spikes for RBD images with exclusive-lock feature enabled.

Thanks,

Ilya

Ilya Dryomov (3):
libceph: replace lreq->ping_sent with req->r_start_stamp
libceph: don't cancel in-flight ping in send_linger_ping()
libceph: include current latency in debugfs osdc dump

 include/linux/ceph/osd_client.h |  4 +-
 net/ceph/debugfs.c              | 12 ++++-
 net/ceph/osd_client.c           | 87 +++++++++++++++------------------
 3 files changed, 51 insertions(+), 52 deletions(-)
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 1/3 libceph: replace lreq->ping_sent with req->r_start_stamp

Eliminate lreq->ping_sent by making more use of req->r_start_stamp. This requires setting req->r_start_stamp outside of account_request() but it only has one other caller apart from send_linger_ping().

---
 include/linux/ceph/osd_client.h |  1 -
 net/ceph/osd_client.c           | 14 ++++++++------
 2 files changed, 8 insertions(+), 7 deletions(-)
include/linux/ceph/osd_client.h +0 -1
index 50b14a5661c7..0847c522f897 100644
@@ -316,7 +316,6 @@ struct ceph_osd_linger_request {
 	struct ceph_osd *osd;
 	struct ceph_osd_request *reg_req;
 	struct ceph_osd_request *ping_req;
-	unsigned long ping_sent;
 	unsigned long watch_valid_thru;
 	struct list_head pending_lworks;
 
net/ceph/osd_client.c +8 -6
index b8e4740d07fe..5053363134ee 100644
@@ -2482,12 +2482,13 @@ static void account_request(struct ceph_osd_request *req)
 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
 	atomic_inc(&req->r_osdc->num_requests);
 
-	req->r_start_stamp = jiffies;
 	req->r_start_latency = ktime_get();
 }
 
 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
 {
+	req->r_start_stamp = jiffies;
+
 	ceph_osdc_get_request(req);
 	account_request(req);
 	__submit_request(req, wrlocked);
@@ -3195,11 +3196,11 @@ static void linger_ping_cb(struct ceph_osd_request *req)
 	}
 
 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
-	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
+	     __func__, lreq, lreq->linger_id, req->r_result, req->r_start_stamp,
 	     lreq->last_error);
 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
 		if (!req->r_result) {
-			lreq->watch_valid_thru = lreq->ping_sent;
+			lreq->watch_valid_thru = req->r_start_stamp;
 		} else if (!lreq->last_error) {
 			lreq->last_error = normalize_watch_error(req->r_result);
 			queue_watch_error(lreq);
@@ -3218,6 +3219,7 @@ static void send_linger_ping(struct ceph_osd_linger_request *lreq)
 {
 	struct ceph_osd_client *osdc = lreq->osdc;
 	struct ceph_osd_request *req;
+	unsigned long now = jiffies;
 	int ret;
 
 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
@@ -3225,10 +3227,8 @@ static void send_linger_ping(struct ceph_osd_linger_request *lreq)
 		return;
 	}
 
-	lreq->ping_sent = jiffies;
 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
-	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
-	     lreq->register_gen);
+	     __func__, lreq, lreq->linger_id, now, lreq->register_gen);
 
 	if (lreq->ping_req) {
 		if (lreq->ping_req->r_osd)
@@ -3251,6 +3251,8 @@ static void send_linger_ping(struct ceph_osd_linger_request *lreq)
 	req->r_linger = true;
 	lreq->ping_req = req;
 
+	req->r_start_stamp = now;
+
 	ceph_osdc_get_request(req);
 	account_request(req);
 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
Signature · 1 line
2.55.0
Tested-by Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] libceph: replace lreq->ping_sent with req->r_start_stamp

Hi Ilya

This patch lgtm. Tested it and works well.

Thanks,
Xiubo

Quoted 89 lines · Ilya Dryomov <[email protected]> 于2026年11月10日周二 21:11写道:

Eliminate lreq->ping_sent by making more use of req->r_start_stamp. This requires setting req->r_start_stamp outside of account_request() but it only has one other caller apart from send_linger_ping().

---
 include/linux/ceph/osd_client.h |  1 -
 net/ceph/osd_client.c           | 14 ++++++++------
 2 files changed, 8 insertions(+), 7 deletions(-)
include/linux/ceph/osd_client.h +0 -1
index 50b14a5661c7..0847c522f897 100644
@@ -316,7 +316,6 @@ struct ceph_osd_linger_request {
        struct ceph_osd *osd;
        struct ceph_osd_request *reg_req;
        struct ceph_osd_request *ping_req;
-       unsigned long ping_sent;
        unsigned long watch_valid_thru;
        struct list_head pending_lworks;
 
net/ceph/osd_client.c +8 -7
index b8e4740d07fe..5053363134ee 100644
@@ -2482,12 +2482,13 @@ static void account_request(struct ceph_osd_request *req)
        req->r_flags |= CEPH_OSD_FLAG_ONDISK;
        atomic_inc(&req->r_osdc->num_requests);
 
-       req->r_start_stamp = jiffies;
        req->r_start_latency = ktime_get();
 }
 
 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
 {
+       req->r_start_stamp = jiffies;
+
        ceph_osdc_get_request(req);
        account_request(req);
        __submit_request(req, wrlocked);
@@ -3195,11 +3196,11 @@ static void linger_ping_cb(struct ceph_osd_request *req)
        }
 
        dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
-            __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
+            __func__, lreq, lreq->linger_id, req->r_result, req->r_start_stamp,
             lreq->last_error);
        if (lreq->register_gen == req->r_ops[0].watch.gen) {
                if (!req->r_result) {
-                       lreq->watch_valid_thru = lreq->ping_sent;
+                       lreq->watch_valid_thru = req->r_start_stamp;
                } else if (!lreq->last_error) {
                        lreq->last_error = normalize_watch_error(req->r_result);
                        queue_watch_error(lreq);
@@ -3218,6 +3219,7 @@ static void send_linger_ping(struct ceph_osd_linger_request *lreq)
 {
        struct ceph_osd_client *osdc = lreq->osdc;
        struct ceph_osd_request *req;
+       unsigned long now = jiffies;
        int ret;
 
        if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
@@ -3225,10 +3227,8 @@ static void send_linger_ping(struct ceph_osd_linger_request *lreq)
                return;
        }
 
-       lreq->ping_sent = jiffies;
        dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
-            __func__, lreq, lreq->linger_id, lreq->ping_sent,
-            lreq->register_gen);
+            __func__, lreq, lreq->linger_id, now, lreq->register_gen);
 
        if (lreq->ping_req) {
                if (lreq->ping_req->r_osd)
@@ -3251,6 +3251,8 @@ static void send_linger_ping(struct ceph_osd_linger_request *lreq)
        req->r_linger = true;
        lreq->ping_req = req;
 
+       req->r_start_stamp = now;
+
        ceph_osdc_get_request(req);
        account_request(req);
        req->r_tid = atomic64_inc_return(&osdc->last_tid);
--

2.55.0

author maintainer
#3 lore raw

PATCH 2/3 libceph: don't cancel in-flight ping in send_linger_ping()

In reply to Ilya Dryomov

Linger pings are emitted every 5 seconds (by default) but currently there can only ever be one linger ping in flight: send_linger_ping() always tries to revoke and free the OSD request that corresponds to the previous ping before allocating a fresh instance for the next ping. Because these linger ping requests go through the same per-OSD queue as regular OSD requests, this can cause some pings to not be delivered to the OSD on time or be lost entirely in environments with high latency or where low/moderate latency is coupled with huge spikes. That in turn can cause the watch to time out on the OSD, exposing this client instance to the risk of erroneous blocklisting in case another client manages to proceed with breaking exclusive lock before the watch gets reestablished.

Rework send_linger_ping() and related logic to allow for multiple in-flight linger pings. In hindsight, this should have been done in commit 75dbb685f4e8 ("libceph: fix potential use-after-free on linger ping and resends").

---
 include/linux/ceph/osd_client.h |  3 +-
 net/ceph/osd_client.c           | 73 +++++++++++++++------------------
 2 files changed, 33 insertions(+), 43 deletions(-)
include/linux/ceph/osd_client.h +1 -2
index 0847c522f897..7349ca09a227 100644
@@ -314,8 +314,7 @@ struct ceph_osd_linger_request {
 	bool is_watch;                  /* watch or notify */
 
 	struct ceph_osd *osd;
-	struct ceph_osd_request *reg_req;
-	struct ceph_osd_request *ping_req;
+	struct list_head osd_reqs;	/* w/ r_private_item */
 	unsigned long watch_valid_thru;
 	struct list_head pending_lworks;
 
net/ceph/osd_client.c +32 -41

169 lines not on this page · raw message

Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH 2/3] libceph: don't cancel in-flight ping in send_linger_ping()

On 11/10/26 21:10, Ilya Dryomov wrote:
[...]

Quoted 10 lines
net/ceph/osd_client.c +0 -6
@@ -3189,12 +3183,6 @@ static void linger_ping_cb(struct ceph_osd_request *req)
      struct ceph_osd_linger_request *lreq = req->r_priv;
 
      mutex_lock(&lreq->lock);
-     if (req != lreq->ping_req) {
-             dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
-                  __func__, lreq, lreq->linger_id, req, lreq->ping_req);
-             goto out;
-     }
-

[...]

net/ceph/osd_client.c static void linger_commit_cb(struct ceph_osd_request *req) · line 3085

Quoted 7 lines
net/ceph/osd_client.c +3 -1
-out:
+     list_del_init(&req->r_private_item);
      mutex_unlock(&lreq->lock);
+
+     ceph_osdc_put_request(req);
      linger_put(lreq);
 }

The callbacks now assume that the request is still on lreq->osd_reqs, i.e. that they still own the list's reference, but that isn't guaranteed. handle_reply() calls finish_request() (which clears req->r_osd) and drops osd->lock and osdc->lock before __complete_request() runs the callback. If send_linger() or __linger_cancel() gets in between, it sees req->r_osd == NULL, skips cancel_linger_request() and does:

        list_del_init(&req->r_private_item);
        ceph_osdc_put_request(req);

Then the callback does list_del_init() on an already empty entry and puts the request a second time, and __complete_request() follows with complete_all() and its own put on freed memory:

handle_reply() send_linger() / __linger_cancel()

    finish_request(req)  /* r_osd = NULL */
    up_read(&osdc->lock)
                                       mutex_lock(&lreq->lock)
                                       req->r_osd == NULL, no cancel
                                       list_del_init(), put  /* 2 -> 1 */
                                       mutex_unlock(&lreq->lock)
    __complete_request(req)
      linger_ping_cb(req)
        list_del_init()  /* no-op */
        put                          /* 1 -> 0, freed */
      complete_all(&req->r_completion)  /* UAF */
      ceph_osdc_put_request(req)        /* underflow */

The same applies to linger_commit_cb() and linger_reconnect_cb(). The old code was safe here because of the req != lreq->ping_req/reg_req check and because the lreq held its own reference to the request.

The window is naturally small, but it does not have to be: the callback can be waiting on lreq->lock while send_linger() holds it across a GFP_NOIO allocation. To show it deterministically I added a test-only msleep() in __complete_request() for r_linger requests (before calling r_callback) and ran rbd on a KASAN kernel against a vstart cluster. Closing the window in two ways:

1) rbd unmap (__linger_cancel()) while a ping callback is delayed
2) "ceph osd down <primary of rbd_header>" while a ping callback is delayed (osdmap -> scan_requests() -> send_linger()); this is the case one would expect to see in the field, an OSD flapping while a ping reply is being handled

Without this series, both runs are clean (the callback sees a refcount of 2). With the series applied, both runs end with:

libceph: lping: run cb req 000000000cb00fcd ref 1

  BUG: KASAN: slab-use-after-free in __raw_spin_lock_irqsave+0x4d/0xa0
  Read of size 1 at addr ffff888258fa9f00 by task kworker/5:1/137
   complete_all+0x97/0x190
   __complete_request+0x9c/0x3b0 [libceph]
   handle_reply+0x995/0x1120 [libceph]
   osd_dispatch+0x9f/0x160 [libceph]
  Freed by task 137:
   mempool_free+0xd8/0x150
   linger_ping_cb+0x21b/0x6e0 [libceph]
   __complete_request+0x90/0x3b0 [libceph]
   handle_reply+0x995/0x1120 [libceph]
  ...
  refcount_t: underflow; use-after-free.
  kernel BUG at lib/refcount.c:37!

Treating a request that is no longer on lreq->osd_reqs as stale fixes it for me, in all three callbacks:

        mutex_lock(&lreq->lock);
        if (list_empty(&req->r_private_item)) {
                dout("%s lreq %p linger_id %llu stale req %p\n", __func__,
                     lreq, lreq->linger_id, req);
                mutex_unlock(&lreq->lock);
                linger_put(lreq);
                return;
        }
        ...

Both tests above are clean with it (the callback then sees a refcount of 1 and returns without a put). Just making the put conditional (put = !list_empty(&req->r_private_item)) is also enough for the UAF and passes the same tests, but I think skipping the reply is better, because the series also stopped filtering stale replies: with the req != lreq->reg_req check gone, a reply to a superseded reg/reconnect request is now processed as if it were current. To check, I delayed a reconnect callback, marked the primary down a second time inside that window so that send_linger() replaced the reconnect request, and enabled the douts:

without the series:

    linger_reconnect_cb lreq ... unknown req (0000000040e0d613 !=

000000006664a4c9)
series + conditional put:
linger_reconnect_cb lreq ... result 0 last_error 0 <- stale request
linger_reconnect_cb lreq ... result 0 last_error 0 <- current request
series + skipping stale requests:
linger_reconnect_cb lreq ... stale req 00000000433ad8d9 linger_reconnect_cb lreq ... result 0 last_error 0

Here the stale reply was a success, so nothing visible happened. But an error in a stale reconnect reply would now set lreq->last_error and queue a watch error, and in linger_commit_cb() a stale reply would re-run linger_reg_commit_complete() and, for a notify, overwrite lreq->notify_id. I haven't tried to trigger those consequences.

One more observation, not a bug: there is now no limit on the number of in-flight pings. With the primary OSD SIGSTOPped, the number of watch-ping requests in osdc grows by one every osd_keepalive_timeout:
11 after 39 seconds, while without the series it never goes above 1. That is the point of the patch of course, but nothing bounds it for an unresponsive OSD whose session stays up. In my vstart setup the stopped OSD was not marked down at all during a 90 second run. An osdmap change does drain the list through send_linger() (as in the test above), and with osd_request_timeout=15 the count peaked at 3 before the first ping timed out and the watch error (-110) stopped further pings. (The test kernel also carries a local patch that resets an OSD session when it doesn't answer keepalives. That patch is what cleared the pings after ~40 seconds; the count was back at 14 another 48 seconds later.) With lots of mapped images that could add up.

Would it make sense to stop sending new pings once the oldest one is older than the watch timeout, given that the watch is going to be torn down anyway at that point?

Thanks,
Xiubo

author maintainer
#5 lore raw

PATCH 3/3 libceph: include current latency in debugfs osdc dump

In reply to Ilya Dryomov

For regular OSD requests, it's very straightforward.

For linger requests, reference the oldest corresponding OSD request still in flight and in addition to that surface the otherwise unused lreq->watch_valid_thru timestamp.

---
 net/ceph/debugfs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
net/ceph/debugfs.c +10 -2
index 83c270bce63c..b068e25f2acc 100644
@@ -219,7 +219,8 @@ static void dump_request(struct seq_file *s, struct ceph_osd_request *req)
 	seq_printf(s, "%llu\t", req->r_tid);
 	dump_target(s, &req->r_t);
 
-	seq_printf(s, "\t%d", req->r_attempts);
+	seq_printf(s, "\t%d\t%u", req->r_attempts,
+		   jiffies_to_msecs(jiffies - req->r_start_stamp));
 
 	for (i = 0; i < req->r_num_ops; i++) {
 		struct ceph_osd_req_op *op = &req->r_ops[i];
@@ -255,10 +256,17 @@ static void dump_requests(struct seq_file *s, struct ceph_osd *osd)
 static void dump_linger_request(struct seq_file *s,
 				struct ceph_osd_linger_request *lreq)
 {
+	struct ceph_osd_request *req =
+	    list_first_entry_or_null(&lreq->osd_reqs, struct ceph_osd_request,
+				     r_private_item);
+
 	seq_printf(s, "%llu\t", lreq->linger_id);
 	dump_target(s, &lreq->t);
 
-	seq_printf(s, "\t%u\t%s%s/%d\n", lreq->register_gen,
+	seq_printf(s, "\t%u\t%u\t%u\t%s%s/%d\n", lreq->register_gen,
+		   req ? jiffies_to_msecs(jiffies - req->r_start_stamp) : 0,
+		   lreq->is_watch && lreq->committed && !lreq->last_error ?
+                        jiffies_to_msecs(jiffies - lreq->watch_valid_thru) : 0,
 		   lreq->is_watch ? "W" : "N", lreq->committed ? "C" : "",
 		   lreq->last_error);
 }
Signature · 1 line
2.55.0
#6 lore raw

Re: [PATCH 3/3] libceph: include current latency in debugfs osdc dump

On 11/10/26 21:10, Ilya Dryomov wrote:
[...]

net/ceph/debugfs.c static void dump_requests(struct seq_file *s, struct ceph_osd *osd) · line 267

Quoted 13 lines

static void dump_linger_request(struct seq_file *s,
struct ceph_osd_linger_request *lreq)
{

net/ceph/debugfs.c +6 -1
+     struct ceph_osd_request *req =
+         list_first_entry_or_null(&lreq->osd_reqs, struct ceph_osd_request,
+                                  r_private_item);
+
      seq_printf(s, "%llu\t", lreq->linger_id);
      dump_target(s, &lreq->t);
 
-     seq_printf(s, "\t%u\t%s%s/%d\n", lreq->register_gen,
+     seq_printf(s, "\t%u\t%u\t%u\t%s%s/%d\n", lreq->register_gen,
+                req ? jiffies_to_msecs(jiffies - req->r_start_stamp) : 0,

lreq->osd_reqs is only protected by lreq->lock: the linger callbacks remove and put the request holding just lreq->lock, after handle_reply() has dropped osd->lock and osdc->lock. So holding osdc->lock and osd->lock here doesn't keep the first entry alive, and the req->r_start_stamp dereference can hit a freed request.

I could trigger it by delaying a linger_ping_cb() call (test-only msleep() in __complete_request()) and reading osdc in that window with a test-only msleep() between list_first_entry_or_null() and the seq_printf():

  BUG: KASAN: slab-use-after-free in dump_linger_requests+0x3b5/0x450 [libceph]
  Read of size 8 at addr ffff888233b79460 by task cat/8550
   dump_linger_requests+0x3b5/0x450 [libceph]
   osdc_show+0x21e/0x580 [libceph]
   seq_read_iter+0x2e9/0x12d0
  Freed by task 137:
   mempool_free+0xd8/0x150
   handle_reply+0x995/0x1120 [libceph]
   osd_dispatch+0x9f/0x160 [libceph]

Taking lreq->lock in dump_linger_request() fixes it, and the same test is clean with it. The osd->lock -> lreq->lock order is the one handle_timeout() already uses:

        struct ceph_osd_request *req;
        unsigned int age = 0;

        mutex_lock(&lreq->lock);
        req = list_first_entry_or_null(&lreq->osd_reqs, struct ceph_osd_request,
                                       r_private_item);
        if (req)
                age = jiffies_to_msecs(jiffies - req->r_start_stamp);
        ...
        mutex_unlock(&lreq->lock);

That also makes the watch_valid_thru/committed/last_error reads consistent with each other.

Minor: the continuation line of the watch_valid_thru argument is indented with spaces instead of tabs.

Thanks,
Xiubo

maintainer
#7 lore raw
In reply to Ilya Dryomov

Hi Ilya,

Two further problems in 2/3.

linger_ping_cb() applies every ping whose register_gen still matches. With osd_request_timeout set, handle_timeout() abort_request()s a ping whose r_start_stamp is older than that timeout, and then send_linger_ping() runs in the same pass because last_error is still 0. The queued callback still owns that request: it stores -ETIMEDOUT and queue_watch_error(). rbd_watch_errcb() clears the exclusive-lock owner and re-registers the watch. The default timeout is 0, so a default mount does not abort. Previously req != lreq->ping_req dropped this completion. An older ping's error has to be ignored once a later ping for the same register_gen has been sent or has already succeeded.

__linger_cancel() walks lreq->osd_reqs without lreq->lock. The callbacks unlink under that lock alone, after handle_reply() has dropped osdc->lock. send_linger() takes lreq->lock around its walk; __linger_cancel() needs to as well, or unwatch can list_del_init() a node the callback is unlinking.

Thanks,
Alex