1
0
mirror of https://github.com/postgres/postgres.git synced 2026-01-26 09:41:40 +03:00

Fix some rounding code for shared memory.

InitializeShmemGUCs() always added 1 to the value calculated for
shared_memory_size_in_huge_pages, which is unnecessary if the
shared memory size is divisible by the huge page size.

CreateAnonymousSegment() neglected to check for overflow when
rounding up to a multiple of the huge page size.

These are arguably bugs, but they seem extremely unlikely to be
causing problems in practice, so no back-patch.

Author: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
Reviewed-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CAO6_Xqq2vZbva0R9eQSY0p2kfksX2aP4r%3D%2BZ_q1HBYNU%3Dm8bBg%40mail.gmail.com
This commit is contained in:
Nathan Bossart
2026-01-23 10:46:49 -06:00
parent a36164e746
commit 8eef2df189
2 changed files with 4 additions and 2 deletions

View File

@@ -617,7 +617,7 @@ CreateAnonymousSegment(Size *size)
GetHugePageSize(&hugepagesize, &mmap_flags);
if (allocsize % hugepagesize != 0)
allocsize += hugepagesize - (allocsize % hugepagesize);
allocsize = add_size(allocsize, hugepagesize - (allocsize % hugepagesize));
ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
PG_MMAP_FLAGS | mmap_flags, -1, 0);

View File

@@ -363,7 +363,9 @@ InitializeShmemGUCs(void)
{
Size hp_required;
hp_required = add_size(size_b / hp_size, 1);
hp_required = size_b / hp_size;
if (size_b % hp_size != 0)
hp_required = add_size(hp_required, 1);
sprintf(buf, "%zu", hp_required);
SetConfigOption("shared_memory_size_in_huge_pages", buf,
PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);