diff mbox series

[v6,2/6] netlink: Add new netlink_release function

Message ID 20230614234129.3264175-3-anjali.k.kulkarni@oracle.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series Process connector bug fixes & enhancements | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 4920 this patch: 4920
netdev/cc_maintainers warning 2 maintainers not CCed: kuniyu@amazon.com fw@strlen.de
netdev/build_clang success Errors and warnings before: 1912 this patch: 1912
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 5138 this patch: 5138
netdev/checkpatch fail ERROR: trailing whitespace WARNING: Unnecessary space before function pointer arguments
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Anjali Kulkarni June 14, 2023, 11:41 p.m. UTC
A new function netlink_release is added in netlink_sock to store the
protocol's release function. This is called when the socket is deleted.
This can be supplied by the protocol via the release function in
netlink_kernel_cfg. This is being added for the NETLINK_CONNECTOR
protocol, so it can free it's data when socket is deleted.

Signed-off-by: Anjali Kulkarni <anjali.k.kulkarni@oracle.com>
---
 include/linux/netlink.h  | 1 +
 net/netlink/af_netlink.c | 6 ++++++
 net/netlink/af_netlink.h | 4 ++++
 3 files changed, 11 insertions(+)

Comments

Liam R. Howlett June 30, 2023, 8:23 p.m. UTC | #1
* Anjali Kulkarni <anjali.k.kulkarni@oracle.com> [230614 19:41]:
> A new function netlink_release is added in netlink_sock to store the
> protocol's release function. This is called when the socket is deleted.
> This can be supplied by the protocol via the release function in
> netlink_kernel_cfg. This is being added for the NETLINK_CONNECTOR
> protocol, so it can free it's data when socket is deleted.
> 
> Signed-off-by: Anjali Kulkarni <anjali.k.kulkarni@oracle.com>
> ---
>  include/linux/netlink.h  | 1 +
>  net/netlink/af_netlink.c | 6 ++++++
>  net/netlink/af_netlink.h | 4 ++++
>  3 files changed, 11 insertions(+)
> 
> diff --git a/include/linux/netlink.h b/include/linux/netlink.h
> index d73cfe5b6bc2..0db4ffe6186b 100644
> --- a/include/linux/netlink.h
> +++ b/include/linux/netlink.h
> @@ -50,6 +50,7 @@ struct netlink_kernel_cfg {
>  	struct mutex	*cb_mutex;
>  	int		(*bind)(struct net *net, int group);
>  	void		(*unbind)(struct net *net, int group);
> +	void            (*release) (struct sock *sk, unsigned long *groups);
>  };
>  
>  struct sock *__netlink_kernel_create(struct net *net, int unit,
> diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
> index e75e5156e4ac..383c10c6e6e3 100644
> --- a/net/netlink/af_netlink.c
> +++ b/net/netlink/af_netlink.c
> @@ -677,6 +677,7 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol,
>  	struct netlink_sock *nlk;
>  	int (*bind)(struct net *net, int group);
>  	void (*unbind)(struct net *net, int group);
> +	void (*release)(struct sock *sock, unsigned long *groups);
>  	int err = 0;
>  
>  	sock->state = SS_UNCONNECTED;
> @@ -704,6 +705,7 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol,
>  	cb_mutex = nl_table[protocol].cb_mutex;
>  	bind = nl_table[protocol].bind;
>  	unbind = nl_table[protocol].unbind;
> +	release = nl_table[protocol].release;
>  	netlink_unlock_table();
>  
>  	if (err < 0)
> @@ -719,6 +721,7 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol,
>  	nlk->module = module;
>  	nlk->netlink_bind = bind;
>  	nlk->netlink_unbind = unbind;
> +	nlk->netlink_release = release;
>  out:
>  	return err;
>  
> @@ -763,6 +766,8 @@ static int netlink_release(struct socket *sock)
>  	 * OK. Socket is unlinked, any packets that arrive now
>  	 * will be purged.
>  	 */
> +	if (nlk->netlink_release)
> +		nlk->netlink_release(sk, nlk->groups);
>  
>  	/* must not acquire netlink_table_lock in any way again before unbind
>  	 * and notifying genetlink is done as otherwise it might deadlock
> @@ -2091,6 +2096,7 @@ __netlink_kernel_create(struct net *net, int unit, struct module *module,
>  		if (cfg) {
>  			nl_table[unit].bind = cfg->bind;
>  			nl_table[unit].unbind = cfg->unbind;
> +			nl_table[unit].release = cfg->release;
>  			nl_table[unit].flags = cfg->flags;
>  		}
>  		nl_table[unit].registered = 1;
> diff --git a/net/netlink/af_netlink.h b/net/netlink/af_netlink.h
> index 90a3198a9b7f..cb2688aa347a 100644
> --- a/net/netlink/af_netlink.h
> +++ b/net/netlink/af_netlink.h
> @@ -42,6 +42,8 @@ struct netlink_sock {
>  	void			(*netlink_rcv)(struct sk_buff *skb);
>  	int			(*netlink_bind)(struct net *net, int group);
>  	void			(*netlink_unbind)(struct net *net, int group);
> +	void			(*netlink_release)(struct sock *sk,
> +						   unsigned long *groups);
>  	struct module		*module;
>  
>  	struct rhash_head	node;
> @@ -64,6 +66,8 @@ struct netlink_table {
>  	struct module		*module;
>  	int			(*bind)(struct net *net, int group);
>  	void			(*unbind)(struct net *net, int group);
> +	void                    (*release)(struct sock *sk, 

The line above ends in a space.

> +					   unsigned long *groups);
>  	int			registered;
>  };
>  
> -- 
> 2.41.0
>
Anjali Kulkarni June 30, 2023, 8:39 p.m. UTC | #2
> On Jun 30, 2023, at 1:23 PM, Liam Howlett <liam.howlett@oracle.com> wrote:
> 
> * Anjali Kulkarni <anjali.k.kulkarni@oracle.com> [230614 19:41]:
>> A new function netlink_release is added in netlink_sock to store the
>> protocol's release function. This is called when the socket is deleted.
>> This can be supplied by the protocol via the release function in
>> netlink_kernel_cfg. This is being added for the NETLINK_CONNECTOR
>> protocol, so it can free it's data when socket is deleted.
>> 
>> Signed-off-by: Anjali Kulkarni <anjali.k.kulkarni@oracle.com>
>> ---
>> include/linux/netlink.h | 1 +
>> net/netlink/af_netlink.c | 6 ++++++
>> net/netlink/af_netlink.h | 4 ++++
>> 3 files changed, 11 insertions(+)
>> 
>> diff --git a/include/linux/netlink.h b/include/linux/netlink.h
>> index d73cfe5b6bc2..0db4ffe6186b 100644
>> --- a/include/linux/netlink.h
>> +++ b/include/linux/netlink.h
>> @@ -50,6 +50,7 @@ struct netlink_kernel_cfg {
>> 	struct mutex	*cb_mutex;
>> 	int		(*bind)(struct net *net, int group);
>> 	void		(*unbind)(struct net *net, int group);
>> +	void (*release) (struct sock *sk, unsigned long *groups);
>> };
>> 
>> struct sock *__netlink_kernel_create(struct net *net, int unit,
>> diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
>> index e75e5156e4ac..383c10c6e6e3 100644
>> --- a/net/netlink/af_netlink.c
>> +++ b/net/netlink/af_netlink.c
>> @@ -677,6 +677,7 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol,
>> 	struct netlink_sock *nlk;
>> 	int (*bind)(struct net *net, int group);
>> 	void (*unbind)(struct net *net, int group);
>> +	void (*release)(struct sock *sock, unsigned long *groups);
>> 	int err = 0;
>> 
>> 	sock->state = SS_UNCONNECTED;
>> @@ -704,6 +705,7 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol,
>> 	cb_mutex = nl_table[protocol].cb_mutex;
>> 	bind = nl_table[protocol].bind;
>> 	unbind = nl_table[protocol].unbind;
>> +	release = nl_table[protocol].release;
>> 	netlink_unlock_table();
>> 
>> 	if (err < 0)
>> @@ -719,6 +721,7 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol,
>> 	nlk->module = module;
>> 	nlk->netlink_bind = bind;
>> 	nlk->netlink_unbind = unbind;
>> +	nlk->netlink_release = release;
>> out:
>> 	return err;
>> 
>> @@ -763,6 +766,8 @@ static int netlink_release(struct socket *sock)
>> 	 * OK. Socket is unlinked, any packets that arrive now
>> 	 * will be purged.
>> 	 */
>> +	if (nlk->netlink_release)
>> +		nlk->netlink_release(sk, nlk->groups);
>> 
>> 	/* must not acquire netlink_table_lock in any way again before unbind
>> 	 * and notifying genetlink is done as otherwise it might deadlock
>> @@ -2091,6 +2096,7 @@ __netlink_kernel_create(struct net *net, int unit, struct module *module,
>> 		if (cfg) {
>> 			nl_table[unit].bind = cfg->bind;
>> 			nl_table[unit].unbind = cfg->unbind;
>> +			nl_table[unit].release = cfg->release;
>> 			nl_table[unit].flags = cfg->flags;
>> 		}
>> 		nl_table[unit].registered = 1;
>> diff --git a/net/netlink/af_netlink.h b/net/netlink/af_netlink.h
>> index 90a3198a9b7f..cb2688aa347a 100644
>> --- a/net/netlink/af_netlink.h
>> +++ b/net/netlink/af_netlink.h
>> @@ -42,6 +42,8 @@ struct netlink_sock {
>> 	void			(*netlink_rcv)(struct sk_buff *skb);
>> 	int			(*netlink_bind)(struct net *net, int group);
>> 	void			(*netlink_unbind)(struct net *net, int group);
>> +	void			(*netlink_release)(struct sock *sk,
>> +						 unsigned long *groups);
>> 	struct module		*module;
>> 
>> 	struct rhash_head	node;
>> @@ -64,6 +66,8 @@ struct netlink_table {
>> 	struct module		*module;
>> 	int			(*bind)(struct net *net, int group);
>> 	void			(*unbind)(struct net *net, int group);
>> +	void (*release)(struct sock *sk, 
> 
> The line above ends in a space.
> 

Thanks, will fix in v7.

>> +					 unsigned long *groups);
>> 	int			registered;
>> };
>> 
>> -- 
>> 2.41.0
diff mbox series

Patch

diff --git a/include/linux/netlink.h b/include/linux/netlink.h
index d73cfe5b6bc2..0db4ffe6186b 100644
--- a/include/linux/netlink.h
+++ b/include/linux/netlink.h
@@ -50,6 +50,7 @@  struct netlink_kernel_cfg {
 	struct mutex	*cb_mutex;
 	int		(*bind)(struct net *net, int group);
 	void		(*unbind)(struct net *net, int group);
+	void            (*release) (struct sock *sk, unsigned long *groups);
 };
 
 struct sock *__netlink_kernel_create(struct net *net, int unit,
diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index e75e5156e4ac..383c10c6e6e3 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -677,6 +677,7 @@  static int netlink_create(struct net *net, struct socket *sock, int protocol,
 	struct netlink_sock *nlk;
 	int (*bind)(struct net *net, int group);
 	void (*unbind)(struct net *net, int group);
+	void (*release)(struct sock *sock, unsigned long *groups);
 	int err = 0;
 
 	sock->state = SS_UNCONNECTED;
@@ -704,6 +705,7 @@  static int netlink_create(struct net *net, struct socket *sock, int protocol,
 	cb_mutex = nl_table[protocol].cb_mutex;
 	bind = nl_table[protocol].bind;
 	unbind = nl_table[protocol].unbind;
+	release = nl_table[protocol].release;
 	netlink_unlock_table();
 
 	if (err < 0)
@@ -719,6 +721,7 @@  static int netlink_create(struct net *net, struct socket *sock, int protocol,
 	nlk->module = module;
 	nlk->netlink_bind = bind;
 	nlk->netlink_unbind = unbind;
+	nlk->netlink_release = release;
 out:
 	return err;
 
@@ -763,6 +766,8 @@  static int netlink_release(struct socket *sock)
 	 * OK. Socket is unlinked, any packets that arrive now
 	 * will be purged.
 	 */
+	if (nlk->netlink_release)
+		nlk->netlink_release(sk, nlk->groups);
 
 	/* must not acquire netlink_table_lock in any way again before unbind
 	 * and notifying genetlink is done as otherwise it might deadlock
@@ -2091,6 +2096,7 @@  __netlink_kernel_create(struct net *net, int unit, struct module *module,
 		if (cfg) {
 			nl_table[unit].bind = cfg->bind;
 			nl_table[unit].unbind = cfg->unbind;
+			nl_table[unit].release = cfg->release;
 			nl_table[unit].flags = cfg->flags;
 		}
 		nl_table[unit].registered = 1;
diff --git a/net/netlink/af_netlink.h b/net/netlink/af_netlink.h
index 90a3198a9b7f..cb2688aa347a 100644
--- a/net/netlink/af_netlink.h
+++ b/net/netlink/af_netlink.h
@@ -42,6 +42,8 @@  struct netlink_sock {
 	void			(*netlink_rcv)(struct sk_buff *skb);
 	int			(*netlink_bind)(struct net *net, int group);
 	void			(*netlink_unbind)(struct net *net, int group);
+	void			(*netlink_release)(struct sock *sk,
+						   unsigned long *groups);
 	struct module		*module;
 
 	struct rhash_head	node;
@@ -64,6 +66,8 @@  struct netlink_table {
 	struct module		*module;
 	int			(*bind)(struct net *net, int group);
 	void			(*unbind)(struct net *net, int group);
+	void                    (*release)(struct sock *sk, 
+					   unsigned long *groups);
 	int			registered;
 };