PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
dsm.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "lib/ilist.h"
#include "miscadmin.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/pg_shmem.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/resowner_private.h"
Include dependency graph for dsm.c:

Go to the source code of this file.

Data Structures

struct  dsm_segment_detach_callback
 
struct  dsm_segment
 
struct  dsm_control_item
 
struct  dsm_control_header
 

Macros

#define PG_DYNSHMEM_CONTROL_MAGIC   0x9a503d32
 
#define PG_DYNSHMEM_FIXED_SLOTS   64
 
#define PG_DYNSHMEM_SLOTS_PER_BACKEND   2
 
#define INVALID_CONTROL_SLOT   ((uint32) -1)
 

Typedefs

typedef struct
dsm_segment_detach_callback 
dsm_segment_detach_callback
 
typedef struct dsm_control_item dsm_control_item
 
typedef struct dsm_control_header dsm_control_header
 

Functions

static void dsm_cleanup_for_mmap (void)
 
static void dsm_postmaster_shutdown (int code, Datum arg)
 
static dsm_segmentdsm_create_descriptor (void)
 
static bool dsm_control_segment_sane (dsm_control_header *control, Size mapped_size)
 
static uint64 dsm_control_bytes_needed (uint32 nitems)
 
void dsm_postmaster_startup (PGShmemHeader *shim)
 
void dsm_cleanup_using_control_segment (dsm_handle old_control_handle)
 
static void dsm_backend_startup (void)
 
dsm_segmentdsm_create (Size size, int flags)
 
dsm_segmentdsm_attach (dsm_handle h)
 
void dsm_backend_shutdown (void)
 
void dsm_detach_all (void)
 
void * dsm_resize (dsm_segment *seg, Size size)
 
void * dsm_remap (dsm_segment *seg)
 
void dsm_detach (dsm_segment *seg)
 
void dsm_pin_mapping (dsm_segment *seg)
 
void dsm_unpin_mapping (dsm_segment *seg)
 
void dsm_pin_segment (dsm_segment *seg)
 
void dsm_unpin_segment (dsm_handle handle)
 
dsm_segmentdsm_find_mapping (dsm_handle h)
 
void * dsm_segment_address (dsm_segment *seg)
 
Size dsm_segment_map_length (dsm_segment *seg)
 
dsm_handle dsm_segment_handle (dsm_segment *seg)
 
void on_dsm_detach (dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
 
void cancel_on_dsm_detach (dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
 
void reset_on_dsm_detach (void)
 

Variables

static bool dsm_init_done = false
 
static dlist_head dsm_segment_list = DLIST_STATIC_INIT(dsm_segment_list)
 
static dsm_handle dsm_control_handle
 
static dsm_control_headerdsm_control
 
static Size dsm_control_mapped_size = 0
 
static void * dsm_control_impl_private = NULL
 

Macro Definition Documentation

#define INVALID_CONTROL_SLOT   ((uint32) -1)
#define PG_DYNSHMEM_CONTROL_MAGIC   0x9a503d32

Definition at line 47 of file dsm.c.

Referenced by dsm_control_segment_sane(), and dsm_postmaster_startup().

#define PG_DYNSHMEM_FIXED_SLOTS   64

Definition at line 54 of file dsm.c.

Referenced by dsm_postmaster_startup().

#define PG_DYNSHMEM_SLOTS_PER_BACKEND   2

Definition at line 55 of file dsm.c.

Referenced by dsm_postmaster_startup().

Typedef Documentation

Function Documentation

void cancel_on_dsm_detach ( dsm_segment seg,
on_dsm_detach_callback  function,
Datum  arg 
)

Definition at line 1041 of file dsm.c.

References dsm_segment_detach_callback::arg, slist_mutable_iter::cur, dsm_segment_detach_callback::function, dsm_segment::on_detach, pfree(), slist_container, slist_delete_current(), and slist_foreach_modify.

Referenced by test_shm_mq_setup().

1043 {
1044  slist_mutable_iter iter;
1045 
1046  slist_foreach_modify(iter, &seg->on_detach)
1047  {
1049 
1051  if (cb->function == function && cb->arg == arg)
1052  {
1053  slist_delete_current(&iter);
1054  pfree(cb);
1055  break;
1056  }
1057  }
1058 }
slist_node * cur
Definition: ilist.h:241
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:716
void pfree(void *pointer)
Definition: mcxt.c:992
on_dsm_detach_callback function
Definition: dsm.c:62
slist_head on_detach
Definition: dsm.c:77
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
void * arg
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:652
dsm_segment* dsm_attach ( dsm_handle  h)

Definition at line 550 of file dsm.c.

References Assert, dsm_segment::control_slot, dlist_iter::cur, dlist_container, dlist_foreach, dsm_backend_startup(), dsm_create_descriptor(), dsm_detach(), dsm_impl_op(), dsm_init_done, DSM_OP_ATTACH, elog, ERROR, dsm_segment::handle, dsm_control_item::handle, i, dsm_segment::impl_private, INVALID_CONTROL_SLOT, IsUnderPostmaster, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_segment::mapped_address, dsm_segment::mapped_size, dsm_control_header::nitems, NULL, and dsm_control_item::refcnt.

Referenced by dsa_attach(), get_segment_by_index(), ParallelWorkerMain(), and test_shm_mq_main().

551 {
552  dsm_segment *seg;
553  dlist_iter iter;
554  uint32 i;
555  uint32 nitems;
556 
557  /* Unsafe in postmaster (and pointless in a stand-alone backend). */
559 
560  if (!dsm_init_done)
562 
563  /*
564  * Since this is just a debugging cross-check, we could leave it out
565  * altogether, or include it only in assert-enabled builds. But since the
566  * list of attached segments should normally be very short, let's include
567  * it always for right now.
568  *
569  * If you're hitting this error, you probably want to attempt to find an
570  * existing mapping via dsm_find_mapping() before calling dsm_attach() to
571  * create a new one.
572  */
574  {
575  seg = dlist_container(dsm_segment, node, iter.cur);
576  if (seg->handle == h)
577  elog(ERROR, "can't attach the same segment more than once");
578  }
579 
580  /* Create a new segment descriptor. */
581  seg = dsm_create_descriptor();
582  seg->handle = h;
583 
584  /* Bump reference count for this segment in shared memory. */
585  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
586  nitems = dsm_control->nitems;
587  for (i = 0; i < nitems; ++i)
588  {
589  /* If the reference count is 0, the slot is actually unused. */
590  if (dsm_control->item[i].refcnt == 0)
591  continue;
592 
593  /* If the handle doesn't match, it's not the slot we want. */
594  if (dsm_control->item[i].handle != seg->handle)
595  continue;
596 
597  /*
598  * If the reference count is 1, the slot is still in use, but the
599  * segment is in the process of going away. Treat that as if we
600  * didn't find a match.
601  */
602  if (dsm_control->item[i].refcnt == 1)
603  break;
604 
605  /* Otherwise we've found a match. */
606  dsm_control->item[i].refcnt++;
607  seg->control_slot = i;
608  break;
609  }
610  LWLockRelease(DynamicSharedMemoryControlLock);
611 
612  /*
613  * If we didn't find the handle we're looking for in the control segment,
614  * it probably means that everyone else who had it mapped, including the
615  * original creator, died before we got to this point. It's up to the
616  * caller to decide what to do about that.
617  */
619  {
620  dsm_detach(seg);
621  return NULL;
622  }
623 
624  /* Here's where we actually try to map the segment. */
626  &seg->mapped_address, &seg->mapped_size, ERROR);
627 
628  return seg;
629 }
Size mapped_size
Definition: dsm.c:76
dsm_handle handle
Definition: dsm.c:83
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
static void dsm_backend_startup(void)
Definition: dsm.c:406
static dlist_head dsm_segment_list
Definition: dsm.c:125
uint32 nitems
Definition: dsm.c:93
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
#define ERROR
Definition: elog.h:43
bool IsUnderPostmaster
Definition: globals.c:100
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:95
unsigned int uint32
Definition: c.h:265
void * mapped_address
Definition: dsm.c:75
dsm_handle handle
Definition: dsm.c:72
dlist_node * cur
Definition: ilist.h:161
static dsm_control_header * dsm_control
Definition: dsm.c:135
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void * impl_private
Definition: dsm.c:74
uint32 control_slot
Definition: dsm.c:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
static dsm_segment * dsm_create_descriptor(void)
Definition: dsm.c:1095
uint32 refcnt
Definition: dsm.c:84
static bool dsm_init_done
Definition: dsm.c:106
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:715
int i
#define INVALID_CONTROL_SLOT
Definition: dsm.c:57
#define elog
Definition: elog.h:219
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:162
void dsm_backend_shutdown ( void  )

Definition at line 637 of file dsm.c.

References dlist_head_element, dlist_is_empty(), and dsm_detach().

Referenced by shmem_exit().

638 {
640  {
641  dsm_segment *seg;
642 
644  dsm_detach(seg);
645  }
646 }
static dlist_head dsm_segment_list
Definition: dsm.c:125
#define dlist_head_element(type, membername, lhead)
Definition: ilist.h:487
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:715
static void dsm_backend_startup ( void  )
static

Definition at line 406 of file dsm.c.

References Assert, dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, dsm_control_segment_sane(), DSM_IMPL_NONE, dsm_impl_op(), dsm_init_done, DSM_OP_ATTACH, DSM_OP_DETACH, dynamic_shared_memory_type, ereport, errcode(), errhint(), errmsg(), ERROR, FATAL, NULL, and WARNING.

Referenced by dsm_attach(), and dsm_create().

407 {
408  /* If dynamic shared memory is disabled, reject this. */
410  ereport(ERROR,
411  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
412  errmsg("dynamic shared memory is disabled"),
413  errhint("Set dynamic_shared_memory_type to a value other than \"none\".")));
414 
415 #ifdef EXEC_BACKEND
416  {
417  void *control_address = NULL;
418 
419  /* Attach control segment. */
422  &dsm_control_impl_private, &control_address,
424  dsm_control = control_address;
425  /* If control segment doesn't look sane, something is badly wrong. */
427  {
429  &dsm_control_impl_private, &control_address,
431  ereport(FATAL,
432  (errcode(ERRCODE_INTERNAL_ERROR),
433  errmsg("dynamic shared memory control segment is not valid")));
434  }
435  }
436 #endif
437 
438  dsm_init_done = true;
439 }
static void * dsm_control_impl_private
Definition: dsm.c:137
int errhint(const char *fmt,...)
Definition: elog.c:987
static dsm_handle dsm_control_handle
Definition: dsm.c:134
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
int dynamic_shared_memory_type
Definition: dsm_impl.c:112
#define ereport(elevel, rest)
Definition: elog.h:122
static bool dsm_control_segment_sane(dsm_control_header *control, Size mapped_size)
Definition: dsm.c:1129
#define WARNING
Definition: elog.h:40
static dsm_control_header * dsm_control
Definition: dsm.c:135
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define DSM_IMPL_NONE
Definition: dsm_impl.h:17
static bool dsm_init_done
Definition: dsm.c:106
int errmsg(const char *fmt,...)
Definition: elog.c:797
static Size dsm_control_mapped_size
Definition: dsm.c:136
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:162
static void dsm_cleanup_for_mmap ( void  )
static

