From patchwork Tue Sep 7 21:53:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 12479503 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80B54C433EF for ; Tue, 7 Sep 2021 22:26:08 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (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 0FB1B60C51 for ; Tue, 7 Sep 2021 22:26:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 0FB1B60C51 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=bsdimp.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:58882 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mNjXa-0007iT-0o for qemu-devel@archiver.kernel.org; Tue, 07 Sep 2021 18:26:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35468) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mNj2n-0006YU-Ht for qemu-devel@nongnu.org; Tue, 07 Sep 2021 17:54:17 -0400 Received: from mail-io1-xd30.google.com ([2607:f8b0:4864:20::d30]:38873) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mNj2l-0001AU-Ab for qemu-devel@nongnu.org; Tue, 07 Sep 2021 17:54:17 -0400 Received: by mail-io1-xd30.google.com with SMTP id a13so465091iol.5 for ; Tue, 07 Sep 2021 14:54:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CxhQtk7C61C53+7/JcUhiUB/ovxqI2TDiE1M4q5e7Do=; b=uLBx45n3Mk4FSjFHzeH+rkffSE3x/a0KGg3Z6tPQG3iC8LvrOlMlH/9E0Hs1cxDFk5 WMAG3rvETHODxyAzR0O07dX89chsI59C0QRTPExvVNeOeVq7pIytwx1u9vcY7PrdXZcO EoPtYnX0AcWa5h2UnpSiPe8lBdDUa8MuodRoyS5ky+YZQNqKzfAXRODbFGjeNwsgfR8a YkJZsUh8VXv9Qbfb80/vxx06yGOy2dvWKOPSIARQCHkL/4FQM21ILkgvNbom18hu0WWh MIhw8hcLgi2jlgYTLhXha8HEy/q8kFCk4X4CjnnLmC7ft1sVHjTeM0hY0JXg2oiTunqm xU5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CxhQtk7C61C53+7/JcUhiUB/ovxqI2TDiE1M4q5e7Do=; b=PwOrS9juurzp4B1XpEeW2e6BfYPGi6umE8UMw7yQyiq5XSgSn06IqPtfjrlTXX0Tpj v/pj6gPru7En6gpfgiacUnbB7jINm62er0FpwJF2i8ISXgIxewn/FpJoqOvvMeZlTogw Ja9T1zcVphGSGuBf5HzoiKSezk1iQDH+wx3l4QSko1be88EEuOAv/vOnMEEVVtFLoDDF +QTZT9miLl/not/Nj8AUq/w+tFfalcvPsH/ML4at2UFzyuRbJkAIwvm1twPurNN+5T18 Fm0weD3WZfVqfQaMzJmkVbdf1+/Wz7w+INiVCllPL6NnRSE5IVmtgany4YOeCOsbT+pc nuXA== X-Gm-Message-State: AOAM5300zRcFjRQL2B2hP3G7f0s85pbWHD7gmMIrMaSFKdua8xwW+jEQ nKm9ZMYAKZk14a2OO20I0ELChP9vj+qIJf/8gSU= X-Google-Smtp-Source: ABdhPJx1F3U3eOZaCytAdJUUzH/43Aie9yZqOzzQsVr6NPa294uiP9GCN5DBs8uuWmvoEYM31n0QyQ== X-Received: by 2002:a02:90d0:: with SMTP id c16mr468011jag.106.1631051654110; Tue, 07 Sep 2021 14:54:14 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id t17sm192477iln.24.2021.09.07.14.54.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Sep 2021 14:54:13 -0700 (PDT) From: imp@bsdimp.com To: qemu-devel@nongnu.org Subject: [PULL 40/42] bsd-user: Implement interlock for atomic operations Date: Tue, 7 Sep 2021 15:53:30 -0600 Message-Id: <20210907215332.30737-41-imp@bsdimp.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210907215332.30737-1-imp@bsdimp.com> References: <20210907215332.30737-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::d30; envelope-from=imp@bsdimp.com; helo=mail-io1-xd30.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevans@freebsd.org, Richard Henderson , Warner Losh , Warner Losh , Stacey Son Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: Warner Losh Implement the internlock in fork_start() and fork_end() to properly cope with atomic operations and to safely keep state for parent and child processes. Signed-off-by: Stacey Son Signed-off-by: Warner Losh Reviewed-by: Richard Henderson --- bsd-user/main.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/bsd-user/main.c b/bsd-user/main.c index 768f4ee41e..659dd2b512 100644 --- a/bsd-user/main.c +++ b/bsd-user/main.c @@ -69,15 +69,39 @@ unsigned long target_dflssiz = TARGET_DFLSSIZ; /* initial data size limit */ unsigned long target_maxssiz = TARGET_MAXSSIZ; /* max stack size */ unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount to grow stack */ +/* Helper routines for implementing atomic operations. */ void fork_start(void) { + start_exclusive(); + cpu_list_lock(); + mmap_fork_start(); } void fork_end(int child) { if (child) { + CPUState *cpu, *next_cpu; + /* + * Child processes created by fork() only have a single thread. Discard + * information about the parent threads. + */ + CPU_FOREACH_SAFE(cpu, next_cpu) { + if (cpu != thread_cpu) { + QTAILQ_REMOVE_RCU(&cpus, cpu, node); + } + } + mmap_fork_end(child); + /* + * qemu_init_cpu_list() takes care of reinitializing the exclusive + * state, so we don't need to end_exclusive() here. + */ + qemu_init_cpu_list(); gdbserver_fork(thread_cpu); + } else { + mmap_fork_end(child); + cpu_list_unlock(); + end_exclusive(); } }