Skip to content

src/mon/MgrStatMonitor: fix invalid iterator increment in calc_pool_availability() - #63398

Merged
NitzanMordhai merged 1 commit into
ceph:mainfrom
NitzanMordhai:wip-nitzan-calc-pool-availability-valgrind-invalid-read
Jun 23, 2025
Merged

NitzanMordhai merged 1 commit into
ceph:mainfrom
NitzanMordhai:wip-nitzan-calc-pool-availability-valgrind-invalid-read

Conversation

@NitzanMordhai

@NitzanMordhai NitzanMordhai commented May 21, 2025 •

Copy link
Copy Markdown
Contributor

Erasing entries from pool_availability inside a range-for
loop invalidated the hidden iterator, triggering an
“Invalid read” under Valgrind.

  • Use std::erase_if(pool_availability, predicate) for
    atomic removal.
  • Refactor the stats-update loop to use structured bindings
    and a clear ++it for readability.

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

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

@NitzanMordhai
NitzanMordhai requested review from Copilot and kamoltat May 21, 2025 11:50
@NitzanMordhai
NitzanMordhai requested a review from a team as a code owner May 21, 2025 11:50

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 fixes an invalid iterator increment in calc_pool_availability() by using std::erase_if for atomic removals and refactors loops to structured bindings for clarity.

  • Replace manual erase-in-loop with std::erase_if to avoid invalidated iterators.
  • Refactor pool-add and stats-update loops to use structured bindings.
Comments suppressed due to low confidence (2)

src/mon/MgrStatMonitor.cc:74

  • [nitpick] Using _ as a placeholder can be unclear or conflict with other code; consider naming it unused or annotating with [[maybe_unused]].