Definition at line 293 of file dsm.c.

References AllocateDir(), buf, closedir(), dirent::d_name, DEBUG2, elog, ereport, errcode_for_file_access(), errmsg(), ERROR, FreeDir(), MAXPGPATH, NULL, PG_DYNSHMEM_DIR, PG_DYNSHMEM_MMAP_FILE_PREFIX, ReadDir(), snprintf(), and unlink().

Referenced by dsm_postmaster_startup().

294 {
295  DIR *dir;
296  struct dirent *dent;
297 
298  /* Open the directory; can't use AllocateDir in postmaster. */
299  if ((dir = AllocateDir(PG_DYNSHMEM_DIR)) == NULL)
300  ereport(ERROR,
302  errmsg("could not open directory \"%s\": %m",
303  PG_DYNSHMEM_DIR)));
304 
305  /* Scan for something with a name of the correct format. */
306  while ((dent = ReadDir(dir, PG_DYNSHMEM_DIR)) != NULL)
307  {
308  if (strncmp(dent->d_name, PG_DYNSHMEM_MMAP_FILE_PREFIX,
309  strlen(PG_DYNSHMEM_MMAP_FILE_PREFIX)) == 0)
310  {
311  char buf[MAXPGPATH];
312 
313  snprintf(buf, MAXPGPATH, PG_DYNSHMEM_DIR "/%s", dent->d_name);
314 
315  elog(DEBUG2, "removing file \"%s\"", buf);
316 
317  /* We found a matching file; so remove it. */
318  if (unlink(buf) != 0)
319  {
320  int save_errno;
321 
322  save_errno = errno;
323  closedir(dir);
324  errno = save_errno;
325 
326  ereport(ERROR,
328  errmsg("could not remove file \"%s\": %m", buf)));
329  }
330  }
331  }
332 
333  /* Cleanup complete. */
334  FreeDir(dir);
335 }
#define PG_DYNSHMEM_DIR
Definition: dsm_impl.h:51
int closedir(DIR *)
Definition: dirent.c:113
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
Definition: dirent.h:9
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
#define DEBUG2
Definition: elog.h:24
static char * buf
Definition: pg_test_fsync.c:65
int errcode_for_file_access(void)
Definition: elog.c:598
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2284
int unlink(const char *filename)
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:226
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2350
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_DYNSHMEM_MMAP_FILE_PREFIX
Definition: dsm_impl.h:52
char d_name[MAX_PATH]
Definition: dirent.h:14
#define elog
Definition: elog.h:219
int FreeDir(DIR *dir)
Definition: fd.c:2393
void dsm_cleanup_using_control_segment ( dsm_handle  old_control_handle)

