From nobody Wed Sep 07 23:55:46 2022 X-Original-To: dev-commits-src-main@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 4MNJzk434Xz4bTbn; Wed, 7 Sep 2022 23:55:46 +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 4MNJzk3B83z3k6X; Wed, 7 Sep 2022 23:55:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1662594946; 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=auryLpOtXOD1Jfslv8DFdwGLzD11HHD6sInpVUQf80o=; b=KptQB8N35zc3UQOPoxz6ce3zLjdXkEJsFzxh6CclloMg8jfhuf1OSGtMEFK3iPbncelkcG AZ3wvjbMovs6nmkOBT5NIOS3yLNPd/N+otrV4wot6/Q1RtmKXi42SBFKiLQPGxxfgtOn1K mk6eGExYOVGm3K84F7SuacSCn99hnIq3us2Y6w6eWoCGVODJ9UWPDJXp2HOuHc0p9Qd6hw FzIGrMLMxhS053/gK+qtIJv+D6yheQ9M7q6GkUoJo/3sT5fDeHvp/H2hf+Yd1DWcSXu6ym f+wlzvV5imk1M50pjLeNOOAjamsZxqNDXhKmXpBI744yeUY9tLlX0KO+WBnegQ== 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 4MNJzk240bzhTS; Wed, 7 Sep 2022 23:55:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 287NtkHN054707; Wed, 7 Sep 2022 23:55:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 287NtkVv054706; Wed, 7 Sep 2022 23:55:46 GMT (envelope-from git) Date: Wed, 7 Sep 2022 23:55:46 GMT Message-Id: <202209072355.287NtkVv054706@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Bjoern A. Zeeb" Subject: git: ac07a3b8b6ec - main - LinuxKPI: netdev: expose napi state List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: ac07a3b8b6ec14059136f87a1aba0bf4e3333361 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1662594946; 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=auryLpOtXOD1Jfslv8DFdwGLzD11HHD6sInpVUQf80o=; b=qG1N6npNDW4hPvWuK9FnUre7++pgsBTgg3HMHb1ZQY7dNdHraDz7dnOXp9AjgN5aEzwaAm Sa3Lj5twayvoWOEktCBoFAs9a/wj5V5Izb5r5DTiMhIqs1afj+UQNz0TLvzEzVe/zkFKM4 /25lgyvPQZVCdHt3nUGT6P6egRyYnFboT1uAi7Fh4b5bdpKsWV94k4noLZFiy2HBUrD15G jdCDiE4J1zOTK4ZZowOo0CQGftMNt/ydGq3treOOlKCpiq3xf3XgOR2BwWdCJSiQHw3pi7 WvkYQanNlRXH3OBrmoTAsqAYzDu5cjfigDSTb7/lLaGrYcSIWzh6raTlcTXRNA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1662594946; a=rsa-sha256; cv=none; b=M/yYX2RaTpuzx2UmcgL/4xj2Z5xbsTejjpCS/cOCEo6jmlTrK+6N1UhKaHupnYGX9bJQYz cC6BdD+R/uvdeL2AwprM9tu5XQG1xp4kj3nvSHRaTWzHt3f3npr2MGeK4pDGv8YrYsfPSw ASa+tDknN77ZUTX0ars1oIuwjQFm7Z0KCv5DIRcY8MDhn5PeOOEHOLJs/MuqJxsTHS+ayw 9qKdV0/vpwYfSWQVvGrQcreli2awXSfl0u6+HS2nLiaIW9B7DpWtPd/29SOq3JFvkdXJ2o q9DXFR2lUtnDUwr0WsGzBKEwxWb2q3Q1rpuvcDAMet0xHRrdM/B1WuF72eioHg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by bz: URL: https://cgit.FreeBSD.org/src/commit/?id=ac07a3b8b6ec14059136f87a1aba0bf4e3333361 commit ac07a3b8b6ec14059136f87a1aba0bf4e3333361 Author: Bjoern A. Zeeb AuthorDate: 2022-09-07 23:35:41 +0000 Commit: Bjoern A. Zeeb CommitDate: 2022-09-07 23:35:41 +0000 LinuxKPI: netdev: expose napi state There are drivers directly accessing napi->state testing for bits (NAPI_STATE_SCHED encountered so far). Rename the internal _flags struct field to state and expose our internal state flag bits along with the one official aliased. As I left in a comment, I wished Linux would hide these accesses behind inline functions or by other means and not public expose the implementation details. MFC after: 1 week --- .../linuxkpi/common/include/linux/netdevice.h | 29 ++++++++++- sys/compat/linuxkpi/common/src/linux_netdev.c | 57 ++++++++++------------ 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/sys/compat/linuxkpi/common/include/linux/netdevice.h b/sys/compat/linuxkpi/common/include/linux/netdevice.h index f8c03f92b025..a904b7e70490 100644 --- a/sys/compat/linuxkpi/common/include/linux/netdevice.h +++ b/sys/compat/linuxkpi/common/include/linux/netdevice.h @@ -5,7 +5,7 @@ * Copyright (c) 2013-2019 Mellanox Technologies, Ltd. * All rights reserved. * Copyright (c) 2020-2021 The FreeBSD Foundation - * Copyright (c) 2020-2021 Bjoern A. Zeeb + * Copyright (c) 2020-2022 Bjoern A. Zeeb * * Portions of this software were developed by Björn Zeeb * under sponsorship from the FreeBSD Foundation. @@ -182,6 +182,30 @@ int unregister_inetaddr_notifier(struct notifier_block *); #define NAPI_POLL_WEIGHT 64 /* budget */ +/* + * There are drivers directly testing napi state bits, so we need to publicly + * expose them. If you ask me, those accesses should be hid behind an + * inline function and the bit flags not be directly exposed. + */ +enum napi_state_bits { + /* + * Official Linux flags encountered. + */ + NAPI_STATE_SCHED = 1, + + /* + * Our internal versions (for now). + */ + /* Do not schedule new things while we are waiting to clear things. */ + LKPI_NAPI_FLAG_DISABLE_PENDING = 0, + /* To synchronise that only one poll is ever running. */ + LKPI_NAPI_FLAG_IS_SCHEDULED = 1, + /* If trying to schedule while poll is running. Need to re-schedule. */ + LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN = 2, + /* When shutting down forcefully prevent anything from running task/poll. */ + LKPI_NAPI_FLAG_SHUTDOWN = 3, +}; + struct napi_struct { TAILQ_ENTRY(napi_struct) entry; @@ -191,11 +215,12 @@ struct napi_struct { int budget; int rx_count; + /* * These flags mostly need to be checked/changed atomically * (multiple together in some cases). */ - volatile unsigned long _flags; + volatile unsigned long state; /* FreeBSD internal. */ /* Use task for now, so we can easily switch between direct and task. */ diff --git a/sys/compat/linuxkpi/common/src/linux_netdev.c b/sys/compat/linuxkpi/common/src/linux_netdev.c index 27e29b40ea44..3055b9c46767 100644 --- a/sys/compat/linuxkpi/common/src/linux_netdev.c +++ b/sys/compat/linuxkpi/common/src/linux_netdev.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2021 The FreeBSD Foundation + * Copyright (c) 2022 Bjoern A. Zeeb * * This software was developed by Björn Zeeb under sponsorship from * the FreeBSD Foundation. @@ -48,14 +49,6 @@ MALLOC_DEFINE(M_NETDEV, "lkpindev", "Linux KPI netdevice compat"); #define NAPI_UNLOCK(_ndev) mtx_unlock(&(_ndev)->napi_mtx) /* -------------------------------------------------------------------------- */ -/* Do not schedule new things while we are waiting to clear things. */ -#define LKPI_NAPI_FLAG_DISABLE_PENDING 0 -/* To synchronise that only one poll is ever running. */ -#define LKPI_NAPI_FLAG_IS_SCHEDULED 1 -/* If trying to schedule while poll is running. Need to re-schedule. */ -#define LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN 2 -/* When shutting down forcefully prevent anything from running task/poll. */ -#define LKPI_NAPI_FLAG_SHUTDOWN 3 #define LKPI_NAPI_FLAGS \ "\20\1DISABLE_PENDING\2IS_SCHEDULED\3LOST_RACE_TRY_AGAIN" @@ -74,17 +67,17 @@ SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug_napi, CTLFLAG_RWTUN, #define NAPI_TRACE(_n) if (debug_napi & DNAPI_TRACE) \ printf("NAPI_TRACE %s:%d %u %p (%#jx %b)\n", __func__, __LINE__, \ - (unsigned int)ticks, _n, (uintmax_t)(_n)->_flags, \ - (int)(_n)->_flags, LKPI_NAPI_FLAGS) + (unsigned int)ticks, _n, (uintmax_t)(_n)->state, \ + (int)(_n)->state, LKPI_NAPI_FLAGS) #define NAPI_TRACE2D(_n, _d) if (debug_napi & DNAPI_TRACE) \ printf("NAPI_TRACE %s:%d %u %p (%#jx %b) %d\n", __func__, __LINE__, \ - (unsigned int)ticks, _n, (uintmax_t)(_n)->_flags, \ - (int)(_n)->_flags, LKPI_NAPI_FLAGS, _d) + (unsigned int)ticks, _n, (uintmax_t)(_n)->state, \ + (int)(_n)->state, LKPI_NAPI_FLAGS, _d) #define NAPI_TRACE_TASK(_n, _p, _c) if (debug_napi & DNAPI_TRACE_TASK) \ printf("NAPI_TRACE %s:%d %u %p (%#jx %b) pending %d count %d " \ "rx_count %d\n", __func__, __LINE__, \ - (unsigned int)ticks, _n, (uintmax_t)(_n)->_flags, \ - (int)(_n)->_flags, LKPI_NAPI_FLAGS, _p, _c, (_n)->rx_count) + (unsigned int)ticks, _n, (uintmax_t)(_n)->state, \ + (int)(_n)->state, LKPI_NAPI_FLAGS, _p, _c, (_n)->rx_count) #define NAPI_TODO() if (debug_napi & DNAPI_TODO) \ printf("NAPI_TODO %s:%d %d\n", __func__, __LINE__, ticks) #define NAPI_IMPROVE() if (debug_napi & DNAPI_IMPROVE) \ @@ -118,7 +111,7 @@ linuxkpi_napi_schedule_prep(struct napi_struct *napi) /* Can can only update/return if all flags agree. */ do { - old = READ_ONCE(napi->_flags); + old = READ_ONCE(napi->state); /* If we are stopping, cannot run again. */ if ((old & BIT(LKPI_NAPI_FLAG_DISABLE_PENDING)) != 0) { @@ -132,7 +125,7 @@ linuxkpi_napi_schedule_prep(struct napi_struct *napi) new |= BIT(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN); new |= BIT(LKPI_NAPI_FLAG_IS_SCHEDULED); - } while (atomic_cmpset_acq_long(&napi->_flags, old, new) == 0); + } while (atomic_cmpset_acq_long(&napi->state, old, new) == 0); NAPI_TRACE(napi); return ((old & BIT(LKPI_NAPI_FLAG_IS_SCHEDULED)) == 0); @@ -157,14 +150,14 @@ again: goto again; /* Bandaid for now. */ - if (test_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &napi->_flags)) + if (test_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &napi->state)) goto again; do { - new = old = READ_ONCE(napi->_flags); + new = old = READ_ONCE(napi->state); clear_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &new); clear_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &new); - } while (atomic_cmpset_acq_long(&napi->_flags, old, new) == 0); + } while (atomic_cmpset_acq_long(&napi->state, old, new) == 0); NAPI_TRACE2D(napi, rc); } @@ -175,9 +168,9 @@ linuxkpi___napi_schedule(struct napi_struct *napi) int rc; NAPI_TRACE(napi); - if (test_bit(LKPI_NAPI_FLAG_SHUTDOWN, &napi->_flags)) { - clear_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &napi->_flags); - clear_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->_flags); + if (test_bit(LKPI_NAPI_FLAG_SHUTDOWN, &napi->state)) { + clear_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &napi->state); + clear_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state); NAPI_TRACE(napi); return; } @@ -229,7 +222,7 @@ linuxkpi_napi_complete_done(struct napi_struct *napi, int ret) return (true); do { - new = old = READ_ONCE(napi->_flags); + new = old = READ_ONCE(napi->state); /* * If we lost a race before, we need to re-schedule. @@ -238,7 +231,7 @@ linuxkpi_napi_complete_done(struct napi_struct *napi, int ret) if (!test_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &old)) clear_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &new); clear_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &new); - } while (atomic_cmpset_acq_long(&napi->_flags, old, new) == 0); + } while (atomic_cmpset_acq_long(&napi->state, old, new) == 0); NAPI_TRACE(napi); @@ -263,10 +256,10 @@ void linuxkpi_napi_disable(struct napi_struct *napi) { NAPI_TRACE(napi); - set_bit(LKPI_NAPI_FLAG_DISABLE_PENDING, &napi->_flags); - while (test_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->_flags)) + set_bit(LKPI_NAPI_FLAG_DISABLE_PENDING, &napi->state); + while (test_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state)) pause_sbt("napidslp", SBT_1MS, 0, C_HARDCLOCK); - clear_bit(LKPI_NAPI_FLAG_DISABLE_PENDING, &napi->_flags); + clear_bit(LKPI_NAPI_FLAG_DISABLE_PENDING, &napi->state); } void @@ -274,11 +267,11 @@ linuxkpi_napi_enable(struct napi_struct *napi) { NAPI_TRACE(napi); - KASSERT(!test_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->_flags), + KASSERT(!test_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state), ("%s: enabling napi %p already scheduled\n", __func__, napi)); mb(); /* Let us be scheduled. */ - clear_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->_flags); + clear_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state); } void @@ -287,7 +280,7 @@ linuxkpi_napi_synchronize(struct napi_struct *napi) NAPI_TRACE(napi); #if defined(SMP) /* Check & sleep while a napi is scheduled. */ - while (test_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->_flags)) + while (test_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state)) pause_sbt("napisslp", SBT_1MS, 0, C_HARDCLOCK); #else mb(); @@ -350,7 +343,7 @@ linuxkpi_netif_napi_add(struct net_device *ndev, struct napi_struct *napi, NAPI_UNLOCK(ndev); /* Anything else to do on the ndev? */ - clear_bit(LKPI_NAPI_FLAG_SHUTDOWN, &napi->_flags); + clear_bit(LKPI_NAPI_FLAG_SHUTDOWN, &napi->state); } static void @@ -361,7 +354,7 @@ lkpi_netif_napi_del_locked(struct napi_struct *napi) ndev = napi->dev; NAPI_LOCK_ASSERT(ndev); - set_bit(LKPI_NAPI_FLAG_SHUTDOWN, &napi->_flags); + set_bit(LKPI_NAPI_FLAG_SHUTDOWN, &napi->state); TAILQ_REMOVE(&ndev->napi_head, napi, entry); while (taskqueue_cancel(ndev->napi_tq, &napi->napi_task, NULL) != 0) taskqueue_drain(ndev->napi_tq, &napi->napi_task);