Skip to content

static-delta: Avoid copying bspatch output - #3554

Merged
cgwalters merged 3 commits into
ostreedev:mainfrom
razzeee:update-oom
Oct 16, 2026
Merged

cgwalters merged 3 commits into
ostreedev:mainfrom
razzeee:update-oom

Conversation

@razzeee

@razzeee razzeee commented Dec 13, 2025 •

Copy link
Copy Markdown
Contributor

Hey,

my C is very bad, so this was found and done with AI. After looking at the reports an flatpak/flatpak#6255

I would expect this, to be slightly less performant, but work more reliably on systems with less RAM. Most of the things we do here, also seem to have been done in other code files here and there, so it doesn't seem to be unprecedented to go this route.

I haven't been able to runtime test this and I'm unsure how - especially validating, that it still works in the wild (e.g. doesn't break deltas)

@openshift-ci

openshift-ci Bot commented Dec 13, 2025

Copy link
Copy Markdown

Hi @razzeee. Thanks for your PR.

I'm waiting for a github.com member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This is a great approach to solving the OOM issue with large deltas. Using a memory-mapped temporary file is a solid strategy to reduce memory pressure. The implementation of reading the patched data in chunks is also well done.

However, I've found a critical issue with the handling of zero-sized output files. The new implementation using mmap will fail in this case, which was handled correctly by the previous g_malloc0 implementation. This would cause a regression for any delta that results in a zero-byte file.

I've added a few detailed comments with suggestions on how to fix this by special-casing when content_size is zero. Please take a look. With these changes, this should be a very robust improvement!

@simonmicro simonmicro left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would be interested to learn more about your reasoning behind changing the code around _ostree_repo_bare_content_write.

@cgwalters

Copy link
Copy Markdown
Member

Sorry about the delay in review. This will need a rebase.

But bigger picture - I think what we should try to do here is actually pass the tempfile down into the commit path because that code is also going to allocate a tempfile and then commit, so we're just copying data from one tempfile to another...

@razzeee
razzeee force-pushed the update-oom branch 2 times, most recently from 6e2df36 to cb66c5b Compare August 14, 2026 22:09
@razzeee razzeee changed the title fix: Replace in-memory bspatch output buffer with a memory-mapped tem… static-delta: Avoid copying bspatch output Aug 14, 2026
@razzeee

razzeee commented Aug 15, 2026

Copy link
Copy Markdown
Contributor Author

The failing job seems to be unrelated

@cgwalters
cgwalters requested a balanced review from Copilot October 14, 2026 18:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The mapped-output path introduces data-integrity and disk-exhaustion failure risks.

2 open findings
What changed in this PR

Reduces static-delta patch memory usage by writing bspatch output directly into a mapped temporary file instead of copying a heap buffer.

Changes:

  • Adds mapped-output allocation, checksum calculation, and cleanup.
  • Switches patch execution to the mapped-content helpers.
  • Extends private state to track mappings.
File Description
src/​libostree/​ostree-repo-static-delta-processing.c Uses mapped output for bspatch.
src/​libostree/​ostree-repo-private.h Declares mapping helpers and expands private storage.
src/​libostree/​ostree-repo-commit.c Implements mapping lifecycle and checksum handling.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/libostree/ostree-repo-commit.c Resolved
Comment thread src/libostree/ostree-repo-commit.c Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Filesystem-dependent allocation and mapped-output error handling need runtime validation and final human review.

1 open finding
2 resolved since last review

🧠 Review effort: Balanced

Comment on lines +580 to +581
static const char zeros[64 * 1024] = { 0 };
for (size_t offset = 0; offset < real->content_len;)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Filesystem-dependent allocation and mapped-I/O failure paths need runtime validation and final human review.

1 open finding

🧠 Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Storage-integrity-sensitive changes need runtime filesystem validation and human review, with an unresolved opcode-compatibility regression.

2 open findings

🧠 Review effort: Balanced

Comment thread src/libostree/ostree-repo-commit.c Outdated
Comment on lines +560 to +561
if (G_UNLIKELY (real->bytes_written != 0))
return glnx_throw (error, "Cannot map bare content after writing");
Close the read source and release its object name and retained xattrs at the common exit, including when an opcode fails before closing the object.
Map patch output into the content writer tempfile instead of allocating a file-sized heap buffer and copying it. Reserve storage before mapping, with bounded writes and a flush when preallocation is unsupported.

Checksum only appended bytes and restore the write offset after unmapping. Cover empty and multi-chunk output, subsequent writes, writes before patches, and repeated patches through the offline executor.
Force ENOSYS and EOPNOTSUPP, then verify stored bytes and checksums across patch/write sequences. Inject write and sync errors and check error propagation, missing target objects, and temporary-file cleanup.
@razzeee

razzeee commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

I've separated this into multiple commits now, to help with review, as it increased a bit in line count.

@cgwalters cgwalters left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall looks good as is

if (!G_IN_SET (errno, ENOSYS, EOPNOTSUPP))
return glnx_throw_errno_prefix (error, "fallocate");

/* Materialize the backing file without a file-sized heap allocation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmmm I think glibc posix_fallocate has this fallback already


/* Map the writer's own temporary file, avoiding a separate patch buffer and copy. */
gboolean
_ostree_repo_bare_content_map (OstreeRepoBareContent *barewrite, guint8 **out_buf,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is OK as is but I'd prefer if we returned the length alongside the buf as a followup - kind of like a Rust slice. It's too bad glib (or glibc, or C) don't have a standard type for this, in Rust it's just &mut [u8] of course.

@cgwalters
cgwalters merged commit 1bc34f3 into ostreedev:main Oct 16, 2026
40 checks passed
@razzeee
razzeee deleted the update-oom branch October 18, 2026 16:32
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.

4 participants