Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * procsignal.c
4 : : * Routines for interprocess signaling
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/storage/ipc/procsignal.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include <signal.h>
18 : : #include <unistd.h>
19 : :
20 : : #include "access/parallel.h"
21 : : #include "commands/async.h"
22 : : #include "miscadmin.h"
23 : : #include "pgstat.h"
24 : : #include "port/pg_bitutils.h"
25 : : #include "replication/logicalctl.h"
26 : : #include "replication/logicalworker.h"
27 : : #include "replication/walsender.h"
28 : : #include "storage/condition_variable.h"
29 : : #include "storage/ipc.h"
30 : : #include "storage/latch.h"
31 : : #include "storage/proc.h"
32 : : #include "storage/shmem.h"
33 : : #include "storage/sinval.h"
34 : : #include "storage/smgr.h"
35 : : #include "tcop/tcopprot.h"
36 : : #include "utils/memutils.h"
37 : : #include "utils/wait_event.h"
38 : :
39 : : /*
40 : : * The SIGUSR1 signal is multiplexed to support signaling multiple event
41 : : * types. The specific reason is communicated via flags in shared memory.
42 : : * We keep a boolean flag for each possible "reason", so that different
43 : : * reasons can be signaled to a process concurrently. (However, if the same
44 : : * reason is signaled more than once nearly simultaneously, the process may
45 : : * observe it only once.)
46 : : *
47 : : * Each process that wants to receive signals registers its process ID
48 : : * in the ProcSignalSlots array. The array is indexed by ProcNumber to make
49 : : * slot allocation simple, and to avoid having to search the array when you
50 : : * know the ProcNumber of the process you're signaling. (We do support
51 : : * signaling without ProcNumber, but it's a bit less efficient.)
52 : : *
53 : : * The fields in each slot are protected by a spinlock, pss_mutex. pss_pid can
54 : : * also be read without holding the spinlock, as a quick preliminary check
55 : : * when searching for a particular PID in the array.
56 : : *
57 : : * pss_signalFlags are intended to be set in cases where we don't need to
58 : : * keep track of whether or not the target process has handled the signal,
59 : : * but sometimes we need confirmation, as when making a global state change
60 : : * that cannot be considered complete until all backends have taken notice
61 : : * of it. For such use cases, we set a bit in pss_barrierCheckMask and then
62 : : * increment the current "barrier generation"; when the new barrier generation
63 : : * (or greater) appears in the pss_barrierGeneration flag of every process,
64 : : * we know that the message has been received everywhere.
65 : : */
66 : : typedef struct
67 : : {
68 : : pg_atomic_uint32 pss_pid;
69 : : int pss_cancel_key_len; /* 0 means no cancellation is possible */
70 : : uint8 pss_cancel_key[MAX_CANCEL_KEY_LENGTH];
71 : : volatile sig_atomic_t pss_signalFlags[NUM_PROCSIGNALS];
72 : : slock_t pss_mutex; /* protects the above fields */
73 : :
74 : : /* Barrier-related fields (not protected by pss_mutex) */
75 : : pg_atomic_uint64 pss_barrierGeneration;
76 : : pg_atomic_uint32 pss_barrierCheckMask;
77 : : ConditionVariable pss_barrierCV;
78 : : } ProcSignalSlot;
79 : :
80 : : /*
81 : : * Information that is global to the entire ProcSignal system can be stored
82 : : * here.
83 : : *
84 : : * psh_barrierGeneration is the highest barrier generation in existence.
85 : : */
86 : : struct ProcSignalHeader
87 : : {
88 : : pg_atomic_uint64 psh_barrierGeneration;
89 : : ProcSignalSlot psh_slot[FLEXIBLE_ARRAY_MEMBER];
90 : : };
91 : :
92 : : /*
93 : : * We reserve a slot for each possible ProcNumber, plus one for each
94 : : * possible auxiliary process type. (This scheme assumes there is not
95 : : * more than one of any auxiliary process type at a time, except for
96 : : * IO workers.)
97 : : */
98 : : #define NumProcSignalSlots (MaxBackends + NUM_AUXILIARY_PROCS)
99 : :
100 : : /* Check whether the relevant type bit is set in the flags. */
101 : : #define BARRIER_SHOULD_CHECK(flags, type) \
102 : : (((flags) & (((uint32) 1) << (uint32) (type))) != 0)
103 : :
104 : : /* Clear the relevant type bit from the flags. */
105 : : #define BARRIER_CLEAR_BIT(flags, type) \
106 : : ((flags) &= ~(((uint32) 1) << (uint32) (type)))
107 : :
108 : : NON_EXEC_STATIC ProcSignalHeader *ProcSignal = NULL;
109 : : static ProcSignalSlot *MyProcSignalSlot = NULL;
110 : :
111 : : static bool CheckProcSignal(ProcSignalReason reason);
112 : : static void CleanupProcSignalState(int status, Datum arg);
113 : : static void ResetProcSignalBarrierBits(uint32 flags);
114 : :
115 : : /*
116 : : * ProcSignalShmemSize
117 : : * Compute space needed for ProcSignal's shared memory
118 : : */
119 : : Size
6071 tgl@sss.pgh.pa.us 120 :CBC 3297 : ProcSignalShmemSize(void)
121 : : {
122 : : Size size;
123 : :
1433 rhaas@postgresql.org 124 : 3297 : size = mul_size(NumProcSignalSlots, sizeof(ProcSignalSlot));
2278 125 : 3297 : size = add_size(size, offsetof(ProcSignalHeader, psh_slot));
126 : 3297 : return size;
127 : : }
128 : :
129 : : /*
130 : : * ProcSignalShmemInit
131 : : * Allocate and initialize ProcSignal's shared memory
132 : : */
133 : : void
6071 tgl@sss.pgh.pa.us 134 : 1150 : ProcSignalShmemInit(void)
135 : : {
136 : 1150 : Size size = ProcSignalShmemSize();
137 : : bool found;
138 : :
2278 rhaas@postgresql.org 139 : 1150 : ProcSignal = (ProcSignalHeader *)
140 : 1150 : ShmemInitStruct("ProcSignal", size, &found);
141 : :
142 : : /* If we're first, initialize. */
6071 tgl@sss.pgh.pa.us 143 [ + - ]: 1150 : if (!found)
144 : : {
145 : : int i;
146 : :
2278 rhaas@postgresql.org 147 : 1150 : pg_atomic_init_u64(&ProcSignal->psh_barrierGeneration, 0);
148 : :
1433 149 [ + + ]: 150534 : for (i = 0; i < NumProcSignalSlots; ++i)
150 : : {
2278 151 : 149384 : ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
152 : :
594 heikki.linnakangas@i 153 : 149384 : SpinLockInit(&slot->pss_mutex);
154 : 149384 : pg_atomic_init_u32(&slot->pss_pid, 0);
347 155 : 149384 : slot->pss_cancel_key_len = 0;
2278 rhaas@postgresql.org 156 [ + - + - : 746920 : MemSet(slot->pss_signalFlags, 0, sizeof(slot->pss_signalFlags));
+ - + - +
+ ]
157 : 149384 : pg_atomic_init_u64(&slot->pss_barrierGeneration, PG_UINT64_MAX);
158 : 149384 : pg_atomic_init_u32(&slot->pss_barrierCheckMask, 0);
1840 tmunro@postgresql.or 159 : 149384 : ConditionVariableInit(&slot->pss_barrierCV);
160 : : }
161 : : }
6071 tgl@sss.pgh.pa.us 162 : 1150 : }
163 : :
164 : : /*
165 : : * ProcSignalInit
166 : : * Register the current process in the ProcSignal array
167 : : */
168 : : void
311 heikki.linnakangas@i 169 : 21549 : ProcSignalInit(const uint8 *cancel_key, int cancel_key_len)
170 : : {
171 : : ProcSignalSlot *slot;
172 : : uint64 barrier_generation;
173 : : uint32 old_pss_pid;
174 : :
347 175 [ + - - + ]: 21549 : Assert(cancel_key_len >= 0 && cancel_key_len <= MAX_CANCEL_KEY_LENGTH);
742 176 [ - + ]: 21549 : if (MyProcNumber < 0)
742 heikki.linnakangas@i 177 [ # # ]:UBC 0 : elog(ERROR, "MyProcNumber not set");
742 heikki.linnakangas@i 178 [ - + ]:CBC 21549 : if (MyProcNumber >= NumProcSignalSlots)
742 heikki.linnakangas@i 179 [ # # ]:UBC 0 : elog(ERROR, "unexpected MyProcNumber %d in ProcSignalInit (max %d)", MyProcNumber, NumProcSignalSlots);
742 heikki.linnakangas@i 180 :CBC 21549 : slot = &ProcSignal->psh_slot[MyProcNumber];
181 : :
594 182 [ - + ]: 21549 : SpinLockAcquire(&slot->pss_mutex);
183 : :
184 : : /* Value used for sanity check below */
381 michael@paquier.xyz 185 : 21549 : old_pss_pid = pg_atomic_read_u32(&slot->pss_pid);
186 : :
187 : : /* Clear out any leftover signal reasons */
6071 tgl@sss.pgh.pa.us 188 [ + - + - : 107745 : MemSet(slot->pss_signalFlags, 0, NUM_PROCSIGNALS * sizeof(sig_atomic_t));
+ - + - +
+ ]
189 : :
190 : : /*
191 : : * Initialize barrier state. Since we're a brand-new process, there
192 : : * shouldn't be any leftover backend-private state that needs to be
193 : : * updated. Therefore, we can broadcast the latest barrier generation and
194 : : * disregard any previously-set check bits.
195 : : *
196 : : * NB: This only works if this initialization happens early enough in the
197 : : * startup sequence that we haven't yet cached any state that might need
198 : : * to be invalidated. That's also why we have a memory barrier here, to be
199 : : * sure that any later reads of memory happen strictly after this.
200 : : */
2278 rhaas@postgresql.org 201 : 21549 : pg_atomic_write_u32(&slot->pss_barrierCheckMask, 0);
202 : : barrier_generation =
203 : 21549 : pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration);
204 : 21549 : pg_atomic_write_u64(&slot->pss_barrierGeneration, barrier_generation);
205 : :
347 heikki.linnakangas@i 206 [ + + ]: 21549 : if (cancel_key_len > 0)
207 : 13840 : memcpy(slot->pss_cancel_key, cancel_key, cancel_key_len);
208 : 21549 : slot->pss_cancel_key_len = cancel_key_len;
594 209 : 21549 : pg_atomic_write_u32(&slot->pss_pid, MyProcPid);
210 : :
211 : 21549 : SpinLockRelease(&slot->pss_mutex);
212 : :
213 : : /* Spinlock is released, do the check */
381 michael@paquier.xyz 214 [ - + ]: 21549 : if (old_pss_pid != 0)
381 michael@paquier.xyz 215 [ # # ]:UBC 0 : elog(LOG, "process %d taking over ProcSignal slot %d, but it's not empty",
216 : : MyProcPid, MyProcNumber);
217 : :
218 : : /* Remember slot location for CheckProcSignal */
6071 tgl@sss.pgh.pa.us 219 :CBC 21549 : MyProcSignalSlot = slot;
220 : :
221 : : /* Set up to release the slot on process exit */
742 heikki.linnakangas@i 222 : 21549 : on_shmem_exit(CleanupProcSignalState, (Datum) 0);
6071 tgl@sss.pgh.pa.us 223 : 21549 : }
224 : :
225 : : /*
226 : : * CleanupProcSignalState
227 : : * Remove current process from ProcSignal mechanism
228 : : *
229 : : * This function is called via on_shmem_exit() during backend shutdown.
230 : : */
231 : : static void
232 : 21549 : CleanupProcSignalState(int status, Datum arg)
233 : : {
234 : : pid_t old_pid;
742 heikki.linnakangas@i 235 : 21549 : ProcSignalSlot *slot = MyProcSignalSlot;
236 : :
237 : : /*
238 : : * Clear MyProcSignalSlot, so that a SIGUSR1 received after this point
239 : : * won't try to access it after it's no longer ours (and perhaps even
240 : : * after we've unmapped the shared memory segment).
241 : : */
242 [ - + ]: 21549 : Assert(MyProcSignalSlot != NULL);
4426 rhaas@postgresql.org 243 : 21549 : MyProcSignalSlot = NULL;
244 : :
245 : : /* sanity check */
594 heikki.linnakangas@i 246 [ - + ]: 21549 : SpinLockAcquire(&slot->pss_mutex);
247 : 21549 : old_pid = pg_atomic_read_u32(&slot->pss_pid);
248 [ - + ]: 21549 : if (old_pid != MyProcPid)
249 : : {
250 : : /*
251 : : * don't ERROR here. We're exiting anyway, and don't want to get into
252 : : * infinite loop trying to exit
253 : : */
594 heikki.linnakangas@i 254 :UBC 0 : SpinLockRelease(&slot->pss_mutex);
6071 tgl@sss.pgh.pa.us 255 [ # # ]: 0 : elog(LOG, "process %d releasing ProcSignal slot %d, but it contains %d",
256 : : MyProcPid, (int) (slot - ProcSignal->psh_slot), (int) old_pid);
257 : 0 : return; /* XXX better to zero the slot anyway? */
258 : : }
259 : :
260 : : /* Mark the slot as unused */
594 heikki.linnakangas@i 261 :CBC 21549 : pg_atomic_write_u32(&slot->pss_pid, 0);
347 262 : 21549 : slot->pss_cancel_key_len = 0;
263 : :
264 : : /*
265 : : * Make this slot look like it's absorbed all possible barriers, so that
266 : : * no barrier waits block on it.
267 : : */
2278 rhaas@postgresql.org 268 : 21549 : pg_atomic_write_u64(&slot->pss_barrierGeneration, PG_UINT64_MAX);
269 : :
594 heikki.linnakangas@i 270 : 21549 : SpinLockRelease(&slot->pss_mutex);
271 : :
272 : 21549 : ConditionVariableBroadcast(&slot->pss_barrierCV);
273 : : }
274 : :
275 : : /*
276 : : * SendProcSignal
277 : : * Send a signal to a Postgres process
278 : : *
279 : : * Providing procNumber is optional, but it will speed up the operation.
280 : : *
281 : : * On success (a signal was sent), zero is returned.
282 : : * On error, -1 is returned, and errno is set (typically to ESRCH or EPERM).
283 : : *
284 : : * Not to be confused with ProcSendSignal
285 : : */
286 : : int
742 287 : 5918 : SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
288 : : {
289 : : volatile ProcSignalSlot *slot;
290 : :
291 [ + + ]: 5918 : if (procNumber != INVALID_PROC_NUMBER)
292 : : {
594 293 [ - + ]: 5856 : Assert(procNumber < NumProcSignalSlots);
742 294 : 5856 : slot = &ProcSignal->psh_slot[procNumber];
295 : :
594 296 [ + + ]: 5856 : SpinLockAcquire(&slot->pss_mutex);
297 [ + - ]: 5856 : if (pg_atomic_read_u32(&slot->pss_pid) == pid)
298 : : {
299 : : /* Atomically set the proper flag */
6071 tgl@sss.pgh.pa.us 300 : 5856 : slot->pss_signalFlags[reason] = true;
594 heikki.linnakangas@i 301 : 5856 : SpinLockRelease(&slot->pss_mutex);
302 : : /* Send signal */
6071 tgl@sss.pgh.pa.us 303 : 5856 : return kill(pid, SIGUSR1);
304 : : }
594 heikki.linnakangas@i 305 :UBC 0 : SpinLockRelease(&slot->pss_mutex);
306 : : }
307 : : else
308 : : {
309 : : /*
310 : : * procNumber not provided, so search the array using pid. We search
311 : : * the array back to front so as to reduce search overhead. Passing
312 : : * INVALID_PROC_NUMBER means that the target is most likely an
313 : : * auxiliary process, which will have a slot near the end of the
314 : : * array.
315 : : */
316 : : int i;
317 : :
1433 rhaas@postgresql.org 318 [ + - ]:CBC 2796 : for (i = NumProcSignalSlots - 1; i >= 0; i--)
319 : : {
2278 320 : 2796 : slot = &ProcSignal->psh_slot[i];
321 : :
594 heikki.linnakangas@i 322 [ + + ]: 2796 : if (pg_atomic_read_u32(&slot->pss_pid) == pid)
323 : : {
324 [ - + ]: 62 : SpinLockAcquire(&slot->pss_mutex);
325 [ + - ]: 62 : if (pg_atomic_read_u32(&slot->pss_pid) == pid)
326 : : {
327 : : /* Atomically set the proper flag */
328 : 62 : slot->pss_signalFlags[reason] = true;
329 : 62 : SpinLockRelease(&slot->pss_mutex);
330 : : /* Send signal */
331 : 62 : return kill(pid, SIGUSR1);
332 : : }
594 heikki.linnakangas@i 333 :UBC 0 : SpinLockRelease(&slot->pss_mutex);
334 : : }
335 : : }
336 : : }
337 : :
6071 tgl@sss.pgh.pa.us 338 : 0 : errno = ESRCH;
339 : 0 : return -1;
340 : : }
341 : :
342 : : /*
343 : : * EmitProcSignalBarrier
344 : : * Send a signal to every Postgres process
345 : : *
346 : : * The return value of this function is the barrier "generation" created
347 : : * by this operation. This value can be passed to WaitForProcSignalBarrier
348 : : * to wait until it is known that every participant in the ProcSignal
349 : : * mechanism has absorbed the signal (or started afterwards).
350 : : *
351 : : * Note that it would be a bad idea to use this for anything that happens
352 : : * frequently, as interrupting every backend could cause a noticeable
353 : : * performance hit.
354 : : *
355 : : * Callers are entitled to assume that this function will not throw ERROR
356 : : * or FATAL.
357 : : */
358 : : uint64
2278 rhaas@postgresql.org 359 :CBC 595 : EmitProcSignalBarrier(ProcSignalBarrierType type)
360 : : {
2099 andres@anarazel.de 361 : 595 : uint32 flagbit = 1 << (uint32) type;
362 : : uint64 generation;
363 : :
364 : : /*
365 : : * Set all the flags.
366 : : *
367 : : * Note that pg_atomic_fetch_or_u32 has full barrier semantics, so this is
368 : : * totally ordered with respect to anything the caller did before, and
369 : : * anything that we do afterwards. (This is also true of the later call to
370 : : * pg_atomic_add_fetch_u64.)
371 : : */
1433 rhaas@postgresql.org 372 [ + + ]: 61875 : for (int i = 0; i < NumProcSignalSlots; i++)
373 : : {
2278 374 : 61280 : volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
375 : :
376 : 61280 : pg_atomic_fetch_or_u32(&slot->pss_barrierCheckMask, flagbit);
377 : : }
378 : :
379 : : /*
380 : : * Increment the generation counter.
381 : : */
382 : : generation =
383 : 595 : pg_atomic_add_fetch_u64(&ProcSignal->psh_barrierGeneration, 1);
384 : :
385 : : /*
386 : : * Signal all the processes, so that they update their advertised barrier
387 : : * generation.
388 : : *
389 : : * Concurrency is not a problem here. Backends that have exited don't
390 : : * matter, and new backends that have joined since we entered this
391 : : * function must already have current state, since the caller is
392 : : * responsible for making sure that the relevant state is entirely visible
393 : : * before calling this function in the first place. We still have to wake
394 : : * them up - because we can't distinguish between such backends and older
395 : : * backends that need to update state - but they won't actually need to
396 : : * change any state.
397 : : */
1433 398 [ + + ]: 61875 : for (int i = NumProcSignalSlots - 1; i >= 0; i--)
399 : : {
2278 400 : 61280 : volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
594 heikki.linnakangas@i 401 : 61280 : pid_t pid = pg_atomic_read_u32(&slot->pss_pid);
402 : :
2278 rhaas@postgresql.org 403 [ + + ]: 61280 : if (pid != 0)
404 : : {
594 heikki.linnakangas@i 405 [ - + ]: 3819 : SpinLockAcquire(&slot->pss_mutex);
406 : 3819 : pid = pg_atomic_read_u32(&slot->pss_pid);
407 [ + - ]: 3819 : if (pid != 0)
408 : : {
409 : : /* see SendProcSignal for details */
410 : 3819 : slot->pss_signalFlags[PROCSIG_BARRIER] = true;
411 : 3819 : SpinLockRelease(&slot->pss_mutex);
412 : 3819 : kill(pid, SIGUSR1);
413 : : }
414 : : else
594 heikki.linnakangas@i 415 :UBC 0 : SpinLockRelease(&slot->pss_mutex);
416 : : }
417 : : }
418 : :
2278 rhaas@postgresql.org 419 :CBC 595 : return generation;
420 : : }
421 : :
422 : : /*
423 : : * WaitForProcSignalBarrier - wait until it is guaranteed that all changes
424 : : * requested by a specific call to EmitProcSignalBarrier() have taken effect.
425 : : */
426 : : void
427 : 579 : WaitForProcSignalBarrier(uint64 generation)
428 : : {
2099 andres@anarazel.de 429 [ - + ]: 579 : Assert(generation <= pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration));
430 : :
1404 tmunro@postgresql.or 431 [ + + ]: 579 : elog(DEBUG1,
432 : : "waiting for all backends to process ProcSignalBarrier generation "
433 : : UINT64_FORMAT,
434 : : generation);
435 : :
1433 rhaas@postgresql.org 436 [ + + ]: 60729 : for (int i = NumProcSignalSlots - 1; i >= 0; i--)
437 : : {
1840 tmunro@postgresql.or 438 : 60150 : ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
439 : : uint64 oldval;
440 : :
441 : : /*
442 : : * It's important that we check only pss_barrierGeneration here and
443 : : * not pss_barrierCheckMask. Bits in pss_barrierCheckMask get cleared
444 : : * before the barrier is actually absorbed, but pss_barrierGeneration
445 : : * is updated only afterward.
446 : : */
2278 rhaas@postgresql.org 447 : 60150 : oldval = pg_atomic_read_u64(&slot->pss_barrierGeneration);
448 [ + + ]: 62953 : while (oldval < generation)
449 : : {
1404 tmunro@postgresql.or 450 [ - + ]: 2803 : if (ConditionVariableTimedSleep(&slot->pss_barrierCV,
451 : : 5000,
452 : : WAIT_EVENT_PROC_SIGNAL_BARRIER))
1404 tmunro@postgresql.or 453 [ # # ]:UBC 0 : ereport(LOG,
454 : : (errmsg("still waiting for backend with PID %d to accept ProcSignalBarrier",
455 : : (int) pg_atomic_read_u32(&slot->pss_pid))));
2278 rhaas@postgresql.org 456 :CBC 2803 : oldval = pg_atomic_read_u64(&slot->pss_barrierGeneration);
457 : : }
1840 tmunro@postgresql.or 458 : 60150 : ConditionVariableCancelSleep();
459 : : }
460 : :
1404 461 [ + + ]: 579 : elog(DEBUG1,
462 : : "finished waiting for all backends to process ProcSignalBarrier generation "
463 : : UINT64_FORMAT,
464 : : generation);
465 : :
466 : : /*
467 : : * The caller is probably calling this function because it wants to read
468 : : * the shared state or perform further writes to shared state once all
469 : : * backends are known to have absorbed the barrier. However, the read of
470 : : * pss_barrierGeneration was performed unlocked; insert a memory barrier
471 : : * to separate it from whatever follows.
472 : : */
2278 rhaas@postgresql.org 473 : 579 : pg_memory_barrier();
474 : 579 : }
475 : :
476 : : /*
477 : : * Handle receipt of an interrupt indicating a global barrier event.
478 : : *
479 : : * All the actual work is deferred to ProcessProcSignalBarrier(), because we
480 : : * cannot safely access the barrier generation inside the signal handler as
481 : : * 64bit atomics might use spinlock based emulation, even for reads. As this
482 : : * routine only gets called when PROCSIG_BARRIER is sent that won't cause a
483 : : * lot of unnecessary work.
484 : : */
485 : : static void
2099 andres@anarazel.de 486 : 2763 : HandleProcSignalBarrierInterrupt(void)
487 : : {
488 : 2763 : InterruptPending = true;
489 : 2763 : ProcSignalBarrierPending = true;
490 : : /* latch will be set by procsignal_sigusr1_handler */
491 : 2763 : }
492 : :
493 : : /*
494 : : * Perform global barrier related interrupt checking.
495 : : *
496 : : * Any backend that participates in ProcSignal signaling must arrange to
497 : : * call this function periodically. It is called from CHECK_FOR_INTERRUPTS(),
498 : : * which is enough for normal backends, but not necessarily for all types of
499 : : * background processes.
500 : : */
501 : : void
2278 rhaas@postgresql.org 502 : 2762 : ProcessProcSignalBarrier(void)
503 : : {
504 : : uint64 local_gen;
505 : : uint64 shared_gen;
506 : : volatile uint32 flags;
507 : :
2099 andres@anarazel.de 508 [ - + ]: 2762 : Assert(MyProcSignalSlot);
509 : :
510 : : /* Exit quickly if there's no work to do. */
2278 rhaas@postgresql.org 511 [ - + ]: 2762 : if (!ProcSignalBarrierPending)
2278 rhaas@postgresql.org 512 :UBC 0 : return;
2278 rhaas@postgresql.org 513 :CBC 2762 : ProcSignalBarrierPending = false;
514 : :
515 : : /*
516 : : * It's not unlikely to process multiple barriers at once, before the
517 : : * signals for all the barriers have arrived. To avoid unnecessary work in
518 : : * response to subsequent signals, exit early if we already have processed
519 : : * all of them.
520 : : */
2099 andres@anarazel.de 521 : 2762 : local_gen = pg_atomic_read_u64(&MyProcSignalSlot->pss_barrierGeneration);
522 : 2762 : shared_gen = pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration);
523 : :
524 [ - + ]: 2762 : Assert(local_gen <= shared_gen);
525 : :
526 [ - + ]: 2762 : if (local_gen == shared_gen)
2099 andres@anarazel.de 527 :UBC 0 : return;
528 : :
529 : : /*
530 : : * Get and clear the flags that are set for this backend. Note that
531 : : * pg_atomic_exchange_u32 is a full barrier, so we're guaranteed that the
532 : : * read of the barrier generation above happens before we atomically
533 : : * extract the flags, and that any subsequent state changes happen
534 : : * afterward.
535 : : *
536 : : * NB: In order to avoid race conditions, we must zero
537 : : * pss_barrierCheckMask first and only afterwards try to do barrier
538 : : * processing. If we did it in the other order, someone could send us
539 : : * another barrier of some type right after we called the
540 : : * barrier-processing function but before we cleared the bit. We would
541 : : * have no way of knowing that the bit needs to stay set in that case, so
542 : : * the need to call the barrier-processing function again would just get
543 : : * forgotten. So instead, we tentatively clear all the bits and then put
544 : : * back any for which we don't manage to successfully absorb the barrier.
545 : : */
2278 rhaas@postgresql.org 546 :CBC 2762 : flags = pg_atomic_exchange_u32(&MyProcSignalSlot->pss_barrierCheckMask, 0);
547 : :
548 : : /*
549 : : * If there are no flags set, then we can skip doing any real work.
550 : : * Otherwise, establish a PG_TRY block, so that we don't lose track of
551 : : * which types of barrier processing are needed if an ERROR occurs.
552 : : */
1882 553 [ + - ]: 2762 : if (flags != 0)
554 : : {
1768 tgl@sss.pgh.pa.us 555 : 2762 : bool success = true;
556 : :
1882 rhaas@postgresql.org 557 [ + - ]: 2762 : PG_TRY();
558 : : {
559 : : /*
560 : : * Process each type of barrier. The barrier-processing functions
561 : : * should normally return true, but may return false if the
562 : : * barrier can't be absorbed at the current time. This should be
563 : : * rare, because it's pretty expensive. Every single
564 : : * CHECK_FOR_INTERRUPTS() will return here until we manage to
565 : : * absorb the barrier, and that cost will add up in a hurry.
566 : : *
567 : : * NB: It ought to be OK to call the barrier-processing functions
568 : : * unconditionally, but it's more efficient to call only the ones
569 : : * that might need us to do something based on the flags.
570 : : */
571 [ + + ]: 8286 : while (flags != 0)
572 : : {
573 : : ProcSignalBarrierType type;
1768 tgl@sss.pgh.pa.us 574 : 2762 : bool processed = true;
575 : :
1882 rhaas@postgresql.org 576 : 2762 : type = (ProcSignalBarrierType) pg_rightmost_one_pos32(flags);
577 [ + + - ]: 2762 : switch (type)
578 : : {
1492 tmunro@postgresql.or 579 : 676 : case PROCSIGNAL_BARRIER_SMGRRELEASE:
580 : 676 : processed = ProcessBarrierSmgrRelease();
1882 rhaas@postgresql.org 581 : 676 : break;
82 msawada@postgresql.o 582 :GNC 2086 : case PROCSIGNAL_BARRIER_UPDATE_XLOG_LOGICAL_INFO:
583 : 2086 : processed = ProcessBarrierUpdateXLogLogicalInfo();
584 : 2086 : break;
585 : : }
586 : :
587 : : /*
588 : : * To avoid an infinite loop, we must always unset the bit in
589 : : * flags.
590 : : */
1882 rhaas@postgresql.org 591 :CBC 2762 : BARRIER_CLEAR_BIT(flags, type);
592 : :
593 : : /*
594 : : * If we failed to process the barrier, reset the shared bit
595 : : * so we try again later, and set a flag so that we don't bump
596 : : * our generation.
597 : : */
598 [ + - ]: 2762 : if (!processed)
599 : : {
1882 rhaas@postgresql.org 600 :UBC 0 : ResetProcSignalBarrierBits(((uint32) 1) << type);
601 : 0 : success = false;
602 : : }
603 : : }
604 : : }
605 : 0 : PG_CATCH();
606 : : {
607 : : /*
608 : : * If an ERROR occurred, we'll need to try again later to handle
609 : : * that barrier type and any others that haven't been handled yet
610 : : * or weren't successfully absorbed.
611 : : */
612 : 0 : ResetProcSignalBarrierBits(flags);
613 : 0 : PG_RE_THROW();
614 : : }
1882 rhaas@postgresql.org 615 [ - + ]:CBC 2762 : PG_END_TRY();
616 : :
617 : : /*
618 : : * If some barrier types were not successfully absorbed, we will have
619 : : * to try again later.
620 : : */
621 [ - + ]: 2762 : if (!success)
1882 rhaas@postgresql.org 622 :UBC 0 : return;
623 : : }
624 : :
625 : : /*
626 : : * State changes related to all types of barriers that might have been
627 : : * emitted have now been handled, so we can update our notion of the
628 : : * generation to the one we observed before beginning the updates. If
629 : : * things have changed further, it'll get fixed up when this function is
630 : : * next called.
631 : : */
2099 andres@anarazel.de 632 :CBC 2762 : pg_atomic_write_u64(&MyProcSignalSlot->pss_barrierGeneration, shared_gen);
1840 tmunro@postgresql.or 633 : 2762 : ConditionVariableBroadcast(&MyProcSignalSlot->pss_barrierCV);
634 : : }
635 : :
636 : : /*
637 : : * If it turns out that we couldn't absorb one or more barrier types, either
638 : : * because the barrier-processing functions returned false or due to an error,
639 : : * arrange for processing to be retried later.
640 : : */
641 : : static void
1882 rhaas@postgresql.org 642 :UBC 0 : ResetProcSignalBarrierBits(uint32 flags)
643 : : {
644 : 0 : pg_atomic_fetch_or_u32(&MyProcSignalSlot->pss_barrierCheckMask, flags);
645 : 0 : ProcSignalBarrierPending = true;
646 : 0 : InterruptPending = true;
647 : 0 : }
648 : :
649 : : /*
650 : : * CheckProcSignal - check to see if a particular reason has been
651 : : * signaled, and clear the signal flag. Should be called after receiving
652 : : * SIGUSR1.
653 : : */
654 : : static bool
6071 tgl@sss.pgh.pa.us 655 :CBC 89808 : CheckProcSignal(ProcSignalReason reason)
656 : : {
657 : 89808 : volatile ProcSignalSlot *slot = MyProcSignalSlot;
658 : :
659 [ + + ]: 89808 : if (slot != NULL)
660 : : {
661 : : /*
662 : : * Careful here --- don't clear flag if we haven't seen it set.
663 : : * pss_signalFlags is of type "volatile sig_atomic_t" to allow us to
664 : : * read it here safely, without holding the spinlock.
665 : : */
666 [ + + ]: 89680 : if (slot->pss_signalFlags[reason])
667 : : {
668 : 7038 : slot->pss_signalFlags[reason] = false;
669 : 7038 : return true;
670 : : }
671 : : }
672 : :
673 : 82770 : return false;
674 : : }
675 : :
676 : : /*
677 : : * procsignal_sigusr1_handler - handle SIGUSR1 signal.
678 : : */
679 : : void
680 : 11226 : procsignal_sigusr1_handler(SIGNAL_ARGS)
681 : : {
682 [ + + ]: 11226 : if (CheckProcSignal(PROCSIG_CATCHUP_INTERRUPT))
683 : 2814 : HandleCatchupInterrupt();
684 : :
685 [ + + ]: 11226 : if (CheckProcSignal(PROCSIG_NOTIFY_INTERRUPT))
686 : 11 : HandleNotifyInterrupt();
687 : :
3972 rhaas@postgresql.org 688 [ + + ]: 11226 : if (CheckProcSignal(PROCSIG_PARALLEL_MESSAGE))
689 : 1364 : HandleParallelMessageInterrupt();
690 : :
3205 andres@anarazel.de 691 [ + + ]: 11226 : if (CheckProcSignal(PROCSIG_WALSND_INIT_STOPPING))
692 : 43 : HandleWalSndInitStopping();
693 : :
2099 694 [ + + ]: 11226 : if (CheckProcSignal(PROCSIG_BARRIER))
695 : 2763 : HandleProcSignalBarrierInterrupt();
696 : :
1804 fujii@postgresql.org 697 [ + + ]: 11226 : if (CheckProcSignal(PROCSIG_LOG_MEMORY_CONTEXT))
698 : 9 : HandleLogMemoryContextInterrupt();
699 : :
1161 akapila@postgresql.o 700 [ + + ]: 11226 : if (CheckProcSignal(PROCSIG_PARALLEL_APPLY_MESSAGE))
701 : 15 : HandleParallelApplyMessageInterrupt();
702 : :
33 heikki.linnakangas@i 703 [ + + ]:GNC 11226 : if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT))
704 : 19 : HandleRecoveryConflictInterrupt();
705 : :
3810 rhaas@postgresql.org 706 :CBC 11226 : SetLatch(MyLatch);
6071 tgl@sss.pgh.pa.us 707 : 11226 : }
708 : :
709 : : /*
710 : : * Send a query cancellation signal to backend.
711 : : *
712 : : * Note: This is called from a backend process before authentication. We
713 : : * cannot take LWLocks yet, but that's OK; we rely on atomic reads of the
714 : : * fields in the ProcSignal slots.
715 : : */
716 : : void
311 heikki.linnakangas@i 717 : 16 : SendCancelRequest(int backendPID, const uint8 *cancel_key, int cancel_key_len)
718 : : {
228 719 [ - + ]: 16 : if (backendPID == 0)
720 : : {
228 heikki.linnakangas@i 721 [ # # ]:UBC 0 : ereport(LOG, (errmsg("invalid cancel request with PID 0")));
722 : 0 : return;
723 : : }
724 : :
725 : : /*
726 : : * See if we have a matching backend. Reading the pss_pid and
727 : : * pss_cancel_key fields is racy, a backend might die and remove itself
728 : : * from the array at any time. The probability of the cancellation key
729 : : * matching wrong process is miniscule, however, so we can live with that.
730 : : * PIDs are reused too, so sending the signal based on PID is inherently
731 : : * racy anyway, although OS's avoid reusing PIDs too soon.
732 : : */
594 heikki.linnakangas@i 733 [ + - ]:CBC 275 : for (int i = 0; i < NumProcSignalSlots; i++)
734 : : {
735 : 275 : ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
736 : : bool match;
737 : :
738 [ + + ]: 275 : if (pg_atomic_read_u32(&slot->pss_pid) != backendPID)
739 : 259 : continue;
740 : :
741 : : /* Acquire the spinlock and re-check */
742 [ - + ]: 16 : SpinLockAcquire(&slot->pss_mutex);
743 [ - + ]: 16 : if (pg_atomic_read_u32(&slot->pss_pid) != backendPID)
744 : : {
594 heikki.linnakangas@i 745 :UBC 0 : SpinLockRelease(&slot->pss_mutex);
746 : 0 : continue;
747 : : }
748 : : else
749 : : {
347 heikki.linnakangas@i 750 [ + - + - ]:CBC 32 : match = slot->pss_cancel_key_len == cancel_key_len &&
751 : 16 : timingsafe_bcmp(slot->pss_cancel_key, cancel_key, cancel_key_len) == 0;
752 : :
594 753 : 16 : SpinLockRelease(&slot->pss_mutex);
754 : :
755 [ + - ]: 16 : if (match)
756 : : {
757 : : /* Found a match; signal that backend to cancel current op */
758 [ - + ]: 16 : ereport(DEBUG2,
759 : : (errmsg_internal("processing cancel request: sending SIGINT to process %d",
760 : : backendPID)));
761 : :
762 : : /*
763 : : * If we have setsid(), signal the backend's whole process
764 : : * group
765 : : */
766 : : #ifdef HAVE_SETSID
767 : 16 : kill(-backendPID, SIGINT);
768 : : #else
769 : : kill(backendPID, SIGINT);
770 : : #endif
771 : : }
772 : : else
773 : : {
774 : : /* Right PID, wrong key: no way, Jose */
594 heikki.linnakangas@i 775 [ # # ]:UBC 0 : ereport(LOG,
776 : : (errmsg("wrong key in cancel request for process %d",
777 : : backendPID)));
778 : : }
594 heikki.linnakangas@i 779 :CBC 16 : return;
780 : : }
781 : : }
782 : :
783 : : /* No matching backend */
594 heikki.linnakangas@i 784 [ # # ]:UBC 0 : ereport(LOG,
785 : : (errmsg("PID %d in cancel request did not match any process",
786 : : backendPID)));
787 : : }
|