From patchwork Tue Jan 10 17:39:14 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: 640861 Delivered-To: patch@linaro.org Received: by 2002:a17:522:f3c4:b0:4b4:3859:abed with SMTP id in4csp2848355pvb; Tue, 10 Jan 2023 10:13:09 -0800 (PST) X-Google-Smtp-Source: AMrXdXs2YlckNonDY9UdNCoUkOwYhC4ncdO2EtaZUaL6FLNKYsgBtfVa8ubzQqqu9bTU8fWQ1E/R X-Received: by 2002:a81:780b:0:b0:4c5:2d62:96d6 with SMTP id t11-20020a81780b000000b004c52d6296d6mr13405152ywc.44.1673374389538; Tue, 10 Jan 2023 10:13:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673374389; cv=none; d=google.com; s=arc-20160816; b=JhceMJUO0rsS1VfWUJqg5RQBh2AOC092cvAGshtIbxALQ5fQMW9/E4fY/L2a9KROHl P2+p9zioNAirH94AB6n/e2/y+pMRLWbdIC84z7SFIAsj1/lvfj3fl+f49DYkyHsk+2jJ zFVyu9SGqnq8og/AdqwazEXTaKWgX9WoDhTEPNc+oGFrq68IZ1J7YYNjheigCFNDCTRY DcwPOhcTiCHIrZmwS/EIs3hlnF1Nn6kPdCPIXW0Of5IKaKf1tYZVOoA6aLPe2LhOCYg9 S2Zw5PMgkaILQfuUa0K7qt8z5S4S18JPUEEayKiO7nlaCl7BhMh97ZEszkbHa7l4H4vf CnEw== 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=CPdVE2hCd/x5Gl1DBAkwRbdDFsCMANSJgVBRMiSNU68=; b=rToaktrGTrYfPVfMWiFMD9f1iLvS3FyvRUcevXwpNr2Y2OjFMOhb3JKu5IEvnLYTdT 4V29fJMu8XSP4jrJVyY4i+3ioE8SywgDXxbMVJ4TOjDyt01NLLQGN7LCfA26yCvOylrB OWHtdy9TyzafTC2mJufw0t6Weld1hWmD4t8h43WVqN48O8lkdppzI56sh3vhw+Ck6G5B I1TxYqI4UylICME08rU7Bf2fPsqTr6ini+cjbatdaF/vRUjfOiMVNQ9tjRV/hY3M47Xo Tbxtpuo2cmwbx7SPc3YGJrdK/vAAj64aJkTdyxl+evDhn6FuAL6wDWJorXI1ouRprMld 6oLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Bsex59+y; 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 ESMTP id dv8-20020a05620a1b8800b00705b49de404si3978522qkb.675.2023.01.10.10.13.09 for ; Tue, 10 Jan 2023 10:13:09 -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=Bsex59+y; 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 1pFIb7-0003XU-L2; Tue, 10 Jan 2023 12:39:41 -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 1pFIb5-0003Uh-2U for qemu-devel@nongnu.org; Tue, 10 Jan 2023 12:39:39 -0500 Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pFIb0-0001Dp-Uf for qemu-devel@nongnu.org; Tue, 10 Jan 2023 12:39:38 -0500 Received: by mail-wr1-x431.google.com with SMTP id bn26so12618214wrb.0 for ; Tue, 10 Jan 2023 09:39:34 -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=CPdVE2hCd/x5Gl1DBAkwRbdDFsCMANSJgVBRMiSNU68=; b=Bsex59+yEYVB/GABeTSnBAQEghj28lXiJ0latzWwi6j4CigPrKz3uIF7jmGPFv/Gpm oWYDqR4M/UZqXEMRcFlsf/u4UHH0RHyj1dgsambVht3UnsavC9IIsvrLLrgg9OYJabtj /ZxIhIdr/NBBk8QrVrsnz39qSQweYFcnvqXyFf/4NVXNpbcHo+1V25TN3ukUDivCnNSe 3eesdanScDDznpyMs7uDzHetsPXiIutyQ4rlWHqsYF7VsjfouWjjNghQA2bH9yPNgcyw sPuBN7FGFfMc0xp/Mzz1GRUzUyLAvYXoRe6pz5id3aQIQ5ulT+ATjh907KadRU8lX++p ae/g== 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=CPdVE2hCd/x5Gl1DBAkwRbdDFsCMANSJgVBRMiSNU68=; b=4qDFHV/ifnzmRkLbDYv02fROamgBApvYHT2VMYdeSJlIw4DaR+RnJ1XFwChycndsDJ l1K//w5fb4P/zo8Gapis5AayYuVbu/4S8Hiq0yHl3MOMf8knrJoMuFuWkhIC9qBENEmc PmSSbKXwJNNBv+mSPd9V/zfxCMidfeRI5R8tDI/FSRX+LUJUhwD5lNlcNnnlyvVm/OVg bUuDvnUeeGqUaXaqDjTt3m/r7S3oyMGEb8rJDqCtD6Zy/0tI65yCdhO2VI+BI284/3+B xggjCa0jqxojtSP57YkAWJB3BTp4CTUNLg/4d/nx+4Nwh9RBzUbGHcS8R9qhKrNIBY6l V8og== X-Gm-Message-State: AFqh2krWYJ39zuNFP9mOGv9/dnHTDjTDqzksYFsU8MS2d8htV6kCEvqV GvND4i61iuaYDrKflc9+KnFPgw== X-Received: by 2002:a5d:5707:0:b0:2b9:d6ba:21f1 with SMTP id a7-20020a5d5707000000b002b9d6ba21f1mr12188374wrv.21.1673372374149; Tue, 10 Jan 2023 09:39:34 -0800 (PST) Received: from zen.linaroharston ([185.81.254.11]) by smtp.gmail.com with ESMTPSA id j15-20020a5d452f000000b0028f9132e9ddsm11737129wra.39.2023.01.10.09.39.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jan 2023 09:39:32 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 45F431FFC9; Tue, 10 Jan 2023 17:39:25 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Richard Henderson , Alexandre Iooss , Beraldo Leal , Thomas Huth , John Snow , Eduardo Habkost , Elena Ufimtseva , Ed Maste , Yanan Wang , Cleber Rosa , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Li-Wen Hsu , Markus Armbruster , Jagannathan Raman , =?utf-8?q?Daniel_P=2E_Berrang?= =?utf-8?q?=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Michael Roth , Wainer dos Santos Moschetta , =?utf-8?q?Alex_Benn?= =?utf-8?q?=C3=A9e?= , qemu-arm@nongnu.org, Marcel Apfelbaum , Peter Maydell , Paolo Bonzini , Mahmoud Mandour , John G Johnson , Emilio Cota Subject: [PATCH 18/26] cpu: free cpu->tb_jmp_cache with RCU Date: Tue, 10 Jan 2023 17:39:14 +0000 Message-Id: <20230110173922.265055-19-alex.bennee@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230110173922.265055-1-alex.bennee@linaro.org> References: <20230110173922.265055-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::431; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x431.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 Message-Id: <20230109224954.161672-2-cota@braap.org> Signed-off-by: Alex Bennée Reviewed-by: Richard Henderson --- accel/tcg/tb-jmp-cache.h | 1 + accel/tcg/cpu-exec.c | 3 +-- cpu.c | 11 ++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) 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 356fe348de..6bd29227f3 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); + } } /*