Definition at line 211 of file dsm.c.

References DEBUG1, DEBUG2, dsm_control_segment_sane(), DSM_IMPL_NONE, dsm_impl_op(), DSM_OP_ATTACH, DSM_OP_DESTROY, DSM_OP_DETACH, dynamic_shared_memory_type, elog, dsm_control_item::handle, i, dsm_control_header::item, LOG, dsm_control_header::nitems, NULL, and dsm_control_item::refcnt.

Referenced by PGSharedMemoryCreate().

212 {
213  void *mapped_address = NULL;
214  void *junk_mapped_address = NULL;
215  void *impl_private = NULL;
216  void *junk_impl_private = NULL;
217  Size mapped_size = 0;
218  Size junk_mapped_size = 0;
219  uint32 nitems;
220  uint32 i;
221  dsm_control_header *old_control;
222 
223  /* If dynamic shared memory is disabled, there's nothing to do. */
225  return;
226 
227  /*
228  * Try to attach the segment. If this fails, it probably just means that
229  * the operating system has been rebooted and the segment no longer
230  * exists, or an unrelated process has used the same shm ID. So just fall
231  * out quietly.
232  */
233  if (!dsm_impl_op(DSM_OP_ATTACH, old_control_handle, 0, &impl_private,
234  &mapped_address, &mapped_size, DEBUG1))
235  return;
236 
237  /*
238  * We've managed to reattach it, but the contents might not be sane. If
239  * they aren't, we disregard the segment after all.
240  */
241  old_control = (dsm_control_header *) mapped_address;
242  if (!dsm_control_segment_sane(old_control, mapped_size))
243  {
244  dsm_impl_op(DSM_OP_DETACH, old_control_handle, 0, &impl_private,
245  &mapped_address, &mapped_size, LOG);
246  return;
247  }
248 
249  /*
250  * OK, the control segment looks basically valid, so we can use it to get
251  * a list of segments that need to be removed.
252  */
253  nitems = old_control->nitems;
254  for (i = 0; i < nitems; ++i)
255  {
256  dsm_handle handle;
257  uint32 refcnt;
258 
259  /* If the reference count is 0, the slot is actually unused. */
260  refcnt = old_control->item[i].refcnt;
261  if (refcnt == 0)
262  continue;
263 
264  /* Log debugging information. */
265  handle = old_control->item[i].handle;
266  elog(DEBUG2, "cleaning up orphaned dynamic shared memory with ID %u (reference count %u)",
267  handle, refcnt);
268 
269  /* Destroy the referenced segment. */
270  dsm_impl_op(DSM_OP_DESTROY, handle, 0, &junk_impl_private,
271  &junk_mapped_address, &junk_mapped_size, LOG);
272  }
273 
274  /* Destroy the old control segment, too. */
275  elog(DEBUG2,
276  "cleaning up dynamic shared memory control segment with ID %u",
277  old_control_handle);
278  dsm_impl_op(DSM_OP_DESTROY, old_control_handle, 0, &impl_private,
279  &mapped_address, &mapped_size, LOG);
280 }
#define DEBUG1
Definition: elog.h:25
uint32 dsm_handle
Definition: dsm_impl.h:55
dsm_handle handle
Definition: dsm.c:83
#define LOG
Definition: elog.h:26
uint32 nitems
Definition: dsm.c:93
#define DEBUG2
Definition: elog.h:24
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:95
int dynamic_shared_memory_type
Definition: dsm_impl.c:112
unsigned int uint32
Definition: c.h:265
static bool dsm_control_segment_sane(dsm_control_header *control, Size mapped_size)
Definition: dsm.c:1129
#define NULL
Definition: c.h:226
#define DSM_IMPL_NONE
Definition: dsm_impl.h:17
size_t Size
Definition: c.h:353
uint32 refcnt
Definition: dsm.c:84
int i
#define elog
Definition: elog.h:219
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:162
static uint64 dsm_control_bytes_needed ( uint32  nitems)
static

Definition at line 1147 of file dsm.c.

References offsetof.

Referenced by dsm_control_segment_sane(), and dsm_postmaster_startup().

1148 {
1149  return offsetof(dsm_control_header, item)
1150  +sizeof(dsm_control_item) * (uint64) nitems;
1151 }
struct dsm_control_item dsm_control_item
#define offsetof(type, field)
Definition: c.h:551
static bool dsm_control_segment_sane ( dsm_control_header control,
Size  mapped_size 
)
static

Definition at line 1129 of file dsm.c.

References dsm_control_bytes_needed(), dsm_control_header::magic, dsm_control_header::maxitems, dsm_control_header::nitems, offsetof, and PG_DYNSHMEM_CONTROL_MAGIC.

Referenced by dsm_backend_startup(), dsm_cleanup_using_control_segment(), and dsm_postmaster_shutdown().

1130 {
1131  if (mapped_size < offsetof(dsm_control_header, item))
1132  return false; /* Mapped size too short to read header. */
1133  if (control->magic != PG_DYNSHMEM_CONTROL_MAGIC)
1134  return false; /* Magic number doesn't match. */
1135  if (dsm_control_bytes_needed(control->maxitems) > mapped_size)
1136  return false; /* Max item count won't fit in map. */
1137  if (control->nitems > control->maxitems)
1138  return false; /* Overfull. */
1139  return true;
1140 }
uint32 maxitems
Definition: dsm.c:94
static uint64 dsm_control_bytes_needed(uint32 nitems)
Definition: dsm.c:1147
uint32 nitems
Definition: dsm.c:93
uint32 magic
Definition: dsm.c:92
#define PG_DYNSHMEM_CONTROL_MAGIC
Definition: dsm.c:47
#define offsetof(type, field)
Definition: c.h:551
dsm_segment* dsm_create ( Size  size,
int  flags 
)

Definition at line 459 of file dsm.c.

References Assert, dsm_segment::control_slot, dlist_delete(), dsm_backend_startup(), dsm_create_descriptor(), DSM_CREATE_NULL_IF_MAXSEGMENTS, DSM_HANDLE_INVALID, dsm_impl_op(), dsm_init_done, DSM_OP_CREATE, DSM_OP_DESTROY, ereport, errcode(), errmsg(), ERROR, dsm_segment::handle, dsm_control_item::handle, i, dsm_segment::impl_private, dsm_control_item::impl_private_pm_handle, IsUnderPostmaster, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_segment::mapped_address, dsm_segment::mapped_size, dsm_control_header::maxitems, dsm_control_header::nitems, dsm_segment::node, NULL, pfree(), dsm_control_item::pinned, random(), dsm_control_item::refcnt, ResourceOwnerForgetDSM(), dsm_segment::resowner, and WARNING.

