From patchwork Mon Nov 29 14:09:29 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: 519709 Delivered-To: patch@linaro.org Received: by 2002:ac0:c605:0:0:0:0:0 with SMTP id p5csp6141469imj; Mon, 29 Nov 2021 06:19:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJzn3HHhsASmu85Bbo7UDqIQN2V61s7m2sFoBbaEJDiBF/ZWUp7OoRqKfS/OKGvYl0wu37SX X-Received: by 2002:a05:600c:4f44:: with SMTP id m4mr37643429wmq.95.1638195540768; Mon, 29 Nov 2021 06:19:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1638195540; cv=none; d=google.com; s=arc-20160816; b=lpqdoUsLJBqfREqsQyOlbVRAI78mONtb325dy7yIKK16n9zFFepRBQTOpmE20DlIsR 68arbBmYpLN235FB/2ib/Poax5JRXls2HLMTR8QBqXhpmLZUYqny5x+Y/FSZdM1TPrIw DLrEX2a1go/IDi7EUbclWvbGYQIGDaTHXf7Kk9cBRFFz2kcvlZsqPMhrk06KOHwf++jy TJFxDSzp5/L2xVYFrd+sS5XkFXWYmZyMiPAY2M1+i6nhrd7zTXbxYrQpKzkenx6vz7lf wF2oUgs/L0Vene+I9uOLFmwUkKz201i9Dilj9g3kSg53zMD2C3I99Z0C+AWiPYhtWWJQ edOQ== 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=bUO0SB+BMTWj4cM859jVLA0edAg0vIZYqctggby2tKg=; b=I9IAluNS25t9KongnZcWDFO3Fe2vaC7ITaSmHb8ZseYopGJ74MxGdDLrP8tRUMmk0o Ei88lA5NZUH2fvvYC2bMKQc4CCD0lsVXVuQKsjSgHXqpxjuuz/wWewKQ6czo703NZQNM pURd/9rDREIghGiwFM7pU6r7Xy5nr7KKYqMkv+PJL0zJ6OSA2pO4VmEY1XrheiM2mTKw w1/MAZl9+aCuvg83ebTedi+KsNmq9LyrQdVe0rzDiUHC2/v9SEIp4jw/dvFTLdtTu3PL ZW8aGUCwiHfxBVx9/lhqrViAjlPxGWitjSModG0VJfBWz11TEDKANMuedQxvFgeSMfdu Juww== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=vBCptxwG; 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 f10si31058893wmg.131.2021.11.29.06.19.00 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 29 Nov 2021 06:19:00 -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=vBCptxwG; 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]:57990 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mrhUh-0005aH-TK for patch@linaro.org; Mon, 29 Nov 2021 09:18:59 -0500 Received: from eggs.gnu.org ([209.51.188.92]:56444) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mrhLo-0001Vh-TD for qemu-devel@nongnu.org; Mon, 29 Nov 2021 09:09:48 -0500 Received: from [2a00:1450:4864:20::42e] (port=39855 helo=mail-wr1-x42e.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mrhLm-0006Wh-R1 for qemu-devel@nongnu.org; Mon, 29 Nov 2021 09:09:48 -0500 Received: by mail-wr1-x42e.google.com with SMTP id a18so37122846wrn.6 for ; Mon, 29 Nov 2021 06:09:46 -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=bUO0SB+BMTWj4cM859jVLA0edAg0vIZYqctggby2tKg=; b=vBCptxwGyZhz+7Y8v8Q0Z2bHKlAj7rmBwo+/nSQALIhXMZakiGhxHXs8e5PLgeY/CD fcBzwfA/ySpJEx3QFx/B+d6EiQ/JuKwDV+mIeKGzeFwrkFsPeNs77bA9E6m01c08c4zA eA1xDfiGzHPOpyNhEdBc95Drx+h4hJpvkqGJAl2MgSfHf9GkaSy/m+iCgP5jIAI5RnQ3 YIrgunHiIdbpEb+i/BmNAYySHkWg8C2ApS8Q6GwgNVcdgBwKkEibV9Y/F724jTHJ+Xmb OAqcZRzBqiLrqDUcHzZyRrpRdg6iHiDRj4qp9If/zkckERTTgWS6kviNyYjjFdBUoTij YnWA== 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=bUO0SB+BMTWj4cM859jVLA0edAg0vIZYqctggby2tKg=; b=aVC6kgG0fIHEHfVmp4CG/JO8qfyXqzne0BQXW6FpWVlYddgy2y6wZHo5k8zk61E7YZ /UPUR30cR2OerBaIZzd6qYYJSCXXhPAQnAURMrGdjx0mnAiPUsSOaoduCaX8kDFJ9gTP YkuO5TWvSlcsqopBevZWjWvRxzvmVKL3UXmc4rZQ0Jg+zXpm06PPTjfS0P9iSqxDyrCF XnrkcVsR1dZ2jbN2MVlNkhXoAqU5H6aM9HnhN8nibs2C2lUepKJyFFAoTuOQgqJ7M+4+ BNOMcOe98li9xiyUAKS07I56lllGaYqQhmIJtH+uQX+PRbdpBIZQOihCPWR0ZOuTEgTV 8Kcg== X-Gm-Message-State: AOAM531y+18jPq5IPzPBkffa196YHFnVvQsXcV15z8Q3ZjBLxAw1Jrm5 TS+KZpadgx9MFynxhvVnaXR7Kg== X-Received: by 2002:adf:ef52:: with SMTP id c18mr34807909wrp.162.1638194985251; Mon, 29 Nov 2021 06:09:45 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id z6sm19448585wmp.9.2021.11.29.06.09.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Nov 2021 06:09:39 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 5770B1FF9C; Mon, 29 Nov 2021 14:09:33 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v3 5/8] gdbstub: handle a potentially racing TaskState Date: Mon, 29 Nov 2021 14:09:29 +0000 Message-Id: <20211129140932.4115115-6-alex.bennee@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211129140932.4115115-1-alex.bennee@linaro.org> References: <20211129140932.4115115-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::42e (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::42e; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x42e.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: fam@euphon.net, berrange@redhat.com, =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Richard Henderson , f4bug@amsat.org, stefanha@redhat.com, crosa@redhat.com, pbonzini@redhat.com, =?utf-8?q?Alex_?= =?utf-8?q?Benn=C3=A9e?= , aurelien@aurel32.net 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: <20211123205729.2205806-6-alex.bennee@linaro.org> --- 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