From nobody Mon Feb 9 11:33:38 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1572458895; cv=none; d=zoho.com; s=zohoarc; b=Ku8kBHXJ6aOl1K2R93Eb71jS0LJgAYKzZauRQO9DFegKp+th84r7xISv3xbaWQq3VYgxR5hN79n56XpnI82cNFd4W4lopuHf61ALCsHZwYKxTsYsrn7rBefZnEp6VAcO/1ZCaefMzyLlR1e3y/RVSegT2llZOYRY0IZKdyca6EM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1572458895; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ErecksiIOhITuQQ8j9Avsjeo5XKjidYPrSs0hlkuhB8=; b=KJ2HMJaLf1F8TPlkXhimfFlU39QkjbYEtmldWc/Sc5V/w5LF5v8QDGEZc/lDu5hKqF8IEuokVB5PzZrEz1OjFtwwxqS7ldksLma5339r6JbEG7LGJMQfJ0o7wY2/diVaYktk0CITK8C/f7Gy7cjS0O0No3QlBAmZX/kZHfw8pdM= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1572458895183777.0833150637395; Wed, 30 Oct 2019 11:08:15 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iPsNB-0003SH-Ti; Wed, 30 Oct 2019 18:07:09 +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 1iPsNA-0003S6-HO for xen-devel@lists.xenproject.org; Wed, 30 Oct 2019 18:07:08 +0000 Received: from esa1.hc3370-68.iphmx.com (unknown [216.71.145.142]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 146ad26f-fb40-11e9-9534-12813bfff9fa; Wed, 30 Oct 2019 18:07:07 +0000 (UTC) X-Inumbo-ID: 146ad26f-fb40-11e9-9534-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1572458828; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jfbtYdJwge8ip7QrjfcNVyOxqCMPmsXPyaIHTxNZ/EA=; b=XXZPZuPC0hKX+uCrqPUzXLX4aOgoJbmt5TQF1eVCH39W+YTKC3slnD4d zV5hjVDkbMvJlckERc39zbtExk5RN87iCZ51V0dOsy3oso3stgwh18sGo KZjIXMxVakwF5fNqAF6asHM4YDWMhGtG/TuUA9k/4uvV278vPG6g/ZOLK M=; Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=anthony.perard@citrix.com; spf=Pass smtp.mailfrom=anthony.perard@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: None (esa1.hc3370-68.iphmx.com: no sender authenticity information available from domain of anthony.perard@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa1.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="anthony.perard@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa1.hc3370-68.iphmx.com: domain of anthony.perard@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa1.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="anthony.perard@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 (esa1.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=esa1.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: 3HNMKJ+m1/MA1ZdnDzAyP9c4QiSDoXAmwkD7zlUDOi0dOLSmwj1SzVerz99kRPsHbGuNvwdbls MMMoaLSmTiObMTmpdTrjHHuMUVWZQv39KNgWbkLaJ6VeyK9rNT4FDyupxCtfpEoxwUlYu9nITh iqYE7rdBkvQCSlnq38OAKloLGT2fVZa+Q3NSQtggubgyZ2UKQ+2kN00E9pbe6Mb1ocHHXJu0tH H7GGY9AOBSw8wl58bjHpr8F5eFpoXpjA6ttELNmfvvcTKNoiZRUbqKJ0tLJbUuhM8b6EHXL9S2 GZc= X-SBRS: 2.7 X-MesageID: 7739499 X-Ironport-Server: esa1.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.68,248,1569297600"; d="scan'208";a="7739499" From: Anthony PERARD To: Date: Wed, 30 Oct 2019 18:06:59 +0000 Message-ID: <20191030180704.261320-2-anthony.perard@citrix.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191030180704.261320-1-anthony.perard@citrix.com> References: <20191030180704.261320-1-anthony.perard@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [XEN PATCH for-4.13 v2 1/6] libxl: Introduce libxl__ev_child_kill_deregister 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: Anthony PERARD , =?UTF-8?q?J=C3=BCrgen=20Gro=C3=9F?= , Ian Jackson , Wei Liu Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Allow to deregister the callback associated with a child death event. The death isn't immediate will need to be collected later, so the ev_child machinery register its own callback. libxl__ev_child_kill_deregister() might be called by an AO operation that is finishing/cleaning up without a chance for libxl to be notified of the child death (via SIGCHLD). So it is possible that the application calls libxl_ctx_free() while there are still child around. To avoid the application getting unexpected SIGCHLD, the libxl__ao responsible for killing a child will have to wait until it has been properly reaped. Signed-off-by: Anthony PERARD Acked-by: Ian Jackson --- Notes: v2: - Rename new fn to libxl__ev_child_kill_deregister - Rework documentation of the new API and if ev_child - Add debug log in libxl__ao_complete - Always call libxl_report_child_exitstatus() in child callback. tools/libxl/libxl_event.c | 6 ++++- tools/libxl/libxl_fork.c | 48 ++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 15 ++++++++--- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/tools/libxl/libxl_event.c b/tools/libxl/libxl_event.c index 0370b6acdd1c..43155368de76 100644 --- a/tools/libxl/libxl_event.c +++ b/tools/libxl/libxl_event.c @@ -1878,6 +1878,9 @@ void libxl__ao_complete(libxl__egc *egc, libxl__ao *a= o, int rc) ao->complete =3D 1; ao->rc =3D rc; LIBXL_LIST_REMOVE(ao, inprogress_entry); + if (ao->outstanding_killed_child) + LOG(DEBUG, "ao %p: .. but waiting for %d fork to exit", + ao, ao->outstanding_killed_child); libxl__ao_complete_check_progress_reports(egc, ao); } =20 @@ -1891,7 +1894,8 @@ static bool ao_work_outstanding(libxl__ao *ao) * decrement progress_reports_outstanding, and call * libxl__ao_complete_check_progress_reports. */ - return !ao->complete || ao->progress_reports_outstanding; + return !ao->complete || ao->progress_reports_outstanding + || ao->outstanding_killed_child; } =20 void libxl__ao_complete_check_progress_reports(libxl__egc *egc, libxl__ao = *ao) diff --git a/tools/libxl/libxl_fork.c b/tools/libxl/libxl_fork.c index eea3d5d4e68e..0f1b6b518c5c 100644 --- a/tools/libxl/libxl_fork.c +++ b/tools/libxl/libxl_fork.c @@ -678,6 +678,54 @@ int libxl__ev_child_xenstore_reopen(libxl__gc *gc, con= st char *what) { return rc; } =20 +typedef struct ev_child_killed { + libxl__ao *ao; + libxl__ev_child ch; +} ev_child_killed; +static void deregistered_child_callback(libxl__egc *, libxl__ev_child *, + pid_t, int status); + +void libxl__ev_child_kill_deregister(libxl__ao *ao, libxl__ev_child *ch, + int sig) +{ + AO_GC; + + if (!libxl__ev_child_inuse(ch)) + return; + + pid_t pid =3D ch->pid; + + ev_child_killed *new_ch =3D GCNEW(new_ch); + new_ch->ao =3D ao; + new_ch->ch.pid =3D pid; + new_ch->ch.callback =3D deregistered_child_callback; + LIBXL_LIST_INSERT_HEAD(&CTX->children, &new_ch->ch, entry); + ao->outstanding_killed_child++; + + LIBXL_LIST_REMOVE(ch, entry); + ch->pid =3D -1; + int r =3D kill(pid, sig); + if (r) + LOGED(ERROR, ao->domid, + "failed to kill child [%ld] with signal %d", + (unsigned long)pid, sig); +} + +static void deregistered_child_callback(libxl__egc *egc, + libxl__ev_child *ch, + pid_t pid, + int status) +{ + ev_child_killed *ck =3D CONTAINER_OF(ch, *ck, ch); + EGC_GC; + + libxl_report_child_exitstatus(CTX, XTL_ERROR, + "killed fork (dying as expected)", + pid, status); + ck->ao->outstanding_killed_child--; + libxl__ao_complete_check_progress_reports(egc, ck->ao); +} + /* * Local variables: * mode: C diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 6a614658c25d..4e433e110664 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -730,6 +730,7 @@ struct libxl__ao { libxl__poller *poller; uint32_t domid; LIBXL_TAILQ_ENTRY(libxl__ao) entry_for_callback; + int outstanding_killed_child; }; =20 #define LIBXL_INIT_GC(gc,ctx) do{ \ @@ -1155,9 +1156,14 @@ _hidden int libxl__ctx_evtchn_init(libxl__gc *gc); /= * for libxl_ctx_alloc */ * The parent may signal the child but it must not reap it. That will * be done by the event machinery. * - * It is not possible to "deregister" the child death event source. - * It will generate exactly one event callback; until then the childw - * is Active and may not be reused. + * The child death event will generate exactly one event callback; until + * then the childw is Active and may not be reused. + * + * libxl__ev_child_kill_deregister: Active -> Idle + * This will transfer ownership of the child process death event from + * `ch' to `ao', thus deregister the callback. + * The `ao' completion will wait until the child have been reaped by the + * event machinery. */ _hidden pid_t libxl__ev_child_fork(libxl__gc *gc, libxl__ev_child *childw_= out, libxl__ev_child_callback *death); @@ -1165,6 +1171,9 @@ static inline void libxl__ev_child_init(libxl__ev_chi= ld *childw_out) { childw_out->pid =3D -1; } static inline int libxl__ev_child_inuse(const libxl__ev_child *childw_out) { return childw_out->pid >=3D 0; } +_hidden void libxl__ev_child_kill_deregister(libxl__ao *ao, + libxl__ev_child *ch, + int sig); =20 /* Useable (only) in the child to once more make the ctx useable for * xenstore operations. logs failure in the form "what: