From patchwork Mon Nov 29 17:14:46 2021 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: 519716 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp6385479imj; Mon, 29 Nov 2021 09:16:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJznJkzTmNeNtw6Ow5Meag5+bNO9XYCwvYW5R1emj02thqbun7KYi5JfdIk+UoGET1xN2DaZ X-Received: by 2002:a05:6402:5246:: with SMTP id t6mr75117661edd.18.1638206201675; Mon, 29 Nov 2021 09:16:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1638206201; cv=none; d=google.com; s=arc-20160816; b=KMzZTcgBfhBY/nLhy/jogBJGuFUIdEc91yx5dAx8qsZbkqMzcJ2OeT5ER+r/zrzWCg dfJX1yUZUwH5e7AkAFmEURNLZa9gmX49UWB5a++5O4sS6EOyzh4fZUKT6CWedH5UAxHm wAMdRRiRRj0Zsu5cA4XhbWsH79FawqamGgoJFiaJqtlGnABIFKEpVQgkGPzLSRgVvFAK UIfFpOfmJAc13nUYmdHNOWNLhytfwZ4r8ZIksZ8eXgLh/8b3kAb3eQzRmuI1TLP+YrbA RRX3BkuZGaAgJUCxCgxXZ5mr5bi1W4XtZk+WXYb5tYQzDKqJoQ/6v9Up8Hy8svGRu/0s +rgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=x+l21wdy++j9QmwvxPd2IqpwqliVMr1XUKlqo6h12D4=; b=HGPLuqTJ5AAgyDNRKeFBYS4y+r5afhUHQHh3eT0L5f8XEQzt96VqBw6Kbck7zbezuA v23G55NnLAKQnbXy4lgDsX0/HsThZIbWM5tUbinQLNpBV2UTeMZEkP/l57wawGQD4lzJ TEA4tzN2e+CusOSQQRFUFGKjczNU1PQfuYgBLmH2tr0yr4pFWLaoGEiEMris4YO9k9l2 jREC6Srb4zDLq9TUpzFXiv0OLY5V9jMxcK+ngtizRBVy2ZBS4nsSnde7sdqtMOXUQIwZ 7GC2jMgCjxzQve7hngopAh7xOs35wuWjqQU3PTjgFoFCtdeRBdPjBwuqz7aVjyKJicOt 6EBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=DRQn2yd5; 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=fail (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 l18si28610738edb.148.2021.11.29.09.16.41 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 29 Nov 2021 09:16:41 -0800 (PST) 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=fail header.i=@linaro.org header.s=google header.b=DRQn2yd5; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:46456 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mrkGe-0002Yj-PE for patch@linaro.org; Mon, 29 Nov 2021 12:16:40 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57106) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mrkF4-0002UO-Ig for qemu-devel@nongnu.org; Mon, 29 Nov 2021 12:15:02 -0500 Received: from [2a00:1450:4864:20::333] (port=34451 helo=mail-wm1-x333.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mrkF2-0001UA-2r for qemu-devel@nongnu.org; Mon, 29 Nov 2021 12:15:02 -0500 Received: by mail-wm1-x333.google.com with SMTP id m25-20020a7bcb99000000b0033aa12cdd33so6308984wmi.1 for ; Mon, 29 Nov 2021 09:14:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=x+l21wdy++j9QmwvxPd2IqpwqliVMr1XUKlqo6h12D4=; b=DRQn2yd5NGsW0YIqsADrsxZlhGReayCc+sMSS6Vwo1t4//eNJa6hianzHRF6QSM5E1 WL7ru5ouYW3EWvHO1pb/yvqP+irqS6dPL4Ji5hXzleecsVFP3j6C5dwn3TXNY17ZwmDu v1mHxQaC+/gQTzqt8IVBEgej6r63dWlHJFQoUQVRII5ii+kdFcs81J+/BhWGLuu7z9b2 LAvhNJ6D6puYt9l0cZyLTEUj/MlsaesM54hHJee1Bpm1l9gZW+DzKczw+dRFqlSSeihX 2dMw4JilL/87tK+sNnnrh4xho50sQy9BLp8NRnCwrmJBcsAYY5zH09QGq+Yq7m2Q4tTX TR/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=x+l21wdy++j9QmwvxPd2IqpwqliVMr1XUKlqo6h12D4=; b=IHzeZeLXsbgxFK0808GtSzl1X+R3+kA7Rd7bLxjQCC8pZ4iGIQMsTIimQ9GZn//tGO DZBQUlojLziPuYHVWggwbYSHWo08ApMnhhGHfRdGhnMJb67wLoWAXuQ7RDQ9GWX7wk8/ qFCV9PWRS929Hebx0LiQzOv+eJ+a0xsezUDd+0scIaFwrNjb4nMrBG43zV3bzRVydE+u xuzm9SlHizUflTgGLth2+khPvnEcSCfY6LBP2qpS56gTGYhi6GZfeZ1sbru/xvUER4eS IWbJb4G3to0j8la2GnnBffYXwgxMJILBzBWpkRBVUOOhNKVXQe9Zr7afJPApXl3Zw33E aWew== X-Gm-Message-State: AOAM533pA90XBPh4ae5D4MpETlODyijJAqeO1KeCPI24xFXaZR2UHYVh vdHmQS+3BPtBH9u7uQpquSiZ5Q== X-Received: by 2002:a1c:a58d:: with SMTP id o135mr37524229wme.93.1638206098815; Mon, 29 Nov 2021 09:14:58 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id t8sm17726614wmq.32.2021.11.29.09.14.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Nov 2021 09:14:55 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 38DC01FF9C; Mon, 29 Nov 2021 17:14:50 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: richard.henderson@linaro.org, peter.maydell@linaro.org Subject: [PULL 5/8] gdbstub: handle a potentially racing TaskState Date: Mon, 29 Nov 2021 17:14:46 +0000 Message-Id: <20211129171449.4176301-6-alex.bennee@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211129171449.4176301-1-alex.bennee@linaro.org> References: <20211129171449.4176301-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::333 (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::333; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x333.google.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 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, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: , Cc: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , =?utf-8?q?A?= =?utf-8?q?lex_Benn=C3=A9e?= , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" When dealing with multi-threaded userspace programs there is a race condition with the addition of cpu->opaque (aka TaskState). This is due to cpu_copy calling cpu_create which updates the global vCPU list. However the task state isn't set until later. This shouldn't be a problem because the new thread can't have executed anything yet but the gdbstub code does liberally iterate through the CPU list in various places. This sticking plaster ensure the not yet fully realized vCPU is given an pid of -1 which should be enough to ensure it doesn't show up anywhere else. In the longer term I think the code that manages the association between vCPUs and attached GDB processes could do with a clean-up and re-factor. Signed-off-by: Alex Bennée Tested-by: Richard Henderson Reviewed-by: Richard Henderson Cc: Richard Henderson Resolves: https://gitlab.com/qemu-project/qemu/-/issues/730 Message-Id: <20211129140932.4115115-6-alex.bennee@linaro.org> diff --git a/gdbstub.c b/gdbstub.c index 23baaef40e..141d7bc4ec 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -94,7 +94,7 @@ static inline int cpu_gdb_index(CPUState *cpu) { #if defined(CONFIG_USER_ONLY) TaskState *ts = (TaskState *) cpu->opaque; - return ts->ts_tid; + return ts ? ts->ts_tid : -1; #else return cpu->cpu_index + 1; #endif