From nobody Mon Jul 31 12:26:16 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RDyCz4Tvhz4q5f0; Mon, 31 Jul 2023 12:26:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RDyCF6X83z3xxb; Mon, 31 Jul 2023 12:26:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1690806378; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=pF//ODRnA034Wu6LN2FzEwZdd1V+vmaW7jRSoyiX+h4=; b=ZyvFREG//ucbCpKmuo+IQAQmiOVpMQ+KDMWV+NXlWo2KKLJ3xlwef0wYtyfFN6OtFQzcQY 9HlI2UUD9wdA/RiHqz+SyepAJpjbb3LP2tE/0t1Rqu74XMpWpWAyQAVQjnXxiWvKcvObUE EOhUw51h2gWCmNdscXpaPpO2iVBelKlcD+hmgsbpLX+YMu+pXEe8eFZVczZzNdmhPAEIOS 9TLfCxYwEH7BmpzDsZXK26I21luateRrdfvS0jT9mtvJe7AC+cQatc6DVWnVwVr/E8Q1/7 Zhtr7g1cRipL+mMVeFaZrg++88XTw4qP2GIy6GC/1iwjSPL5UaW6RhNQouOfSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1690806378; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=pF//ODRnA034Wu6LN2FzEwZdd1V+vmaW7jRSoyiX+h4=; b=lEhSGjeb5eMrKjtuglGjluADd4FD1fU4PBjuaUj1q9BOkrjMWQblb8I+n3DndGnGXAywln dh+9VJFLvmavesNG/BO7C9+U7DxFjdO5vxw0nC58wN//6vInF7pWU/SZ1kluchCqTKjD55 A6dm1+DY+sk8MVf4s/v4wKzvryDR4JHHiRYsR2T0MM6YYSOJZTVPrTvEC6mAbdxrmBLa3y Vfl8Q0/kJwbCKoLfi2C2As+l0hn3n2z7fDbY1rKFYM5+lNdpWPlksaPF4tmxEzty8VO/sT F5RHaq+MSZ3LfiaH3vk/2wigMf+whYUrqbso8mRbhpof+IMvBbjTy0PnC/m9mQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1690806378; a=rsa-sha256; cv=none; b=iARU49PySBx6OA3Lqphr3A/y3lnLXyCNDkH7hKg3qHRjWO0fiqnHwjuYQadr7sTeAVP+xs LOQXvggFwDvY6P5DgjZCoXt1EUSKGoFkpDY4oHcYyETqlzhkB3IHPB7PU2N1fFysOyaISt 5qYFUdSOeCWWwX05m+ZZlSFeYwWySO0LWLqHNXeq0iTLGViGbqjrhsYOGo0W7PXJgw6XTa ZhCLa2H3lNfwZG1rXdBzJ4exROyOV4TJGiY3hGo4tAoYf7sJ0p8hWUVqvqt6uWD5oOOCpD 6CfKxGkTZ7+omlwMNDVriMOFDcrU6N5pUVFuq2MZmFJFPlHCtQAVMBoWwSNd5A== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RDyCD60wHzwJv; Mon, 31 Jul 2023 12:26:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 36VCQGtB043768; Mon, 31 Jul 2023 12:26:16 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 36VCQGw9043767; Mon, 31 Jul 2023 12:26:16 GMT (envelope-from git) Date: Mon, 31 Jul 2023 12:26:16 GMT Message-Id: <202307311226.36VCQGw9043767@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Marcin Wojtas Subject: git: 8a6ba4eefb66 - stable/13 - ena: Initialize statistics before the interface is available List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mw X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 8a6ba4eefb66159c092330576e9e50748b4db737 Auto-Submitted: auto-generated The branch stable/13 has been updated by mw: URL: https://cgit.FreeBSD.org/src/commit/?id=8a6ba4eefb66159c092330576e9e50748b4db737 commit 8a6ba4eefb66159c092330576e9e50748b4db737 Author: Osama Abboud AuthorDate: 2023-01-18 13:19:07 +0000 Commit: Marcin Wojtas CommitDate: 2023-07-31 12:23:34 +0000 ena: Initialize statistics before the interface is available In [1], the FBSD community exposed a bug in the fbsd/ena driver. Bug description: ---------------- Current function call order is as follows: 1. ena_attach() 1.1. ena_setup_ifnet() 1.1.1. Registration of ena_get_counter() 1.1.2. ether_ifattach(ifp, adapter->mac_addr); 1.2. Statistics allocation and initialization. At point 1.1.2, when ether_ifattach() returns, the interface is available, and stats can be read before they are allocated, leading to kernel panic. Also fixed a potential memory leak by freeing the stats since they were not freed in case the following calls failed. Fix: ---- This commit moves the statistics allocation and initialization to happen before ena_setup_ifnet() [1] https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=268934 Fixes: 9b8d05b8ac78 ("Add support for Amazon Elastic Network Adapter (ENA) NIC") Fixes: 30217e2dff10 ("Rework counting of hardware statistics in ENA driver") MFC after: 2 weeks Sponsored by: Amazon, Inc. (cherry picked from commit b9e80b5280b75f2c641d680245df44b8ff26a7b0) --- sys/dev/ena/ena.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/sys/dev/ena/ena.c b/sys/dev/ena/ena.c index 4dcbb709c586..a974a20638b9 100644 --- a/sys/dev/ena/ena.c +++ b/sys/dev/ena/ena.c @@ -3480,6 +3480,15 @@ ena_reset_task(void *arg, int pending) ENA_LOCK_UNLOCK(); } +static void +ena_free_stats(struct ena_adapter *adapter) +{ + ena_free_counters((counter_u64_t *)&adapter->hw_stats, + sizeof(struct ena_hw_stats)); + ena_free_counters((counter_u64_t *)&adapter->dev_stats, + sizeof(struct ena_stats_dev)); + +} /** * ena_attach - Device Initialization Routine * @pdev: device information struct @@ -3657,6 +3666,13 @@ ena_attach(device_t pdev) /* initialize rings basic information */ ena_init_io_rings(adapter); + /* Initialize statistics */ + ena_alloc_counters((counter_u64_t *)&adapter->dev_stats, + sizeof(struct ena_stats_dev)); + ena_alloc_counters((counter_u64_t *)&adapter->hw_stats, + sizeof(struct ena_hw_stats)); + ena_sysctl_add_nodes(adapter); + /* setup network interface */ rc = ena_setup_ifnet(pdev, adapter, &get_feat_ctx); if (unlikely(rc != 0)) { @@ -3678,13 +3694,6 @@ ena_attach(device_t pdev) taskqueue_start_threads(&adapter->metrics_tq, 1, PI_NET, "%s metricsq", device_get_nameunit(adapter->pdev)); - /* Initialize statistics */ - ena_alloc_counters((counter_u64_t *)&adapter->dev_stats, - sizeof(struct ena_stats_dev)); - ena_alloc_counters((counter_u64_t *)&adapter->hw_stats, - sizeof(struct ena_hw_stats)); - ena_sysctl_add_nodes(adapter); - #ifdef DEV_NETMAP rc = ena_netmap_attach(adapter); if (rc != 0) { @@ -3707,6 +3716,7 @@ err_detach: ether_ifdetach(adapter->ifp); #endif /* DEV_NETMAP */ err_msix_free: + ena_free_stats(adapter); ena_com_dev_reset(adapter->ena_dev, ENA_REGS_RESET_INIT_ERR); ena_free_mgmnt_irq(adapter); ena_disable_msix(adapter); @@ -3779,10 +3789,7 @@ ena_detach(device_t pdev) netmap_detach(adapter->ifp); #endif /* DEV_NETMAP */ - ena_free_counters((counter_u64_t *)&adapter->hw_stats, - sizeof(struct ena_hw_stats)); - ena_free_counters((counter_u64_t *)&adapter->dev_stats, - sizeof(struct ena_stats_dev)); + ena_free_stats(adapter); rc = ena_free_rx_dma_tag(adapter); if (unlikely(rc != 0))