[net-next,v5,1/2] netconsole: Create a allocation helper

Message ID 20230810095452.3171106-2-leitao@debian.org
State New
Headers
Series [net-next,v5,1/2] netconsole: Create a allocation helper |

Commit Message

Breno Leitao Aug. 10, 2023, 9:54 a.m. UTC
  De-duplicate the initialization and allocation code for struct
netconsole_target.

The same allocation and initialization code is duplicated in two
different places in the netconsole subsystem, when the netconsole target
is initialized by command line parameters (alloc_param_target()), and
dynamically by sysfs (make_netconsole_target()).

Create a helper function, and call it from the two different functions.

Suggested-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
 drivers/net/netconsole.c | 42 +++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 20 deletions(-)
  

Comments

Simon Horman Aug. 10, 2023, 8:17 p.m. UTC | #1
On Thu, Aug 10, 2023 at 02:54:50AM -0700, Breno Leitao wrote:
> De-duplicate the initialization and allocation code for struct
> netconsole_target.
> 
> The same allocation and initialization code is duplicated in two
> different places in the netconsole subsystem, when the netconsole target
> is initialized by command line parameters (alloc_param_target()), and
> dynamically by sysfs (make_netconsole_target()).
> 
> Create a helper function, and call it from the two different functions.
> 
> Suggested-by: Eric Dumazet <edumazet@google.com>
> Signed-off-by: Breno Leitao <leitao@debian.org>
> ---
>  drivers/net/netconsole.c | 42 +++++++++++++++++++++-------------------
>  1 file changed, 22 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
> index 87f18aedd3bd..f93b98d64a3c 100644
> --- a/drivers/net/netconsole.c
> +++ b/drivers/net/netconsole.c
> @@ -167,19 +167,16 @@ static void netconsole_target_put(struct netconsole_target *nt)
>  
>  #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
>  
> -/* Allocate new target (from boot/module param) and setup netpoll for it */
> -static struct netconsole_target *alloc_param_target(char *target_config)
> +/* Allocate and initialize with defaults.
> + * Note that these targets get their config_item fields zeroed-out.
> + */
> +static struct netconsole_target *alloc_and_init(void)
>  {
> -	int err = -ENOMEM;
>  	struct netconsole_target *nt;
>  
> -	/*
> -	 * Allocate and initialize with defaults.
> -	 * Note that these targets get their config_item fields zeroed-out.
> -	 */
>  	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
>  	if (!nt)
> -		goto fail;
> +		return nt;
>  
>  	nt->np.name = "netconsole";
>  	strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
> @@ -187,6 +184,21 @@ static struct netconsole_target *alloc_param_target(char *target_config)
>  	nt->np.remote_port = 6666;
>  	eth_broadcast_addr(nt->np.remote_mac);
>  
> +	return nt;
> +}
> +
> +/* Allocate new target (from boot/module param) and setup netpoll for it */
> +static struct netconsole_target *alloc_param_target(char *target_config)
> +{
> +	struct netconsole_target *nt;
> +	int err;

Hi Breno,

This function returns err.
However, clang-16 W=1 and Smatch warn that there is a case
where this may occur without err having being initialised.

> +
> +	nt = alloc_and_init();
> +	if (!nt) {
> +		err = -ENOMEM;
> +		goto fail;
> +	}
> +
>  	if (*target_config == '+') {
>  		nt->extended = true;
>  		target_config++;

...
  
Breno Leitao Aug. 11, 2023, 8:58 a.m. UTC | #2
Hello Simon,

On Thu, Aug 10, 2023 at 10:17:18PM +0200, Simon Horman wrote:
> On Thu, Aug 10, 2023 at 02:54:50AM -0700, Breno Leitao wrote:
> > De-duplicate the initialization and allocation code for struct
> > netconsole_target.
> > 
> > The same allocation and initialization code is duplicated in two
> > different places in the netconsole subsystem, when the netconsole target
> > is initialized by command line parameters (alloc_param_target()), and
> > dynamically by sysfs (make_netconsole_target()).
> > 
> > Create a helper function, and call it from the two different functions.
> > 
> > Suggested-by: Eric Dumazet <edumazet@google.com>
> > Signed-off-by: Breno Leitao <leitao@debian.org>
> > ---
> >  drivers/net/netconsole.c | 42 +++++++++++++++++++++-------------------
> >  1 file changed, 22 insertions(+), 20 deletions(-)
> > 
> > diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
> > index 87f18aedd3bd..f93b98d64a3c 100644
> > --- a/drivers/net/netconsole.c
> > +++ b/drivers/net/netconsole.c
> > @@ -167,19 +167,16 @@ static void netconsole_target_put(struct netconsole_target *nt)
> >  
> >  #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
> >  
> > -/* Allocate new target (from boot/module param) and setup netpoll for it */
> > -static struct netconsole_target *alloc_param_target(char *target_config)
> > +/* Allocate and initialize with defaults.
> > + * Note that these targets get their config_item fields zeroed-out.
> > + */
> > +static struct netconsole_target *alloc_and_init(void)
> >  {
> > -	int err = -ENOMEM;
> >  	struct netconsole_target *nt;
> >  
> > -	/*
> > -	 * Allocate and initialize with defaults.
> > -	 * Note that these targets get their config_item fields zeroed-out.
> > -	 */
> >  	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
> >  	if (!nt)
> > -		goto fail;
> > +		return nt;
> >  
> >  	nt->np.name = "netconsole";
> >  	strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
> > @@ -187,6 +184,21 @@ static struct netconsole_target *alloc_param_target(char *target_config)
> >  	nt->np.remote_port = 6666;
> >  	eth_broadcast_addr(nt->np.remote_mac);
> >  
> > +	return nt;
> > +}
> > +
> > +/* Allocate new target (from boot/module param) and setup netpoll for it */
> > +static struct netconsole_target *alloc_param_target(char *target_config)
> > +{
> > +	struct netconsole_target *nt;
> > +	int err;
> 
> Hi Breno,
> 
> This function returns err.
> However, clang-16 W=1 and Smatch warn that there is a case
> where this may occur without err having being initialised.

That can really happen, if we get into this function:

        if (*target_config == 'r') {
                if (!nt->extended) {
                        pr_err("Netconsole configuration error. Release feature requires extended log message");
                        goto fail;

	fail:
		return ERR_PTR(err);


Let me update it.
  

Patch

diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index 87f18aedd3bd..f93b98d64a3c 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -167,19 +167,16 @@  static void netconsole_target_put(struct netconsole_target *nt)
 
 #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
 
-/* Allocate new target (from boot/module param) and setup netpoll for it */
-static struct netconsole_target *alloc_param_target(char *target_config)
+/* Allocate and initialize with defaults.
+ * Note that these targets get their config_item fields zeroed-out.
+ */
+static struct netconsole_target *alloc_and_init(void)
 {
-	int err = -ENOMEM;
 	struct netconsole_target *nt;
 
-	/*
-	 * Allocate and initialize with defaults.
-	 * Note that these targets get their config_item fields zeroed-out.
-	 */
 	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
 	if (!nt)
-		goto fail;
+		return nt;
 
 	nt->np.name = "netconsole";
 	strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
@@ -187,6 +184,21 @@  static struct netconsole_target *alloc_param_target(char *target_config)
 	nt->np.remote_port = 6666;
 	eth_broadcast_addr(nt->np.remote_mac);
 
+	return nt;
+}
+
+/* Allocate new target (from boot/module param) and setup netpoll for it */
+static struct netconsole_target *alloc_param_target(char *target_config)
+{
+	struct netconsole_target *nt;
+	int err;
+
+	nt = alloc_and_init();
+	if (!nt) {
+		err = -ENOMEM;
+		goto fail;
+	}
+
 	if (*target_config == '+') {
 		nt->extended = true;
 		target_config++;
@@ -664,23 +676,13 @@  static const struct config_item_type netconsole_target_type = {
 static struct config_item *make_netconsole_target(struct config_group *group,
 						  const char *name)
 {
-	unsigned long flags;
 	struct netconsole_target *nt;
+	unsigned long flags;
 
-	/*
-	 * Allocate and initialize with defaults.
-	 * Target is disabled at creation (!enabled).
-	 */
-	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
+	nt = alloc_and_init();
 	if (!nt)
 		return ERR_PTR(-ENOMEM);
 
-	nt->np.name = "netconsole";
-	strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
-	nt->np.local_port = 6665;
-	nt->np.remote_port = 6666;
-	eth_broadcast_addr(nt->np.remote_mac);
-
 	/* Initialize the config_item member */
 	config_item_init_type_name(&nt->item, name, &netconsole_target_type);