From patchwork Mon Sep 25 14:48:35 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: 726029 Delivered-To: patch@linaro.org Received: by 2002:adf:ea87:0:b0:31d:da82:a3b4 with SMTP id s7csp2434088wrm; Mon, 25 Sep 2023 07:56:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGeGmKhjJMdK6BN2d4QNHOdX52sTFtuIDpc9I4TqCHd3HbV9S1SgCKVy1CLXFXryIl06E+d X-Received: by 2002:a05:622a:1a83:b0:417:9e48:44d7 with SMTP id s3-20020a05622a1a8300b004179e4844d7mr8456566qtc.1.1695653769689; Mon, 25 Sep 2023 07:56:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695653769; cv=none; d=google.com; s=arc-20160816; b=bNtyLFTw5zDWG2uXosjLO6GsrH8R1MeA5NH3ypVT3KlQey9A/FKWQuFouyhPN/ie8g rAhv0K0XIVd2r1U1EcldJZETLJxOCJgKFHhDNI15yEhI22xHEugFui7Zv9rrGzwNjf54 LNNxLA66Q9hKU6e1s+kbzB4QWjZXvLR8S3Y0Nxslv566URp0dSnUg9TuhDpGygYfAnOz 5J6p9D0IXGSqtvYnHkzm8JhTN/GGCkJ9gicjGK0LPILjPcZ9w+AStYYHZ3UFv7XLZ8sl fxV7caTXuP3wDR2kGBM1iE0xrxiqSA2N/9raMFPGSGCPTGrvhd9mJAugy0/EUU1T6Ilo tPVA== 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=bHq5bIYTwX4KI919eN/k0HIJh2GL06+mtrfpk9KLLYo=; fh=mlkRjkvEMCBvV2NmkuZ9E+MTYOKspqfxSPvD1d2xG14=; b=wjickjO7l+E6RLGFKd34UNVbnGIdzeFNKE4/AVpgE5AR1AtTe1gLQ7iCobmnaaMgT5 NtF0CqhzH7jpDfiuIALg7H0bz3HSS0ee/S3VBcSC4OAbcdmh1UBsyCamlbHbTHoqczT7 WzDthhrptjt1LpfgxuU3hLxDdBSAtYzvDXxZJ+bGjzGtUkSo1DlI6TgYKbBjy7KDZuEI iDJ0d3a9NBHddvEda24C+54vOpsZCu7qF7aGsKNE1HmZMsK7pwxj8qWr/b2JfY6Lc0z3 z6iafNWfp9pF5Tcy21tt3InnNyXdlc2hePI4KbawNSz8NiZivH92VRwalWn4izLBQdIo jSJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="Iu/jASGA"; 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 b7-20020a05622a020700b0040fe1ce310dsi1137823qtx.736.2023.09.25.07.56.09 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 25 Sep 2023 07:56:09 -0700 (PDT) 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="Iu/jASGA"; 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 1qkmtn-0001Xv-4a; Mon, 25 Sep 2023 10:49:23 -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 1qkmtk-0001Kl-8R for qemu-devel@nongnu.org; Mon, 25 Sep 2023 10:49:20 -0400 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qkmtS-0008IX-W3 for qemu-devel@nongnu.org; Mon, 25 Sep 2023 10:49:19 -0400 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-4053c6f0d55so61797745e9.0 for ; Mon, 25 Sep 2023 07:49:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1695653341; x=1696258141; 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=bHq5bIYTwX4KI919eN/k0HIJh2GL06+mtrfpk9KLLYo=; b=Iu/jASGAr/RdOVqAT5CuAGMIExdRoXunQyWhlRWUJtd6P663oPyDht0LuBXvJRWXjv hdb01Z/xPW/w+5txPuhw0c2g+zGWmRzLJXqr8R1Eysexh/o0MQ94DBWx5hQu0bKxt6Cs VhNF91/Byh7nbjUJWsASAghULz2jZDc9I2HWFjp4GnbCqhouzwj9wp3luGtW/XgXrSOe 5qZUDPGJbWR69iIPon2NNaBYugQ8NddIQ0oLoGisMdKsCssB+AlIIUzu4fpt4R+UNdE6 ZIEfXbZ4oY+85/CeN/nJp4RBCWd/7knuk+1wfQ3XEW7bT179NvD4A+I3teXtf3kjKib0 X5HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695653341; x=1696258141; 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=bHq5bIYTwX4KI919eN/k0HIJh2GL06+mtrfpk9KLLYo=; b=DCUqYh1iU96rd3d6tEz+luarJ8D+BlHhmtpXfZHekOEvcZCy21q8nZz6VZJ7qvKf1j iTVGeC9UZRvCTOaYYbbXcU5l9JMW4bqv8YxysUoSu5IGkUMHSX4w1S7Hi2JAdg2MW6N5 ORluNGG3ZnAROYnZYM+keoq2n9AOP9/VcXWxdgG9mHjHZbdX0icDmyi/w5/9Yb43IG6w l+mfwPM4fzeZ3iJCTPEYw8MDuW66YV8pD9RM1K1bLfZY2H7oZE18y1PaA4yrj3B1BiJ2 bYwdLsgJqeB1LThdexo+l1PNF2j1i8idlec/QYH9ozfXdvGnYNiv69zVUgJZyi7rqTAn lE4Q== X-Gm-Message-State: AOJu0YzKUQtTEfjiKo2o70MRI5RS+g4N+9NrTP/6EB4QU/m/sy1jqu0i w3J91ts7ObuthQquq/DlCqD14g== X-Received: by 2002:adf:fa0f:0:b0:321:7050:6fb6 with SMTP id m15-20020adffa0f000000b0032170506fb6mr6903023wrr.67.1695653341463; Mon, 25 Sep 2023 07:49:01 -0700 (PDT) Received: from zen.linaroharston ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id s17-20020a5d69d1000000b003141e629cb6sm12021354wrw.101.2023.09.25.07.48.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 07:49:00 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 6901B1FFC4; Mon, 25 Sep 2023 15:48:56 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Alistair Francis , =?utf-8?q?C=C3=A9dric_Le_Go?= =?utf-8?q?ater?= , Marcin Juszkiewicz , John Snow , libvir-list@redhat.com, =?utf-8?q?Marc-Andr?= =?utf-8?q?=C3=A9_Lureau?= , qemu-s390x@nongnu.org, Song Gao , Daniel Henrique Barboza , Marcel Apfelbaum , Bastian Koppelmann , Liu Zhiwei , Weiwei Li , Nicholas Piggin , Radoslaw Biernacki , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Cleber Rosa , Paolo Bonzini , Mahmoud Mandour , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Thomas Huth , Wainer dos Santos Moschetta , Richard Henderson , Bin Meng , Alexandre Iooss , Xiaojuan Yang , qemu-ppc@nongnu.org, David Hildenbrand , =?utf-8?q?Alex_Benn=C3=A9e?= , Yanan Wang , Peter Maydell , qemu-riscv@nongnu.org, qemu-arm@nongnu.org, Palmer Dabbelt , Ilya Leoshkevich , Laurent Vivier , Yoshinori Sato , Leif Lindholm , Beraldo Leal , Akihiko Odaki Subject: [PATCH 12/31] contrib/plugins: Use GRWLock in execlog Date: Mon, 25 Sep 2023 15:48:35 +0100 Message-Id: <20230925144854.1872513-13-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230925144854.1872513-1-alex.bennee@linaro.org> References: <20230925144854.1872513-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32f; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x32f.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=unavailable 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: Akihiko Odaki 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 Message-Id: <20230912224107.29669-5-akihiko.odaki@daynix.com> Signed-off-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) {