Referenced by dsa_create(), InitializeParallelDSM(), make_new_segment(), and setup_dynamic_shared_memory().

460 {
461  dsm_segment *seg;
462  uint32 i;
463  uint32 nitems;
464 
465  /* Unsafe in postmaster (and pointless in a stand-alone backend). */
467 
468  if (!dsm_init_done)
470 
471  /* Create a new segment descriptor. */
472  seg = dsm_create_descriptor();
473 
474  /* Loop until we find an unused segment identifier. */
475  for (;;)
476  {
477  Assert(seg->mapped_address == NULL && seg->mapped_size == 0);
478  seg->handle = random();
479  if (seg->handle == DSM_HANDLE_INVALID) /* Reserve sentinel */
480  continue;
481  if (dsm_impl_op(DSM_OP_CREATE, seg->handle, size, &seg->impl_private,
482  &seg->mapped_address, &seg->mapped_size, ERROR))
483  break;
484  }
485 
486  /* Lock the control segment so we can register the new segment. */
487  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
488 
489  /* Search the control segment for an unused slot. */
490  nitems = dsm_control->nitems;
491  for (i = 0; i < nitems; ++i)
492  {
493  if (dsm_control->item[i].refcnt == 0)
494  {
495  dsm_control->item[i].handle = seg->handle;
496  /* refcnt of 1 triggers destruction, so start at 2 */
497  dsm_control->item[i].refcnt = 2;
499  dsm_control->item[i].pinned = false;
500  seg->control_slot = i;
501  LWLockRelease(DynamicSharedMemoryControlLock);
502  return seg;
503  }
504  }
505 
506  /* Verify that we can support an additional mapping. */
507  if (nitems >= dsm_control->maxitems)
508  {
509  if ((flags & DSM_CREATE_NULL_IF_MAXSEGMENTS) != 0)
510  {
511  LWLockRelease(DynamicSharedMemoryControlLock);
513  &seg->mapped_address, &seg->mapped_size, WARNING);
514  if (seg->resowner != NULL)
515  ResourceOwnerForgetDSM(seg->resowner, seg);
516  dlist_delete(&seg->node);
517  pfree(seg);
518  return NULL;
519  }
520  ereport(ERROR,
521  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
522  errmsg("too many dynamic shared memory segments")));
523  }
524 
525  /* Enter the handle into a new array slot. */
526  dsm_control->item[nitems].handle = seg->handle;
527  /* refcnt of 1 triggers destruction, so start at 2 */
528  dsm_control->item[nitems].refcnt = 2;
530  dsm_control->item[nitems].pinned = false;
531  seg->control_slot = nitems;
532  dsm_control->nitems++;
533  LWLockRelease(DynamicSharedMemoryControlLock);
534 
535  return seg;
536 }
dlist_node node
Definition: dsm.c:70
uint32 maxitems
Definition: dsm.c:94
Size mapped_size
Definition: dsm.c:76
dsm_handle handle
Definition: dsm.c:83
long random(void)
Definition: random.c:22
int errcode(int sqlerrcode)
Definition: elog.c:575
static void dsm_backend_startup(void)
Definition: dsm.c:406
uint32 nitems
Definition: dsm.c:93
bool pinned
Definition: dsm.c:86
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
#define DSM_CREATE_NULL_IF_MAXSEGMENTS
Definition: dsm.h:20
bool IsUnderPostmaster
Definition: globals.c:100
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:95
unsigned int uint32
Definition: c.h:265
void * mapped_address
Definition: dsm.c:75
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
dsm_handle handle
Definition: dsm.c:72
static dsm_control_header * dsm_control
Definition: dsm.c:135
ResourceOwner resowner
Definition: dsm.c:71
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void * impl_private
Definition: dsm.c:74
void ResourceOwnerForgetDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1254
uint32 control_slot
Definition: dsm.c:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
static dsm_segment * dsm_create_descriptor(void)
Definition: dsm.c:1095
uint32 refcnt
Definition: dsm.c:84
static bool dsm_init_done
Definition: dsm.c:106
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
void * impl_private_pm_handle
Definition: dsm.c:85
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:162
static dsm_segment * dsm_create_descriptor ( void  )
static

Definition at line 1095 of file dsm.c.

References dsm_segment::control_slot, CurrentResourceOwner, dlist_push_head(), dsm_segment::impl_private, INVALID_CONTROL_SLOT, dsm_segment::mapped_address, dsm_segment::mapped_size, MemoryContextAlloc(), dsm_segment::node, NULL, dsm_segment::on_detach, ResourceOwnerEnlargeDSMs(), ResourceOwnerRememberDSM(), dsm_segment::resowner, slist_init(), and TopMemoryContext.

Referenced by dsm_attach(), and dsm_create().

1096 {
1097  dsm_segment *seg;
1098 
1100 
1103 
1104  /* seg->handle must be initialized by the caller */
1106  seg->impl_private = NULL;
1107  seg->mapped_address = NULL;
1108  seg->mapped_size = 0;
1109 
1112 
1113  slist_init(&seg->on_detach);
1114 
1115  return seg;
1116 }
dlist_node node
Definition: dsm.c:70
void ResourceOwnerRememberDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1245
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:300
Size mapped_size
Definition: dsm.c:76
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static dlist_head dsm_segment_list
Definition: dsm.c:125
static void slist_init(slist_head *head)
Definition: ilist.h:554
void ResourceOwnerEnlargeDSMs(ResourceOwner owner)
Definition: resowner.c:1234
void * mapped_address
Definition: dsm.c:75
MemoryContext TopMemoryContext
Definition: mcxt.c:43
slist_head on_detach
Definition: dsm.c:77
ResourceOwner resowner
Definition: dsm.c:71
#define NULL
Definition: c.h:226
void * impl_private
Definition: dsm.c:74
uint32 control_slot
Definition: dsm.c:73
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:749
#define INVALID_CONTROL_SLOT
Definition: dsm.c:57
void dsm_detach ( dsm_segment seg)

Definition at line 715 of file dsm.c.

References arg, dsm_segment_detach_callback::arg, Assert, dsm_segment::control_slot, dlist_delete(), dsm_impl_op(), DSM_OP_DESTROY, DSM_OP_DETACH, dsm_segment_detach_callback::function, dsm_segment::handle, dsm_control_item::handle, dsm_segment::impl_private, INVALID_CONTROL_SLOT, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_segment::mapped_address, dsm_segment::mapped_size, dsm_segment::node, NULL, dsm_segment::on_detach, pfree(), dsm_control_item::pinned, dsm_control_item::refcnt, ResourceOwnerForgetDSM(), dsm_segment::resowner, slist_container, slist_is_empty(), slist_pop_head_node(), and WARNING.

