From nobody Mon Feb 9 17:20:37 2026 Received: from zg8tmtyylji0my4xnjeumjiw.icoremail.net (zg8tmtyylji0my4xnjeumjiw.icoremail.net [162.243.161.220]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 72C8E14AD37; Thu, 9 May 2024 09:36:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.243.161.220 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715247413; cv=none; b=WJhJPDFjLyWWOHh16eW94JGsLkWqCpj/qKaxyoD2kpJ5A8WcQr1j8GksgmvvhXpLm0z5baisBirYVaMUk5aWRf2FqbGXc52HNZ6ngbMs8Y/2gVRvxxGPuW701Ftafhrb2n9Pay4goz6o13BeiaZIr/JmELn8hjK+tJsSuxsCvJ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715247413; c=relaxed/simple; bh=3zCGowu0qqLGgytGgGhf26xsCg/8t3/q/Is3l5ly7iQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=pqSYQS8nMx47bzDzcYF8H3CLIADGHMchZAy+BpDhtwngOF9mthlXGBTosxgxY6ulj3ouy2woeR+0b5nfwmPj/yztruoGqDbHqasxBp6LmVrCaIqHnnx9Rbe2ZK9o9SwcFwkoOnVAoj9sMg8sHL4/+RgSxiKKAwZX7aNKaouA1tY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zju.edu.cn; spf=pass smtp.mailfrom=zju.edu.cn; arc=none smtp.client-ip=162.243.161.220 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zju.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zju.edu.cn Received: from ubuntu.localdomain (unknown [221.192.180.131]) by mail-app4 (Coremail) with SMTP id cS_KCgD3hLIimTxmfcJPAA--.62319S2; Thu, 09 May 2024 17:36:37 +0800 (CST) From: Duoming Zhou To: netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-hams@vger.kernel.org, pabeni@redhat.com, kuba@kernel.org, edumazet@google.com, jreuter@yaina.de, dan.carpenter@linaro.org, rkannoth@marvell.com, davem@davemloft.net, lars@oddbit.com, Duoming Zhou Subject: [PATCH net v7 1/3] ax25: Use kernel universal linked list to implement ax25_dev_list Date: Thu, 9 May 2024 17:36:33 +0800 Message-Id: <85bba3af651ca0e1a519da8d0d715b949891171c.1715247018.git.duoming@zju.edu.cn> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-CM-TRANSID: cS_KCgD3hLIimTxmfcJPAA--.62319S2 X-Coremail-Antispam: 1UD129KBjvJXoWxCryUJFyUZrW5Kr1DAFy7ZFb_yoWrur15pF ZIkF1fArZ7Jr1UAr4DWF1xWr1YvryUt39rAry5uF1Skw1qq3s8Jr1ktryUJryUGrW3Ar18 J34UXr4DAr48ur7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9K14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26F4U JVW0owA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oV Cq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0 I7IYx2IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r 4UM4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628v n2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCY02Avz4vE14v_GFWl42xK82IYc2Ij64vIr4 1l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK 67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI 8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAv wI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14 v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjfU0gAwDUUUU X-CM-SenderInfo: qssqjiasttq6lmxovvfxof0/1tbiAwIQAWY7nwoPXwAesW Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The origin ax25_dev_list implements its own single linked list, which is complicated and error-prone. For example, when deleting the node of ax25_dev_list in ax25_dev_device_down(), we have to operate on the head node and other nodes separately. This patch uses kernel universal linked list to replace original ax25_dev_list, which make the operation of ax25_dev_list easier. We should do "dev->ax25_ptr =3D ax25_dev;" and "dev->ax25_ptr =3D NULL;" while holding the spinlock, otherwise the ax25_dev_device_up() and ax25_dev_device_down() could race. Suggested-by: Dan Carpenter Signed-off-by: Duoming Zhou --- include/net/ax25.h | 3 +-- net/ax25/ax25_dev.c | 40 +++++++++++++++------------------------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/include/net/ax25.h b/include/net/ax25.h index 0d939e5aee4..c2a85fd3f5e 100644 --- a/include/net/ax25.h +++ b/include/net/ax25.h @@ -216,7 +216,7 @@ typedef struct { struct ctl_table; =20 typedef struct ax25_dev { - struct ax25_dev *next; + struct list_head list; =20 struct net_device *dev; netdevice_tracker dev_tracker; @@ -330,7 +330,6 @@ int ax25_addr_size(const ax25_digi *); void ax25_digi_invert(const ax25_digi *, ax25_digi *); =20 /* ax25_dev.c */ -extern ax25_dev *ax25_dev_list; extern spinlock_t ax25_dev_lock; =20 #if IS_ENABLED(CONFIG_AX25) diff --git a/net/ax25/ax25_dev.c b/net/ax25/ax25_dev.c index 282ec581c07..f16ee5c09d0 100644 --- a/net/ax25/ax25_dev.c +++ b/net/ax25/ax25_dev.c @@ -22,11 +22,12 @@ #include #include #include +#include #include #include #include =20 -ax25_dev *ax25_dev_list; +static LIST_HEAD(ax25_dev_list); DEFINE_SPINLOCK(ax25_dev_lock); =20 ax25_dev *ax25_addr_ax25dev(ax25_address *addr) @@ -34,7 +35,7 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr) ax25_dev *ax25_dev, *res =3D NULL; =20 spin_lock_bh(&ax25_dev_lock); - for (ax25_dev =3D ax25_dev_list; ax25_dev !=3D NULL; ax25_dev =3D ax25_de= v->next) + list_for_each_entry(ax25_dev, &ax25_dev_list, list) if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) =3D=3D = 0) { res =3D ax25_dev; ax25_dev_hold(ax25_dev); @@ -59,7 +60,6 @@ void ax25_dev_device_up(struct net_device *dev) } =20 refcount_set(&ax25_dev->refcount, 1); - dev->ax25_ptr =3D ax25_dev; ax25_dev->dev =3D dev; netdev_hold(dev, &ax25_dev->dev_tracker, GFP_KERNEL); ax25_dev->forward =3D NULL; @@ -85,8 +85,8 @@ void ax25_dev_device_up(struct net_device *dev) #endif =20 spin_lock_bh(&ax25_dev_lock); - ax25_dev->next =3D ax25_dev_list; - ax25_dev_list =3D ax25_dev; + list_add(&ax25_dev->list, &ax25_dev_list); + dev->ax25_ptr =3D ax25_dev; spin_unlock_bh(&ax25_dev_lock); ax25_dev_hold(ax25_dev); =20 @@ -111,32 +111,25 @@ void ax25_dev_device_down(struct net_device *dev) /* * Remove any packet forwarding that points to this device. */ - for (s =3D ax25_dev_list; s !=3D NULL; s =3D s->next) + list_for_each_entry(s, &ax25_dev_list, list) if (s->forward =3D=3D dev) s->forward =3D NULL; =20 - if ((s =3D ax25_dev_list) =3D=3D ax25_dev) { - ax25_dev_list =3D s->next; - goto unlock_put; - } - - while (s !=3D NULL && s->next !=3D NULL) { - if (s->next =3D=3D ax25_dev) { - s->next =3D ax25_dev->next; + list_for_each_entry(s, &ax25_dev_list, list) { + if (s =3D=3D ax25_dev) { + list_del(&s->list); goto unlock_put; } - - s =3D s->next; } - spin_unlock_bh(&ax25_dev_lock); dev->ax25_ptr =3D NULL; + spin_unlock_bh(&ax25_dev_lock); ax25_dev_put(ax25_dev); return; =20 unlock_put: + dev->ax25_ptr =3D NULL; spin_unlock_bh(&ax25_dev_lock); ax25_dev_put(ax25_dev); - dev->ax25_ptr =3D NULL; netdev_put(dev, &ax25_dev->dev_tracker); ax25_dev_put(ax25_dev); } @@ -200,16 +193,13 @@ struct net_device *ax25_fwd_dev(struct net_device *de= v) */ void __exit ax25_dev_free(void) { - ax25_dev *s, *ax25_dev; + ax25_dev *s, *n; =20 spin_lock_bh(&ax25_dev_lock); - ax25_dev =3D ax25_dev_list; - while (ax25_dev !=3D NULL) { - s =3D ax25_dev; - netdev_put(ax25_dev->dev, &ax25_dev->dev_tracker); - ax25_dev =3D ax25_dev->next; + list_for_each_entry_safe(s, n, &ax25_dev_list, list) { + netdev_put(s->dev, &s->dev_tracker); + list_del(&s->list); kfree(s); } - ax25_dev_list =3D NULL; spin_unlock_bh(&ax25_dev_lock); } --=20 2.17.1