From patchwork Tue Sep 12 06:57:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 1832714 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=OpCt3XPK; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RlDx256m5z1yhL for ; Tue, 12 Sep 2023 17:00:06 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qfxLy-0000OP-AO; Tue, 12 Sep 2023 02:58:30 -0400 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 1qfxLv-0000MR-Nc for qemu-devel@nongnu.org; Tue, 12 Sep 2023 02:58:27 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qfxLt-0007TO-Fu for qemu-devel@nongnu.org; Tue, 12 Sep 2023 02:58:27 -0400 Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-26f9521de4cso3711317a91.0 for ; Mon, 11 Sep 2023 23:58:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1694501904; x=1695106704; darn=nongnu.org; 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=2Hs61unU0rT5Xs/IozPbclEmnlWYOJEOWqFw7vuLJvQ=; b=OpCt3XPKFoXqE1ljaRunsk/2+xlEl6ENjGFMBQGmJm/C/oFT19DZd2x/eH4BtAdIj1 p12a1yMV7QOa7NM7Uz0+rrzsMRpRew2xTuZQzU84eNKag4Z51FnOB9TBl9h8dLuMobOg Sn1Nf5ZgHmpGIp9Y4lEMqpxdbosD3uWbCz7IxFSlkUPKc4XruBnWTUIBtMMvOWYOej9T uV/I9ZQwz4PbCCHmSUEZqT/KqGsQgL3ukqsX00Yqtnm+QS9X8jJ95VZOH5DlFnNZZTLR bPFHzwYU27DCGbysLhXAqZ3CUP3xj0xxN46V/gpYW9iCr8ULUwAmfIpFeAYRBoyfzjHn Md/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694501904; x=1695106704; 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=2Hs61unU0rT5Xs/IozPbclEmnlWYOJEOWqFw7vuLJvQ=; b=n9UuBYvrFM/CnT/R1OHy7RnhqTWMgV0gRR2ev7PtzCAaAMp8Bbm8Bvp2gvpN9UQMBf qdMp7bFigvSQGyZlvREzY8bLgQpXMnCSCccPR3l7V1U8+jnVynYTT+1pH2Gp2XcBFj/Y RD5O3rWokx3jKNBVAmqxs54kN525odV/FtGIa75+H9HzzjXzC8iWkzXW4fP+61LTdA4B 2nHBrOepkeIRyBnH8V20d1c+PwdlEbKGMDejaKprphTxu1aWWtLduKDKzrNnbB4umbrL fBxISDlRsJKhuqJmrGvdPQfNp9XgCiyOaNaWkDHuWt1FxrX1zHn9XWZFLTsC12wejD16 dRxA== X-Gm-Message-State: AOJu0YykyVEuaYAEYC/UUTQbT4oFhmX6Q1Zj7iSku5PwyrDZ1iXWeS31 kKHN4KFdADdynhCzSDjgo5o+RpjXmlDAsAVEwik= X-Google-Smtp-Source: AGHT+IHnFOoC4oAXqxcReWOWhhKQZok/pSMDGoyXh3WwLPNnP8r6kYmINNfWWF7ow+4Bm0myfKeL7g== X-Received: by 2002:a17:90a:aa85:b0:268:1e95:4e25 with SMTP id l5-20020a17090aaa8500b002681e954e25mr9287013pjq.17.1694501904059; Mon, 11 Sep 2023 23:58:24 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id i8-20020a17090a2ac800b002696aeb72e5sm9315058pjg.2.2023.09.11.23.58.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 23:58:23 -0700 (PDT) From: Akihiko Odaki To: Cc: =?utf-8?q?Alex_Benn=C3=A9e?= , Mikhail Tyutin , Aleksandr Anenkov , qemu-devel@nongnu.org, Akihiko Odaki , Alexandre Iooss , Mahmoud Mandour Subject: [PATCH v2 03/11] contrib/plugins: Use GRWLock in execlog Date: Tue, 12 Sep 2023 15:57:58 +0900 Message-ID: <20230912065811.27796-4-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230912065811.27796-1-akihiko.odaki@daynix.com> References: <20230912065811.27796-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::1032; envelope-from=akihiko.odaki@daynix.com; helo=mail-pj1-x1032.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org execlog had the following comment: > As we could have multiple threads trying to do this we need to > serialise the expansion under a lock. Threads accessing already > created entries can continue without issue even if the ptr array > gets reallocated during resize. However, when the ptr array gets reallocated, the other threads may have a stale reference to the old buffer. This results in use-after-free. Use GRWLock to properly fix this issue. Fixes: 3d7caf145e ("contrib/plugins: add execlog to log instruction execution and memory access") Signed-off-by: Akihiko Odaki Reviewed-by: Alex Bennée --- contrib/plugins/execlog.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/contrib/plugins/execlog.c b/contrib/plugins/execlog.c index 7129d526f8..82dc2f584e 100644 --- a/contrib/plugins/execlog.c +++ b/contrib/plugins/execlog.c @@ -19,7 +19,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION; /* Store last executed instruction on each vCPU as a GString */ static GPtrArray *last_exec; -static GMutex expand_array_lock; +static GRWLock expand_array_lock; static GPtrArray *imatches; static GArray *amatches; @@ -28,18 +28,16 @@ static GArray *amatches; * Expand last_exec array. * * As we could have multiple threads trying to do this we need to - * serialise the expansion under a lock. Threads accessing already - * created entries can continue without issue even if the ptr array - * gets reallocated during resize. + * serialise the expansion under a lock. */ static void expand_last_exec(int cpu_index) { - g_mutex_lock(&expand_array_lock); + g_rw_lock_writer_lock(&expand_array_lock); while (cpu_index >= last_exec->len) { GString *s = g_string_new(NULL); g_ptr_array_add(last_exec, s); } - g_mutex_unlock(&expand_array_lock); + g_rw_lock_writer_unlock(&expand_array_lock); } /** @@ -51,8 +49,10 @@ static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t info, GString *s; /* Find vCPU in array */ + g_rw_lock_reader_lock(&expand_array_lock); g_assert(cpu_index < last_exec->len); s = g_ptr_array_index(last_exec, cpu_index); + g_rw_lock_reader_unlock(&expand_array_lock); /* Indicate type of memory access */ if (qemu_plugin_mem_is_store(info)) { @@ -80,10 +80,14 @@ static void vcpu_insn_exec(unsigned int cpu_index, void *udata) GString *s; /* Find or create vCPU in array */ + g_rw_lock_reader_lock(&expand_array_lock); if (cpu_index >= last_exec->len) { + g_rw_lock_reader_unlock(&expand_array_lock); expand_last_exec(cpu_index); + g_rw_lock_reader_lock(&expand_array_lock); } s = g_ptr_array_index(last_exec, cpu_index); + g_rw_lock_reader_unlock(&expand_array_lock); /* Print previous instruction in cache */ if (s->len) {