Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp31662pxb; Tue, 12 Apr 2022 15:59:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwMUhk2bdCjO2Jln7+72WIIhL23HDp2dGiiL13j+JM5T5yAvFvfyhZKxZaJLFuizYzOT89Y X-Received: by 2002:a63:b51a:0:b0:39d:ac7f:d5ba with SMTP id y26-20020a63b51a000000b0039dac7fd5bamr1400318pge.497.1649804390730; Tue, 12 Apr 2022 15:59:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649804390; cv=none; d=google.com; s=arc-20160816; b=oya+AtOUOD60+5+o1ZQtdJ+FGift6Pk0cLB89VXcejEf3TEuOsfdOKVu//FnvQv/A4 sg2aQHU00hca8ikYWuHojiaSjkGAGg++EyK20Ty4kBpjBG9hjitjnNbBEqUKv08D36EV 18MarC4Hpb0wgR3dfaknzIAIgTa9W8eMw6V+dsSZnocwjcog+du51EYZnSA+tLb49hZ0 Klw4oU0nsgrhCaeXpM1hINvh4ZksCI1qrf35z73W/aMBsfbvk858tWAQBNhdXVUKQQ9L tr5HIo6dKeR45vrDi4qzzfjmpBZQVS7l3rB0lUeZ5YpI84JCbQMaou+Eu10ZXdiu7rFN a1ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=UiT3RVqcqw/2HXUfTd1U2cSmXIntDYXehGziaThTk/w=; b=nMH74pIDbX8V7c1lvz0zb3TVnE0XP0qBffkpiYwPDOb0g2c+dWvDJva1Q51Z57fmW1 SyiEdW85NkJvlkq+iR9vbKr4Qblt/ogFiD2LoMfu16Z0GCIp48F4HixdqVfdmkcfhTLT 49mKao+Wyalif+VBKlxxrjjA4lf+vxFVU5gXTzXhD6JZJJwEVq6K26U9rhb14U5apogd FUJG/AqxoJQWNUmOWl+X1m0+4S9D5anClmkIZYS4s3AfOkokKS6YffJ9YMqwRe/PSSXF 7LAa9cYemt3a/mt1+BLf1XvO0gX7kLB/xnCyrOHv3+aOZqbK1dtM471iF8ZlUKghmSVk Fk3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="JGp/u8YO"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id g4-20020a170902d1c400b00153b2d1663fsi11130036plb.583.2022.04.12.15.59.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 15:59:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="JGp/u8YO"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2D31A1DB892; Tue, 12 Apr 2022 14:41:16 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354357AbiDLMwJ (ORCPT + 99 others); Tue, 12 Apr 2022 08:52:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355271AbiDLMsJ (ORCPT ); Tue, 12 Apr 2022 08:48:09 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1032108; Tue, 12 Apr 2022 05:16:25 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id c64so9949460edf.11; Tue, 12 Apr 2022 05:16:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UiT3RVqcqw/2HXUfTd1U2cSmXIntDYXehGziaThTk/w=; b=JGp/u8YOUoDMQ9pAhLEQ+0LMkcB5cI0GNQNFLXAOk4BGHADL07FszPBr5jGzRq8Tn0 slB2wZTGjbUgLYXJfWCTwYRsijMCC2I4oME+8AnMdSZ5bLyrxNqUjALgI/gCk/uyjsKQ pcctSnVwr3XQefY2xtkgv6RvCt0K9TlCjvnqXpSlgMYnp611tNu6hmJCSZ3oLhcuLsWP W0uFp//crFSEGAj02bMTUcJeIBv2pLqFJH6TeE2nZRY9ffeHW72lqboltt+IE7Dc+bqK /TAVsU0AjGorOdL17dllOsWrJn1sJ/ovvtzz+Lp7xifmGMY5UHcGdTjhxtBzCW8IsN7N W5BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UiT3RVqcqw/2HXUfTd1U2cSmXIntDYXehGziaThTk/w=; b=Cg/xEoszwE03X4liShCMrfzG0dKtgNJUt5zW+v/GXmy7HP1StzlTPA65C+5DNUixH3 kjXnLfAFLuPXAm7i6b9N3EBQt0vxjrZJLhaHvhSMHYL6JuxNP/IGjIRPtIKTl/Hjz5kf H3jipnNbdzniYs+qNSiKCK+tSE6AUhP6z61yCX/V/BecbUYQvfRzMqQd8ljjmHg6Zc+h xu6jqm/sowwqqnDzjYvsSk31yguZVexSDK322uWcZXRp6s18jD/V7Kupp4SdI0jkBDEn Zodsff4GiGJ5HIINSVHGPl5M+VeTtNuc7yoMhPcjLy+FSKC9M253lPeYyjWtZRpPZIBy NTtQ== X-Gm-Message-State: AOAM533q0ytZd48mzqVgxuyRfvFyuH9yPRlrxpuld40D9NLPNZy6OeT0 OzGXWUfPKhUupepxEahUPBc= X-Received: by 2002:a05:6402:11d0:b0:419:65c5:cde4 with SMTP id j16-20020a05640211d000b0041965c5cde4mr38884523edw.73.1649765784091; Tue, 12 Apr 2022 05:16:24 -0700 (PDT) Received: from localhost.localdomain (i130160.upc-i.chello.nl. [62.195.130.160]) by smtp.googlemail.com with ESMTPSA id b5-20020a17090630c500b006e8044fa76bsm8827900ejb.143.2022.04.12.05.16.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 05:16:23 -0700 (PDT) From: Jakob Koschel To: "David S. Miller" Cc: Jakub Kicinski , Paolo Abeni , Andrew Lunn , Vivien Didelot , Florian Fainelli , Vladimir Oltean , Lars Povlsen , Steen Hegelund , UNGLinuxDriver@microchip.com, Ariel Elior , Manish Chopra , Edward Cree , Martin Habets , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Jiri Pirko , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Casper Andersson , Bjarni Jonasson , Jakob Koschel , Christophe JAILLET , Michael Walle , Jason Gunthorpe , Colin Ian King , Arnd Bergmann , Eric Dumazet , Di Zhu , Xu Wang , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, bpf@vger.kernel.org, Mike Rapoport , "Brian Johannesmeyer" , Cristiano Giuffrida , "Bos, H.J." , Vladimir Oltean Subject: [PATCH net-next v3 02/18] net: dsa: sja1105: remove use of iterator after list_for_each_entry() loop Date: Tue, 12 Apr 2022 14:15:41 +0200 Message-Id: <20220412121557.3553555-3-jakobkoschel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412121557.3553555-1-jakobkoschel@gmail.com> References: <20220412121557.3553555-1-jakobkoschel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Vladimir Oltean The link below explains that there is a desire to syntactically change list_for_each_entry() and list_for_each() such that it becomes impossible to use the iterator variable outside the scope of the loop. Although sja1105_insert_gate_entry() makes legitimate use of the iterator pointer when it breaks out, the pattern it uses may become illegal, so it needs to change. It is deemed acceptable to use a copy of the loop iterator, and sja1105_insert_gate_entry() only needs to know the list_head element before which the list insertion should be made. So let's profit from the occasion and refactor the list iteration to a dedicated function. An additional benefit is given by the fact that with the helper function in place, we no longer need to special-case the empty list, since it is equivalent to not having found any gating entry larger than the specified interval in the list. We just need to insert at the tail of that list (list_add vs list_add_tail on an empty list does the same thing). Link: https://patchwork.kernel.org/project/netdevbpf/patch/20220407102900.3086255-3-jakobkoschel@gmail.com/#24810127 Signed-off-by: Vladimir Oltean Signed-off-by: Jakob Koschel --- drivers/net/dsa/sja1105/sja1105_vl.c | 46 ++++++++++++++++++---------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/drivers/net/dsa/sja1105/sja1105_vl.c b/drivers/net/dsa/sja1105/sja1105_vl.c index b7e95d60a6e4..369be2ac3587 100644 --- a/drivers/net/dsa/sja1105/sja1105_vl.c +++ b/drivers/net/dsa/sja1105/sja1105_vl.c @@ -7,6 +7,27 @@ #define SJA1105_SIZE_VL_STATUS 8 +static struct list_head * +sja1105_first_entry_longer_than(struct list_head *entries, + s64 interval, + struct netlink_ext_ack *extack) +{ + struct sja1105_gate_entry *p; + + list_for_each_entry(p, entries, list) { + if (p->interval == interval) { + NL_SET_ERR_MSG_MOD(extack, "Gate conflict"); + return ERR_PTR(-EBUSY); + } + + if (interval < p->interval) + return &p->list; + } + + /* Empty list, or specified interval is largest within the list */ + return entries; +} + /* Insert into the global gate list, sorted by gate action time. */ static int sja1105_insert_gate_entry(struct sja1105_gating_config *gating_cfg, struct sja1105_rule *rule, @@ -14,6 +35,7 @@ static int sja1105_insert_gate_entry(struct sja1105_gating_config *gating_cfg, struct netlink_ext_ack *extack) { struct sja1105_gate_entry *e; + struct list_head *pos; int rc; e = kzalloc(sizeof(*e), GFP_KERNEL); @@ -24,25 +46,15 @@ static int sja1105_insert_gate_entry(struct sja1105_gating_config *gating_cfg, e->gate_state = gate_state; e->interval = entry_time; - if (list_empty(&gating_cfg->entries)) { - list_add(&e->list, &gating_cfg->entries); - } else { - struct sja1105_gate_entry *p; - - list_for_each_entry(p, &gating_cfg->entries, list) { - if (p->interval == e->interval) { - NL_SET_ERR_MSG_MOD(extack, - "Gate conflict"); - rc = -EBUSY; - goto err; - } - - if (e->interval < p->interval) - break; - } - list_add(&e->list, p->list.prev); + pos = sja1105_first_entry_longer_than(&gating_cfg->entries, + e->interval, extack); + if (IS_ERR(pos)) { + rc = PTR_ERR(pos); + goto err; } + list_add(&e->list, pos->prev); + gating_cfg->num_entries++; return 0; -- 2.25.1