for (auto [poolid, _] : digest.pool_pg_unavailable_map) {

src/mon/MgrStatMonitor.cc:82

  • Consider adding unit tests for calc_pool_availability() to cover pool addition, removal, and status transitions to prevent regressions.
std::erase_if(pool_availability, [&](auto& kv) {

Comment thread src/mon/MgrStatMonitor.cc Resolved
@NitzanMordhai
NitzanMordhai force-pushed the wip-nitzan-calc-pool-availability-valgrind-invalid-read branch from a44b1ef to b09e266 Compare May 21, 2025 11:53
@NitzanMordhai
NitzanMordhai requested a review from Copilot May 21, 2025 11:54

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 invalid iterator usage in calc_pool_availability() by switching to atomic erasure and cleaning up loop logic.

  • Use std::erase_if to remove stale pools safely
  • Refactor addition and stats‐update loops with structured bindings and clear iterator increments
Comments suppressed due to low confidence (1)

src/mon/MgrStatMonitor.cc:82

  • The new removal logic using std::erase_if and the subsequent availability updates lack unit tests to verify pool addition, removal, and status transitions; consider adding coverage for these cases.
std::erase_if(pool_availability, [&](auto& kv) {

Comment thread src/mon/MgrStatMonitor.cc Resolved
Comment thread src/mon/MgrStatMonitor.cc Outdated

Copilot AI May 21, 2025

Copy link

Choose a reason for hiding this comment

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

Using operator[] on digest.pool_pg_unavailable_map for a lookup can inadvertently insert default entries; consider using find or at to avoid side-effects on the map.

Suggested change
const bool currently_avail = digest.pool_pg_unavailable_map[poolid].empty();
auto it = digest.pool_pg_unavailable_map.find(poolid);
const bool currently_avail = (it != digest.pool_pg_unavailable_map.end()) && it->second.empty();

Copilot uses AI. Check for mistakes.

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.

@NitzanMordhai - copilot may have a point here. If you are sure you are not inserting - maybe add a comment explaining?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ronen-fr, it sounds reasonable. will change it

Comment thread src/mon/MgrStatMonitor.cc Outdated

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.

as we are still C++-20: don't you get 'unused' warnings for the '_'?
If so - maybe mark it as [[maybe_unused]]] ?

Comment thread src/mon/MgrStatMonitor.cc Outdated

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.

can it be
(const auto& kv)
?

Comment thread src/mon/MgrStatMonitor.cc Outdated

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.

I'd suggest using fmt::format() for the whole modified code.

@ronen-fr

Copy link
Copy Markdown
Contributor

LGTM, but note Copilot's comment (and some nits).

@ronen-fr ronen-fr 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.

but see notes

@NitzanMordhai
NitzanMordhai force-pushed the wip-nitzan-calc-pool-availability-valgrind-invalid-read branch from b09e266 to 6e4b99c Compare May 22, 2025 09:01
@NitzanMordhai
NitzanMordhai requested a review from Copilot May 22, 2025 09:02

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 fixes an invalid iterator usage in calc_pool_availability() by replacing the unsafe removal loop with std::erase_if and refactoring the stats-update loop for clarity.

  • Replace manual iterator increment and erasure with an atomic removal using std::erase_if
  • Refactor loops to use structured bindings and clearer logging
Comments suppressed due to low confidence (1)

src/mon/MgrStatMonitor.cc:105

  • Accessing it->second when it equals digest.pool_pg_unavailable_map.end() leads to undefined behavior. Consider splitting the condition to first check if the iterator is not equal to end before accessing its second element.
const bool currently_avail = (it == digest.pool_pg_unavailable_map.end()) && it->second.empty();

@NitzanMordhai
NitzanMordhai force-pushed the wip-nitzan-calc-pool-availability-valgrind-invalid-read branch from 6e4b99c to db90c27 Compare May 22, 2025 09:11
@NitzanMordhai

Copy link
Copy Markdown
Contributor Author

fixing the currently_avail .

@NitzanMordhai
NitzanMordhai requested a review from Copilot May 22, 2025 09:13

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 fixes an invalid iterator increment in calc_pool_availability() by refactoring the pool availability update loop and using std::erase_if to safely remove pools during iteration. Key changes include:

  • Replacing the manual iterator erase pattern with std::erase_if.
  • Refactoring loops to leverage structured bindings and clearer logging.
  • Updating logging to use fmt::format for consistency.

Comment thread src/mon/MgrStatMonitor.cc Resolved
Comment thread src/mon/MgrStatMonitor.cc Outdated

Copilot AI May 22, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] Ensure that the use of fmt::format for logging is consistent with the remainder of the codebase so that logging remains uniform across modules.

Suggested change
dout(20) << fmt::format("{}", __func__) << dendl;
dout(20) << __func__ << dendl;

Copilot uses AI. Check for mistakes.
@NitzanMordhai
NitzanMordhai force-pushed the wip-nitzan-calc-pool-availability-valgrind-invalid-read branch 2 times, most recently from c05d19f to d6dc687 Compare May 22, 2025 09:22
@NitzanMordhai
NitzanMordhai requested a review from Copilot May 22, 2025 09:22

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 fixes an invalid iterator increment issue in calc_pool_availability() by using std::erase_if to atomically remove invalid pool entries and refactors the stats-update loop for better clarity and maintainability.

  • Replace manual iterator handling with std::erase_if for safe removal.
  • Use structured bindings and clearer iterator increments.
  • Improve logging messages via fmt::format for enhanced readability.

@yaarith

yaarith commented May 28, 2025

Copy link
Copy Markdown
Contributor

@shraddhaag please take a look

@rzarzynski

Copy link
Copy Markdown
Contributor

Ping @shraddhaag.

@shraddhaag shraddhaag 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 a lot Nitzan!

@github-actions

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

@kamoltat

Copy link
Copy Markdown
Member

@NitzanMordhai
NitzanMordhai force-pushed the wip-nitzan-calc-pool-availability-valgrind-invalid-read branch from d6dc687 to 98be774 Compare June 23, 2025 05:01
…vailability()

Erasing entries from `pool_availability` inside a range-for
loop invalidated the hidden iterator, triggering an
“Invalid read” under Valgrind.

- Use `std::erase_if(pool_availability, predicate)` for
  atomic removal.
- Refactor the stats-update loop to use structured bindings
  and a clear `++it` for readability.

Fixes: https://tracker.ceph.com/issues/71271
Signed-off-by: Nitzan Mordechai <[email protected]>
@NitzanMordhai
NitzanMordhai force-pushed the wip-nitzan-calc-pool-availability-valgrind-invalid-read branch from 98be774 to 7369a4d Compare June 23, 2025 05:16
@NitzanMordhai

Copy link
Copy Markdown
Contributor Author

jenkins test make check

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

jenkins test api

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

jenkins test make check arm64

@NitzanMordhai
NitzanMordhai merged commit afd7739 into ceph:main Jun 23, 2025
@NitzanMordhai
NitzanMordhai deleted the wip-nitzan-calc-pool-availability-valgrind-invalid-read branch June 23, 2025 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants