Skip to content

Release the mapped memory at the end of PrepareHugePages()#7

Open
caims0315 wants to merge 2 commits into
ashutosh-bapat:dev/shbuf_resizefrom
caims0315:dev/micai_munmap
Open

Release the mapped memory at the end of PrepareHugePages()#7
caims0315 wants to merge 2 commits into
ashutosh-bapat:dev/shbuf_resizefrom
caims0315:dev/micai_munmap

Conversation

@caims0315

Copy link
Copy Markdown

No description provided.

Minshen Cai added 2 commits March 2, 2026 13:50
Key changes:

- Refactor ShmemResizeStructInSegment() to verify internally: Instead of returning the shared memory location pointer for the caller to verify, the function now validates pointer consistency internally via a new helper VerifyAfterShmemUpdateInSegment(), which fatals on mismatch.
- Align buffer block pointer before verification in ShmemResizeStructInSegment(): When the target segment is BUFFERS_SHMEM_SEGMENT, align the stored shared memory location pointer to PG_IO_ALIGN_SIZE (via TYPEALIGN) before passing it to the verification helper, matching the alignment applied during initial allocation.
- Unify round_off_mapping_sizes() and round_off_mapping_sizes_for_hugepages() into a single round_off_mapping_sizes(): The merged function takes a pagesize parameter (rounds up both shmem_req_size and shmem_reserved to multiples of pagesize, no-op when pagesize is 0). After this change, round_off_mapping_sizes() is only invoked by BufferManagerShmemSize() (for non-main segments) and CalculateShmemSize() (for the main segment).
- Refine BufferManagerShmemSize(): Introduce a padsize (header + spinlock aligned to PG_IO_ALIGN_SIZE) as the base offset for each segment's size calculation. For every segment except MAIN_SHMEM_SEGMENT, compute both shmem_req_size and shmem_reserved, then round off to both BLCKSZ and huge page size via round_off_mapping_sizes(). After this change, BufferManagerShmemSize() is only called from CalculateShmemSize() — the previous direct call in pg_resize_shared_buffers() is replaced with a CalculateShmemSize() call.
- Refine CalculateShmemSize(): Round off MAIN_SHMEM_SEGMENT sizes to both BLCKSZ and huge page size via round_off_mapping_sizes().
- Parameterize PrepareHugePages(): Remove its internal CalculateShmemSize() call and local mapping_sizes array. The caller now passes pre-computed sizes.
- Extract a new method UpdateShmemGUCs(Size size_b): It updates all internal configurations related with Shared Memory. This allows pg_resize_shared_buffers() to call it after a live shared memory resize.
- Downgrade ftruncate failure in AnonymousShmemResize(): Change from ereport(ERROR) to elog(WARNING) + return false, making the failure non-fatal so the caller can handle it gracefully.
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.

1 participant