Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp315744lqh; Mon, 6 May 2024 22:52:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCURP/5n+3DFtfWHyM1ZxQ2EtJAHvWL+x6yMZ9b2Yc/SZqjnMnFKR562Edz8IOeBIGfvFN9YSMf1nxwy8tgcgSepUoO6Uwi2JH7hLr+9Mg== X-Google-Smtp-Source: AGHT+IE4TH6xcuWrqgARz0poPo9mr4fXltoCQaUoyEoZPhU48wKXo0Yo+Iv0MRF3GLi7bA1vw2KI X-Received: by 2002:a05:6a21:2d87:b0:1af:ab09:c555 with SMTP id ty7-20020a056a212d8700b001afab09c555mr6375177pzb.47.1715061171959; Mon, 06 May 2024 22:52:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715061171; cv=pass; d=google.com; s=arc-20160816; b=gDT3OsucEnpacmorMRMHGmkAgo9T9GHczZuDc+ug+eNTaD7J5CpbqSEUPmT//GN5qI wqGfE+La8eB0oy21xPmiTg92zid8h7tbQC3A2tyuc4XsDMp31c0AmBA2vRXz2aOcTnPa Cdp9i3+hLAn2kRIM2zbOzkKm+lQK6icE9GOoac7AK50Z0wiezHwQpGC1dgk7awUx3Y1f 63a0CdA9E8w+gveZe8msJ8Z+qX6CZyNJ0MWKOR9gGOTBaz+xMrh8KUdEcHy4P+HvdiAe TKgjbVsAdXJQzFdrfCo7XhtpJgmJ9d05BX3vAiSIGtcdezczKnbyr1KzPHcEqGgejk8W aPgA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:from; bh=UNBrnv2patLrgh+tPhTXaJspoFgdaNH9+QO4hAxA0Zk=; fh=IE3AZvDm9DC3cdTbYv1PuJ+zr4mdKz6MWG85KR/OiZ4=; b=04a4ZbDA3sdLTHtY7an5wTucsPm7kxffqENKvpqA0wvUdRdfL+cAl51RvKWsNEiEPO pxpls05qV7hGOvi/7RJEY6Puw7OpwYDYQJLFpMZgSemLSxQrJ124upQBlfQ5SEf1ph1R Jaz/K7FXCNhL4kgGEOk/Sl+IjmOWdOdtIEqMUyl5EMHs45tChpo8xv1keQm7cur+QFu5 xKvusB+jT3QhM/Xka5fJ6XPgOM+Rs0HXH1VZHDc1g4iLls1DLe4jhmt2VpLY/nDzapMJ eqyffiXUqNOQMd1PjTZDcqjaW0Y8Q7SJraCtxzdevlThJfLP6KpyIBbPucArUtneKfeI 7nag==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=zju.edu.cn); spf=pass (google.com: domain of linux-kernel+bounces-170707-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170707-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id u16-20020a170903125000b001e48565b405si9197388plh.522.2024.05.06.22.52.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 22:52:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-170707-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=zju.edu.cn); spf=pass (google.com: domain of linux-kernel+bounces-170707-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170707-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 904E5281F7B for ; Tue, 7 May 2024 05:52:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4D9CC6F086; Tue, 7 May 2024 05:52:32 +0000 (UTC) Received: from zg8tmja2lje4os43os4xodqa.icoremail.net (zg8tmja2lje4os43os4xodqa.icoremail.net [206.189.79.184]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 213476BB20; Tue, 7 May 2024 05:52:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=206.189.79.184 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715061151; cv=none; b=KXCgAdZbPPAmpCgM/1sR87kFXBf/y6GSwgJCp6vMwUrgK6ICkQuDCsZjTAdzDOVEsiUzv1PSjgY1269OooJaOaKr7hkdhit9SFE/abYg6dZaxNMhuGyuXsbR5hDe8Na/8Sx1uElHKLzp/1efYWlLwkO7F19cnrh62ezxI4NzpQo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715061151; c=relaxed/simple; bh=iPoHwd0Unl4KmoAupFeQEy87jbtpri5U8RLKSUTq3xE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=AKpWmNIuia7nAr6KY78HtjfpN4osKd+UCJGtrKprV0qBmzTCoxBA2wng250RjuzGDDZ2nACXtL5L9GEiezo2k+J5XEMEcujLK+NSeal1rugYZvxJTb7gPpmXs1cxMitCJr6xiQzlbEQ7N8DM6jipJ2NKdpT22VoiHQEAVTTrtrg= 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=206.189.79.184 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.179.90]) by mail-app2 (Coremail) with SMTP id by_KCgD3FKSNwTlmWzFDAA--.40357S2; Tue, 07 May 2024 13:52:16 +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, davem@davemloft.net, jreuter@yaina.de, horms@kernel.org, Markus.Elfring@web.de, dan.carpenter@linaro.org, lars@oddbit.com, Duoming Zhou Subject: [PATCH net v4 1/4] ax25: Use kernel universal linked list to implement ax25_dev_list Date: Tue, 7 May 2024 13:52:12 +0800 Message-Id: <5022fa6a280c3fa852bf3724149251c41ee8303f.1715059894.git.duoming@zju.edu.cn> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-CM-TRANSID:by_KCgD3FKSNwTlmWzFDAA--.40357S2 X-Coremail-Antispam: 1UD129KBjvJXoW3GFyfGF1fury3AFWrAr1UWrg_yoW7WFWDpF ZIkF1rArZ7Jr1UAr4DWF1xWr1YyryUt3yDAry5uFySkw1DX3s8Jr1ktryUJryUGrW3Ar18 J34UWr4DAr48ZF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9K14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26F4U JVW0owA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oV Cq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0 I7IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r 4UM4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628v n2kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCY02Avz4vE14v_GrWl42xK82IYc2Ij64vIr4 1l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK 67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI 8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAv wI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14 v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjfUra9-UUUUU X-CM-SenderInfo: qssqjiasttq6lmxovvfxof0/1tbiAwMOAWY4-AkEPQA8sh Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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. There are two points that need to notice: [1] We should add a check to judge whether the list is empty before INIT_LIST_HEAD in ax25_dev_device_up(), otherwise it will empty the list for each new ax25_dev added. [2] We should do "dev->ax25_ptr = ax25_dev;" and "dev->ax25_ptr = NULL;" while holding the spinlock, otherwise the ax25_dev_device_up() and ax25_dev_device_down() could race, we're not guaranteed to find a match ax25_dev in ax25_dev_device_down(). Suggested-by: Dan Carpenter Signed-off-by: Duoming Zhou --- Changes in v4: - Make the linux list API as a separate update step. - Add a check before INIT_LIST_HEAD. - Do "dev->ax25_ptr = ax25_dev;" while holding the spinlock. include/net/ax25.h | 4 ++-- net/ax25/ax25_dev.c | 42 +++++++++++++++++------------------------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/include/net/ax25.h b/include/net/ax25.h index 0d939e5aee4..92c6aa4f9a6 100644 --- a/include/net/ax25.h +++ b/include/net/ax25.h @@ -216,7 +216,7 @@ typedef struct { struct ctl_table; typedef struct ax25_dev { - struct ax25_dev *next; + struct list_head list; struct net_device *dev; netdevice_tracker dev_tracker; @@ -330,7 +330,7 @@ int ax25_addr_size(const ax25_digi *); void ax25_digi_invert(const ax25_digi *, ax25_digi *); /* ax25_dev.c */ -extern ax25_dev *ax25_dev_list; +static struct list_head ax25_dev_list; extern spinlock_t ax25_dev_lock; #if IS_ENABLED(CONFIG_AX25) diff --git a/net/ax25/ax25_dev.c b/net/ax25/ax25_dev.c index 282ec581c07..d4e1e36a6a8 100644 --- a/net/ax25/ax25_dev.c +++ b/net/ax25/ax25_dev.c @@ -22,11 +22,11 @@ #include #include #include +#include #include #include #include -ax25_dev *ax25_dev_list; DEFINE_SPINLOCK(ax25_dev_lock); ax25_dev *ax25_addr_ax25dev(ax25_address *addr) @@ -34,7 +34,7 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr) ax25_dev *ax25_dev, *res = NULL; spin_lock_bh(&ax25_dev_lock); - for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next) + list_for_each_entry(ax25_dev, &ax25_dev_list, list) if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) { res = ax25_dev; ax25_dev_hold(ax25_dev); @@ -52,6 +52,9 @@ void ax25_dev_device_up(struct net_device *dev) { ax25_dev *ax25_dev; + /* Initialized the list for the first entry */ + if (!ax25_dev_list.next) + INIT_LIST_HEAD(&ax25_dev_list); ax25_dev = kzalloc(sizeof(*ax25_dev), GFP_KERNEL); if (!ax25_dev) { printk(KERN_ERR "AX.25: ax25_dev_device_up - out of memory\n"); @@ -59,7 +62,6 @@ void ax25_dev_device_up(struct net_device *dev) } refcount_set(&ax25_dev->refcount, 1); - dev->ax25_ptr = ax25_dev; ax25_dev->dev = dev; netdev_hold(dev, &ax25_dev->dev_tracker, GFP_KERNEL); ax25_dev->forward = NULL; @@ -85,8 +87,8 @@ void ax25_dev_device_up(struct net_device *dev) #endif spin_lock_bh(&ax25_dev_lock); - ax25_dev->next = ax25_dev_list; - ax25_dev_list = ax25_dev; + list_add(&ax25_dev->list, &ax25_dev_list); + dev->ax25_ptr = ax25_dev; spin_unlock_bh(&ax25_dev_lock); ax25_dev_hold(ax25_dev); @@ -111,32 +113,25 @@ void ax25_dev_device_down(struct net_device *dev) /* * Remove any packet forwarding that points to this device. */ - for (s = ax25_dev_list; s != NULL; s = s->next) + list_for_each_entry(s, &ax25_dev_list, list) if (s->forward == dev) s->forward = NULL; - if ((s = ax25_dev_list) == ax25_dev) { - ax25_dev_list = s->next; - goto unlock_put; - } - - while (s != NULL && s->next != NULL) { - if (s->next == ax25_dev) { - s->next = ax25_dev->next; + list_for_each_entry(s, &ax25_dev_list, list) { + if (s == ax25_dev) { + list_del(&s->list); goto unlock_put; } - - s = s->next; } - spin_unlock_bh(&ax25_dev_lock); dev->ax25_ptr = NULL; + spin_unlock_bh(&ax25_dev_lock); ax25_dev_put(ax25_dev); return; unlock_put: + dev->ax25_ptr = NULL; spin_unlock_bh(&ax25_dev_lock); ax25_dev_put(ax25_dev); - dev->ax25_ptr = NULL; netdev_put(dev, &ax25_dev->dev_tracker); ax25_dev_put(ax25_dev); } @@ -200,16 +195,13 @@ struct net_device *ax25_fwd_dev(struct net_device *dev) */ void __exit ax25_dev_free(void) { - ax25_dev *s, *ax25_dev; + ax25_dev *s, *n; spin_lock_bh(&ax25_dev_lock); - ax25_dev = ax25_dev_list; - while (ax25_dev != NULL) { - s = ax25_dev; - netdev_put(ax25_dev->dev, &ax25_dev->dev_tracker); - ax25_dev = 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 = NULL; spin_unlock_bh(&ax25_dev_lock); } -- 2.17.1