diff options
author | Ivan Maidanski <ivmai@mail.ru> | 2021-09-01 23:32:14 +0300 |
---|---|---|
committer | Ivan Maidanski <ivmai@mail.ru> | 2021-09-12 09:33:21 +0300 |
commit | 74f6542e6a52e35b8dd526b1d5ad12fbb567938c (patch) | |
tree | c7f2194a9f0cc145a4e3800f8142ff484f7ec11f | |
parent | 6f998be44f30bdc3d0e532290bc9aa5b939dd2d5 (diff) | |
download | bdwgc-74f6542e6a52e35b8dd526b1d5ad12fbb567938c.tar.gz |
Fix GC_proc_fd value in child process at fork (Solaris)
(a cherry-pick of commits 8ab50d337, 7d8bd9be9 from 'master')
In case of PROC_VDB mode is initialized, GC_proc_fd contains a file
descriptor to "/proc/<pid>/pagedata". After forking, the child
process has a different pid value, thus the old file descriptor
should be closed and the one with the updated file name should be
opened in turn.
* include/private/gc_priv.h [!GC_DISABLE_INCREMENTAL
&& CAN_HANDLE_FORK] (GC_dirty_update_child): Declare (as a function
or as a macro).
* os_dep.c [PROC_VDB && !THREADS] (saved_proc_pid): New static
variable.
* os_dep.c [PROC_VDB] (GC_proc_fd): Initialize to -1 (instead of 0).
* os_dep.c [PROC_VDB] (proc_dirty_open_files): New static function
(part of code is moved from GC_dirty_init).
* os_dep.c [PROC_VDB && !THREADS] (proc_dirty_open_files): Store
pid to saved_proc_pid when true is returned.
* os_dep.c [PROC_VDB && CAN_HANDLE_FORK] (GC_dirty_update_child): New
GC_INNER function.
* os_dep.c [PROC_VDB] (GC_dirty_init): Call proc_dirty_open_files().
* os_dep.c [PROC_VDB && !THREADS] (GC_proc_read_dirty): Call
proc_dirty_open_files() if current pid differs from saved_proc_pid.
* pthread_support.c [CAN_HANDLE_FORK && !GC_DISABLE_INCREMENTAL]
(fork_child_proc): Call GC_dirty_update_child().
-rw-r--r-- | include/private/gc_priv.h | 11 | ||||
-rw-r--r-- | os_dep.c | 66 | ||||
-rw-r--r-- | pthread_support.c | 5 |
3 files changed, 69 insertions, 13 deletions
diff --git a/include/private/gc_priv.h b/include/private/gc_priv.h index 79aee7cf..629418f3 100644 --- a/include/private/gc_priv.h +++ b/include/private/gc_priv.h @@ -2214,6 +2214,17 @@ GC_EXTERN GC_bool GC_print_back_height; /* pointer-free system call buffers in the heap are */ /* not protected. */ +# ifdef CAN_HANDLE_FORK +# if defined(PROC_VDB) + GC_INNER void GC_dirty_update_child(void); + /* Update pid-specific resources (like /proc file */ + /* descriptors) needed by the dirty bits implementation */ + /* after fork in the child process. */ +# else +# define GC_dirty_update_child() (void)0 +# endif +# endif /* CAN_HANDLE_FORK */ + GC_INNER GC_bool GC_dirty_init(void); /* Returns true if dirty bits are maintained (otherwise */ /* it is OK to be called again if the client invokes */ @@ -3545,32 +3545,57 @@ STATIC void GC_protect_heap(void) # include <sys/procfs.h> # endif +# ifndef THREADS + static pid_t saved_proc_pid; /* pid used to compose /proc file name */ +# endif + # define INITIAL_BUF_SZ 16384 STATIC size_t GC_proc_buf_size = INITIAL_BUF_SZ; STATIC char *GC_proc_buf = NULL; - STATIC int GC_proc_fd = 0; + STATIC int GC_proc_fd = -1; -GC_INNER GC_bool GC_dirty_init(void) -{ + static GC_bool proc_dirty_open_files(void) + { char buf[40]; + pid_t pid = getpid(); - if (GC_bytes_allocd != 0 || GC_bytes_allocd_before_gc != 0) { - memset(GC_written_pages, 0xff, sizeof(page_hash_table)); - GC_VERBOSE_LOG_PRINTF( - "Allocated %lu bytes: all pages may have been written\n", - (unsigned long)(GC_bytes_allocd + GC_bytes_allocd_before_gc)); - } - - (void)snprintf(buf, sizeof(buf), "/proc/%ld/pagedata", (long)getpid()); + (void)snprintf(buf, sizeof(buf), "/proc/%ld/pagedata", (long)pid); buf[sizeof(buf) - 1] = '\0'; GC_proc_fd = open(buf, O_RDONLY); - if (GC_proc_fd < 0) { + if (-1 == GC_proc_fd) { WARN("/proc open failed; cannot enable GC incremental mode\n", 0); return FALSE; } if (syscall(SYS_fcntl, GC_proc_fd, F_SETFD, FD_CLOEXEC) == -1) WARN("Could not set FD_CLOEXEC for /proc\n", 0); +# ifndef THREADS + saved_proc_pid = pid; /* updated on success only */ +# endif + return TRUE; + } +# ifdef CAN_HANDLE_FORK + GC_INNER void GC_dirty_update_child(void) + { + if (-1 == GC_proc_fd) + return; /* GC incremental mode is off */ + + close(GC_proc_fd); + if (!proc_dirty_open_files()) + GC_incremental = FALSE; /* should be safe to turn it off */ + } +# endif /* CAN_HANDLE_FORK */ + +GC_INNER GC_bool GC_dirty_init(void) +{ + if (GC_bytes_allocd != 0 || GC_bytes_allocd_before_gc != 0) { + memset(GC_written_pages, 0xff, sizeof(page_hash_table)); + GC_VERBOSE_LOG_PRINTF( + "Allocated %lu bytes: all pages may have been written\n", + (unsigned long)(GC_bytes_allocd + GC_bytes_allocd_before_gc)); + } + if (!proc_dirty_open_files()) + return FALSE; GC_proc_buf = GC_scratch_alloc(GC_proc_buf_size); if (GC_proc_buf == NULL) ABORT("Insufficient space for /proc read"); @@ -3584,6 +3609,23 @@ GC_INLINE void GC_proc_read_dirty(GC_bool output_unneeded) char * bufp = GC_proc_buf; int i; +# ifndef THREADS + /* If the current pid differs from the saved one, then we are in */ + /* the forked (child) process, the current /proc file should be */ + /* closed, the new one should be opened with the updated path. */ + /* Note, this is not needed for multi-threaded case because */ + /* fork_child_proc() reopens the file right after fork. */ + if (getpid() != saved_proc_pid + && (-1 == GC_proc_fd /* no need to retry */ + || (close(GC_proc_fd), !proc_dirty_open_files()))) { + /* Failed to reopen the file. Punt! */ + if (!output_unneeded) + memset(GC_grungy_pages, 0xff, sizeof(page_hash_table)); + memset(GC_written_pages, 0xff, sizeof(page_hash_table)); + return; + } +# endif + BZERO(GC_grungy_pages, sizeof(GC_grungy_pages)); if (READ(GC_proc_fd, bufp, GC_proc_buf_size) <= 0) { /* Retry with larger buffer. */ diff --git a/pthread_support.c b/pthread_support.c index e8b9c136..3ac604b8 100644 --- a/pthread_support.c +++ b/pthread_support.c @@ -1123,7 +1123,10 @@ static void fork_child_proc(void) /* Turn off parallel marking in the child, since we are probably */ /* just going to exec, and we would have to restart mark threads. */ GC_parallel = FALSE; -# endif /* PARALLEL_MARK */ +# endif +# ifndef GC_DISABLE_INCREMENTAL + GC_dirty_update_child(); +# endif RESTORE_CANCEL(fork_cancel_state); UNLOCK(); /* Even though after a fork the child only inherits the single */ |