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 637 of file win32_shmem.c.

638 {
639  if (*newval != 0)
640  {
641  GUC_check_errdetail("huge_page_size must be 0 on this platform.");
642  return false;
643  }
644  return true;
645 }
#define newval
#define GUC_check_errdetail
Definition: guc.h:437

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:1202
int errhint(const char *fmt,...)
Definition: elog.c:1316
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define _(x)
Definition: elog.c:91
#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 625 of file win32_shmem.c.

626 {
627  if (hugepagesize)
628  *hugepagesize = 0;
629  if (mmap_flags)
630  *mmap_flags = 0;
631 }

◆ 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
char * DataDir
Definition: globals.c:66
#define malloc(a)
Definition: header.h:50

References 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 
223  ShmemProtectiveRegion = VirtualAlloc(NULL, PROTECTIVE_REGION_SIZE,
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")));
245  ereport(DEBUG1,
246  (errmsg_internal("disabling huge pages")));
247  }
249  {
250  ereport(DEBUG1,
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 
264 retry:
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
312  ereport(FATAL,
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)
338  ereport(FATAL,
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))
348  ereport(FATAL,
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)
373  ereport(FATAL,
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;
397  UsedShmemSegSize = size;
398  UsedShmemSegID = hmap2;
399 
400  /* Register on-exit routine to delete the new segment */
402 
403  *shim = hdr;
404  return hdr;
405 }
#define MAXALIGN(LEN)
Definition: c.h:795
size_t Size
Definition: c.h:589
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
#define LOG
Definition: elog.h:31
#define DEBUG1
Definition: elog.h:30
int huge_pages
Definition: guc_tables.c:535
#define free(a)
Definition: header.h:65
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
@ 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:322
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
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:544
static Size UsedShmemSegSize
Definition: win32_shmem.c:46
static char * GetSharedMemName(void)
Definition: win32_shmem.c:65

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(), PGShmemMagic, pgwin32_SharedMemoryDelete(), PointerGetDatum(), PROTECTIVE_REGION_SIZE, ShmemProtectiveRegion, PGShmemHeader::totalsize, UsedShmemSegAddr, UsedShmemSegID, and UsedShmemSegSize.

◆ PGSharedMemoryDetach()

void PGSharedMemoryDetach ( void  )

Definition at line 500 of file win32_shmem.c.

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

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 467 of file win32_shmem.c.

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

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

◆ PGSharedMemoryReAttach()

void PGSharedMemoryReAttach ( void  )

Definition at line 419 of file win32_shmem.c.

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

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 568 of file win32_shmem.c.

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

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 544 of file win32_shmem.c.

545 {
548 }
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312

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().