From patchwork Tue Oct 4 11:33:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 612216 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp106181pvb; Tue, 4 Oct 2022 04:35:07 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5RvIZ3Mj8+WXVsvsH0DyoLPyTiYOK2SyjI4mVFJnMxH5Ly+cM5Wvh16tIlVMh8wcVDaCc5 X-Received: by 2002:a05:620a:2584:b0:6ca:bf8e:5e2a with SMTP id x4-20020a05620a258400b006cabf8e5e2amr16560618qko.390.1664883307116; Tue, 04 Oct 2022 04:35:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664883307; cv=none; d=google.com; s=arc-20160816; b=BirkhBLG6HyCAX+3YpIaBhy36qm4zJeOvUZlU86IDRNesXwogb1LM62lgPm6rQ2wXz 5h35s4UyRulNUjUHrBI+8sI0d/CKgx3CdOHBSa1cwoOkkBMwkdugk7I11HEiTj/JHZ+j sznQNturb33qQLLUX9Eca/rxi4hRqDjYJXDLER6vKbT0DRSpd0+kiir8tiAsFMIZrFzZ ujs8FRfmPC5Xqkvw6Db7dQZa3g8wFBt+JaJRRWz3IKUXrvlQTGPW14djVdsqX94k6beV u4Ht2xFkJErBcGR/0Y4bUvdNZWlOS86Uvz+WFpzZh0rurvxkwOMRvgKZsNGTpAaju8B8 FPWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=oF7F6AhtybcPIaHi8qI4fAqedd4ySIwVuVC6Y4/Lx7A=; b=MapNatm6IU0b4i+/8kmsLPMlecXHxblpWPXJyyPqNW0JSAyOV0D9DGC/et1gzPRGh7 glo377EgqbtNIScH7bM6AUe8WJ8uSwMkSiY9A4o7O+SSwEZC0a3OQduEpRNUHjWTub2Y HpT/6nIKugO2MGPWFvQpX0gMthXRUgOYP0L2acem87L2HZ72ZqeZK6ZJhewtTBu0Ng5U kLpjWq6Lhlk7bnWWhIh3SRDXk55rv6QPtOxo+H1SXjvPn49MwCD2dA9wof9ccP69iD4P Y1cDTLA7legdOZ1XSO7ZpS+puAgfCh5b8qTCJo8AhEnZOqEX2LUwdnANENmzEYWnPzQ7 ZhYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WrYIBLhr; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id w15-20020a05622a190f00b0035d435e6b0fsi5655125qtc.96.2022.10.04.04.35.06 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 04 Oct 2022 04:35:07 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WrYIBLhr; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:42606 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ofgCY-0001D0-Jn for patch@linaro.org; Tue, 04 Oct 2022 07:35:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56798) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ofgB8-0001AP-J5 for qemu-devel@nongnu.org; Tue, 04 Oct 2022 07:33:38 -0400 Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]:41606) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ofgB6-000753-DK for qemu-devel@nongnu.org; Tue, 04 Oct 2022 07:33:38 -0400 Received: by mail-wr1-x431.google.com with SMTP id bu30so974116wrb.8 for ; Tue, 04 Oct 2022 04:33:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=oF7F6AhtybcPIaHi8qI4fAqedd4ySIwVuVC6Y4/Lx7A=; b=WrYIBLhrqBjMmGKNl9zifusAbtT0G/gw67S1LpCa1Bp9owLlNvj1po0ScuRoEGn+Gr Wsey3LFu7xS6nCXmx+6NOwQZXDKq9OokGoaY2PBpkmHA6494ZCmeRd1NFz6ifG6kYy7g puBPvjtD4s5EW/YKVTnQjpPVU1pvltEexh+kDvpdY88TfYF3dJkhlHiN5G2QS49lZ5GR OCAulOBiEchCQ3h27dTuHGDZVrAEw6rcgscwe4Bhwce6gMU6RPE6Pa8aQwUMhywUVV/a rQhi84y0X3N5O3LFOyWcxAWRhkfojMB2/IrVgQAIjLCz/PIMSrSbCcfNADRcaRFLkBo8 /EPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=oF7F6AhtybcPIaHi8qI4fAqedd4ySIwVuVC6Y4/Lx7A=; b=IMxq04EN6a84UgyvjsfJNsznLrnlMan2Lu76B5k/Ip+TWcPxkqmDb69AO2wyI6nMqz CDZv4Cz+OQqmjaDfYx4eEXmlOzuMQHBOKxDJoTsafrycSAGdLL1FEqCXA2c8m8ygk6j2 /z9zoQs7E52WWS47Xn4fJ9UsL3nt2RmN7ZHE7l9wOBM5EXUb48d1yGVJOnn5ZTuj3GNb aF7FwLUmlPEr7FgIINp9MvLd6dvuS1K6Mvy4retzkf7MBLA06pF81Ie+NrzEASogVf+b 569+2p9Ahkku1oSWQgZ02iV3lDhPSdBVxDrQSfyidwVqUZfqFxbfIlvosYvl0bAHx3RY 1rtQ== X-Gm-Message-State: ACrzQf2vCT3QM9d1hrsGvQV6StAMN28L94bU0ZXjHYI7pVxXTSxsK3Ps 7x5uRIiTqzTW2THEl9sI1aJy5w== X-Received: by 2002:a5d:6647:0:b0:22e:3ea3:e037 with SMTP id f7-20020a5d6647000000b0022e3ea3e037mr5438492wrw.581.1664883214412; Tue, 04 Oct 2022 04:33:34 -0700 (PDT) Received: from zen.linaroharston ([185.81.254.11]) by smtp.gmail.com with ESMTPSA id b3-20020adfee83000000b0022cc3e67fc5sm12181599wro.65.2022.10.04.04.33.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Oct 2022 04:33:33 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 3FE7D1FFB7; Tue, 4 Oct 2022 12:33:33 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: berrange@redhat.com, ncopa@alpinelinux.org, =?utf-8?q?Alex_Benn=C3=A9e?= , Laurent Vivier , Alexandre Iooss , Mahmoud Mandour Subject: [RFC PATCH] plugins: add [pre|post]fork helpers to linux-user [!TESTED] Date: Tue, 4 Oct 2022 12:33:30 +0100 Message-Id: <20221004113330.2167736-1-alex.bennee@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::431; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x431.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Special care needs to be taken in ensuring locks are in a consistent state across fork events. Add helpers so the plugin system can ensure that. Signed-off-by: Alex Bennée Fixes: https://gitlab.com/qemu-project/qemu/-/issues/358 Reviewed-by: Daniel P. Berrangé Tested-by: Daniel P. Berrangé --- include/qemu/plugin.h | 24 ++++++++++++++++++++++++ linux-user/main.c | 2 ++ plugins/core.c | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/include/qemu/plugin.h b/include/qemu/plugin.h index 145f8a221a..810c6b4588 100644 --- a/include/qemu/plugin.h +++ b/include/qemu/plugin.h @@ -224,6 +224,23 @@ void qemu_plugin_disable_mem_helpers(CPUState *cpu); */ void qemu_plugin_user_exit(void); +/** + * qemu_plugin_user_prefork_lock(): take plugin lock before forking + * + * This is a user-mode only helper to take the internal plugin lock + * before a fork event. This is ensure a consistent lock state + */ +void qemu_plugin_user_prefork_lock(void); + +/** + * qemu_plugin_user_postfork(): reset the plugin lock + * @is_child: is this thread the child + * + * This user-mode only helper resets the lock state after a fork so we + * can continue using the plugin interface. + */ +void qemu_plugin_user_postfork(bool is_child); + #else /* !CONFIG_PLUGIN */ static inline void qemu_plugin_add_opts(void) @@ -287,6 +304,13 @@ static inline void qemu_plugin_disable_mem_helpers(CPUState *cpu) static inline void qemu_plugin_user_exit(void) { } + +static inline void qemu_plugin_prefork_lock(void) +{ } + +static inline qemu_plugin_user_postfork(bool is_child) +{ } + #endif /* !CONFIG_PLUGIN */ #endif /* QEMU_PLUGIN_H */ diff --git a/linux-user/main.c b/linux-user/main.c index 88fccfe261..a17fed045b 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -142,10 +142,12 @@ void fork_start(void) start_exclusive(); mmap_fork_start(); cpu_list_lock(); + qemu_plugin_user_prefork_lock(); } void fork_end(int child) { + qemu_plugin_user_postfork(child); mmap_fork_end(child); if (child) { CPUState *cpu, *next_cpu; diff --git a/plugins/core.c b/plugins/core.c index c3ae284994..ccb770a485 100644 --- a/plugins/core.c +++ b/plugins/core.c @@ -526,6 +526,26 @@ void qemu_plugin_user_exit(void) qemu_plugin_atexit_cb(); } +/* + * Helpers for *-user to ensure locks are sane across fork() events. + */ + +void qemu_plugin_user_prefork_lock(void) +{ + qemu_rec_mutex_lock(&plugin.lock); +} + +void qemu_plugin_user_postfork(bool is_child) +{ + if (is_child) { + /* should we just reset via plugin_init? */ + qemu_rec_mutex_init(&plugin.lock); + } else { + qemu_rec_mutex_unlock(&plugin.lock); + } +} + + /* * Call this function after longjmp'ing to the main loop. It's possible that the * last instruction of a TB might have used helpers, and therefore the