Skip to content

crimson/osd: guard recovery clone overlap with try_lock_for_read - #69668

Merged
knrt10 merged 1 commit into
ceph:mainfrom
knrt10:fix-buffer-pos
Jul 9, 2026
Merged

knrt10 merged 1 commit into
ceph:mainfrom
knrt10:fix-buffer-pos

Conversation

@knrt10

@knrt10 knrt10 commented Jun 23, 2026

Copy link
Copy Markdown
Member

Crimson calc_clone_subsets() and calc_head_subsets() could use a neighbor clone as a clone_subset source during recovery without checking whether that clone was safe to read. Classic OSD takes a read lock on the neighbor clone via try_lock_for_read() before using it for osd_recover_clone_overlap.

Under snap trim + recovery stress, recovery could clone_range from a clone that snap trim was modifying, producing wrong object content (ceph_test_rados: incorrect buffer at pos 1462272).

Add RecoveryCloneLockManager and wire it through replicated recovery so neighbor clones are only selected when a read lock can be taken, and hold those locks until push/pull recovery completes.

Fixes: https://tracker.ceph.com/issues/76499

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@knrt10
knrt10 requested a review from a team as a code owner June 23, 2026 10:20
Copilot AI review requested due to automatic review settings June 23, 2026 10:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a Crimson replicated recovery correctness issue where clone-overlap recovery could read from a neighboring clone while it was concurrently being modified (e.g., by snap trim). It introduces a mechanism to only select neighbor clones for clone-overlap when they can be safely read-locked, and keeps those locks held for the duration of push/pull recovery.

Changes:

  • Introduces RecoveryCloneLockManager to acquire/hold/release read locks on neighbor clone ObjectContexts used for clone-overlap recovery.
  • Wires clone-lock acquisition via try_lock_for_read callbacks into calc_clone_subsets() / calc_head_subsets() and threads the lock manager through replicated push/pull recovery flows.
  • Ensures recovery paths release clone locks on completion and on error/cleanup paths.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/crimson/osd/replicated_recovery_backend.h Extends replicated recovery APIs to pass/retain a clone lock manager across recovery operations.
src/crimson/osd/replicated_recovery_backend.cc Implements lock acquisition helpers, threads lock manager through push/pull setup, and releases locks on completion/errors.
src/crimson/osd/recovery_backend.h Adds RecoveryCloneLockManager and stores it in push/pull tracking structs.
src/crimson/osd/recovery_backend.cc Releases clone locks during recovery backend cleanup.
src/crimson/osd/object_metadata_helper.h Adds callback-based try_lock_for_read/release_locks hooks to subset-calculation helpers.
src/crimson/osd/object_metadata_helper.cc Applies lock-guarded neighbor selection in clone/head subset calculations and supports early lock release when clone-overlap is abandoned.
src/crimson/osd/object_context.h Exposes try_get_read_lock() / put_read_lock() helpers for ObjectContext read-lock management.

Comment thread src/crimson/osd/recovery_backend.h Resolved
@knrt10

knrt10 commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

@tchaikov tchaikov left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, the fix looks right to me. locking the neighbor clones before using them as a clone_subset source is what classic does, and holding them until the push/pull completes matches ObcLockManager.

one thing i'd like to reconsider: the two std::function params on calc_clone_subsets() / calc_head_subsets(). they push the locking and the release bookkeeping down into object_metadata_helper, which i'd rather keep pure and unit-testable.

i think it reads better if calc_* just returns the candidate clones in preference order and the backend locks the first one it can. the loop already walks best-first, so we keep the "can't lock i, try i-1" behavior, and the lock policy stays in the backend:

std::optional<clone_candidate_t> commit_first_lockable(   // {clone, overlap}
    std::span<const clone_candidate_t> candidates, RecoveryCloneLockManager& mgr) {
  for (const auto& c : candidates) {
    if (!pg.get_local_missing().is_missing(c.clone) &&
        mgr.try_lock_for_read(c.clone, pg.obc_registry)) {
      return c;
    }
  }
  return std::nullopt;
}

then calc_* doesn't know about locks at all, and release_locks (and the return subsets; patch in the too-many-holes branch) goes away.

unrelated: can release_locks() be the destructor instead of ceph_assert(locks.empty())? the defaulted move-assign (push_info.clone_lock_manager = std::move(...)) replaces the map without unlocking, so a non-empty target would silently leak a read lock and block snap trim on that clone. unlock_for_read() just decrements and wakes on zero, so it's safe from a dtor.

not blocking, the correctness is the main thing. thanks again.

