Netdev List
 help / color / mirror / Atom feed
From: Will Deacon <will@kernel.org>
To: Antonio Quartulli <antonio@openvpn.net>
Cc: netdev@vger.kernel.org, Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Donald Hunter <donald.hunter@gmail.com>,
	Shuah Khan <shuah@kernel.org>,
	sd@queasysnail.net, ryazanov.s.a@gmail.com,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	Simon Horman <horms@kernel.org>,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	Xiao Liang <shaw.leon@gmail.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Boqun Feng <boqun.feng@gmail.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [PATCH net-next v16 06/26] kref/refcount: implement kref_put_sock()
Date: Thu, 19 Dec 2024 17:20:41 +0000	[thread overview]
Message-ID: <20241219172040.GA25368@willie-the-truck> (raw)
In-Reply-To: <20241219-b4-ovpn-v16-6-3e3001153683@openvpn.net>

On Thu, Dec 19, 2024 at 02:42:00AM +0100, Antonio Quartulli wrote:
> Similarly so kref_put_lock(), decrease the refcount
> and call bh_lock_sock(sk) if it reached 0.
> 
> This kref_put variant comes handy when in need of
> atomically cleanup any socket context along with
> setting the refcount to 0.
> 
> Cc: Will Deacon <will@kernel.org> (maintainer:ATOMIC INFRASTRUCTURE)
> Cc: Peter Zijlstra <peterz@infradead.org> (maintainer:ATOMIC INFRASTRUCTURE)
> Cc: Boqun Feng <boqun.feng@gmail.com> (reviewer:ATOMIC INFRASTRUCTURE)
> Cc: Mark Rutland <mark.rutland@arm.com> (reviewer:ATOMIC INFRASTRUCTURE)
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: Antonio Quartulli <antonio@openvpn.net>
> ---
>  include/linux/kref.h     | 11 +++++++++++
>  include/linux/refcount.h |  3 +++
>  lib/refcount.c           | 32 ++++++++++++++++++++++++++++++++

[...]

> diff --git a/lib/refcount.c b/lib/refcount.c
> index a207a8f22b3ca35890671e51c480266d89e4d8d6..76a728581aa49a41ef13f5141f3f2e9816d72e75 100644
> --- a/lib/refcount.c
> +++ b/lib/refcount.c
> @@ -7,6 +7,7 @@
>  #include <linux/refcount.h>
>  #include <linux/spinlock.h>
>  #include <linux/bug.h>
> +#include <net/sock.h>
>  
>  #define REFCOUNT_WARN(str)	WARN_ONCE(1, "refcount_t: " str ".\n")
>  
> @@ -156,6 +157,37 @@ bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock)
>  }
>  EXPORT_SYMBOL(refcount_dec_and_lock);
>  
> +/**
> + * refcount_dec_and_lock_sock - return holding locked sock if able to decrement
> + *				refcount to 0
> + * @r: the refcount
> + * @sock: the sock to be locked
> + *
> + * Similar to atomic_dec_and_lock(), it will WARN on underflow and fail to
> + * decrement when saturated at REFCOUNT_SATURATED.
> + *
> + * Provides release memory ordering, such that prior loads and stores are done
> + * before, and provides a control dependency such that free() must come after.
> + * See the comment on top.
> + *
> + * Return: true and hold sock if able to decrement refcount to 0, false
> + *	   otherwise
> + */
> +bool refcount_dec_and_lock_sock(refcount_t *r, struct sock *sock)
> +{
> +	if (refcount_dec_not_one(r))
> +		return false;
> +
> +	bh_lock_sock(sock);
> +	if (!refcount_dec_and_test(r)) {
> +		bh_unlock_sock(sock);
> +		return false;
> +	}
> +
> +	return true;
> +}
> +EXPORT_SYMBOL(refcount_dec_and_lock_sock);

It feels a little out-of-place to me having socket-specific functions in
lib/refcount.c. I'd suggest sticking this somewhere else _or_ maybe we
could generate this pattern of code:

#define REFCOUNT_DEC_AND_LOCKNAME(lockname, locktype, lock, unlock)	\
static __always_inline							\
bool refcount_dec_and_lock_##lockname(refcount_t *r, locktype *l)	\
{									\
	...

inside a generator macro in refcount.h, like we do for seqlocks in
linux/seqlock.h. The downside of that is the cost of inlining.

Will

  reply	other threads:[~2024-12-19 17:20 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-19  1:41 [PATCH net-next v16 00/26] Introducing OpenVPN Data Channel Offload Antonio Quartulli
2024-12-19  1:41 ` [PATCH net-next v16 01/26] net: introduce OpenVPN Data Channel Offload (ovpn) Antonio Quartulli
2024-12-19  1:41 ` [PATCH net-next v16 02/26] ovpn: add basic netlink support Antonio Quartulli
2024-12-20 11:00   ` Donald Hunter
2024-12-19  1:41 ` [PATCH net-next v16 03/26] ovpn: add basic interface creation/destruction/management routines Antonio Quartulli
2024-12-20 11:06   ` Donald Hunter
2024-12-19  1:41 ` [PATCH net-next v16 04/26] ovpn: keep carrier always on for MP interfaces Antonio Quartulli
2024-12-19  1:41 ` [PATCH net-next v16 05/26] ovpn: introduce the ovpn_peer object Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 06/26] kref/refcount: implement kref_put_sock() Antonio Quartulli
2024-12-19 17:20   ` Will Deacon [this message]
2024-12-31  7:31     ` Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 07/26] ovpn: introduce the ovpn_socket object Antonio Quartulli
2025-01-03 17:00   ` Sabrina Dubroca
2025-01-05 23:27     ` Antonio Quartulli
2025-01-08 10:55       ` Antonio Quartulli
2025-01-09 11:28       ` Sabrina Dubroca
2024-12-19  1:42 ` [PATCH net-next v16 08/26] ovpn: implement basic TX path (UDP) Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 09/26] ovpn: implement basic RX " Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 10/26] ovpn: implement packet processing Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 11/26] ovpn: store tunnel and transport statistics Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 12/26] ipv6: export inet6_stream_ops via EXPORT_SYMBOL_GPL Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 13/26] ovpn: implement TCP transport Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 14/26] skb: implement skb_send_sock_locked_with_flags() Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 15/26] ovpn: add support for MSG_NOSIGNAL in tcp_sendmsg Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 16/26] ovpn: implement multi-peer support Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 17/26] ovpn: implement peer lookup logic Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 18/26] ovpn: implement keepalive mechanism Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 19/26] ovpn: add support for updating local UDP endpoint Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 20/26] ovpn: add support for peer floating Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 21/26] ovpn: implement peer add/get/dump/delete via netlink Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 22/26] ovpn: implement key add/get/del/swap " Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 23/26] ovpn: kill key and notify userspace in case of IV exhaustion Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 24/26] ovpn: notify userspace when a peer is deleted Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 25/26] ovpn: add basic ethtool support Antonio Quartulli
2024-12-19  1:42 ` [PATCH net-next v16 26/26] testing/selftests: add test tool and scripts for ovpn module Antonio Quartulli
2024-12-20  4:02   ` Jakub Kicinski
2024-12-31  7:42     ` Antonio Quartulli

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=20241219172040.GA25368@willie-the-truck \
    --to=will@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=antonio@openvpn.net \
    --cc=boqun.feng@gmail.com \
    --cc=donald.hunter@gmail.com \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=peterz@infradead.org \
    --cc=ryazanov.s.a@gmail.com \
    --cc=sd@queasysnail.net \
    --cc=shaw.leon@gmail.com \
    --cc=shuah@kernel.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