Referenced by check_for_freed_segments(), destroy_superblock(), DestroyParallelContext(), dsa_detach(), dsm_attach(), dsm_backend_shutdown(), dsm_detach_all(), ResourceOwnerReleaseInternal(), test_shm_mq(), test_shm_mq_main(), and test_shm_mq_pipelined().

716 {
717  /*
718  * Invoke registered callbacks. Just in case one of those callbacks
719  * throws a further error that brings us back here, pop the callback
720  * before invoking it, to avoid infinite error recursion.
721  */
722  while (!slist_is_empty(&seg->on_detach))
723  {
724  slist_node *node;
726  on_dsm_detach_callback function;
727  Datum arg;
728 
729  node = slist_pop_head_node(&seg->on_detach);
731  function = cb->function;
732  arg = cb->arg;
733  pfree(cb);
734 
735  function(seg, arg);
736  }
737 
738  /*
739  * Try to remove the mapping, if one exists. Normally, there will be, but
740  * maybe not, if we failed partway through a create or attach operation.
741  * We remove the mapping before decrementing the reference count so that
742  * the process that sees a zero reference count can be certain that no
743  * remaining mappings exist. Even if this fails, we pretend that it
744  * works, because retrying is likely to fail in the same way.
745  */
746  if (seg->mapped_address != NULL)
747  {
749  &seg->mapped_address, &seg->mapped_size, WARNING);
750  seg->impl_private = NULL;
751  seg->mapped_address = NULL;
752  seg->mapped_size = 0;
753  }
754 
755  /* Reduce reference count, if we previously increased it. */
757  {
758  uint32 refcnt;
759  uint32 control_slot = seg->control_slot;
760 
761  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
762  Assert(dsm_control->item[control_slot].handle == seg->handle);
763  Assert(dsm_control->item[control_slot].refcnt > 1);
764  refcnt = --dsm_control->item[control_slot].refcnt;
766  LWLockRelease(DynamicSharedMemoryControlLock);
767 
768  /* If new reference count is 1, try to destroy the segment. */
769  if (refcnt == 1)
770  {
771  /* A pinned segment should never reach 1. */
772  Assert(!dsm_control->item[control_slot].pinned);
773 
774  /*
775  * If we fail to destroy the segment here, or are killed before we
776  * finish doing so, the reference count will remain at 1, which
777  * will mean that nobody else can attach to the segment. At
778  * postmaster shutdown time, or when a new postmaster is started
779  * after a hard kill, another attempt will be made to remove the
780  * segment.
781  *
782  * The main case we're worried about here is being killed by a
783  * signal before we can finish removing the segment. In that
784  * case, it's important to be sure that the segment still gets
785  * removed. If we actually fail to remove the segment for some
786  * other reason, the postmaster may not have any better luck than
787  * we did. There's not much we can do about that, though.
788  */
789  if (dsm_impl_op(DSM_OP_DESTROY, seg->handle, 0, &seg->impl_private,
790  &seg->mapped_address, &seg->mapped_size, WARNING))
791  {
792  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
793  Assert(dsm_control->item[control_slot].handle == seg->handle);
794  Assert(dsm_control->item[control_slot].refcnt == 1);
795  dsm_control->item[control_slot].refcnt = 0;
796  LWLockRelease(DynamicSharedMemoryControlLock);
797  }
798  }
799  }
800 
801  /* Clean up our remaining backend-private data structures. */
802  if (seg->resowner != NULL)
803  ResourceOwnerForgetDSM(seg->resowner, seg);
804  dlist_delete(&seg->node);
805  pfree(seg);
806 }
dlist_node node
Definition: dsm.c:70
Size mapped_size
Definition: dsm.c:76
dsm_handle handle
Definition: dsm.c:83
void(* on_dsm_detach_callback)(dsm_segment *, Datum arg)
Definition: dsm.h:56
bool pinned
Definition: dsm.c:86
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
void pfree(void *pointer)
Definition: mcxt.c:992
on_dsm_detach_callback function
Definition: dsm.c:62
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:95
unsigned int uint32
Definition: c.h:265
void * mapped_address
Definition: dsm.c:75
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
static slist_node * slist_pop_head_node(slist_head *head)
Definition: ilist.h:596
slist_head on_detach
Definition: dsm.c:77
static bool slist_is_empty(slist_head *head)
Definition: ilist.h:563
#define WARNING
Definition: elog.h:40
dsm_handle handle
Definition: dsm.c:72
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
uintptr_t Datum
Definition: postgres.h:374
static dsm_control_header * dsm_control
Definition: dsm.c:135
ResourceOwner resowner
Definition: dsm.c:71
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void * impl_private
Definition: dsm.c:74
void ResourceOwnerForgetDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1254
uint32 control_slot
Definition: dsm.c:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
uint32 refcnt
Definition: dsm.c:84
void * arg
#define INVALID_CONTROL_SLOT
Definition: dsm.c:57
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:162
void dsm_detach_all ( void  )

Definition at line 655 of file dsm.c.

References dlist_head_element, dlist_is_empty(), dsm_control, dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, dsm_detach(), dsm_impl_op(), DSM_OP_DETACH, ERROR, and NULL.

Referenced by pgarch_start(), pgstat_start(), StartBackgroundWorker(), and SysLogger_Start().

656 {
657  void *control_address = dsm_control;
658 
660  {
661  dsm_segment *seg;
662 
664  dsm_detach(seg);
665  }
666 
667  if (control_address != NULL)
669  &dsm_control_impl_private, &control_address,
671 }
static void * dsm_control_impl_private
Definition: dsm.c:137
static dsm_handle dsm_control_handle
Definition: dsm.c:134
static dlist_head dsm_segment_list
Definition: dsm.c:125
#define ERROR
Definition: elog.h:43
#define dlist_head_element(type, membername, lhead)
Definition: ilist.h:487
static dsm_control_header * dsm_control
Definition: dsm.c:135
#define NULL
Definition: c.h:226
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:715
static Size dsm_control_mapped_size
Definition: dsm.c:136
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:162
dsm_segment* dsm_find_mapping ( dsm_handle  h)

Definition at line 970 of file dsm.c.

References dlist_iter::cur, dlist_container, dlist_foreach, dsm_segment::handle, and NULL.

