From patchwork Wed May 27 14:54:54 2020 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: 11573161 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D814990 for ; Wed, 27 May 2020 15:05:05 +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 AE3B320899 for ; Wed, 27 May 2020 15:05:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="CbyLBUnv" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AE3B320899 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:50982 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jdxc8-00082F-Rn for patchwork-qemu-devel@patchwork.kernel.org; Wed, 27 May 2020 11:05:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44572) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jdxSc-00020J-FP for qemu-devel@nongnu.org; Wed, 27 May 2020 10:55:14 -0400 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]:54101) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jdxSa-0008FY-K5 for qemu-devel@nongnu.org; Wed, 27 May 2020 10:55:14 -0400 Received: by mail-wm1-x341.google.com with SMTP id l26so3322211wme.3 for ; Wed, 27 May 2020 07:55:12 -0700 (PDT) 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=O14rdwBIfGRpuBDr9I2ETfU73nIhdtOBuqe+I2h5Fx0=; b=CbyLBUnvLVk7vdoH626HCWP6G+l08I9ZsdW26iA2YbqsfQ9K03Q/X/ELDuMmcCLTZK y/MyE3XJdGZYEHk7cjHY+QJH6JuqRmKytepkgjaG/tZL5Tmhre6ghGxmWQD/hXTuF40l k+yZEP0Yoid+wLU5l67xStImgqjsqcu1LoxELQPE8ClcVj1U+uKQMAHlu/mgwR4AXR+r XBLfCWJuXPEAg+EavnAAVWAzHO/wo1DNAhTDN1EsadkmBmJmsTX0MinrgSN3WwcmEXOu DXtmudFqNKLLwwHEeDzwZjGkfLp7k/K6kYCVwmMhjErbEl4qPNLHat+NCTNlx086jhgE L4gA== 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=O14rdwBIfGRpuBDr9I2ETfU73nIhdtOBuqe+I2h5Fx0=; b=D149rrGpID8f1LTOz3WRdzKmw+B/lVILjVakx668Ujus9qRqDHo/IvaE1ENkPjqnon C47tjIRyZDGp4eW2pr/Z9mic3qmtDB1V9M8qqTDA8hfY1TQ8dgaMAxfIUDYihJr1SYZ9 gpnIzMyd3ysyI4IrvWDBI/UDek045aWYZAepzoTHmiOxZoG3lPoVc70/C6lMPu4PuE30 41W6gxeBvwV3uGnGrBkores2FYJ5VXxRB9qp2vC+BWE3U+NZNxyiz4wFBtcAZfC7l++y uoBCxPRViJ1oMz3y+TnXi1TknyOMMUqm10ge4Knazr3UfjgbwrjTH7m9+kmvLEisaWeZ RZ2Q== X-Gm-Message-State: AOAM532QQTAI2BMR2Q4YpVirkkpOMYmqyPqOZ2t5eyJ/zxCGr7cM5Tzz 7cmY9z+b9qpmNPxc+SPJ6BId8g== X-Google-Smtp-Source: ABdhPJzighiTBQBmB3/r0ZArkqDnYzocaI3U46wPGZ0wZWrE8NN4hw85nLHmSgHnH3sy17ximOOxeQ== X-Received: by 2002:a1c:4e0c:: with SMTP id g12mr4770362wmh.25.1590591311115; Wed, 27 May 2020 07:55:11 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id y4sm3103534wro.91.2020.05.27.07.55.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 May 2020 07:55:04 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 1507F1FF9A; Wed, 27 May 2020 15:54:57 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org Subject: [PULL 11/12] linux-user: properly "unrealize" vCPU object Date: Wed, 27 May 2020 15:54:54 +0100 Message-Id: <20200527145455.2550-12-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200527145455.2550-1-alex.bennee@linaro.org> References: <20200527145455.2550-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::341; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x341.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. 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_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN 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: "Daniel P . Berrange" , Eduardo Habkost , Nikolay Igotti , Markus Armbruster , Riku Voipio , qemu-devel@nongnu.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Paolo Bonzini , =?utf-8?q?Alex_Benn=C3=A9e?= , Laurent Vivier Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" We shouldn't be messing around with the CPU list in linux-user save for the very special case of do_fork(). When threads end we need to properly follow QOM object lifetime handling and allow the eventual cpu_common_unrealizefn to both remove the CPU and ensure any clean-up actions are taken place, for example calling plugin exit hooks. There is still a race condition to avoid so use the linux-user specific clone_lock instead of the cpu_list_lock to avoid it. Signed-off-by: Alex Bennée Reviewed-by: Philippe Mathieu-Daudé Cc: Nikolay Igotti Cc: Paolo Bonzini Cc: Daniel P. Berrange Cc: Eduardo Habkost Cc: Markus Armbruster Message-Id: <20200520140541.30256-14-alex.bennee@linaro.org> diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 05f03919ff0..7f6700c54e3 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -7635,30 +7635,33 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, return -TARGET_ERESTARTSYS; } - cpu_list_lock(); + pthread_mutex_lock(&clone_lock); if (CPU_NEXT(first_cpu)) { - TaskState *ts; + TaskState *ts = cpu->opaque; - /* Remove the CPU from the list. */ - QTAILQ_REMOVE_RCU(&cpus, cpu, node); + object_property_set_bool(OBJECT(cpu), false, "realized", NULL); + object_unref(OBJECT(cpu)); + /* + * At this point the CPU should be unrealized and removed + * from cpu lists. We can clean-up the rest of the thread + * data without the lock held. + */ - cpu_list_unlock(); + pthread_mutex_unlock(&clone_lock); - ts = cpu->opaque; if (ts->child_tidptr) { put_user_u32(0, ts->child_tidptr); do_sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX, NULL, NULL, 0); } thread_cpu = NULL; - object_unref(OBJECT(cpu)); g_free(ts); rcu_unregister_thread(); pthread_exit(NULL); } - cpu_list_unlock(); + pthread_mutex_unlock(&clone_lock); preexit_cleanup(cpu_env, arg1); _exit(arg1); return 0; /* avoid warning */