From patchwork Mon Mar 9 14:49:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: George Dunlap X-Patchwork-Id: 11427301 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4A1B114BC for ; Mon, 9 Mar 2020 14:50:42 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2626C21655 for ; Mon, 9 Mar 2020 14:50:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=citrix.com header.i=@citrix.com header.b="YuvaY7mc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2626C21655 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=citrix.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1jBJj2-0001U0-2B; Mon, 09 Mar 2020 14:49:48 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1jBJj0-0001TL-5L for xen-devel@lists.xenproject.org; Mon, 09 Mar 2020 14:49:46 +0000 X-Inumbo-ID: 34e77c5e-6215-11ea-ac49-12813bfff9fa Received: from esa2.hc3370-68.iphmx.com (unknown [216.71.145.153]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 34e77c5e-6215-11ea-ac49-12813bfff9fa; Mon, 09 Mar 2020 14:49:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1583765382; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vpX4a3rHIYM1IyybvTOd809q8QTiT5idPugMbJedj2o=; b=YuvaY7mcl/VFtFnn7Bj9Wnf8pWQBtL1LhSG99Yv8IJvwHNzthuIq/Z5N WOJ37AA2kbM2PBfryC8HFrVd7lpn14kH4NciiTa+hXa1+daqvzUaoPjg+ cQqgkjNm5DT69FdRmvm59tdZl0OKoLg7Ml6DOZcPFOV/YW8YuUcMNVtYs k=; Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=george.dunlap@citrix.com; spf=Pass smtp.mailfrom=George.Dunlap@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of george.dunlap@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="George.Dunlap@citrix.com"; x-sender="george.dunlap@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa2.hc3370-68.iphmx.com: domain of George.Dunlap@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="George.Dunlap@citrix.com"; x-sender="George.Dunlap@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ip4:168.245.78.127 ~all" Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="George.Dunlap@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: osQTl3yeAYW32Phj1vVIyVWnf4CqZ5ZHEO45EI87+5O1GZXyNs+6jpWyr8VYc91kpTkMLoqz1+ pbFNPfyjdWaF79r/e4p9pTof+DgCjjSESoPCDN04zkceZmE/YRDDKcOIVVs+IuC6sdhowV+P+R ZPQ4MYp6By2r4sXE0dilt5y1j67MilCCeSKYtPdcVUdeao2mkjapnwaAvr5vESfa9iVSeIqswg AWz5flbcABng/VjitTwm+2Vg27Yi8tbnPMFLOv9FymJ5Rlqc6ODEbdo4m4drQSmpY8ec4LUjR1 38g= X-SBRS: 2.7 X-MesageID: 13644933 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.70,533,1574139600"; d="scan'208";a="13644933" From: George Dunlap To: Date: Mon, 9 Mar 2020 14:49:31 +0000 Message-ID: <20200309144932.866097-2-george.dunlap@citrix.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200309144932.866097-1-george.dunlap@citrix.com> References: <20200309144932.866097-1-george.dunlap@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v4 2/3] golang/xenlight: Notify xenlight of SIGCHLD X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Nick Rosbrook , Ian Jackson , George Dunlap Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" libxl forks external processes and waits for them to complete; it therefore needs to be notified when children exit. In absence of instructions to the contrary, libxl sets up its own SIGCHLD handlers. Golang always unmasks and handles SIGCHLD itself. libxl thankfully notices this and throws an assert() rather than clobbering SIGCHLD handlers. Tell libxl that we'll be responsible for getting SIGCHLD notifications to it. Arrange for a channel in the context to receive notifications on SIGCHLD, and set up a goroutine that will pass these on to libxl. NB that every libxl context needs a notification; so multiple contexts will each spin up their own goroutine when opening a context, and shut it down on close. libxl also wants to hold on to a const pointer to xenlight_childproc_hooks rather than do a copy; so make a global structure in C space. Make it `static const`, just for extra safety; this requires making a function in the C space to pass it to libxl. While here, add a few comments to make the context set-up a bit easier to follow. Signed-off-by: George Dunlap Reviewed-by: Nick Rosbrook --- v4: - Skip v3 to avoid confusion - Don't bother doing a separate goroutine for libxl_childproc_sigchld_occurred. - Allow chidlproc_hooks to be static const, by: - initializing it in the C space, and - Making a C function to pass it to libxl_childproc_setmode - Cleanup on ctx.sigchld != nil, not == nil - Use struct{} rather than bool for clarity - Add a comment above the sigchldHandler goroutine describing its lifetime v2: - Fix unsafe libxl_childproc_hooks pointer behavior - Close down the SIGCHLD handler first, and make sure it's exited before closing the context - Explicitly decide to have a separate goroutine per ctx NB that due to a bug in libxl, this will hang without Ian's "[PATCH v2 00/10] libxl: event: Fix hang for some applications" series. CC: Nick Rosbrook CC: Ian Jackson --- tools/golang/xenlight/xenlight.go | 70 ++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go index 3f1b0baa0c..56fa31fd7b 100644 --- a/tools/golang/xenlight/xenlight.go +++ b/tools/golang/xenlight/xenlight.go @@ -17,9 +17,17 @@ package xenlight /* + #cgo LDFLAGS: -lxenlight -lyajl -lxentoollog #include #include + +static const libxl_childproc_hooks childproc_hooks = { .chldowner = libxl_sigchld_owner_mainloop }; + +void xenlight_set_chldproc(libxl_ctx *ctx) { + libxl_childproc_setmode(ctx, &childproc_hooks, NULL); +} + */ import "C" @@ -33,6 +41,9 @@ import "C" import ( "fmt" + "os" + "os/signal" + "syscall" "unsafe" ) @@ -74,8 +85,37 @@ func (e Error) Error() string { // Context represents a libxl_ctx. type Context struct { - ctx *C.libxl_ctx - logger *C.xentoollog_logger_stdiostream + ctx *C.libxl_ctx + logger *C.xentoollog_logger_stdiostream + sigchld chan os.Signal + sigchldDone chan struct{} +} + +// Golang always unmasks SIGCHLD, and internally has ways of +// distributing SIGCHLD to multiple recipients. libxl has provision +// for this model: just tell it when a SIGCHLD happened, and it will +// look after its own processes. +// +// This should "play nicely" with other users of SIGCHLD as long as +// they don't reap libxl's processes. +// +// Every context needs to be notified on each SIGCHLD; so spin up a +// new goroutine for each context. If there are a large number of +// contexts, this means each context will be woken up looking through +// its own list of children. +// +// The alternate would be to register a fork callback, such that the +// xenlight package can make a single list of all children, and only +// notify the specific libxl context(s) that have children woken. But +// it's not clear to me this will be much more work than having the +// xenlight go library do the same thing; doing it in separate go +// threads has the potential to do it in parallel. Leave that as an +// optimization for later if it turns out to be a bottleneck. +func sigchldHandler(ctx *Context) { + for _ = range ctx.sigchld { + C.libxl_childproc_sigchld_occurred(ctx.ctx) + } + close(ctx.sigchldDone) } // NewContext returns a new Context. @@ -89,19 +129,45 @@ func NewContext() (ctx *Context, err error) { } }() + // Create a logger ctx.logger = C.xtl_createlogger_stdiostream(C.stderr, C.XTL_ERROR, 0) + // Allocate a context ret := C.libxl_ctx_alloc(&ctx.ctx, C.LIBXL_VERSION, 0, (*C.xentoollog_logger)(unsafe.Pointer(ctx.logger))) if ret != 0 { return ctx, Error(ret) } + // Tell libxl that we'll be dealing with SIGCHLD... + C.xenlight_set_chldproc(ctx.ctx) + + // ...and arrange to keep that promise. + ctx.sigchld = make(chan os.Signal, 2) + ctx.sigchldDone = make(chan struct{}, 1) + signal.Notify(ctx.sigchld, syscall.SIGCHLD) + + // This goroutine will run until the ctx.sigchld is closed in + // ctx.Close(); at which point it will close ctx.sigchldDone. + go sigchldHandler(ctx) + return ctx, nil } // Close closes the Context. func (ctx *Context) Close() error { + // Tell our SIGCHLD notifier to shut down, and wait for it to exit + // before we free the context. + if ctx.sigchld != nil { + signal.Stop(ctx.sigchld) + close(ctx.sigchld) + + <-ctx.sigchldDone + + ctx.sigchld = nil + ctx.sigchldDone = nil + } + if ctx.ctx != nil { ret := C.libxl_ctx_free(ctx.ctx) if ret != 0 {