Skip to content

hashtable: fix dismissHashtable madvise size#3533

Merged
madolson merged 1 commit into
valkey-io:unstablefrom
charsyam:fix/hashtable-dismiss-size
Apr 27, 2026
Merged

hashtable: fix dismissHashtable madvise size#3533
madolson merged 1 commit into
valkey-io:unstablefrom
charsyam:fix/hashtable-dismiss-size

Conversation

@charsyam

Copy link
Copy Markdown
Contributor

The bug was in dismissHashtable(), which computes the size passed to zmadvise_dontneed() for the top-level hashtable
tables.

ht->tables[i] points to a contiguous array of bucket objects, but the code used sizeof(bucket *) instead of
sizeof(bucket) when calculating the length. That means it treated the allocation like an array of pointers rather than an
array of buckets.

As a result, the advised range was much smaller than the actual table allocation. On 64-bit builds, bucket is 64 bytes
while bucket * is 8 bytes, so only about one eighth of the table was covered. This does not usually break correctness,
but it defeats the purpose of the function: after a fork, we want to tell the kernel that the hashtable pages are no
longer needed so we reduce copy-on-write overhead. With the wrong size, most of the table memory was never included in
that hint.

The fix is to use sizeof(bucket) so the full top-level bucket array is passed to zmadvise_dontneed().

Signed-off-by: DaeMyung Kang <charsyam@gmail.com>
@codecov

codecov Bot commented Apr 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.45%. Comparing base (4a42c95) to head (81012b3).
⚠️ Report is 20 commits behind head on unstable.

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #3533      +/-   ##
============================================
+ Coverage     76.40%   76.45%   +0.05%     
============================================
  Files           159      159              
  Lines         79851    79851              
============================================
+ Hits          61008    61049      +41     
+ Misses        18843    18802      -41     
Files with missing lines Coverage Δ
src/hashtable.c 97.84% <100.00%> (ø)

... and 18 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@madolson madolson 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.

Nice catch.

@madolson madolson merged commit bb88665 into valkey-io:unstable Apr 27, 2026
59 checks passed
sarthakaggarwal97 pushed a commit to sarthakaggarwal97/valkey that referenced this pull request Apr 27, 2026
The bug was in dismissHashtable(), which computes the size passed to
zmadvise_dontneed() for the top-level hashtable
  tables.

ht->tables[i] points to a contiguous array of bucket objects, but the
code used sizeof(bucket *) instead of
sizeof(bucket) when calculating the length. That means it treated the
allocation like an array of pointers rather than an
  array of buckets.

As a result, the advised range was much smaller than the actual table
allocation. On 64-bit builds, bucket is 64 bytes
while bucket * is 8 bytes, so only about one eighth of the table was
covered. This does not usually break correctness,
but it defeats the purpose of the function: after a fork, we want to
tell the kernel that the hashtable pages are no
longer needed so we reduce copy-on-write overhead. With the wrong size,
most of the table memory was never included in
  that hint.

The fix is to use sizeof(bucket) so the full top-level bucket array is
passed to zmadvise_dontneed().

Signed-off-by: DaeMyung Kang <charsyam@gmail.com>
sarthakaggarwal97 added a commit to sarthakaggarwal97/valkey that referenced this pull request Apr 27, 2026
madolson pushed a commit that referenced this pull request Apr 27, 2026
The bug was in dismissHashtable(), which computes the size passed to
zmadvise_dontneed() for the top-level hashtable
  tables.

ht->tables[i] points to a contiguous array of bucket objects, but the
code used sizeof(bucket *) instead of
sizeof(bucket) when calculating the length. That means it treated the
allocation like an array of pointers rather than an
  array of buckets.

As a result, the advised range was much smaller than the actual table
allocation. On 64-bit builds, bucket is 64 bytes
while bucket * is 8 bytes, so only about one eighth of the table was
covered. This does not usually break correctness,
but it defeats the purpose of the function: after a fork, we want to
tell the kernel that the hashtable pages are no
longer needed so we reduce copy-on-write overhead. With the wrong size,
most of the table memory was never included in
  that hint.

The fix is to use sizeof(bucket) so the full top-level bucket array is
passed to zmadvise_dontneed().

Signed-off-by: DaeMyung Kang <charsyam@gmail.com>
sarthakaggarwal97 added a commit to sarthakaggarwal97/valkey that referenced this pull request Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants