PostgreSQL Source Code git master
win32_shmem.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/pg_shmem.h"
#include "utils/guc_hooks.h"
Include dependency graph for win32_shmem.c:

Go to the source code of this file.

Macros

#define PROTECTIVE_REGION_SIZE   (10 * WIN32_STACK_RLIMIT)
 

Functions

static bool EnableLockPagesPrivilege (int elevel)
 
static void pgwin32_SharedMemoryDelete (int status, Datum shmId)
 
static char * GetSharedMemName (void)
 
bool PGSharedMemoryIsInUse (unsigned long id1, unsigned long id2)
 
PGShmemHeaderPGSharedMemoryCreate (Size size, PGShmemHeader **shim)
 
void PGSharedMemoryReAttach (void)
 
void PGSharedMemoryNoReAttach (void)
 
void PGSharedMemoryDetach (void)
 
int pgwin32_ReserveSharedMemoryRegion (HANDLE hChild)
 
void GetHugePageSize (Size *hugepagesize, int *mmap_flags)
 
bool check_huge_page_size (int *newval, void **extra, GucSource source)
 

Variables

void * ShmemProtectiveRegion = NULL
 
HANDLE UsedShmemSegID = INVALID_HANDLE_VALUE
 
void * UsedShmemSegAddr = NULL
 
static Size UsedShmemSegSize = 0
 

Macro Definition Documentation

◆ PROTECTIVE_REGION_SIZE

#define PROTECTIVE_REGION_SIZE   (10 * WIN32_STACK_RLIMIT)

Definition at line 41 of file win32_shmem.c.

Function Documentation

◆ check_huge_page_size()

bool check_huge_page_size ( int *  newval,
void **  extra,
GucSource  source 
)

Definition at line 642 of file win32_shmem.c.

643{
644 if (*newval != 0)
645 {
646 GUC_check_errdetail("\"huge_page_size\" must be 0 on this platform.");
647 return false;
648 }
649 return true;
650}
#define newval
#define GUC_check_errdetail
Definition: guc.h:476

References GUC_check_errdetail, and newval.

◆ EnableLockPagesPrivilege()

static bool EnableLockPagesPrivilege ( int  elevel)
static

Definition at line 137 of file win32_shmem.c.

138{
139 HANDLE hToken;
140 TOKEN_PRIVILEGES tp;
141 LUID luid;
142
143 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
144 {
145 ereport(elevel,
146 (errmsg("could not enable user right \"%s\": error code %lu",
147
148 /*
149 * translator: This is a term from Windows and should be translated to
150 * match the Windows localization.
151 */
152 _("Lock pages in memory"),
153 GetLastError()),
154 errdetail("Failed system call was %s.", "OpenProcessToken")));
155 return FALSE;
156 }
157
158 if (!LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &luid))
159 {
160 ereport(elevel,
161 (errmsg("could not enable user right \"%s\": error code %lu", _("Lock pages in memory"), GetLastError()),
162 errdetail("Failed system call was %s.", "LookupPrivilegeValue")));
163 CloseHandle(hToken);
164 return FALSE;
165 }
166 tp.PrivilegeCount = 1;
167 tp.Privileges[0].Luid = luid;
168 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
169
170 if (!AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL))
171 {
172 ereport(elevel,
173 (errmsg("could not enable user right \"%s\": error code %lu", _("Lock pages in memory"), GetLastError()),
174 errdetail("Failed system call was %s.", "AdjustTokenPrivileges")));
175 CloseHandle(hToken);
176 return FALSE;
177 }
178
179 if (GetLastError() != ERROR_SUCCESS)
180 {
181 if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
182 ereport(elevel,
183 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
184 errmsg("could not enable user right \"%s\"", _("Lock pages in memory")),
185 errhint("Assign user right \"%s\" to the Windows user account which runs PostgreSQL.",
186 _("Lock pages in memory"))));
187 else
188 ereport(elevel,
189 (errmsg("could not enable user right \"%s\": error code %lu", _("Lock pages in memory"), GetLastError()),
190 errdetail("Failed system call was %s.", "AdjustTokenPrivileges")));
191 CloseHandle(hToken);
192 return FALSE;
193 }
194
195 CloseHandle(hToken);
196
197 return TRUE;
198}
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define _(x)
Definition: elog.c:90
#define ereport(elevel,...)
Definition: elog.h:149

References _, ereport, errcode(), errdetail(), errhint(), and errmsg().

Referenced by PGSharedMemoryCreate().

◆ GetHugePageSize()

void GetHugePageSize ( Size hugepagesize,
int *  mmap_flags 
)

Definition at line 630 of file win32_shmem.c.

631{
632 if (hugepagesize)
633 *hugepagesize = 0;
634 if (mmap_flags)
635 *mmap_flags = 0;
636}

◆ GetSharedMemName()

static char * GetSharedMemName ( void  )
static

Definition at line 65 of file win32_shmem.c.

66{
67 char *retptr;
68 DWORD bufsize;
69 DWORD r;
70 char *cp;
71
72 bufsize = GetFullPathName(DataDir, 0, NULL, NULL);
73 if (bufsize == 0)
74 elog(FATAL, "could not get size for full pathname of datadir %s: error code %lu",
75 DataDir, GetLastError());
76
77 retptr = malloc(bufsize + 18); /* 18 for Global\PostgreSQL: */
78 if (retptr == NULL)
79 elog(FATAL, "could not allocate memory for shared memory name");
80
81 strcpy(retptr, "Global\\PostgreSQL:");
82 r = GetFullPathName(DataDir, bufsize, retptr + 18, NULL);
83 if (r == 0 || r > bufsize)
84 elog(FATAL, "could not generate full pathname for datadir %s: error code %lu",
85 DataDir, GetLastError());
86
87 /*
88 * XXX: Intentionally overwriting the Global\ part here. This was not the
89 * original approach, but putting it in the actual Global\ namespace
90 * causes permission errors in a lot of cases, so we leave it in the
91 * default namespace for now.
92 */
93 for (cp = retptr; *cp; cp++)
94 if (*cp == '\\')
95 *cp = '/';
96
97 return retptr;
98}
#define FATAL
Definition: elog.h:41
#define elog(elevel,...)
Definition: elog.h:225
char * DataDir
Definition: globals.c:70
#define malloc(a)
Definition: header.h:50
#define bufsize
Definition: indent_globs.h:36

References bufsize, DataDir, elog, FATAL, and malloc.

Referenced by PGSharedMemoryCreate(), and PGSharedMemoryIsInUse().

◆ PGSharedMemoryCreate()

PGShmemHeader * PGSharedMemoryCreate ( Size  size,
PGShmemHeader **  shim 
)

Definition at line 207 of file win32_shmem.c.

209{
210 void *memAddress;
211 PGShmemHeader *hdr;
212 HANDLE hmap,
213 hmap2;
214 char *szShareMem;
215 int i;
216 DWORD size_high;
217 DWORD size_low;
218 SIZE_T largePageSize = 0;
219 Size orig_size = size;
220 DWORD flProtect = PAGE_READWRITE;
221 DWORD desiredAccess;
222
224 MEM_RESERVE, PAGE_NOACCESS);
225 if (ShmemProtectiveRegion == NULL)
226 elog(FATAL, "could not reserve memory region: error code %lu",
227 GetLastError());
228
229 /* Room for a header? */
230 Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
231
232 szShareMem = GetSharedMemName();
233
234 UsedShmemSegAddr = NULL;
235
237 {
238 /* Does the processor support large pages? */
239 largePageSize = GetLargePageMinimum();
240 if (largePageSize == 0)
241 {
243 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
244 errmsg("the processor does not support large pages")));
246 (errmsg_internal("disabling huge pages")));
247 }
249 {
251 (errmsg_internal("disabling huge pages")));
252 }
253 else
254 {
255 /* Huge pages available and privilege enabled, so turn on */
256 flProtect = PAGE_READWRITE | SEC_COMMIT | SEC_LARGE_PAGES;
257
258 /* Round size up as appropriate. */
259 if (size % largePageSize != 0)
260 size += largePageSize - (size % largePageSize);
261 }
262 }
263
264retry:
265#ifdef _WIN64
266 size_high = size >> 32;
267#else
268 size_high = 0;
269#endif
270 size_low = (DWORD) size;
271
272 /*
273 * When recycling a shared memory segment, it may take a short while
274 * before it gets dropped from the global namespace. So re-try after
275 * sleeping for a second, and continue retrying 10 times. (both the 1
276 * second time and the 10 retries are completely arbitrary)
277 */
278 for (i = 0; i < 10; i++)
279 {
280 /*
281 * In case CreateFileMapping() doesn't set the error code to 0 on
282 * success
283 */
284 SetLastError(0);
285
286 hmap = CreateFileMapping(INVALID_HANDLE_VALUE, /* Use the pagefile */
287 NULL, /* Default security attrs */
288 flProtect,
289 size_high, /* Size Upper 32 Bits */
290 size_low, /* Size Lower 32 bits */
291 szShareMem);
292
293 if (!hmap)
294 {
295 if (GetLastError() == ERROR_NO_SYSTEM_RESOURCES &&
297 (flProtect & SEC_LARGE_PAGES) != 0)
298 {
299 elog(DEBUG1, "CreateFileMapping(%zu) with SEC_LARGE_PAGES failed, "
300 "huge pages disabled",
301 size);
302
303 /*
304 * Use the original size, not the rounded-up value, when
305 * falling back to non-huge pages.
306 */
307 size = orig_size;
308 flProtect = PAGE_READWRITE;
309 goto retry;
310 }
311 else
313 (errmsg("could not create shared memory segment: error code %lu", GetLastError()),
314 errdetail("Failed system call was CreateFileMapping(size=%zu, name=%s).",
315 size, szShareMem)));
316 }
317
318 /*
319 * If the segment already existed, CreateFileMapping() will return a
320 * handle to the existing one and set ERROR_ALREADY_EXISTS.
321 */
322 if (GetLastError() == ERROR_ALREADY_EXISTS)
323 {
324 CloseHandle(hmap); /* Close the handle, since we got a valid one
325 * to the previous segment. */
326 hmap = NULL;
327 Sleep(1000);
328 continue;
329 }
330 break;
331 }
332
333 /*
334 * If the last call in the loop still returned ERROR_ALREADY_EXISTS, this
335 * shared memory segment exists and we assume it belongs to somebody else.
336 */
337 if (!hmap)
339 (errmsg("pre-existing shared memory block is still in use"),
340 errhint("Check if there are any old server processes still running, and terminate them.")));
341
342 free(szShareMem);
343
344 /*
345 * Make the handle inheritable
346 */
347 if (!DuplicateHandle(GetCurrentProcess(), hmap, GetCurrentProcess(), &hmap2, 0, TRUE, DUPLICATE_SAME_ACCESS))
349 (errmsg("could not create shared memory segment: error code %lu", GetLastError()),
350 errdetail("Failed system call was DuplicateHandle.")));
351
352 /*
353 * Close the old, non-inheritable handle. If this fails we don't really
354 * care.
355 */
356 if (!CloseHandle(hmap))
357 elog(LOG, "could not close handle to shared memory: error code %lu", GetLastError());
358
359 desiredAccess = FILE_MAP_WRITE | FILE_MAP_READ;
360
361#ifdef FILE_MAP_LARGE_PAGES
362 /* Set large pages if wanted. */
363 if ((flProtect & SEC_LARGE_PAGES) != 0)
364 desiredAccess |= FILE_MAP_LARGE_PAGES;
365#endif
366
367 /*
368 * Get a pointer to the new shared memory segment. Map the whole segment
369 * at once, and let the system decide on the initial address.
370 */
371 memAddress = MapViewOfFileEx(hmap2, desiredAccess, 0, 0, 0, NULL);
372 if (!memAddress)
374 (errmsg("could not create shared memory segment: error code %lu", GetLastError()),
375 errdetail("Failed system call was MapViewOfFileEx.")));
376
377
378
379 /*
380 * OK, we created a new segment. Mark it as created by this process. The
381 * order of assignments here is critical so that another Postgres process
382 * can't see the header as valid but belonging to an invalid PID!
383 */
384 hdr = (PGShmemHeader *) memAddress;
385 hdr->creatorPID = getpid();
386 hdr->magic = PGShmemMagic;
387
388 /*
389 * Initialize space allocation status for segment.
390 */
391 hdr->totalsize = size;
392 hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
393 hdr->dsm_control = 0;
394
395 /* Save info for possible future use */
396 UsedShmemSegAddr = memAddress;
398 UsedShmemSegID = hmap2;
399
400 /* Register on-exit routine to delete the new segment */
402
403 *shim = hdr;
404
405 /* Report whether huge pages are in use */
406 SetConfigOption("huge_pages_status", (flProtect & SEC_LARGE_PAGES) ?
407 "on" : "off", PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
408
409 return hdr;
410}
#define MAXALIGN(LEN)
Definition: c.h:768
#define Assert(condition)
Definition: c.h:815
size_t Size
Definition: c.h:562
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define LOG
Definition: elog.h:31
#define DEBUG1
Definition: elog.h:30
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4332
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:110
@ PGC_INTERNAL
Definition: guc.h:69
int huge_pages
Definition: guc_tables.c:562
#define free(a)
Definition: header.h:65
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
int i
Definition: isn.c:72
@ HUGE_PAGES_ON
Definition: pg_shmem.h:53
@ HUGE_PAGES_TRY
Definition: pg_shmem.h:54
#define PGShmemMagic
Definition: pg_shmem.h:32
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
static pg_noinline void Size size
Definition: slab.c:607
dsm_handle dsm_control
Definition: pg_shmem.h:36
Size freeoffset
Definition: pg_shmem.h:35
pid_t creatorPID
Definition: pg_shmem.h:33
int32 magic
Definition: pg_shmem.h:31
Size totalsize
Definition: pg_shmem.h:34
#define PROTECTIVE_REGION_SIZE
Definition: win32_shmem.c:41
void * ShmemProtectiveRegion
Definition: win32_shmem.c:42
static char * GetSharedMemName(void)
Definition: win32_shmem.c:65
HANDLE UsedShmemSegID
Definition: win32_shmem.c:44
void * UsedShmemSegAddr
Definition: win32_shmem.c:45
static bool EnableLockPagesPrivilege(int elevel)
Definition: win32_shmem.c:137
static void pgwin32_SharedMemoryDelete(int status, Datum shmId)
Definition: win32_shmem.c:549
static Size UsedShmemSegSize
Definition: win32_shmem.c:46

References Assert, PGShmemHeader::creatorPID, DEBUG1, PGShmemHeader::dsm_control, elog, EnableLockPagesPrivilege(), ereport, errcode(), errdetail(), errhint(), errmsg(), errmsg_internal(), FATAL, free, PGShmemHeader::freeoffset, GetSharedMemName(), huge_pages, HUGE_PAGES_ON, HUGE_PAGES_TRY, i, LOG, PGShmemHeader::magic, MAXALIGN, on_shmem_exit(), PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT, PGShmemMagic, pgwin32_SharedMemoryDelete(), PointerGetDatum(), PROTECTIVE_REGION_SIZE, SetConfigOption(), ShmemProtectiveRegion, size, PGShmemHeader::totalsize, UsedShmemSegAddr, UsedShmemSegID, and UsedShmemSegSize.

◆ PGSharedMemoryDetach()

void PGSharedMemoryDetach ( void  )

Definition at line 505 of file win32_shmem.c.

506{
507 /*
508 * Releasing the protective region liberates an unimportant quantity of
509 * address space, but be tidy.
510 */
511 if (ShmemProtectiveRegion != NULL)
512 {
513 if (VirtualFree(ShmemProtectiveRegion, 0, MEM_RELEASE) == 0)
514 elog(LOG, "failed to release reserved memory region (addr=%p): error code %lu",
515 ShmemProtectiveRegion, GetLastError());
516
518 }
519
520 /* Unmap the view, if it's mapped */
521 if (UsedShmemSegAddr != NULL)
522 {
523 if (!UnmapViewOfFile(UsedShmemSegAddr))
524 elog(LOG, "could not unmap view of shared memory: error code %lu",
525 GetLastError());
526
527 UsedShmemSegAddr = NULL;
528 }
529
530 /* And close the shmem handle, if we have one */
531 if (UsedShmemSegID != INVALID_HANDLE_VALUE)
532 {
533 if (!CloseHandle(UsedShmemSegID))
534 elog(LOG, "could not close handle to shared memory: error code %lu",
535 GetLastError());
536
537 UsedShmemSegID = INVALID_HANDLE_VALUE;
538 }
539}

References elog, LOG, ShmemProtectiveRegion, UsedShmemSegAddr, and UsedShmemSegID.

Referenced by PGSharedMemoryNoReAttach(), and pgwin32_SharedMemoryDelete().

◆ PGSharedMemoryIsInUse()

bool PGSharedMemoryIsInUse ( unsigned long  id1,
unsigned long  id2 
)

Definition at line 113 of file win32_shmem.c.

114{
115 char *szShareMem;
116 HANDLE hmap;
117
118 szShareMem = GetSharedMemName();
119
120 hmap = OpenFileMapping(FILE_MAP_READ, FALSE, szShareMem);
121
122 free(szShareMem);
123
124 if (hmap == NULL)
125 return false;
126
127 CloseHandle(hmap);
128 return true;
129}

References free, and GetSharedMemName().

◆ PGSharedMemoryNoReAttach()

void PGSharedMemoryNoReAttach ( void  )

Definition at line 472 of file win32_shmem.c.

473{
475 Assert(UsedShmemSegAddr != NULL);
477
478 /*
479 * Under Windows we will not have mapped the segment, so we don't need to
480 * un-map it. Just reset UsedShmemSegAddr to show we're not attached.
481 */
482 UsedShmemSegAddr = NULL;
483
484 /*
485 * We *must* close the inherited shmem segment handle, else Windows will
486 * consider the existence of this process to mean it can't release the
487 * shmem segment yet. We can now use PGSharedMemoryDetach to do that.
488 */
490}
bool IsUnderPostmaster
Definition: globals.c:119
void PGSharedMemoryDetach(void)
Definition: win32_shmem.c:505

References Assert, IsUnderPostmaster, PGSharedMemoryDetach(), ShmemProtectiveRegion, and UsedShmemSegAddr.

◆ PGSharedMemoryReAttach()

void PGSharedMemoryReAttach ( void  )

Definition at line 424 of file win32_shmem.c.

425{
426 PGShmemHeader *hdr;
427 void *origUsedShmemSegAddr = UsedShmemSegAddr;
428
430 Assert(UsedShmemSegAddr != NULL);
432
433 /*
434 * Release memory region reservations made by the postmaster
435 */
436 if (VirtualFree(ShmemProtectiveRegion, 0, MEM_RELEASE) == 0)
437 elog(FATAL, "failed to release reserved memory region (addr=%p): error code %lu",
438 ShmemProtectiveRegion, GetLastError());
439 if (VirtualFree(UsedShmemSegAddr, 0, MEM_RELEASE) == 0)
440 elog(FATAL, "failed to release reserved memory region (addr=%p): error code %lu",
441 UsedShmemSegAddr, GetLastError());
442
443 hdr = (PGShmemHeader *) MapViewOfFileEx(UsedShmemSegID, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0, UsedShmemSegAddr);
444 if (!hdr)
445 elog(FATAL, "could not reattach to shared memory (key=%p, addr=%p): error code %lu",
446 UsedShmemSegID, UsedShmemSegAddr, GetLastError());
447 if (hdr != origUsedShmemSegAddr)
448 elog(FATAL, "reattaching to shared memory returned unexpected address (got %p, expected %p)",
449 hdr, origUsedShmemSegAddr);
450 if (hdr->magic != PGShmemMagic)
451 elog(FATAL, "reattaching to shared memory returned non-PostgreSQL memory");
452 dsm_set_control_handle(hdr->dsm_control);
453
454 UsedShmemSegAddr = hdr; /* probably redundant */
455}

References Assert, PGShmemHeader::dsm_control, elog, FATAL, IsUnderPostmaster, PGShmemHeader::magic, PGShmemMagic, ShmemProtectiveRegion, UsedShmemSegAddr, and UsedShmemSegID.

◆ pgwin32_ReserveSharedMemoryRegion()

int pgwin32_ReserveSharedMemoryRegion ( HANDLE  hChild)

Definition at line 573 of file win32_shmem.c.

574{
575 void *address;
576
578 Assert(UsedShmemSegAddr != NULL);
580
581 /* ShmemProtectiveRegion */
582 address = VirtualAllocEx(hChild, ShmemProtectiveRegion,
584 MEM_RESERVE, PAGE_NOACCESS);
585 if (address == NULL)
586 {
587 /* Don't use FATAL since we're running in the postmaster */
588 elog(LOG, "could not reserve shared memory region (addr=%p) for child %p: error code %lu",
589 ShmemProtectiveRegion, hChild, GetLastError());
590 return false;
591 }
592 if (address != ShmemProtectiveRegion)
593 {
594 /*
595 * Should never happen - in theory if allocation granularity causes
596 * strange effects it could, so check just in case.
597 *
598 * Don't use FATAL since we're running in the postmaster.
599 */
600 elog(LOG, "reserved shared memory region got incorrect address %p, expected %p",
601 address, ShmemProtectiveRegion);
602 return false;
603 }
604
605 /* UsedShmemSegAddr */
606 address = VirtualAllocEx(hChild, UsedShmemSegAddr, UsedShmemSegSize,
607 MEM_RESERVE, PAGE_READWRITE);
608 if (address == NULL)
609 {
610 elog(LOG, "could not reserve shared memory region (addr=%p) for child %p: error code %lu",
611 UsedShmemSegAddr, hChild, GetLastError());
612 return false;
613 }
614 if (address != UsedShmemSegAddr)
615 {
616 elog(LOG, "reserved shared memory region got incorrect address %p, expected %p",
617 address, UsedShmemSegAddr);
618 return false;
619 }
620
621 return true;
622}

References Assert, elog, LOG, PROTECTIVE_REGION_SIZE, ShmemProtectiveRegion, UsedShmemSegAddr, and UsedShmemSegSize.

◆ pgwin32_SharedMemoryDelete()

static void pgwin32_SharedMemoryDelete ( int  status,
Datum  shmId 
)
static

Definition at line 549 of file win32_shmem.c.

550{
553}
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317

References Assert, DatumGetPointer(), PGSharedMemoryDetach(), and UsedShmemSegID.

Referenced by PGSharedMemoryCreate().

Variable Documentation

◆ ShmemProtectiveRegion

◆ UsedShmemSegAddr

◆ UsedShmemSegID

HANDLE UsedShmemSegID = INVALID_HANDLE_VALUE

◆ UsedShmemSegSize

Size UsedShmemSegSize = 0
static

Definition at line 46 of file win32_shmem.c.

Referenced by PGSharedMemoryCreate(), and pgwin32_ReserveSharedMemoryRegion().