From patchwork Mon Oct 9 16:40:50 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: 731011 Delivered-To: patch@linaro.org Received: by 2002:a5d:574c:0:b0:31d:da82:a3b4 with SMTP id q12csp1311738wrw; Mon, 9 Oct 2023 09:44:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHCsH0uIwralSrtU5gh3T+/WO2yU+ByRXfszKRG7xtIh6L3jle1wtX6uKGWXc10Q3gj8nn2 X-Received: by 2002:ac8:5c82:0:b0:419:5c43:9d1a with SMTP id r2-20020ac85c82000000b004195c439d1amr17468084qta.68.1696869892926; Mon, 09 Oct 2023 09:44:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696869892; cv=none; d=google.com; s=arc-20160816; b=JLdKZvTkNu5qrQnvl0fZbxoTjYxYb7UwNp9MbQkJX4gMfXlnAXfk9aBvSWUBg3p5xf +9ElaH7FCbDxspUU2L23eREHJw6p0XO+aAtUyFRr3qpe8y0bYqKObNt8quTGcasDAo5Z xGfoUb+YeRRP+DSjNW8D3QHbDB2WgFxDPXhDIumme6JldYmYylXowvEzrfKk+dz7Zf4H MZLc7deOuHVwYZbauzADlZsCLq8v0GdZht3JjRHedINCD3HWpRnwA9TJ8r/sKME+n9Oj mPxDhAfwvplBmdyFl9Ii/+MJkB1+Tjmxvx2+MbnHJ2vmyc2bRkWvCMUeNnT3wn1V865e fCQQ== 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=FL21b9FDJBPKO4Cw1gBqBPIr8gb5CEXpVKTgM6hLIl8=; b=MsEevvOXjhbjk3ncZXKm+1JJ9z26xhBWXsaiyPmyKvFnuN7UDhLjgXjJiNy7OjO0QV iPffDwnBnd9UngjLwRaovK67zjIll7sRsvi+Sol6lWjFBGhiaufM3wzVApDgQ/a3jON/ 9VqpXRQNOJGKFk/UjbWrSz0Ri8zG7n/nWt7K2gJrFPin0BAPb1S8kCqaCzGTDH2IYQce uIvKL2/RD4N00ttsa1TMqEWLAkd6Rgtc1jH+4/T6hXYOAEYxCtrQ6sAOMwR1mGbuLepo cjNUwXIpwPyuCygqSPvT0jVGYe+fe2BZKWc7uCTSTWwYS9va/M/s12VU3t79SvhyUV5B RN8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=impcdhpO; 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 y9-20020a05622a164900b004199048a43esi6426224qtj.219.2023.10.09.09.44.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 09 Oct 2023 09:44:52 -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=impcdhpO; 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 1qptMK-0002DX-Mw; Mon, 09 Oct 2023 12:43:56 -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 1qptK8-0005WY-BJ for qemu-devel@nongnu.org; Mon, 09 Oct 2023 12:41:40 -0400 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qptJi-0001Zb-GH for qemu-devel@nongnu.org; Mon, 09 Oct 2023 12:41:40 -0400 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-4054f790190so46130775e9.2 for ; Mon, 09 Oct 2023 09:41:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696869672; x=1697474472; 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=impcdhpOyXeD8kWDzSueYCXvqsGMhfaFS7Yu4M+4j85kacwo8pqAXh5WnWYxRwkQon Q+bvg1eJsLCH5BIq3HS9Wb/Jq+uEz41OOSVhcGBgdI1DC3juMkeXEcHEMgJBs99Kggbi qx4H7RB9d5lTvGm3ycBaXbRe0rKZj/Ir3SnNKL2j0mTXAy52hMKBR7+NlVOVlUyB/k90 zIuAhi1f/IleJcF9CgWfwP47MVe3+p7PY3u2lSXLkVCGEkLfzV5LJvGakmiHgNJiGivW 0BXXt6zhBr1e+c+2SB7yjGnt1LQ+2oJFiLwTPslgw4PHx0kOTmFC+9Wi88o5ePouiRba wMfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696869672; x=1697474472; 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=TteLTHv8NVuQvZv0i+mHCr7nbtYiE2RUoTluKFDjJbXXDEB3t86jpZRJDSRn2l88h2 LmKNxyCgaXpxGP5GcIX6k3hk72ZoaTRULZm/tLNgG8IuYlEyQcdF7HKz3xdE9VEvcmXA NnOsRz9fR6OZoYfQWkYhlyb56Gz+UGbr9urU12/b494mxF7mGV2+D9iyeEfQruw4wM0p 4CgUcHl6fnLufSCiHeGTaYEhH8ZXx8CzxMLN2CGqdEcdWYN8+aSZFp2HNLu6gkoqGfrF 0umHL1Ex379HkNy8LPmn6Pt61Q1YaFfDF2WyIChYuYwuu6K5ycJZ8nuBtE/+Rry8rjIG AIcQ== X-Gm-Message-State: AOJu0YxmKEBdVsQPELEXOzqoJ0M5OQ7gkBsbY2+qK4m1BogBaqxtzcJN dcNSA1YBD4lcLzO+pikSXGRVLg== X-Received: by 2002:a7b:cb8c:0:b0:406:517f:ac71 with SMTP id m12-20020a7bcb8c000000b00406517fac71mr13937205wmi.29.1696869672593; Mon, 09 Oct 2023 09:41:12 -0700 (PDT) Received: from zen.linaroharston ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id a4-20020a05600c224400b003fe2b081661sm13974906wmm.30.2023.10.09.09.41.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 09:41:11 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 5CE991FFC4; Mon, 9 Oct 2023 17:41:06 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Thomas Huth , Xiaojuan Yang , =?utf-8?q?C=C3=A9dric_Le_Goater?= , =?utf-8?q?Alex_Benn=C3=A9?= =?utf-8?q?e?= , Bastian Koppelmann , Brad Smith , Radoslaw Biernacki , David Hildenbrand , Cleber Rosa , Ilya Leoshkevich , qemu-riscv@nongnu.org, Daniel Henrique Barboza , Alexandre Iooss , Nicholas Piggin , Bin Meng , Leif Lindholm , Paolo Bonzini , Eduardo Habkost , Alistair Francis , qemu-ppc@nongnu.org, John Snow , Liu Zhiwei , qemu-arm@nongnu.org, Wainer dos Santos Moschetta , Palmer Dabbelt , Peter Maydell , Yanan Wang , Weiwei Li , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Marcel Apfelbaum , Beraldo Leal , Yoshinori Sato , Mahmoud Mandour , Richard Henderson , Song Gao , Marcin Juszkiewicz , qemu-s390x@nongnu.org, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki Subject: [PATCH 11/25] contrib/plugins: Use GRWLock in execlog Date: Mon, 9 Oct 2023 17:40:50 +0100 Message-Id: <20231009164104.369749-12-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231009164104.369749-1-alex.bennee@linaro.org> References: <20231009164104.369749-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x334.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) {