From patchwork Thu Jan 26 11:22:41 2023 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: 647104 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp188241pvb; Thu, 26 Jan 2023 03:30:53 -0800 (PST) X-Google-Smtp-Source: AMrXdXvXmpA+WXwSp6uRskI116NAdAjK/Y5tflKN10FERe4maTJOkWtfUDVj/rzjCTWKburIJFS5 X-Received: by 2002:a0c:bed2:0:b0:534:26eb:a25d with SMTP id f18-20020a0cbed2000000b0053426eba25dmr50308342qvj.40.1674732653733; Thu, 26 Jan 2023 03:30:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674732653; cv=none; d=google.com; s=arc-20160816; b=qZoaCYQrMqixeQgN2zv5Bg692zItgckERY+iasGFNmIJUAJwe6eboa0kASSxz8LXT2 XTLfOoNFbZu0kvGLFO3D53oSFcsE5BpjEVXigOFatc48LVMQ+Lc+SEcEJJHnzRDW246y ahUKsbpDSdZwiLPg3k2wNMdZC9thAFviZplK4uPh4wz1ljcniCVzGLTklZL/FDYP1NLU 8leiuUYlU7Jcgr6SBTi+r1BUYpG4zaONkCaj5BQa5gMs22OcNAsBW4h+aZ5Eu8/m+7gU /bGn7SpToVzdrDN5IwQTbeXoFM6gnPs1vZRKo72AlS/IeW5+MglKwc0e0LFZHVajx7x1 /3dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to :from:dkim-signature; bh=lJmfcRUvWgySiHRRzSTiq8BtkDV3N+un1/AMo4mWGPE=; b=q1iLjrpbFza/3aiwLCZrAbxceWSuY5W5dKiiAi3UXD6rJ4yV062xiaTea3BWq6uuBS Nbt7vkJOR4SdFLTE2OKNCOGSClo0lREYmplLHqk/+UMm+wCRDmfX0h+opM2AjHliPjhc bDCR/d9ZqBsBLUS5QgAZj6Q2es2rkzmWJCiqSrmMJsr+7xmGkYxhAwX9Ra67JHuEwAyo 98XC9k6VmlvY1+AKowJ6DlO5fnLwQzg4G7WF398iwYRfAzeXbG5GoJ2C9T27ZPi8JL3J G2nIm+3/9x4Agf3Hl45BHNkYMWPjE5InVyblXL5ZfiwIxy51gw+m5A3aQ85t59mRr9kh RPBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=lgcJTr0X; 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=pass (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 a17-20020ad441d1000000b0053519e70990si627943qvq.153.2023.01.26.03.30.53 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 26 Jan 2023 03:30:53 -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=pass header.i=@linaro.org header.s=google header.b=lgcJTr0X; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pL0Lg-00047S-FL; Thu, 26 Jan 2023 06:23:20 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pL0LW-00032u-Ho for qemu-devel@nongnu.org; Thu, 26 Jan 2023 06:23:10 -0500 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pL0LU-0004MV-Fb for qemu-devel@nongnu.org; Thu, 26 Jan 2023 06:23:10 -0500 Received: by mail-wr1-x429.google.com with SMTP id m14so969117wrg.13 for ; Thu, 26 Jan 2023 03:23:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lJmfcRUvWgySiHRRzSTiq8BtkDV3N+un1/AMo4mWGPE=; b=lgcJTr0XRYnZNZfaLecz5ubSSNps8VZmMdDRoDXucd4Buo5d1cQ0sHY1wpLCETngde hsD2wm/65/w9mQMlu01nzU1mxVYhc+MvzjNKN4DQczXiH8vhCBHunxnuH0gOatidVPHc KGau5cLcA3Kiy/Fba2IKHjeDOThi4wsVCFw+dcN/LJGJGgNL0coUgZywa8ZwZrH/Nkiq EIZRzMx3iRiXudN04B1bKTSDj8rQ3N+V2Hb/LfpjibPczB2PMoPxtSCD9VJ9wzXCdMeC pjXX1XotWc+0AM4RXkl5/L4XVi+bILUlY8Q5jUUiGE0br197cB39NJb0x/oaojHSuniJ c3Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lJmfcRUvWgySiHRRzSTiq8BtkDV3N+un1/AMo4mWGPE=; b=glSdj6nbgeu2WYHSjiqLxzBESR1k5lG+kB+8nLDXEN/uu0HFFrBjn5sw1sccQYGarg piJgQRyEiNR+wQXAnnNN1wlJez+U2RxVSSfF+FYn4R4ZqKDGdF+D2RM8fCEYswtWw0by MLZEWwBmNKlvwB1rTWjTrly/VTn9OTy7/8N6wfmTV+S/MZzUm/ltOaJbUXhGP2u5b4fA RQ2jg8Xco+JkdmsfuVjvzWdm0/8eZXAtJ1QO+bgOjXnAbhla7vT3eDJFF2syYQWp8GB5 Kp5hln77BFQ8BrXzkKN8Y0TAC8P3Ve1cfCk1g+p/OwUbO0VtbpL3UYM9v7qt29dnP3Nc l39g== X-Gm-Message-State: AFqh2kqfO/CYjVJ8vPt6jAXTOhcouVzK5/SQYZqjTAXRug1H+wqjRcMK nq5B7CdmrI+l8bl8E0PcCPJl2A== X-Received: by 2002:a5d:6d41:0:b0:2bb:328b:a66d with SMTP id k1-20020a5d6d41000000b002bb328ba66dmr30425971wri.60.1674732186648; Thu, 26 Jan 2023 03:23:06 -0800 (PST) Received: from zen.linaroharston ([185.81.254.11]) by smtp.gmail.com with ESMTPSA id f12-20020a5d4dcc000000b002be5becdb89sm1067929wru.3.2023.01.26.03.22.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 03:23:01 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 5AFD51FFCA; Thu, 26 Jan 2023 11:22:58 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, Emilio Cota , Richard Henderson , =?utf-8?q?Alex_Benn=C3=A9?= =?utf-8?q?e?= , Paolo Bonzini , Eduardo Habkost , Marcel Apfelbaum , =?utf-8?q?Philippe_Mathieu-D?= =?utf-8?q?aud=C3=A9?= , Yanan Wang Subject: [PULL 26/35] cpu: free cpu->tb_jmp_cache with RCU Date: Thu, 26 Jan 2023 11:22:41 +0000 Message-Id: <20230126112250.2584701-27-alex.bennee@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230126112250.2584701-1-alex.bennee@linaro.org> References: <20230126112250.2584701-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x429.google.com 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_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Emilio Cota Fixes the appended use-after-free. The root cause is that during tb invalidation we use CPU_FOREACH, and therefore to safely free a vCPU we must wait for an RCU grace period to elapse. $ x86_64-linux-user/qemu-x86_64 tests/tcg/x86_64-linux-user/munmap-pthread ================================================================= ==1800604==ERROR: AddressSanitizer: heap-use-after-free on address 0x62d0005f7418 at pc 0x5593da6704eb bp 0x7f4961a7ac70 sp 0x7f4961a7ac60 READ of size 8 at 0x62d0005f7418 thread T2 #0 0x5593da6704ea in tb_jmp_cache_inval_tb ../accel/tcg/tb-maint.c:244 #1 0x5593da6704ea in do_tb_phys_invalidate ../accel/tcg/tb-maint.c:290 #2 0x5593da670631 in tb_phys_invalidate__locked ../accel/tcg/tb-maint.c:306 #3 0x5593da670631 in tb_invalidate_phys_page_range__locked ../accel/tcg/tb-maint.c:542 #4 0x5593da67106d in tb_invalidate_phys_range ../accel/tcg/tb-maint.c:614 #5 0x5593da6a64d4 in target_munmap ../linux-user/mmap.c:766 #6 0x5593da6dba05 in do_syscall1 ../linux-user/syscall.c:10105 #7 0x5593da6f564c in do_syscall ../linux-user/syscall.c:13329 #8 0x5593da49e80c in cpu_loop ../linux-user/x86_64/../i386/cpu_loop.c:233 #9 0x5593da6be28c in clone_func ../linux-user/syscall.c:6633 #10 0x7f496231cb42 in start_thread nptl/pthread_create.c:442 #11 0x7f49623ae9ff (/lib/x86_64-linux-gnu/libc.so.6+0x1269ff) 0x62d0005f7418 is located 28696 bytes inside of 32768-byte region [0x62d0005f0400,0x62d0005f8400) freed by thread T148 here: #0 0x7f49627b6460 in __interceptor_free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52 #1 0x5593da5ac057 in cpu_exec_unrealizefn ../cpu.c:180 #2 0x5593da81f851 (/home/cota/src/qemu/build/qemu-x86_64+0x484851) Signed-off-by: Emilio Cota Reviewed-by: Richard Henderson Message-Id: <20230111151628.320011-2-cota@braap.org> Signed-off-by: Alex Bennée Message-Id: <20230124180127.1881110-27-alex.bennee@linaro.org> diff --git a/accel/tcg/tb-jmp-cache.h b/accel/tcg/tb-jmp-cache.h index ff5ffc8fc2..b3f6e78835 100644 --- a/accel/tcg/tb-jmp-cache.h +++ b/accel/tcg/tb-jmp-cache.h @@ -18,6 +18,7 @@ * a load_acquire/store_release to 'tb'. */ struct CPUJumpCache { + struct rcu_head rcu; struct { TranslationBlock *tb; #if TARGET_TB_PCREL diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c index 04cd1f3092..25ec73ef9a 100644 --- a/accel/tcg/cpu-exec.c +++ b/accel/tcg/cpu-exec.c @@ -1064,13 +1064,12 @@ void tcg_exec_realizefn(CPUState *cpu, Error **errp) /* undo the initializations in reverse order */ void tcg_exec_unrealizefn(CPUState *cpu) { - qemu_plugin_vcpu_exit_hook(cpu); #ifndef CONFIG_USER_ONLY tcg_iommu_free_notifier_list(cpu); #endif /* !CONFIG_USER_ONLY */ tlb_destroy(cpu); - g_free(cpu->tb_jmp_cache); + g_free_rcu(cpu->tb_jmp_cache, rcu); } #ifndef CONFIG_USER_ONLY diff --git a/cpu.c b/cpu.c index 4a7d865427..21cf809614 100644 --- a/cpu.c +++ b/cpu.c @@ -176,11 +176,20 @@ void cpu_exec_unrealizefn(CPUState *cpu) vmstate_unregister(NULL, &vmstate_cpu_common, cpu); } #endif + + /* Call the plugin hook before clearing cpu->cpu_index in cpu_list_remove */ if (tcg_enabled()) { - tcg_exec_unrealizefn(cpu); + qemu_plugin_vcpu_exit_hook(cpu); } cpu_list_remove(cpu); + /* + * Now that the vCPU has been removed from the RCU list, we can call + * tcg_exec_unrealizefn, which may free fields using call_rcu. + */ + if (tcg_enabled()) { + tcg_exec_unrealizefn(cpu); + } } /*