osd: Free the old object state in mark_object() only after inserting the new one #169

Open
koolkhel wants to merge 1 commits from koolkhel/vitastor:mark-object-reuse into master
Contributor

Description

mark_object() freed the object's old state entry (remove_object_from_state() + deref_object_state()) before it inserted the new one (add_object_to_set()). When the object is alone in its state and nothing else holds the entry, the state_dict node is freed and the very next insert gets the same block from malloc (glibc's tcache), so the new state has the old address. Callers detect a swap by comparing the pointers: continue_primary_read() retries a corrupted read only if op_data->object_state changed, so it sees "no change" and finishes with EIO although a healthy copy is left. The write path compares the same way. The read pin from 3bf0b45a doesn't help: mark_object(ref=true) drops it before the insert.

Example: replica 3, one copy already marked corrupted by an earlier read (the object alone in state {1 corrupted, 2, 3}); a read then gets EIO from the second copy. The second copy is marked, but the read returns EIO instead of reading the third one. Clients retry EIO after client_eio_retry_interval, so the visible effect is a stall of about a second, not an error, unless the retry interval is 0.

Fix: unlink the object from the old state, insert it into the new one, and release the old entry last, so the two can never share an address. Nothing between the two steps looks at the old entry: state_dict is only walked at peering, and PG flags and counters come from the per-object maps that remove_object_from_state() has already updated.

The if (ref) lines next to this change are the ones #158 touches; the hunks don't overlap.

Tested: new test_read_eio_retries_when_mark_reuses_state in osd_test. Without the fix the second read fails with EIO and sends nothing to the third OSD (the old and new state pointers are equal); with GLIBC_TUNABLES=glibc.malloc.tcache_count=0 the unfixed code passes, so the address reuse is the whole cause. With the fix the read retries on the third OSD and returns the data. The whole osd_test passes.

Contributor Name

Yury Luneff (MIND Software LLC, https://mindsw.io)

CLA

### Description `mark_object()` freed the object's old state entry (`remove_object_from_state()` + `deref_object_state()`) before it inserted the new one (`add_object_to_set()`). When the object is alone in its state and nothing else holds the entry, the `state_dict` node is freed and the very next insert gets the same block from malloc (glibc's tcache), so the new state has the old address. Callers detect a swap by comparing the pointers: `continue_primary_read()` retries a corrupted read only if `op_data->object_state` changed, so it sees "no change" and finishes with EIO although a healthy copy is left. The write path compares the same way. The read pin from 3bf0b45a doesn't help: `mark_object(ref=true)` drops it before the insert. Example: replica 3, one copy already marked corrupted by an earlier read (the object alone in state {1 corrupted, 2, 3}); a read then gets EIO from the second copy. The second copy is marked, but the read returns EIO instead of reading the third one. Clients retry EIO after `client_eio_retry_interval`, so the visible effect is a stall of about a second, not an error, unless the retry interval is 0. Fix: unlink the object from the old state, insert it into the new one, and release the old entry last, so the two can never share an address. Nothing between the two steps looks at the old entry: `state_dict` is only walked at peering, and PG flags and counters come from the per-object maps that `remove_object_from_state()` has already updated. The `if (ref)` lines next to this change are the ones #158 touches; the hunks don't overlap. Tested: new `test_read_eio_retries_when_mark_reuses_state` in `osd_test`. Without the fix the second read fails with EIO and sends nothing to the third OSD (the old and new state pointers are equal); with `GLIBC_TUNABLES=glibc.malloc.tcache_count=0` the unfixed code passes, so the address reuse is the whole cause. With the fix the read retries on the third OSD and returns the data. The whole `osd_test` passes. ### Contributor Name Yury Luneff (MIND Software LLC, https://mindsw.io) ### CLA - [x] I accept Vitastor CLA agreement: https://git.yourcmc.ru/vitalif/vitastor/src/branch/master/CLA-en.md
koolkhel added 1 commit 2026-11-24 03:04:14 +03:00
mark_object() freed the object's old state entry before it inserted the
new one. When the object was alone in its state, malloc returned the
same block, so the new state had the old address. Callers tell a swap
by comparing the pointers, so a primary read that had just marked a
copy corrupted saw no change and failed with EIO instead of retrying on
the copy that is left.

Unlink the object from the old state first, insert it into the new one,
and release the old entry last.
vitalif requested changes 2026-11-24 03:44:42 +03:00
vitalif left a comment
Owner

Rebase

Rebase
This pull request has changes conflicting with the target branch.
  • src/test/osd_test.cpp
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u mark-object-reuse:koolkhel-mark-object-reuse
git checkout koolkhel-mark-object-reuse
Sign in to join this conversation.
No Reviewers
No Label
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/vitastor#169
No description provided.