From patchwork Fri Nov 19 14:51:24 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: 519242 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp2624313imj; Fri, 19 Nov 2021 06:56:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJzBvuqN7NXsM02Echy25DFm32n3PmSjR1117cjMkzTDbaE2GHyyOp4/fM7LzIE29mB9r4xf X-Received: by 2002:a05:622a:346:: with SMTP id r6mr6750139qtw.185.1637333788630; Fri, 19 Nov 2021 06:56:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637333788; cv=none; d=google.com; s=arc-20160816; b=LPPSlbOO9ZGzis841uyB20xT7i9lkkyx3ssNUisINu826ouQZWCVxy3aj8JPsSNd0+ PODRGeIvM/AzpC3NNM2B6rL+AMpclrlYKCmrGSkP7HYT+mqwNiWIr1FxZLhCKGlK61hM 4pjXiwqDu4ow6d2rRDA6EdKNxSa0tSLUZq6d5SlCYEuQYSm+tOh3oK+RoOpCRIdUw2QT Ym4awuacQiJ8JEz4F9CCyy8Mum/bx2pXlJsYTvy2BVNqY7Bar5B1ddZG5SDzzrFEs98K pAmczEzFRBxcsRxwpOFnceG4eK22LjptXmwMrbBIVO+HLEy6XDWhR6f4FfNrR6TMEVPa 37ug== 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:message-id:date:subject:to:from:dkim-signature; bh=szUAPFGBvZnTdl62g3KF4aXspJL5YUpZ4qjN2UnzAC8=; b=Mp0w/9ob18qDCvAF0UL3WoZ74szYF4N3iz338br7zbLpgEt1gexqsMdwbKVKCK5tb0 6+qN4GGDGmGPGrXtNcN07NLkjDj+arfLUmd0ti7cFsY67O5KrLFTI/bSBbNEW6WbughM nW8O1UbGcw5624B70igaWX8zN9m9Bk860e3IvwEsMjuTuA3Ly9BqbpXEInQVoyVfeMIR xJrteQQTlT+41s/bTPZsYxI4FG4IqRJBvMuEfDfCRQ2Yk8Q/PQcy2eSZywt43STMx5Se +SnbtkXJe/Caz3tkKI02p6TKOvI2ajj3SLedYoAz124u+FEyxtvchuCohJ7wHFmju0Ai 8dng== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=EFVSHhMK; 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 o5si57831qkp.16.2021.11.19.06.56.28 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 19 Nov 2021 06:56:28 -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=EFVSHhMK; 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]:42022 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mo5JU-0000hy-4g for patch@linaro.org; Fri, 19 Nov 2021 09:56:28 -0500 Received: from eggs.gnu.org ([209.51.188.92]:49544) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mo5Eo-00037M-7S for qemu-devel@nongnu.org; Fri, 19 Nov 2021 09:51:38 -0500 Received: from [2a00:1450:4864:20::42d] (port=44982 helo=mail-wr1-x42d.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mo5Em-0006Ft-Fl for qemu-devel@nongnu.org; Fri, 19 Nov 2021 09:51:37 -0500 Received: by mail-wr1-x42d.google.com with SMTP id n29so18547449wra.11 for ; Fri, 19 Nov 2021 06:51:36 -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:mime-version :content-transfer-encoding; bh=szUAPFGBvZnTdl62g3KF4aXspJL5YUpZ4qjN2UnzAC8=; b=EFVSHhMKHzX7tbibsT5MAdkOBkz9+rcNYeSZ7XNIa5zFGCzMAfABIW616HRv6mEwZE Hn21O3iFR0u/szHUaQcvLXnQT6eNEjdrsHTbpIT6e1AWfRdkfLo3bcDwnqY9LhPClcTN GON5J0bmIjs0HpjSOMQq9QdrG/f18LQiiYIXSx8UvHEPR/6Mq5YhpM1WkQRt7k5Qz5Ac +op9Ewwdwn7/tnr7WC9LEuM440PfNo7FT+jWauSljm7UZE+tjBGfZNpxuf/w00PiUQgi WB9RxepS8eLxRjVPOz04i2/5XrDfGDCnVrgZmBnRYVhUi/LIPu6IS1ckPRPd/NHShrWq DfTg== 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:mime-version :content-transfer-encoding; bh=szUAPFGBvZnTdl62g3KF4aXspJL5YUpZ4qjN2UnzAC8=; b=glstiDY5EibLX9C3Jp0jU81FCIdO5ybi/O84gpIUs91wTw3qUyWziTRh3JIcsGbgZ5 uPy3rPjiEwizhfFsydKhkui5UwnGbvjD7AV8lkiQ2JqOzNRynId4adIAHutD1NhuqtQf 1EtigqCYM6wWGvgB5zD7Q2KjUYteMuO8rltzbNTmNumICS6bOzSzDFFK+dqM425JOIA2 1KJZ1Zjh7a0F0KkxvgKdctIQMn183EavLZt7YiyN7SjcAHVxsgfR4r0n++gti6MjWxQF bN2o0wLaeiFwShARGkNPVJUSnAYiFx+0RkmdQFInP8JtKDxHAjtl1WBDvojlQBw4V6HO gPeA== X-Gm-Message-State: AOAM532g8+rPRQkrRCyMNqFDPeP7VfowB7Ltf/igcj1K/ATCt5pMSN5f 9bE4/0eDhZojrrGvzbJAhuWLsw== X-Received: by 2002:adf:f7d2:: with SMTP id a18mr8277502wrq.354.1637333494737; Fri, 19 Nov 2021 06:51:34 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id u15sm3748122wmq.13.2021.11.19.06.51.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Nov 2021 06:51:33 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id E89091FF96; Fri, 19 Nov 2021 14:51:32 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [RFC PATCH] gdbstub: handle a potentially racing TaskState Date: Fri, 19 Nov 2021 14:51:24 +0000 Message-Id: <20211119145124.942390-1-alex.bennee@linaro.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::42d (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::42d; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x42d.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: Richard Henderson , =?utf-8?q?Alex_Benn?= =?utf-8?q?=C3=A9e?= , =?utf-8?q?Philippe_Mathieu-Da?= =?utf-8?q?ud=C3=A9?= 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 Cc: Richard Henderson Resolves: https://gitlab.com/qemu-project/qemu/-/issues/730 Tested-by: Richard Henderson Reviewed-by: Richard Henderson --- gdbstub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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