mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: mwrap-perl@80x24.org
Subject: [WIP] attempt to workaround older URCU for signalfd
Date: Mon, 19 Dec 2022 22:57:37 +0000	[thread overview]
Message-ID: <20221219225737.M672846@dcvr> (raw)
In-Reply-To: <20221219111922.1079128-8-e@80x24.org>

Eric Wong <e@80x24.org> wrote:
> Fortunately, there aren't a lot of signalfd users right now
> (except me :x).  In any case, this problem should sort itself
> out in a few years when new URCU releases replace old ones.

> +* signalfd(2)-reliant code will need latest URCU with commit
> +  ea3a28a3f71dd02f (Disable signals in URCU background threads, 2022-09-23)

Fwiw, this was my attempt to workaround current (released) URCU
for fork() users.  Maybe I missed something, but I was still
getting one thread with signals unmasked after forking.  Not
sure it's worth my time to check since I can easily backport
URCU commit ea3a28a3f71dd02f for my own use.  (URCU is fairly
small and fast to build)

diff --git a/mwrap_core.h b/mwrap_core.h
index b014f5d..6d3d474 100644
--- a/mwrap_core.h
+++ b/mwrap_core.h
@@ -963,17 +963,32 @@ static struct src_loc *mwrap_get_bin(const char *buf, size_t len)
 static const char *mwrap_env;
 #include "httpd.h"
 
+/* force call_rcu to start background thread while signals are blocked */
+static void start_call_rcu(void)
+{
+	struct alloc_hdr *h = real_malloc(sizeof(struct alloc_hdr));
+	if (h) {
+		h->real = h;
+		call_rcu(&h->as.dead, free_hdr_rcu);
+	} else
+		fprintf(stderr, "malloc: %m\n");
+	struct cds_lfht *ht = lfht_new(1);
+	if (ht)
+		cds_lfht_destroy(ht, NULL);
+	else
+		fprintf(stderr, "cds_lfht_new: %m\n");
+}
+
 __attribute__((constructor)) static void mwrap_ctor(void)
 {
-	sigset_t set, old;
-	struct alloc_hdr *h;
 	mwrap_env = getenv("MWRAP");
 
 	++locating;
 
 	/* block signals */
-	CHECK(int, 0, sigfillset(&set));
-	CHECK(int, 0, pthread_sigmask(SIG_SETMASK, &set, &old));
+	sigset_t blk, old;
+	CHECK(int, 0, sigfillset(&blk));
+	CHECK(int, 0, pthread_sigmask(SIG_BLOCK, &blk, &old));
 	ensure_initialization();
 	CHECK(int, 0, pthread_key_create(&tlskey, mstate_tsd_dtor));
 
@@ -984,13 +999,7 @@ __attribute__((constructor)) static void mwrap_ctor(void)
 	CMM_STORE_SHARED(totals, lfht_new(16384));
 	if (!CMM_LOAD_SHARED(totals))
 		fprintf(stderr, "failed to allocate totals table\n");
-	h = real_malloc(sizeof(struct alloc_hdr));
-	if (h) { /* force call_rcu to start background thread */
-		h->real = h;
-		call_rcu(&h->as.dead, free_hdr_rcu);
-	} else
-		fprintf(stderr, "malloc: %m\n");
-
+	start_call_rcu();
 	h1d_start();
 	CHECK(int, 0, pthread_sigmask(SIG_SETMASK, &old, NULL));
 	CHECK(int, 0, pthread_atfork(atfork_prepare, atfork_parent,
diff --git a/mymalloc.h b/mymalloc.h
index 518cce3..e611485 100644
--- a/mymalloc.h
+++ b/mymalloc.h
@@ -41,6 +41,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <signal.h>
 
 /* this is fine on most x86-64, especially with file-backed mmap(2) */
 #define DEFAULT_GRANULARITY (64U * 1024U * 1024U)
@@ -126,6 +127,7 @@ ATTR_COLD static void mstate_tsd_dtor(void *p)
 	CHECK(int, 0, pthread_mutex_unlock(&global_mtx));
 }
 
+static void start_call_rcu(void);
 /* see httpd.h */
 static void h1d_atfork_prepare(void);
 static void h1d_atfork_parent(void);
@@ -141,9 +143,14 @@ ATTR_COLD static void atfork_prepare(void)
 ATTR_COLD static void atfork_parent(void)
 {
 	CHECK(int, 0, pthread_mutex_unlock(&global_mtx));
+	sigset_t blk, old;
+	CHECK(int, 0, sigfillset(&blk));
+	CHECK(int, 0, pthread_sigmask(SIG_BLOCK, &blk, &old));
 	call_rcu_after_fork_parent();
 	CHECK(int, 0, pthread_mutex_lock(&global_mtx));
 	h1d_atfork_parent();
+	start_call_rcu();
+	CHECK(int, 0, pthread_sigmask(SIG_SETMASK, &old, NULL));
 	CHECK(int, 0, pthread_mutex_unlock(&global_mtx));
 }
 
@@ -166,8 +173,13 @@ ATTR_COLD static void atfork_child(void)
 		cds_list_add(&ms_tsd->arena_node, &arenas_active);
 	}
 	reset_mutexes();
+	sigset_t blk, old;
+	CHECK(int, 0, sigfillset(&blk));
+	CHECK(int, 0, pthread_sigmask(SIG_BLOCK, &blk, &old));
 	call_rcu_after_fork_child();
 	h1d_start();
+	start_call_rcu();
+	CHECK(int, 0, pthread_sigmask(SIG_SETMASK, &old, NULL));
 }
 
 #if defined(__GLIBC__)

      reply	other threads:[~2022-12-19 22:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-19 11:19 [PATCH 0/7] shrinkage Eric Wong
2022-12-19 11:19 ` [PATCH 1/7] core: shrink src_loc 8 bytes on x86-64 Eric Wong
2022-12-19 11:19 ` [PATCH 2/7] core: shrink src_loc by another " Eric Wong
2022-12-19 11:19 ` [PATCH 3/7] core: drop FreeBSD-specific mutex initialization Eric Wong
2022-12-19 11:19 ` [PATCH 4/7] calloc: consolidate ENOMEM handling Eric Wong
2022-12-19 11:19 ` [PATCH 5/7] *alloc: limit scope of RCU lock Eric Wong
2022-12-19 11:19 ` [PATCH 6/7] core: simplify callers of alloc_insert_rcu Eric Wong
2022-12-19 11:19 ` [PATCH 7/7] README: add some caveats (signalfd usage w/ current URCU) Eric Wong
2022-12-19 22:57   ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221219225737.M672846@dcvr \
    --to=e@80x24.org \
    --cc=mwrap-perl@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mwrap-perl.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).