971 {
972  dlist_iter iter;
973  dsm_segment *seg;
974 
976  {
977  seg = dlist_container(dsm_segment, node, iter.cur);
978  if (seg->handle == h)
979  return seg;
980  }
981 
982  return NULL;
983 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
static dlist_head dsm_segment_list
Definition: dsm.c:125
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
dsm_handle handle
Definition: dsm.c:72
dlist_node * cur
Definition: ilist.h:161
#define NULL
Definition: c.h:226
void dsm_pin_mapping ( dsm_segment seg)

Definition at line 816 of file dsm.c.

References NULL, ResourceOwnerForgetDSM(), and dsm_segment::resowner.

Referenced by dsa_pin_mapping(), get_segment_by_index(), and make_new_segment().

817 {
818  if (seg->resowner != NULL)
819  {
820  ResourceOwnerForgetDSM(seg->resowner, seg);
821  seg->resowner = NULL;
822  }
823 }
ResourceOwner resowner
Definition: dsm.c:71
#define NULL
Definition: c.h:226
void ResourceOwnerForgetDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1254
void dsm_pin_segment ( dsm_segment seg)

Definition at line 856 of file dsm.c.

References dsm_segment::control_slot, dsm_impl_pin_segment(), elog, ERROR, dsm_segment::handle, dsm_segment::impl_private, dsm_control_item::impl_private_pm_handle, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_control_item::pinned, and dsm_control_item::refcnt.

Referenced by dsa_create(), and make_new_segment().

857 {
858  void *handle;
859 
860  /*
861  * Bump reference count for this segment in shared memory. This will
862  * ensure that even if there is no session which is attached to this
863  * segment, it will remain until postmaster shutdown or an explicit call
864  * to unpin.
865  */
866  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
867  if (dsm_control->item[seg->control_slot].pinned)
868  elog(ERROR, "cannot pin a segment that is already pinned");
869  dsm_impl_pin_segment(seg->handle, seg->impl_private, &handle);
870  dsm_control->item[seg->control_slot].pinned = true;
873  LWLockRelease(DynamicSharedMemoryControlLock);
874 }
bool pinned
Definition: dsm.c:86
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
#define ERROR
Definition: elog.h:43
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:95
dsm_handle handle
Definition: dsm.c:72
static dsm_control_header * dsm_control
Definition: dsm.c:135
void * impl_private
Definition: dsm.c:74
uint32 control_slot
Definition: dsm.c:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
uint32 refcnt
Definition: dsm.c:84
void dsm_impl_pin_segment(dsm_handle handle, void *impl_private, void **impl_private_pm_handle)
Definition: dsm_impl.c:1007
void * impl_private_pm_handle
Definition: dsm.c:85
#define elog
Definition: elog.h:219
static void dsm_postmaster_shutdown ( int  code,
Datum  arg 
)
static

Definition at line 344 of file dsm.c.

References DatumGetPointer, DEBUG2, PGShmemHeader::dsm_control, dsm_control, dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, dsm_control_segment_sane(), dsm_impl_op(), DSM_OP_DESTROY, elog, ereport, errmsg(), dsm_control_item::handle, i, dsm_control_header::item, LOG, dsm_control_header::nitems, NULL, and dsm_control_item::refcnt.

Referenced by dsm_postmaster_startup().

345 {
346  uint32 nitems;
347  uint32 i;
348  void *dsm_control_address;
349  void *junk_mapped_address = NULL;
350  void *junk_impl_private = NULL;
351  Size junk_mapped_size = 0;
353 
354  /*
355  * If some other backend exited uncleanly, it might have corrupted the
356  * control segment while it was dying. In that case, we warn and ignore
357  * the contents of the control segment. This may end up leaving behind
358  * stray shared memory segments, but there's not much we can do about that
359  * if the metadata is gone.
360  */
361  nitems = dsm_control->nitems;
363  {
364  ereport(LOG,
365  (errmsg("dynamic shared memory control segment is corrupt")));
366  return;
367  }
368 
369  /* Remove any remaining segments. */
370  for (i = 0; i < nitems; ++i)
371  {
372  dsm_handle handle;
373 
374  /* If the reference count is 0, the slot is actually unused. */
375  if (dsm_control->item[i].refcnt == 0)
376  continue;
377 
378  /* Log debugging information. */
379  handle = dsm_control->item[i].handle;
380  elog(DEBUG2, "cleaning up orphaned dynamic shared memory with ID %u",
381  handle);
382 
383  /* Destroy the segment. */
384  dsm_impl_op(DSM_OP_DESTROY, handle, 0, &junk_impl_private,
385  &junk_mapped_address, &junk_mapped_size, LOG);
386  }
387 
388  /* Remove the control segment itself. */
389  elog(DEBUG2,
390  "cleaning up dynamic shared memory control segment with ID %u",
392  dsm_control_address = dsm_control;
394  &dsm_control_impl_private, &dsm_control_address,
396  dsm_control = dsm_control_address;
397  shim->dsm_control = 0;
398 }
static void * dsm_control_impl_private
Definition: dsm.c:137
uint32 dsm_handle
Definition: dsm_impl.h:55
dsm_handle dsm_control
Definition: pg_shmem.h:36
dsm_handle handle
Definition: dsm.c:83
static dsm_handle dsm_control_handle
Definition: dsm.c:134
#define LOG
Definition: elog.h:26
uint32 nitems
Definition: dsm.c:93
#define DEBUG2
Definition: elog.h:24
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:95
unsigned int uint32
Definition: c.h:265
#define ereport(elevel, rest)
Definition: elog.h:122
static bool dsm_control_segment_sane(dsm_control_header *control, Size mapped_size)
Definition: dsm.c:1129
static dsm_control_header * dsm_control
Definition: dsm.c:135
#define NULL
Definition: c.h:226
size_t Size
Definition: c.h:353
#define DatumGetPointer(X)
Definition: postgres.h:557
uint32 refcnt
Definition: dsm.c:84
int errmsg(const char *fmt,...)
Definition: elog.c:797
static Size dsm_control_mapped_size
Definition: dsm.c:136
int i
void * arg
#define elog
Definition: elog.h:219
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:162
void dsm_postmaster_startup ( PGShmemHeader shim)

Definition at line 146 of file dsm.c.

References Assert, DEBUG2, dsm_cleanup_for_mmap(), PGShmemHeader::dsm_control, dsm_control_bytes_needed(), dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, DSM_HANDLE_INVALID, DSM_IMPL_MMAP, DSM_IMPL_NONE, dsm_impl_op(), DSM_OP_CREATE, dsm_postmaster_shutdown(), dynamic_shared_memory_type, elog, ERROR, IsUnderPostmaster, dsm_control_header::magic, MaxBackends, dsm_control_header::maxitems, dsm_control_header::nitems, NULL, on_shmem_exit(), PG_DYNSHMEM_CONTROL_MAGIC, PG_DYNSHMEM_FIXED_SLOTS, PG_DYNSHMEM_SLOTS_PER_BACKEND, PointerGetDatum, and random().

Referenced by CreateSharedMemoryAndSemaphores().

147 {
148  void *dsm_control_address = NULL;
149  uint32 maxitems;
150  Size segsize;
151 
153 
154  /* If dynamic shared memory is disabled, there's nothing to do. */
156  return;
157 
158  /*
159  * If we're using the mmap implementations, clean up any leftovers.
160  * Cleanup isn't needed on Windows, and happens earlier in startup for
161  * POSIX and System V shared memory, via a direct call to
162  * dsm_cleanup_using_control_segment.
163  */
166 
167  /* Determine size for new control segment. */
168  maxitems = PG_DYNSHMEM_FIXED_SLOTS
170  elog(DEBUG2, "dynamic shared memory system will support %u segments",
171  maxitems);
172  segsize = dsm_control_bytes_needed(maxitems);
173 
174  /*
175  * Loop until we find an unused identifier for the new control segment. We
176  * sometimes use 0 as a sentinel value indicating that no control segment
177  * is known to exist, so avoid using that value for a real control
178  * segment.
179  */
180  for (;;)
181  {
182  Assert(dsm_control_address == NULL);
186  continue;
188  &dsm_control_impl_private, &dsm_control_address,
190  break;
191  }
192  dsm_control = dsm_control_address;
194  elog(DEBUG2,
195  "created dynamic shared memory control segment %u (%zu bytes)",
196  dsm_control_handle, segsize);
198 
199  /* Initialize control segment. */
201  dsm_control->nitems = 0;
202  dsm_control->maxitems = maxitems;
203 }
static void * dsm_control_impl_private
Definition: dsm.c:137
#define DSM_IMPL_MMAP
Definition: dsm_impl.h:21
uint32 maxitems
Definition: dsm.c:94
#define PG_DYNSHMEM_SLOTS_PER_BACKEND
Definition: dsm.c:55
#define PG_DYNSHMEM_FIXED_SLOTS
Definition: dsm.c:54
dsm_handle dsm_control
Definition: pg_shmem.h:36
#define PointerGetDatum(X)
Definition: postgres.h:564
long random(void)
Definition: random.c:22
static dsm_handle dsm_control_handle
Definition: dsm.c:134
static uint64 dsm_control_bytes_needed(uint32 nitems)
Definition: dsm.c:1147
uint32 nitems
Definition: dsm.c:93
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
#define ERROR
Definition: elog.h:43
uint32 magic
Definition: dsm.c:92
int MaxBackends
Definition: globals.c:126
#define DEBUG2
Definition: elog.h:24
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:348
bool IsUnderPostmaster
Definition: globals.c:100
static void dsm_postmaster_shutdown(int code, Datum arg)
Definition: dsm.c:344
int dynamic_shared_memory_type
Definition: dsm_impl.c:112
unsigned int uint32
Definition: c.h:265
static void dsm_cleanup_for_mmap(void)
Definition: dsm.c:293
static dsm_control_header * dsm_control
Definition: dsm.c:135
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define DSM_IMPL_NONE
Definition: dsm_impl.h:17
size_t Size
Definition: c.h:353
static Size dsm_control_mapped_size
Definition: dsm.c:136
#define elog
Definition: elog.h:219
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:162
#define PG_DYNSHMEM_CONTROL_MAGIC
Definition: dsm.c:47
void* dsm_remap ( dsm_segment seg)

Definition at line 697 of file dsm.c.

References dsm_impl_op(), DSM_OP_ATTACH, ERROR, dsm_segment::handle, dsm_segment::impl_private, dsm_segment::mapped_address, and dsm_segment::mapped_size.

698 {
700  &seg->mapped_address, &seg->mapped_size, ERROR);
701 
702  return seg->mapped_address;
703 }
Size mapped_size
Definition: dsm.c:76
#define ERROR
Definition: elog.h:43
void * mapped_address
Definition: dsm.c:75
dsm_handle handle
Definition: dsm.c:72
void * impl_private
Definition: dsm.c:74
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:162
void* dsm_resize ( dsm_segment seg,
Size  size 
)

Definition at line 680 of file dsm.c.

References Assert, dsm_segment::control_slot, dsm_impl_op(), DSM_OP_RESIZE, ERROR, dsm_segment::handle, dsm_segment::impl_private, INVALID_CONTROL_SLOT, dsm_segment::mapped_address, and dsm_segment::mapped_size.

681 {
683  dsm_impl_op(DSM_OP_RESIZE, seg->handle, size, &seg->impl_private,
684  &seg->mapped_address, &seg->mapped_size, ERROR);
685  return seg->mapped_address;
686 }
Size mapped_size
Definition: dsm.c:76
#define ERROR
Definition: elog.h:43
void * mapped_address
Definition: dsm.c:75
dsm_handle handle
Definition: dsm.c:72
#define Assert(condition)
Definition: c.h:671
void * impl_private
Definition: dsm.c:74
uint32 control_slot
Definition: dsm.c:73
#define INVALID_CONTROL_SLOT
Definition: dsm.c:57
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:162
void* dsm_segment_address ( dsm_segment seg)

Definition at line 989 of file dsm.c.

References Assert, dsm_segment::mapped_address, and NULL.

Referenced by dsa_attach(), dsa_create(), get_segment_by_index(), InitializeParallelDSM(), make_new_segment(), ParallelWorkerMain(), setup_dynamic_shared_memory(), and test_shm_mq_main().

990 {
991  Assert(seg->mapped_address != NULL);
992  return seg->mapped_address;
993 }
void * mapped_address
Definition: dsm.c:75
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
dsm_handle dsm_segment_handle ( dsm_segment seg)

Definition at line 1017 of file dsm.c.

References dsm_segment::handle.

Referenced by destroy_superblock(), dsa_create(), LaunchParallelWorkers(), make_new_segment(), PrintDSMLeakWarning(), ResourceOwnerForgetDSM(), and setup_background_workers().

1018 {
1019  return seg->handle;
1020 }
dsm_handle handle
Definition: dsm.c:72
Size dsm_segment_map_length ( dsm_segment seg)

Definition at line 999 of file dsm.c.

References Assert, dsm_segment::mapped_address, dsm_segment::mapped_size, and NULL.

1000 {
1001  Assert(seg->mapped_address != NULL);
1002  return seg->mapped_size;
1003 }
Size mapped_size
Definition: dsm.c:76
void * mapped_address
Definition: dsm.c:75
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void dsm_unpin_mapping ( dsm_segment seg)

Definition at line 835 of file dsm.c.

References Assert, CurrentResourceOwner, NULL, ResourceOwnerEnlargeDSMs(), ResourceOwnerRememberDSM(), and dsm_segment::resowner.

836 {
837  Assert(seg->resowner == NULL);
841 }
void ResourceOwnerRememberDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1245
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ResourceOwnerEnlargeDSMs(ResourceOwner owner)
Definition: resowner.c:1234
ResourceOwner resowner
Definition: dsm.c:71
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void dsm_unpin_segment ( dsm_handle  handle)

Definition at line 888 of file dsm.c.

References Assert, dsm_impl_op(), dsm_impl_unpin_segment(), DSM_OP_DESTROY, elog, ERROR, dsm_control_item::handle, i, dsm_control_item::impl_private_pm_handle, INVALID_CONTROL_SLOT, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_control_header::nitems, NULL, dsm_control_item::pinned, dsm_control_item::refcnt, and WARNING.

Referenced by destroy_superblock(), and dsa_release_in_place().

889 {
890  uint32 control_slot = INVALID_CONTROL_SLOT;
891  bool destroy = false;
892  uint32 i;
893 
894  /* Find the control slot for the given handle. */
895  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
896  for (i = 0; i < dsm_control->nitems; ++i)
897  {
898  /* Skip unused slots. */
899  if (dsm_control->item[i].refcnt == 0)
900  continue;
901 
902  /* If we've found our handle, we can stop searching. */
903  if (dsm_control->item[i].handle == handle)
904  {
905  control_slot = i;
906  break;
907  }
908  }
909 
910  /*
911  * We should definitely have found the slot, and it should not already be
912  * in the process of going away, because this function should only be
913  * called on a segment which is pinned.
914  */
915  if (control_slot == INVALID_CONTROL_SLOT)
916  elog(ERROR, "cannot unpin unknown segment handle");
917  if (!dsm_control->item[control_slot].pinned)
918  elog(ERROR, "cannot unpin a segment that is not pinned");
919  Assert(dsm_control->item[control_slot].refcnt > 1);
920 
921  /*
922  * Allow implementation-specific code to run. We have to do this before
923  * releasing the lock, because impl_private_pm_handle may get modified by
924  * dsm_impl_unpin_segment.
925  */
926  dsm_impl_unpin_segment(handle,
927  &dsm_control->item[control_slot].impl_private_pm_handle);
928 
929  /* Note that 1 means no references (0 means unused slot). */
930  if (--dsm_control->item[control_slot].refcnt == 1)
931  destroy = true;
932  dsm_control->item[control_slot].pinned = false;
933 
934  /* Now we can release the lock. */
935  LWLockRelease(DynamicSharedMemoryControlLock);
936 
937  /* Clean up resources if that was the last reference. */
938  if (destroy)
939  {
940  void *junk_impl_private = NULL;
941  void *junk_mapped_address = NULL;
942  Size junk_mapped_size = 0;
943 
944  /*
945  * For an explanation of how error handling works in this case, see
946  * comments in dsm_detach. Note that if we reach this point, the
947  * current process certainly does not have the segment mapped, because
948  * if it did, the reference count would have still been greater than 1
949  * even after releasing the reference count held by the pin. The fact
950  * that there can't be a dsm_segment for this handle makes it OK to
951  * pass the mapped size, mapped address, and private data as NULL
952  * here.
953  */
954  if (dsm_impl_op(DSM_OP_DESTROY, handle, 0, &junk_impl_private,
955  &junk_mapped_address, &junk_mapped_size, WARNING))
956  {
957  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
958  Assert(dsm_control->item[control_slot].handle == handle);
959  Assert(dsm_control->item[control_slot].refcnt == 1);
960  dsm_control->item[control_slot].refcnt = 0;
961  LWLockRelease(DynamicSharedMemoryControlLock);
962  }
963  }
964 }
void dsm_impl_unpin_segment(dsm_handle handle, void **impl_private)
Definition: dsm_impl.c:1057
dsm_handle handle
Definition: dsm.c:83
uint32 nitems
Definition: dsm.c:93
bool pinned
Definition: dsm.c:86
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
#define ERROR
Definition: elog.h:43
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:95
unsigned int uint32
Definition: c.h:265
#define WARNING
Definition: elog.h:40
static dsm_control_header * dsm_control
Definition: dsm.c:135
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
size_t Size
Definition: c.h:353
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
uint32 refcnt
Definition: dsm.c:84
int i
void * impl_private_pm_handle
Definition: dsm.c:85
#define INVALID_CONTROL_SLOT
Definition: dsm.c:57
#define elog
Definition: elog.h:219
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:162
void on_dsm_detach ( dsm_segment seg,
on_dsm_detach_callback  function,
Datum  arg 
)

Definition at line 1026 of file dsm.c.

References arg, dsm_segment_detach_callback::arg, dsm_segment_detach_callback::function, MemoryContextAlloc(), dsm_segment_detach_callback::node, dsm_segment::on_detach, slist_push_head(), and TopMemoryContext.

Referenced by dsa_attach(), dsa_attach_in_place(), dsa_create(), dsa_create_in_place(), pq_redirect_to_shm_mq(), setup_background_workers(), and shm_mq_attach().

1027 {
1029 
1031  sizeof(dsm_segment_detach_callback));
1032  cb->function = function;
1033  cb->arg = arg;
1034  slist_push_head(&seg->on_detach, &cb->node);
1035 }
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:574
on_dsm_detach_callback function
Definition: dsm.c:62
MemoryContext TopMemoryContext
Definition: mcxt.c:43
slist_head on_detach
Definition: dsm.c:77
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:749
void * arg
void reset_on_dsm_detach ( void  )

