2022-03-25 19:45:33

by Jakob Koschel

[permalink] [raw]
Subject: [PATCH] sctp: replace usage of found with dedicated list iterator variable

To move the list iterator variable into the list_for_each_entry_*()
macro in the future it should be avoided to use the list iterator
variable after the loop body.

To *never* use the list iterator variable after the loop it was
concluded to use a separate iterator variable instead of a
found boolean [1].

This removes the need to use a found variable and simply checking if
the variable was set, can determine if the break/goto was hit.

Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/
Signed-off-by: Jakob Koschel <[email protected]>
---
net/sctp/bind_addr.c | 15 +++++++--------
net/sctp/ipv6.c | 24 +++++++++++-------------
net/sctp/protocol.c | 24 +++++++++++-------------
3 files changed, 29 insertions(+), 34 deletions(-)

diff --git a/net/sctp/bind_addr.c b/net/sctp/bind_addr.c
index 59e653b528b1..c85ade7863bf 100644
--- a/net/sctp/bind_addr.c
+++ b/net/sctp/bind_addr.c
@@ -172,23 +172,22 @@ int sctp_add_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *new,
*/
int sctp_del_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *del_addr)
{
- struct sctp_sockaddr_entry *addr, *temp;
- int found = 0;
+ struct sctp_sockaddr_entry *addr = NULL, *iter, *temp;

/* We hold the socket lock when calling this function,
* and that acts as a writer synchronizing lock.
*/
- list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
- if (sctp_cmp_addr_exact(&addr->a, del_addr)) {
+ list_for_each_entry_safe(iter, temp, &bp->address_list, list) {
+ if (sctp_cmp_addr_exact(&iter->a, del_addr)) {
/* Found the exact match. */
- found = 1;
- addr->valid = 0;
- list_del_rcu(&addr->list);
+ addr = iter;
+ iter->valid = 0;
+ list_del_rcu(&iter->list);
break;
}
}

- if (found) {
+ if (addr) {
kfree_rcu(addr, rcu);
SCTP_DBG_OBJCNT_DEC(addr);
return 0;
diff --git a/net/sctp/ipv6.c b/net/sctp/ipv6.c
index 470dbdc27d58..803950277f56 100644
--- a/net/sctp/ipv6.c
+++ b/net/sctp/ipv6.c
@@ -76,10 +76,8 @@ static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
void *ptr)
{
struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
- struct sctp_sockaddr_entry *addr = NULL;
- struct sctp_sockaddr_entry *temp;
+ struct sctp_sockaddr_entry *addr = NULL, *iter, *temp;
struct net *net = dev_net(ifa->idev->dev);
- int found = 0;

switch (ev) {
case NETDEV_UP:
@@ -97,21 +95,21 @@ static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
break;
case NETDEV_DOWN:
spin_lock_bh(&net->sctp.local_addr_lock);
- list_for_each_entry_safe(addr, temp,
- &net->sctp.local_addr_list, list) {
- if (addr->a.sa.sa_family == AF_INET6 &&
- ipv6_addr_equal(&addr->a.v6.sin6_addr,
+ list_for_each_entry_safe(iter, temp,
+ &net->sctp.local_addr_list, list) {
+ if (iter->a.sa.sa_family == AF_INET6 &&
+ ipv6_addr_equal(&iter->a.v6.sin6_addr,
&ifa->addr) &&
- addr->a.v6.sin6_scope_id == ifa->idev->dev->ifindex) {
- sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
- found = 1;
- addr->valid = 0;
- list_del_rcu(&addr->list);
+ iter->a.v6.sin6_scope_id == ifa->idev->dev->ifindex) {
+ sctp_addr_wq_mgmt(net, iter, SCTP_ADDR_DEL);
+ addr = iter;
+ iter->valid = 0;
+ list_del_rcu(&iter->list);
break;
}
}
spin_unlock_bh(&net->sctp.local_addr_lock);
- if (found)
+ if (addr)
kfree_rcu(addr, rcu);
break;
}
diff --git a/net/sctp/protocol.c b/net/sctp/protocol.c
index 35928fefae33..6f1d7fd83465 100644
--- a/net/sctp/protocol.c
+++ b/net/sctp/protocol.c
@@ -777,10 +777,8 @@ static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
void *ptr)
{
struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
- struct sctp_sockaddr_entry *addr = NULL;
- struct sctp_sockaddr_entry *temp;
+ struct sctp_sockaddr_entry *addr = NULL, *iter, *temp;
struct net *net = dev_net(ifa->ifa_dev->dev);
- int found = 0;

switch (ev) {
case NETDEV_UP:
@@ -797,20 +795,20 @@ static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
break;
case NETDEV_DOWN:
spin_lock_bh(&net->sctp.local_addr_lock);
- list_for_each_entry_safe(addr, temp,
- &net->sctp.local_addr_list, list) {
- if (addr->a.sa.sa_family == AF_INET &&
- addr->a.v4.sin_addr.s_addr ==
- ifa->ifa_local) {
- sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
- found = 1;
- addr->valid = 0;
- list_del_rcu(&addr->list);
+ list_for_each_entry_safe(iter, temp,
+ &net->sctp.local_addr_list, list) {
+ if (iter->a.sa.sa_family == AF_INET &&
+ iter->a.v4.sin_addr.s_addr ==
+ ifa->ifa_local) {
+ sctp_addr_wq_mgmt(net, iter, SCTP_ADDR_DEL);
+ addr = iter;
+ iter->valid = 0;
+ list_del_rcu(&iter->list);
break;
}
}
spin_unlock_bh(&net->sctp.local_addr_lock);
- if (found)
+ if (addr)
kfree_rcu(addr, rcu);
break;
}

base-commit: f443e374ae131c168a065ea1748feac6b2e76613
--
2.25.1


2022-03-25 21:25:48

by Marcelo Ricardo Leitner

[permalink] [raw]
Subject: Re: [PATCH] sctp: replace usage of found with dedicated list iterator variable

On Thu, Mar 24, 2022 at 08:22:57AM +0100, Jakob Koschel wrote:
> To move the list iterator variable into the list_for_each_entry_*()
> macro in the future it should be avoided to use the list iterator
> variable after the loop body.
>
> To *never* use the list iterator variable after the loop it was
> concluded to use a separate iterator variable instead of a
> found boolean [1].
>
> This removes the need to use a found variable and simply checking if
> the variable was set, can determine if the break/goto was hit.
>
> Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/
> Signed-off-by: Jakob Koschel <[email protected]>

Acked-by: Marcelo Ricardo Leitner <[email protected]>