Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 699b1f4

Browse files
committed
Fix thinko in huge_tlb_pages patch.
We calculated the rounded-up size for the allocation, but then failed to use the rounded-up value in the mmap() call. Oops. Also, initialize allocsize, to silence warnings seen with some compilers, as pointed out by Jeff Janes.
1 parent 626a120 commit 699b1f4

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/backend/port/sysv_shmem.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
329329
static void *
330330
CreateAnonymousSegment(Size *size)
331331
{
332-
Size allocsize;
332+
Size allocsize = *size;
333333
void *ptr = MAP_FAILED;
334334

335335
#ifndef MAP_HUGETLB
@@ -358,11 +358,10 @@ CreateAnonymousSegment(Size *size)
358358
*/
359359
int hugepagesize = 2 * 1024 * 1024;
360360

361-
allocsize = *size;
362361
if (allocsize % hugepagesize != 0)
363362
allocsize += hugepagesize - (allocsize % hugepagesize);
364363

365-
ptr = mmap(NULL, *size, PROT_READ | PROT_WRITE,
364+
ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
366365
PG_MMAP_FLAGS | MAP_HUGETLB, -1, 0);
367366
if (huge_tlb_pages == HUGE_TLB_TRY && ptr == MAP_FAILED)
368367
elog(DEBUG1, "mmap with MAP_HUGETLB failed, huge pages disabled: %m");
@@ -372,8 +371,12 @@ CreateAnonymousSegment(Size *size)
372371
if (huge_tlb_pages == HUGE_TLB_OFF ||
373372
(huge_tlb_pages == HUGE_TLB_TRY && ptr == MAP_FAILED))
374373
{
374+
/*
375+
* use the original size, not the rounded up value, when falling
376+
* back to non-huge pages.
377+
*/
375378
allocsize = *size;
376-
ptr = mmap(NULL, *size, PROT_READ | PROT_WRITE, PG_MMAP_FLAGS, -1, 0);
379+
ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE, PG_MMAP_FLAGS, -1, 0);
377380
}
378381

379382
if (ptr == MAP_FAILED)

0 commit comments

Comments
 (0)