From: Antonio Quartulli <a@unstable.cc>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Antonio Quartulli <a@unstable.cc>
Subject: [PATCH] batman-adv: use rcu_replace_pointer() where appropriate
Date: Wed, 20 May 2020 10:41:40 +0200 [thread overview]
Message-ID: <20200520084140.18624-1-a@unstable.cc> (raw)
In a63fc6b7 ("rcu: Upgrade rcu_swap_protected() to rcu_replace_pointer()")
a new helper macro named rcu_replace_pointer() was introduced to
simplify code requiring to switch an rcu pointer to a new value while
extracting the old one.
Use rcu_replace_pointer() where appropriate to make code slimer.
Signed-off-by: Antonio Quartulli <a@unstable.cc>
---
compat-include/linux/rcupdate.h | 27 +++++++++++++++++++++++++++
net/batman-adv/gateway_client.c | 4 ++--
net/batman-adv/hard-interface.c | 4 ++--
net/batman-adv/routing.c | 4 ++--
4 files changed, 33 insertions(+), 6 deletions(-)
create mode 100644 compat-include/linux/rcupdate.h
diff --git a/compat-include/linux/rcupdate.h b/compat-include/linux/rcupdate.h
new file mode 100644
index 00000000..731aa951
--- /dev/null
+++ b/compat-include/linux/rcupdate.h
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2020 B.A.T.M.A.N. contributors:
+ *
+ * Antonio Quartulli
+ *
+ * This file contains macros for maintaining compatibility with older versions
+ * of the Linux kernel.
+ */
+
+#ifndef _NET_BATMAN_ADV_COMPAT_LINUX_RCUPDATE_H_
+#define _NET_BATMAN_ADV_COMPAT_LINUX_RCUPDATE_H_
+
+#include <linux/version.h>
+#include_next <linux/rcupdate.h>
+
+#if LINUX_VERSION_IS_LESS(5, 5, 0)
+
+#define rcu_replace_pointer(rcu_ptr, ptr, c) \
+({ \
+ typeof(ptr) __tmp = rcu_dereference_protected((rcu_ptr), (c)); \
+ rcu_assign_pointer((rcu_ptr), (ptr)); \
+ __tmp; \
+})
+
+#endif /* LINUX_VERSION_IS_LESS(5, 5, 0) */
+
+#endif /* _NET_BATMAN_ADV_COMPAT_LINUX_RCUPDATE_H_ */
diff --git a/net/batman-adv/gateway_client.c b/net/batman-adv/gateway_client.c
index e22e4928..a18dcc68 100644
--- a/net/batman-adv/gateway_client.c
+++ b/net/batman-adv/gateway_client.c
@@ -146,8 +146,8 @@ static void batadv_gw_select(struct batadv_priv *bat_priv,
if (new_gw_node)
kref_get(&new_gw_node->refcount);
- curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
- rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
+ curr_gw_node = rcu_replace_pointer(bat_priv->gw.curr_gw, new_gw_node,
+ true);
if (curr_gw_node)
batadv_gw_node_put(curr_gw_node);
diff --git a/net/batman-adv/hard-interface.c b/net/batman-adv/hard-interface.c
index c7e98a40..3a256af9 100644
--- a/net/batman-adv/hard-interface.c
+++ b/net/batman-adv/hard-interface.c
@@ -473,8 +473,8 @@ static void batadv_primary_if_select(struct batadv_priv *bat_priv,
if (new_hard_iface)
kref_get(&new_hard_iface->refcount);
- curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
- rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
+ curr_hard_iface = rcu_replace_pointer(bat_priv->primary_if,
+ new_hard_iface, 1);
if (!new_hard_iface)
goto out;
diff --git a/net/batman-adv/routing.c b/net/batman-adv/routing.c
index 3632bd97..d343382e 100644
--- a/net/batman-adv/routing.c
+++ b/net/batman-adv/routing.c
@@ -71,13 +71,13 @@ static void _batadv_update_route(struct batadv_priv *bat_priv,
* the code needs to ensure the curr_router variable contains a pointer
* to the replaced best neighbor.
*/
- curr_router = rcu_dereference_protected(orig_ifinfo->router, true);
/* increase refcount of new best neighbor */
if (neigh_node)
kref_get(&neigh_node->refcount);
- rcu_assign_pointer(orig_ifinfo->router, neigh_node);
+ curr_router = rcu_replace_pointer(orig_ifinfo->router, neigh_node,
+ true);
spin_unlock_bh(&orig_node->neigh_list_lock);
batadv_orig_ifinfo_put(orig_ifinfo);
--
2.26.2
next reply other threads:[~2020-05-20 8:41 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-20 8:41 Antonio Quartulli [this message]
2020-05-22 12:26 ` [PATCH] batman-adv: use rcu_replace_pointer() where appropriate Sven Eckelmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200520084140.18624-1-a@unstable.cc \
--to=a@unstable.cc \
--cc=b.a.t.m.a.n@lists.open-mesh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).