From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796379811; b=yazvP1CDPjaGzplLSpWNNcTQ5g9GA3kCqGsGz/PU5raIMZ7cy601MIrIFcIbJDmPrs3b EFZ//3jtp/VHOtW7TnxMxE9yTwBRMP6dVIxuBj8U7XFWeErIt/E1gbq58JDsUczTNcA2r JXaCSMOdJeg9BMe29Vtd2LVLxbqVlEkoVUsgL4EHpicl2DiImRSXlRH8dZxpMBj0c0h5p h2+QKfelfuhOZAAhjRS0PzyhjJ3qVOEbEWG2T6yZ5b0N3mF57BgooUMqVds8yrm4Dh8fv 814YYXMgkW9Q9iLztlacgJnnWQlDRB64NuiAZcwYrFr9q+mQzqTBfszZQdcHP71aoYQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796379811; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=8mvl0L3bltF2uwJDBdRGHGyjGDeD9fuHC1Zokq4rFMw=; b=FMEjmc9Y0tyjmeOm8jfIG1y96SdE54jampyQZM0+dqbYzoA2i3sdZYzHCHKN0CG6n8yV oGOGbgnbn3r10mUFhAJ2M6yraWYkkoZNXVWJY5ygCQVMhxdfAr5L9x7TMmTqaBaQodK7C zOFL1v6zMnaImQQul2xCt3COR5BmP6nbDLSJVWas42MQzJWKJR8eNTMj9RC2nxRhz5ZZQ 2OklJSn8/YmW3U5H+Ci0KiFQ8RfE7Iqc72Zx69d4q8sSD1B7CAZ+6xJPRAMwYNTXgT9Uw x35+Gwou9ftAdfipopZsNmJFPiNKWd4acEqZQfFDxu8PPlQ+tiDnQ5H7zmDynoTGKng== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=cRRBJgoY; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D84A5606A322; Fri, 4 Dec 2026 10:23:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=cRRBJgoY Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4C93A1F000C2; Fri, 4 Dec 2026 10:23:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796379811; bh=8mvl0L3bltF2uwJDBdRGHGyjGDeD9fuHC1Zokq4rFMw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=cRRBJgoYcc39Vdl/yrUyazdE48/VVZ1d3kf/T22rg9M3tme1xThqfENvbPuYNKVvZ cTD4QHn2IbJ6nQWgHWEYOg/Rr7aw2PWt1eUj3bfEZxsRYAZTI2+J2LRcVx+unkd3kK grGYezZgqtCDobhlDxALyQFTNWcX1C78ZxmAecmI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Debarghya Kundu , Pin-yen Lin , Harshitha Ramamurthy , Jakub Kicinski , Sasha Levin Subject: [PATCH 6.12 045/555] gve: Fix race between stats query and queue reconfig Date: Fri, 4 Dec 2026 19:01:28 +0900 Message-ID: <20261204100607.778574631@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Pin-yen Lin [ Upstream commit 7de69da15088ac2add08eb2b5f5c0a79fd461c1e ] Fix a concurrency bug where network statistics queries race with queue reconfiguration or interface teardown, causing a Use-After-Free (UAF) or NULL pointer dereference. Currently, statistics queries access priv->tx and priv->rx without locking. When an interface is brought down or reconfigured, gve_queues_mem_remove() frees these arrays. A concurrent stats query can dereference freed queue structures and crash the kernel. This issue was discovered and actually triggered by the drivers/net/stats.py selftest. When an interface was brought down, querying per-queue statistics via Netlink caused a kernel NULL pointer dereference in gve_get_rx_queue_stats(): BUG: kernel NULL pointer dereference, address: 00000000000000a0 RIP: 0010:gve_get_rx_queue_stats+0x17/0x50 [gve] Call Trace: netdev_stat_queue_sum+0x11c/0x250 netdev_nl_stats_by_netdev+0x21b/0x2d0 netdev_nl_qstats_get_dumpit+0xd2/0x220 genl_dumpit+0x32/0x90 netlink_dump+0x1d2/0x450 __netlink_dump_start+0x1db/0x2a0 genl_family_rcv_msg_dumpit+0x9b/0x100 ... The per-queue stats callbacks run under the netdev instance lock, so this NULL dereference is not a race; it happens on any query while the interface is down. Additionally, concurrent stats queries during queue reconfiguration can race with gve_queues_mem_remove(), causing a Use-After-Free. This affects gve_get_stats(), which does not hold the instance lock. gve_handle_report_stats() also runs without it; it is stopped before queue teardown, but can still read priv->rx[idx] while the single-queue restart path rewrites it. Fix this by introducing stats_lock to serialize stats queries against queue teardown. In gve_queues_mem_remove(), queue pointers are set to NULL under stats_lock before freeing. Stats readers acquire the lock and verify pointers are non-NULL before reading counters. The per-queue stats callbacks already run under the instance lock, but take stats_lock as well for consistency. The single-queue restart path also rewrites priv->rx[idx] in place, so gve_rx_queue_stop() and gve_rx_queue_start() do those writes under stats_lock as well. This change was tested using the DQO RDA queue format. Cc: stable@vger.kernel.org Fixes: f5cedc84a30d ("gve: Add transmit and receive support") Fixes: 2e5e0932dff5 ("gve: add support for basic queue stats") Co-developed-by: Debarghya Kundu Signed-off-by: Debarghya Kundu Signed-off-by: Pin-yen Lin Signed-off-by: Harshitha Ramamurthy Link: https://patch.msgid.link/20261109182036.2910559-2-hramamurthy@google.com Signed-off-by: Jakub Kicinski [ sashal: Dropped the two per-queue stats callback hunks since that code doesn't exist in this tree, and adjusted context plus the lock span in gve_queues_start() to match the older code. ] Signed-off-by: Sasha Levin --- drivers/net/ethernet/google/gve/gve.h | 5 +++++ drivers/net/ethernet/google/gve/gve_main.c | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/google/gve/gve.h b/drivers/net/ethernet/google/gve/gve.h index e19d722e8e9a1..3e768c64a5776 100644 --- a/drivers/net/ethernet/google/gve/gve.h +++ b/drivers/net/ethernet/google/gve/gve.h @@ -802,6 +802,11 @@ struct gve_priv { struct workqueue_struct *gve_wq; struct work_struct service_task; struct work_struct stats_report_task; + /* Protects the priv->tx/priv->rx arrays, the in-place rewrite of + * priv->rx[idx] and the queue counts against stats readers that do + * not hold the netdev instance lock. + */ + spinlock_t stats_lock; unsigned long service_task_flags; unsigned long state_flags; diff --git a/drivers/net/ethernet/google/gve/gve_main.c b/drivers/net/ethernet/google/gve/gve_main.c index 974b493d712d2..a620dc6bc52ef 100644 --- a/drivers/net/ethernet/google/gve/gve_main.c +++ b/drivers/net/ethernet/google/gve/gve_main.c @@ -112,6 +112,7 @@ static void gve_get_stats(struct net_device *dev, struct rtnl_link_stats64 *s) int num_tx_queues; int ring; + spin_lock_bh(&priv->stats_lock); num_tx_queues = gve_num_tx_queues(priv); if (priv->rx) { for (ring = 0; ring < priv->rx_cfg.num_queues; ring++) { @@ -139,6 +140,7 @@ static void gve_get_stats(struct net_device *dev, struct rtnl_link_stats64 *s) s->tx_bytes += bytes; } } + spin_unlock_bh(&priv->stats_lock); } static int gve_alloc_flow_rule_caches(struct gve_priv *priv) @@ -1345,9 +1347,13 @@ static void gve_queues_mem_remove(struct gve_priv *priv) struct gve_rx_alloc_rings_cfg rx_alloc_cfg = {0}; gve_get_curr_alloc_cfgs(priv, &tx_alloc_cfg, &rx_alloc_cfg); - gve_queues_mem_free(priv, &tx_alloc_cfg, &rx_alloc_cfg); + + spin_lock_bh(&priv->stats_lock); priv->tx = NULL; priv->rx = NULL; + spin_unlock_bh(&priv->stats_lock); + + gve_queues_mem_free(priv, &tx_alloc_cfg, &rx_alloc_cfg); } /* The passed-in queue memory is stored into priv and the queues are made live. @@ -1361,6 +1367,7 @@ static int gve_queues_start(struct gve_priv *priv, int err; /* Record new resources into priv */ + spin_lock_bh(&priv->stats_lock); priv->tx = tx_alloc_cfg->tx; priv->rx = rx_alloc_cfg->rx; @@ -1375,9 +1382,11 @@ static int gve_queues_start(struct gve_priv *priv, else priv->num_xdp_queues = 0; + gve_init_sync_stats(priv); + spin_unlock_bh(&priv->stats_lock); + gve_tx_start_rings(priv, 0, tx_alloc_cfg->num_rings); gve_rx_start_rings(priv, rx_alloc_cfg->qcfg->num_queues); - gve_init_sync_stats(priv); err = netif_set_real_num_tx_queues(dev, priv->tx_cfg.num_queues); if (err) @@ -2155,6 +2164,7 @@ void gve_handle_report_stats(struct gve_priv *priv) if (!gve_get_report_stats(priv)) return; + spin_lock_bh(&priv->stats_lock); be64_add_cpu(&priv->stats_report->written_count, 1); /* tx stats */ if (priv->tx) { @@ -2219,6 +2229,7 @@ void gve_handle_report_stats(struct gve_priv *priv) }; } } + spin_unlock_bh(&priv->stats_lock); } /* Handle NIC status register changes, reset requests and report stats */ @@ -2468,8 +2479,10 @@ static int gve_rx_queue_stop(struct net_device *dev, void *per_q_mem, int idx) gve_turnup_and_check_status(priv); gve_per_q_mem = (struct gve_rx_ring *)per_q_mem; + spin_lock_bh(&priv->stats_lock); *gve_per_q_mem = priv->rx[idx]; memset(&priv->rx[idx], 0, sizeof(priv->rx[idx])); + spin_unlock_bh(&priv->stats_lock); return 0; } @@ -2520,7 +2533,9 @@ static int gve_rx_queue_start(struct net_device *dev, void *per_q_mem, int idx) return -EAGAIN; gve_per_q_mem = (struct gve_rx_ring *)per_q_mem; + spin_lock_bh(&priv->stats_lock); priv->rx[idx] = *gve_per_q_mem; + spin_unlock_bh(&priv->stats_lock); /* Single-queue creation requires quiescence on all queues */ gve_turndown(priv); @@ -2556,7 +2571,9 @@ static int gve_rx_queue_start(struct net_device *dev, void *per_q_mem, int idx) * which needs the rtnl lock, will not run till this func returns and * its caller gives up the lock. */ + spin_lock_bh(&priv->stats_lock); memset(&priv->rx[idx], 0, sizeof(priv->rx[idx])); + spin_unlock_bh(&priv->stats_lock); return err; } @@ -2663,6 +2680,7 @@ static int gve_probe(struct pci_dev *pdev, const struct pci_device_id *ent) } INIT_WORK(&priv->service_task, gve_service_task); INIT_WORK(&priv->stats_report_task, gve_stats_report_task); + spin_lock_init(&priv->stats_lock); priv->tx_cfg.max_queues = max_tx_queues; priv->rx_cfg.max_queues = max_rx_queues; -- 2.53.0