@knrt10
knrt10 requested a review from tchaikov July 9, 2026 01:57
@knrt10

knrt10 commented Jul 9, 2026

Copy link
Copy Markdown
Member Author

@tchaikov updated as you suggested.

Crimson recovery could use a neighbor clone as a clone_subset source
during osd_recover_clone_overlap without checking whether that clone
was safe to read. Classic OSD takes a read lock on the neighbor clone
via try_lock_for_read() before using it.

Under snap trim + recovery stress, recovery could clone_range from a
clone that snap trim was modifying, producing wrong object content
(ceph_test_rados: incorrect buffer at pos 1462272).

calc_clone_subsets() and calc_head_subsets() now return clone overlap
candidates in preference order only. ReplicatedRecoveryBackend locks
the first usable neighbor clone from each list and holds those read
locks until push/pull recovery completes. RecoveryCloneLockManager
releases locks in its destructor so move-assign cannot leak a held
lock and block snap trim.

Fixes: https://tracker.ceph.com/issues/76499
Signed-off-by: Kautilya Tripathi <[email protected]>

@Matan-B Matan-B left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Can we add coverage in test/crimson/test_calc_subsets.cc for the new candidate-plan behavior?

@Matan-B Matan-B added this to Crimson Jul 9, 2026
@Matan-B Matan-B moved this to In Progress in Crimson Jul 9, 2026

@tchaikov tchaikov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fix looks right, so lgtm.

for a follow-up: the lock lifecycle is ported straight from classic, but crimson's tri_mutex lets it be simpler than classic can be.

classic's ObcLockManager is deliberately not RAII, its dtor asserts locks.empty(). it has to be manual, because release_object_locks() requeues the recovery / snap-trim / client ops that were blocked on the lock (queue_recovery(), TrimWriteUnblocked, requeue_ops()), and that needs PG context which a dtor doesn't have. so classic releases explicitly at every exit and asserts-empty to catch a missed one.

crimson's read unlock has no such side effect. tri_mutex::unlock_for_read() decrements and calls wake(), and seastar reschedules the blocked fibers itself. no requeue, no PG context. that's what RecoveryCloneLockManager's dtor comment already notes, and it means the read lock is dtor-safe here in a way it never is in classic.

so the explicit release_locks() at six sites, plus the dtor as a backstop, aren't needed. make the clone locks an RAII member of PushInfo/PullInfo and let destruction release them. to free the clone before the recovering entry is torn down, capture the guard in the completion continuation (.finally([locks = std::move(...)]{})) instead of releasing by hand. recalc_subsets then becomes a reassignment of the lock collection, which releases the old locks, rather than a release-then-reacquire.

on the mechanics: ObjectContextLoader::state_t already models a single obc RAII lock (obc + state + release_lock, move-only). it only lacks a sync try-read, it has lock_excl_sync() and the async blocking lock_to(RWREAD), but no non-blocking read acquire. that's one method next to lock_excl_sync:

// state_t
bool try_lock_for_read() {
  if (obc->lock.try_lock_for_read()) {
    state = RWState::RWREAD;
    return true;
  }
  return false;
}

with that, recovery holds a std::vector<Manager> (at most two clones, older + newer), and RecoveryCloneLockManager plus the new ObjectContext::try_get_read_lock/put_read_lock go away.

happy to take this as a follow-up so it doesn't hold up this PR. the one thing to confirm while doing it: the recovering entry is destroyed on every completion and error path, since that becomes the release boundary.

@tchaikov

tchaikov commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

the reason why i propose RAII + Manager is because that the machinery introduced in this pull request is a parallel lock manager where crimson already has. but this is but a nit, so i can take care of it after this change lands.

@knrt10

knrt10 commented Jul 9, 2026

Copy link
Copy Markdown
Member Author

@knrt10

knrt10 commented Jul 9, 2026

Copy link
Copy Markdown
Member Author

merging since qa is passing

@knrt10
knrt10 merged commit e06a677 into ceph:main Jul 9, 2026
6 checks passed
@knrt10
knrt10 deleted the fix-buffer-pos branch July 9, 2026 13:03
@Matan-B Matan-B moved this from In Progress to Merged (Main) in Crimson Jul 9, 2026
@tchaikov

tchaikov commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

the reason why i propose RAII + Manager is because that the machinery introduced in this pull request is a parallel lock manager where crimson already has. but this is but a nit, so i can take care of it after this change lands.

created #70085

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Merged (Main)

Development

Successfully merging this pull request may close these issues.

4 participants