From patchwork Wed Oct 11 10:33:15 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: 731947 Delivered-To: patch@linaro.org Received: by 2002:a5d:54d1:0:b0:31d:da82:a3b4 with SMTP id x17csp326570wrv; Wed, 11 Oct 2023 03:36:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFM0Atj08te5oCD11w31a1JNPdFAneMKfKDZVJXzUSQ3raeXCOtZ80Cl+ihqlJ+Cn/jc0wS X-Received: by 2002:a67:f541:0:b0:452:dae8:df07 with SMTP id z1-20020a67f541000000b00452dae8df07mr18483745vsn.8.1697020592047; Wed, 11 Oct 2023 03:36:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697020592; cv=none; d=google.com; s=arc-20160816; b=IKUbQtcfo7DL8WvKCxVIQTYRvrsmdtFP+WfylQ4ut9CLOSs5drBwVpld4RQZLjPq7A 7+JKhX4JSV2lxvbzk9Vsc0B3ZnK3eTzbJB7M4T6LbTv1UE935tFWl8hohQJYhC4Xoqw7 0aiObdLvB+U0sKyy+ofgLDxrUCa4jBUf4ikVClE65/XAZJTj8RaIjmno3XmbES40VsIX vgxsJ9VapuOfu6QAp1jihAredNvcRot/FEZTOhYvla4xULca9D/A8+N/AKkB49Av1xOh CMKni8rPxzXPdVITU7o+/v4qxYvDG/r4BN4Qkczh8fNIqDMLT/d0rp769N89HJtRpfTA 6agg== 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=IOZHj2WDZdrEovxhRhKdAFEERTpyPMh/xQMpzlYqBTI=; fh=0krr7TACl5TzF2Q+IYI3uEo233L4eFHoGi0NC6HgaMw=; b=jDPfjSpL/F/oCLp9fkl58iibrDTO/Tx2V5J4l6ETw68tEnUkPsIsou/5s+qjAj3Y1G fhzYRrV+Fjv5KpCNjBNTdFgfFX7m73Wg5aKeQ9rgkGy3v3FQomXhC3tu8KwzOu3Ud9M4 Y1WZajzhm39EAkvo3tErGoB/wXwaqxZwACrlmSFRVmkS+eUbySpdC0lv37sTPuUyfgJb SbhYlbzjeMLi1rL/2tlzINCiqsPyvF1bcs3tocXOZMzc6CtJG6grj14FXm+tujajubWE z6pq1qpI4773pow977azKS1yb5Jh4wIOBs9U0V8Qj5uQf6JDLTL42dsFBT5/odhECpXv bGBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zTffMfkY; 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 2-20020ac85742000000b00418142e802dsi9268266qtx.417.2023.10.11.03.36.31 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 11 Oct 2023 03:36:32 -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=zTffMfkY; 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 1qqWXN-0001eG-IA; Wed, 11 Oct 2023 06:33:57 -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 1qqWX6-0001Ky-Mi for qemu-devel@nongnu.org; Wed, 11 Oct 2023 06:33:41 -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 1qqWX4-0005FM-Bz for qemu-devel@nongnu.org; Wed, 11 Oct 2023 06:33:40 -0400 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-405524e6769so4808975e9.1 for ; Wed, 11 Oct 2023 03:33:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1697020416; x=1697625216; 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=IOZHj2WDZdrEovxhRhKdAFEERTpyPMh/xQMpzlYqBTI=; b=zTffMfkYEWsiryCv23xGmYen7FK462DMrXCtUUv886xYqsQhFi2j2EiUP8txYU6i47 1y1nLEpRAdjSmDayJtkikQe4W7ELcQLBWty90xiHa27h6oFjcwBrsjRlkln/8ru+eadC ccUZPEVvstGme921XVVuvLynoYz2axWEDMcb+uYNNexnPGzeFkiKtTjNE/WIWdjTztYJ b1MERWCxk8OXVt72gyXLaFuUgF/+EBCSjfxrRNpuftEedZP5WkqKU+rZM+WEalQPN0Bd pmvjKUwpLBnGN7bf0X0slAMEhm68Jk94xSN7WeF624nkrMSdUPaYdaLOWv125KxBtBlh l2hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697020416; x=1697625216; 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=IOZHj2WDZdrEovxhRhKdAFEERTpyPMh/xQMpzlYqBTI=; b=mhvxjesjWmHaAT0JacG+z9CosoeZwtNmgjhT5RoNa3iB6mrexuqWoqDUVAr6KmWjNU Com/5d5XmKYqoI+Tjy3hUtvwOReVs8K02ZzxVg5jH94hjei2x5Opr5b8TNyP+IMynicm crfkzpX4mK5FVxvS6+/6VVQgpW2HHz5rEIjdAjXCUXPuDZsls6CRniF4PP1uRHFraYe4 QQUK7oquOkt2zlBy5nB8ZQ5JnqvDle9UwDoaWusfZP0RJWSeTrKIw/kN4W1JOKyfv6Mn 7tyBg47Q0or5qfXLziaMIBGchm3awbghkks36dY39Cin1fUikyGbiher+qBtpTSZZr/9 orDg== X-Gm-Message-State: AOJu0YxwBBWcleDMXnuEjT5VpzmhGuspXyx2e6jQYTsIW4z63Vm97Cqw mCVK0SPt/9lWrZQEy5vdrNAL1A== X-Received: by 2002:a7b:c044:0:b0:3fb:c075:b308 with SMTP id u4-20020a7bc044000000b003fbc075b308mr16426874wmc.12.1697020416294; Wed, 11 Oct 2023 03:33:36 -0700 (PDT) Received: from zen.linaroharston ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id ay20-20020a05600c1e1400b0040640073d25sm16646526wmb.16.2023.10.11.03.33.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 03:33:35 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 4D7821FFC4; Wed, 11 Oct 2023 11:33:31 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Akihiko Odaki , =?utf-8?q?Alex_Benn=C3=A9e?= , Alexandre Iooss , Mahmoud Mandour Subject: [PULL 11/25] contrib/plugins: Use GRWLock in execlog Date: Wed, 11 Oct 2023 11:33:15 +0100 Message-Id: <20231011103329.670525-12-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231011103329.670525-1-alex.bennee@linaro.org> References: <20231011103329.670525-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=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: 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 Message-Id: <20231009164104.369749-12-alex.bennee@linaro.org> 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) {