batman-adv: use rcu_replace_pointer() where appropriate

Message ID 20200520084140.18624-1-a@unstable.cc (mailing list archive)
State Accepted, archived
Delegated to: Simon Wunderlich
Headers
Series batman-adv: use rcu_replace_pointer() where appropriate |

Commit Message

Antonio Quartulli May 20, 2020, 8:41 a.m. UTC
  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
  

Comments

Sven Eckelmann May 22, 2020, 12:26 p.m. UTC | #1
On Wednesday, 20 May 2020 10:41:40 CEST Antonio Quartulli wrote:
> 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.

Please use the reference style 'commit a63fc6b75cca ("rcu: Upgrade 
rcu_swap_protected() to rcu_replace_pointer()")' in the future [1]. checkpatch 
will also report something like this when it detects a short commit id:

    ERROR: Please use git commit description style 'commit <12+ chars of sha1> ("<title line>")' - ie: 'commit a63fc6b75cca ("rcu: Upgrade rcu_swap_protected() to rcu_replace_pointer()")'

I will change this for you.

[...]
> +#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) */

To avoid problems with a (potentially) backported patch in the stable
kernels - please #undef rcu_replace_pointer first. I will do this for you.

Applied.

Thanks,
	Sven

[1] https://www.kernel.org/doc/html/v4.17/process/submitting-patches.html#describe-your-changes
  

Patch

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);