Definition at line 1064 of file dsm.c.

References dsm_segment::control_slot, dlist_iter::cur, dlist_container, dlist_foreach, INVALID_CONTROL_SLOT, dsm_segment::on_detach, pfree(), slist_container, slist_is_empty(), and slist_pop_head_node().

Referenced by on_exit_reset().

1065 {
1066  dlist_iter iter;
1067 
1069  {
1070  dsm_segment *seg = dlist_container(dsm_segment, node, iter.cur);
1071 
1072  /* Throw away explicit on-detach actions one by one. */
1073  while (!slist_is_empty(&seg->on_detach))
1074  {
1075  slist_node *node;
1077 
1078  node = slist_pop_head_node(&seg->on_detach);
1079  cb = slist_container(dsm_segment_detach_callback, node, node);
1080  pfree(cb);
1081  }
1082 
1083  /*
1084  * Decrementing the reference count is a sort of implicit on-detach
1085  * action; make sure we don't do that, either.
1086  */
1088  }
1089 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
static dlist_head dsm_segment_list
Definition: dsm.c:125
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
void pfree(void *pointer)
Definition: mcxt.c:992
static slist_node * slist_pop_head_node(slist_head *head)
Definition: ilist.h:596
slist_head on_detach
Definition: dsm.c:77
static bool slist_is_empty(slist_head *head)
Definition: ilist.h:563
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
dlist_node * cur
Definition: ilist.h:161
uint32 control_slot
Definition: dsm.c:73
#define INVALID_CONTROL_SLOT
Definition: dsm.c:57

Variable Documentation

dsm_control_header* dsm_control
static

Definition at line 135 of file dsm.c.

Referenced by dsm_detach_all(), and dsm_postmaster_shutdown().

dsm_handle dsm_control_handle
static
void* dsm_control_impl_private = NULL
static
Size dsm_control_mapped_size = 0
static
bool dsm_init_done = false
static

Definition at line 106 of file dsm.c.

Referenced by dsm_attach(), dsm_backend_startup(), and dsm_create().

dlist_head dsm_segment_list = DLIST_STATIC_INIT(dsm_segment_list)
static

Definition at line 125 of file dsm.c.