From 8c8aeed4c66e06f946773d2682bb21f23e9b1637 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Mon, 23 Nov 2026 11:32:20 +0300 Subject: [PATCH 1/3] osd: Fix scrub object state pins: crash when the object becomes clean, leak in scrub_recalc_state 3bf0b45a made scrub pin its object state and pass ref=true to mark_object(). mark_object() then increments the ref count of the new state, but add_object_to_set() returns NULL when the object becomes clean, so a scrub that drops a vanished misplaced copy segfaults. Reads and writes that mark objects corrupted with ref=true have the same path. scrub_recalc_state() still called mark_object() with ref=false. When it swapped states, the pin stayed on the old state, which then leaked with no objects, and the final deref in continue_primary_scrub() decremented the new, unpinned state: its ref_count wrapped around, or a write to the same object lost its pin. --- src/osd/osd_primary.cpp | 3 +- src/osd/osd_scrub.cpp | 3 +- src/test/osd_test.cpp | 156 ++++++++++++++++++++++++++++++++++++ src/test/osd_test_fixture.h | 11 ++- 4 files changed, 169 insertions(+), 4 deletions(-) diff --git a/src/osd/osd_primary.cpp b/src/osd/osd_primary.cpp index be1b3bee..43ad081a 100644 --- a/src/osd/osd_primary.cpp +++ b/src/osd/osd_primary.cpp @@ -541,7 +541,8 @@ pg_osd_set_state_t *osd_t::mark_object(pg_t & pg, object_id oid, pg_osd_set_stat } // Insert object into the new state and retry object_state = add_object_to_set(pg, oid, new_set, old_pg_state, 2); - if (ref) + // NULL means the object became clean + if (ref && object_state) { object_state->ref_count++; } diff --git a/src/osd/osd_scrub.cpp b/src/osd/osd_scrub.cpp index c5c90f83..f677233d 100644 --- a/src/osd/osd_scrub.cpp +++ b/src/osd/osd_scrub.cpp @@ -785,7 +785,8 @@ void osd_t::scrub_recalc_state(osd_op_t *cur_op) "[PG %u/%u] Scrub detected deleted parts of object %jx:%jx, recalculating its state from version %ju\n", INODE_POOL(op_data->oid.inode), op_data->pg_num, op_data->oid.inode, op_data->oid.stripe, target_ver ); - op_data->object_state = mark_object(*op_data->pg, op_data->oid, op_data->object_state, false, [op_data, target_ver](pg_osd_set_t & new_set) + // We hold the state pinned, so the swap transfers the pin to the new state + op_data->object_state = mark_object(*op_data->pg, op_data->oid, op_data->object_state, true, [op_data, target_ver](pg_osd_set_t & new_set) { int changes = 0; for (int i = new_set.size()-1; i >= 0; i--) diff --git a/src/test/osd_test.cpp b/src/test/osd_test.cpp index e0db38fa..cc337d6a 100644 --- a/src/test/osd_test.cpp +++ b/src/test/osd_test.cpp @@ -299,6 +299,159 @@ void test_scrub_corruption_persists() printf("test_scrub_corruption_persists passed\n"); } +// Scrub finds every copy of the target version deleted but older versions left, +// and recalculates the object state from them (scrub_recalc_state). The state the +// scrub pinned must be released exactly once, whether the object ends up degraded +// or clean (no state at all) +void test_scrub_recalc_state_pin(bool to_clean) +{ + printf("test_scrub_recalc_state_pin(%s)\n", to_clean ? "clean" : "degraded"); + + osd_test_fixture_t f; + f.configure_replicated_pool(/*pool_id*/ 1, /*pg_size*/ 2, /*pg_minsize*/ 1, /*pg_count*/ 1, + { { 1, 2 } }); + f.start(json11::Json::object { + { "osd_num", 1 }, + { "etcd_address", "127.0.0.1:2379" }, + { "immediate_commit", "all" }, + { "block_size", 131072 }, + { "bitmap_granularity", 4096 }, + { "no_recovery", true }, + }); + f.connect_peer(2); + f.connect_peer(3); + f.complete_peering_empty(); + auto & pg = f.pg(1, 1); + assert(pg.state & PG_ACTIVE); + + inode_t inode = INODE_WITH_POOL(1, 1); + object_id oid = { .inode = inode, .stripe = 0 }; + // degraded: the target version is on OSD 1, an older one on OSD 2. + // clean: the target version is only on OSD 3 outside the PG, older ones on OSD 1 and 2 + pg_osd_set_t set = to_clean + ? pg_osd_set_t{ + { .role = 0, .osd_num = 3, .loc_bad = 0 }, + { .role = 0, .osd_num = 1, .loc_bad = LOC_OUTDATED }, + { .role = 0, .osd_num = 2, .loc_bad = LOC_OUTDATED }, + } + : pg_osd_set_t{ + { .role = 0, .osd_num = 1, .loc_bad = 0 }, + { .role = 0, .osd_num = 2, .loc_bad = LOC_OUTDATED }, + }; + assert(f.add_object_to_set(pg, oid, set, pg.state, 0)); + if (to_clean) + f.bs->bitmap_versions[oid] = 3; + + auto *scrub = new osd_op_t(); + scrub->op_type = OSD_OP_IN; + scrub->client_id = 0; + scrub->req.rw.header.magic = SECONDARY_OSD_OP_MAGIC; + scrub->req.rw.header.id = 2; + scrub->req.rw.header.opcode = OSD_OP_SCRUB; + scrub->req.rw.inode = inode; + scrub->req.rw.offset = 0; + scrub->req.rw.len = 0; + int scrub_retval = -1; + scrub->callback = [&scrub_retval](osd_op_t *op) { + scrub_retval = op->reply.hdr.retval; + }; + f.exec(scrub); + + // The target version is deleted everywhere + if (to_clean) + f.peer_complete(f.peer_take(3, OSD_OP_SEC_READ), -ENOENT); + else + { + auto *rd = f.bs->take(BS_OP_READ); + rd->retval = -ENOENT; + rd->callback(rd); + } + // The older version on OSD 2 is asked for (the local one through read_bitmap) + auto *bmp = f.peer_take(2, OSD_OP_SEC_READ_BMP); + *(uint64_t*)bmp->buf = 3; + f.peer_complete(bmp, 8 + 4); + assert(scrub_retval == 0); + delete scrub; + + auto *st = f.get_object_state(pg, oid); + if (to_clean) + assert(!st); + else + assert(st && st->osd_set.size() == 1 && st->osd_set[0].osd_num == 2 && !st->osd_set[0].loc_bad); + // No pin is left behind, and no state is kept without objects + for (auto & kv: pg.state_dict) + assert(kv.second.ref_count == 0 && kv.second.object_count > 0); + printf("test_scrub_recalc_state_pin(%s) passed\n", to_clean ? "clean" : "degraded"); +} + +// Scrub of a misplaced object finds its extra copy outside the PG gone: the copy +// is dropped, the object becomes clean and its pinned state goes away +void test_scrub_drops_gone_misplaced_copy() +{ + printf("test_scrub_drops_gone_misplaced_copy\n"); + + osd_test_fixture_t f; + f.configure_replicated_pool(/*pool_id*/ 1, /*pg_size*/ 2, /*pg_minsize*/ 1, /*pg_count*/ 1, + { { 1, 2 } }); + f.start(json11::Json::object { + { "osd_num", 1 }, + { "etcd_address", "127.0.0.1:2379" }, + { "immediate_commit", "all" }, + { "block_size", 131072 }, + { "bitmap_granularity", 4096 }, + { "no_recovery", true }, + }); + f.connect_peer(2); + f.connect_peer(3); + f.complete_peering_empty(); + auto & pg = f.pg(1, 1); + assert(pg.state & PG_ACTIVE); + + inode_t inode = INODE_WITH_POOL(1, 1); + object_id oid = { .inode = inode, .stripe = 0 }; + pg_osd_set_t set = { + { .role = 0, .osd_num = 1, .loc_bad = 0 }, + { .role = 0, .osd_num = 2, .loc_bad = 0 }, + { .role = 0, .osd_num = 3, .loc_bad = 0 }, + }; + assert(f.add_object_to_set(pg, oid, set, pg.state, 0)); + + auto *scrub = new osd_op_t(); + scrub->op_type = OSD_OP_IN; + scrub->client_id = 0; + scrub->req.rw.header.magic = SECONDARY_OSD_OP_MAGIC; + scrub->req.rw.header.id = 2; + scrub->req.rw.header.opcode = OSD_OP_SCRUB; + scrub->req.rw.inode = inode; + scrub->req.rw.offset = 0; + scrub->req.rw.len = 0; + int scrub_retval = -1; + scrub->callback = [&scrub_retval](osd_op_t *op) { + scrub_retval = op->reply.hdr.retval; + }; + f.exec(scrub); + + auto *local_read = f.bs->take(BS_OP_READ); + memset(local_read->buf, 0xab, local_read->len); + memset(local_read->bitmap, 0xff, 4); + local_read->retval = local_read->len; + local_read->version = 1; + local_read->callback(local_read); + auto *peer_read = f.peer_take(2, OSD_OP_SEC_READ); + memset(peer_read->iov.buf[0].iov_base, 0xab, peer_read->req.sec_rw.len); + memset(peer_read->bitmap, 0xff, 4); + peer_read->reply.sec_rw.attr_len = 4; + peer_read->reply.sec_rw.version = 1; + f.peer_complete(peer_read, peer_read->req.sec_rw.len); + f.peer_complete(f.peer_take(3, OSD_OP_SEC_READ), -ENOENT); + assert(scrub_retval == 0); + delete scrub; + + assert(!f.get_object_state(pg, oid)); + assert(pg.state_dict.empty()); + printf("test_scrub_drops_gone_misplaced_copy passed\n"); +} + // Scrub two replicated OSDs that have identical data but *different* bitmaps. // The expectation is that scrub detects the mismatch and marks at least one // replica as corrupted (or inconsistent) because bitmaps differ even though @@ -4657,6 +4810,9 @@ int main(int narg, char *args[]) test_load_global_config(); test_replicated_write(); test_scrub_corruption_persists(); + test_scrub_drops_gone_misplaced_copy(); + test_scrub_recalc_state_pin(false); + test_scrub_recalc_state_pin(true); test_ec33_scrub_only_first_part(); for (int i = 0; test_scrub_same_data_diff_bitmaps(i); i++) {} test_ec33_recovery_missing_first_part(); diff --git a/src/test/osd_test_fixture.h b/src/test/osd_test_fixture.h index 39778383..be593dec 100644 --- a/src/test/osd_test_fixture.h +++ b/src/test/osd_test_fixture.h @@ -43,12 +43,19 @@ public: } // Local bitmap reads go through read_bitmap() synchronously; record them - // so tests can assert which chain elements were asked + // so tests can assert which chain elements were asked. Objects listed in + // bitmap_versions report that version, the rest are absent std::vector read_bitmap_calls; + std::map bitmap_versions; int read_bitmap(object_id oid, uint64_t target_version, void *bitmap, uint64_t *result_version) override { read_bitmap_calls.push_back(oid); - return -ENOENT; + auto it = bitmap_versions.find(oid); + if (it == bitmap_versions.end()) + return -ENOENT; + if (result_version) + *result_version = it->second; + return 0; } // Pop the first queued op with the given opcode. Aborts if none. -- 2.47.3 From b95ac3cfc418beb995320b225bac3c84b8365542 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Mon, 23 Nov 2026 21:48:00 +0300 Subject: [PATCH 2/3] osd: Check that a failed primary read drops its object state pins The read path pins the object state since 3bf0b45a, but no test checked that the pin is released. Assert that no state in the PG is left pinned after the read fails with EIO and the object is marked corrupted. --- src/test/osd_test.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/osd_test.cpp b/src/test/osd_test.cpp index cc337d6a..cba70796 100644 --- a/src/test/osd_test.cpp +++ b/src/test/osd_test.cpp @@ -3601,6 +3601,9 @@ void test_read_enoent_without_racing_op_marks_corrupted() assert(read_retval == -EIO); assert(f.pg(1, 1).state & PG_HAS_INCOMPLETE); assert(f.pg(1, 1).write_queue.count(oid) == 0); + // The finished read dropped its pins + for (auto & sp: f.pg(1, 1).state_dict) + assert(sp.second.ref_count == 0); delete read; printf("test_read_enoent_without_racing_op_marks_corrupted passed\n"); -- 2.47.3 From 00123a74e2c5c94f204a56c05cb864b3175021a6 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Mon, 23 Nov 2026 23:58:57 +0300 Subject: [PATCH 3/3] osd: Check pins also after a read that waited behind a delete test_read_enoent_waits_behind_running_delete is the only test where a read comes back to a replaced object state and drops its pin on the old one. Assert that no state in the PG is left pinned there too. --- src/test/osd_test.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/osd_test.cpp b/src/test/osd_test.cpp index cba70796..6abe4ca8 100644 --- a/src/test/osd_test.cpp +++ b/src/test/osd_test.cpp @@ -3541,6 +3541,9 @@ void test_read_enoent_waits_behind_running_delete() assert(read_ver == 0); assert(f.pg(1, 1).state == PG_ACTIVE); assert(f.pg(1, 1).write_queue.count(oid) == 0); + // The read dropped its pins, also the one on the state the delete replaced + for (auto & sp: f.pg(1, 1).state_dict) + assert(sp.second.ref_count == 0); delete del; delete read; -- 2.47.3