[PATCH net-next 2/7] netconsole: refactor CPU number formatting into separate function

Breno Leitao posted 7 patches 11 months, 3 weeks ago
There is a newer version of this series
[PATCH net-next 2/7] netconsole: refactor CPU number formatting into separate function
Posted by Breno Leitao 11 months, 3 weeks ago
Extract CPU number formatting logic from prepare_extradata() into a new
append_cpu_nr() function.

This refactoring improves code organization by isolating CPU number
formatting into its own function while reducing the complexity of
prepare_extradata().

The change prepares the codebase for the upcoming taskname feature by
establishing a consistent pattern for handling sysdata features.

The CPU number formatting logic itself remains unchanged; only its
location has moved to improve maintainability.

Signed-off-by: Breno Leitao <leitao@debian.org>
---
 drivers/net/netconsole.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index c086e2fe51f874812379e6f89c421d7d32980f91..26ff2ed4de16bce58e9eeaf8b5b362dfaafaca0a 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -1117,13 +1117,21 @@ static void populate_configfs_item(struct netconsole_target *nt,
 	init_target_config_group(nt, target_name);
 }
 
+static int append_cpu_nr(struct netconsole_target *nt, int offset)
+{
+	/* Append cpu=%d at extradata_complete after userdata str */
+	return scnprintf(&nt->extradata_complete[offset],
+			 MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
+			 raw_smp_processor_id());
+}
+
 /*
  * prepare_extradata - append sysdata at extradata_complete in runtime
  * @nt: target to send message to
  */
 static int prepare_extradata(struct netconsole_target *nt)
 {
-	int sysdata_len, extradata_len;
+	int extradata_len;
 
 	/* userdata was appended when configfs write helper was called
 	 * by update_userdata().
@@ -1133,12 +1141,8 @@ static int prepare_extradata(struct netconsole_target *nt)
 	if (!(nt->sysdata_fields & SYSDATA_CPU_NR))
 		goto out;
 
-	/* Append cpu=%d at extradata_complete after userdata str */
-	sysdata_len = scnprintf(&nt->extradata_complete[nt->userdata_length],
-				MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
-				raw_smp_processor_id());
-
-	extradata_len += sysdata_len;
+	if (nt->sysdata_fields & SYSDATA_CPU_NR)
+		extradata_len += append_cpu_nr(nt, nt->userdata_length);
 
 	WARN_ON_ONCE(extradata_len >
 		     MAX_EXTRADATA_ENTRY_LEN * MAX_EXTRADATA_ITEMS);

-- 
2.43.5
Re: [PATCH net-next 2/7] netconsole: refactor CPU number formatting into separate function
Posted by Simon Horman 11 months, 2 weeks ago
On Fri, Feb 21, 2025 at 05:52:07AM -0800, Breno Leitao wrote:
> Extract CPU number formatting logic from prepare_extradata() into a new
> append_cpu_nr() function.
> 
> This refactoring improves code organization by isolating CPU number
> formatting into its own function while reducing the complexity of
> prepare_extradata().
> 
> The change prepares the codebase for the upcoming taskname feature by
> establishing a consistent pattern for handling sysdata features.
> 
> The CPU number formatting logic itself remains unchanged; only its
> location has moved to improve maintainability.
> 
> Signed-off-by: Breno Leitao <leitao@debian.org>
> ---
>  drivers/net/netconsole.c | 18 +++++++++++-------
>  1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
> index c086e2fe51f874812379e6f89c421d7d32980f91..26ff2ed4de16bce58e9eeaf8b5b362dfaafaca0a 100644
> --- a/drivers/net/netconsole.c
> +++ b/drivers/net/netconsole.c
> @@ -1117,13 +1117,21 @@ static void populate_configfs_item(struct netconsole_target *nt,
>  	init_target_config_group(nt, target_name);
>  }
>  
> +static int append_cpu_nr(struct netconsole_target *nt, int offset)
> +{
> +	/* Append cpu=%d at extradata_complete after userdata str */
> +	return scnprintf(&nt->extradata_complete[offset],
> +			 MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
> +			 raw_smp_processor_id());
> +}
> +
>  /*
>   * prepare_extradata - append sysdata at extradata_complete in runtime
>   * @nt: target to send message to
>   */
>  static int prepare_extradata(struct netconsole_target *nt)
>  {
> -	int sysdata_len, extradata_len;
> +	int extradata_len;
>  
>  	/* userdata was appended when configfs write helper was called
>  	 * by update_userdata().
> @@ -1133,12 +1141,8 @@ static int prepare_extradata(struct netconsole_target *nt)
>  	if (!(nt->sysdata_fields & SYSDATA_CPU_NR))
>  		goto out;
>  
> -	/* Append cpu=%d at extradata_complete after userdata str */
> -	sysdata_len = scnprintf(&nt->extradata_complete[nt->userdata_length],
> -				MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
> -				raw_smp_processor_id());
> -
> -	extradata_len += sysdata_len;
> +	if (nt->sysdata_fields & SYSDATA_CPU_NR)
> +		extradata_len += append_cpu_nr(nt, nt->userdata_length);

Hi Breno,

As this is the only caller of append_cpu_nr() I'm wondering
if it would be nicer if nt was the only argument to append_cpu_nr().

Not a big deal either way, so the above notwithstanding:

Reviewed-by: Simon Horman <horms@kernel.org>

>  
>  	WARN_ON_ONCE(extradata_len >
>  		     MAX_EXTRADATA_ENTRY_LEN * MAX_EXTRADATA_ITEMS);
> 
> -- 
> 2.43.5
> 
>
Re: [PATCH net-next 2/7] netconsole: refactor CPU number formatting into separate function
Posted by Breno Leitao 11 months, 2 weeks ago
Hello Simon,

On Tue, Feb 25, 2025 at 10:17:48AM +0000, Simon Horman wrote:
> On Fri, Feb 21, 2025 at 05:52:07AM -0800, Breno Leitao wrote:
> > Extract CPU number formatting logic from prepare_extradata() into a new
> > append_cpu_nr() function.
> > 
> > This refactoring improves code organization by isolating CPU number
> > formatting into its own function while reducing the complexity of
> > prepare_extradata().
> > 
> > The change prepares the codebase for the upcoming taskname feature by
> > establishing a consistent pattern for handling sysdata features.
> > 
> > The CPU number formatting logic itself remains unchanged; only its
> > location has moved to improve maintainability.
> > 
> > Signed-off-by: Breno Leitao <leitao@debian.org>
> > ---
> >  drivers/net/netconsole.c | 18 +++++++++++-------
> >  1 file changed, 11 insertions(+), 7 deletions(-)
> > 
> > diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
> > index c086e2fe51f874812379e6f89c421d7d32980f91..26ff2ed4de16bce58e9eeaf8b5b362dfaafaca0a 100644
> > --- a/drivers/net/netconsole.c
> > +++ b/drivers/net/netconsole.c
> > @@ -1117,13 +1117,21 @@ static void populate_configfs_item(struct netconsole_target *nt,
> >  	init_target_config_group(nt, target_name);
> >  }
> >  
> > +static int append_cpu_nr(struct netconsole_target *nt, int offset)
> > +{
> > +	/* Append cpu=%d at extradata_complete after userdata str */
> > +	return scnprintf(&nt->extradata_complete[offset],
> > +			 MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
> > +			 raw_smp_processor_id());
> > +}
> > +
> >  /*
> >   * prepare_extradata - append sysdata at extradata_complete in runtime
> >   * @nt: target to send message to
> >   */
> >  static int prepare_extradata(struct netconsole_target *nt)
> >  {
> > -	int sysdata_len, extradata_len;
> > +	int extradata_len;
> >  
> >  	/* userdata was appended when configfs write helper was called
> >  	 * by update_userdata().
> > @@ -1133,12 +1141,8 @@ static int prepare_extradata(struct netconsole_target *nt)
> >  	if (!(nt->sysdata_fields & SYSDATA_CPU_NR))
> >  		goto out;
> >  
> > -	/* Append cpu=%d at extradata_complete after userdata str */
> > -	sysdata_len = scnprintf(&nt->extradata_complete[nt->userdata_length],
> > -				MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
> > -				raw_smp_processor_id());
> > -
> > -	extradata_len += sysdata_len;
> > +	if (nt->sysdata_fields & SYSDATA_CPU_NR)
> > +		extradata_len += append_cpu_nr(nt, nt->userdata_length);
> 
> Hi Breno,
> 
> As this is the only caller of append_cpu_nr() I'm wondering
> if it would be nicer if nt was the only argument to append_cpu_nr().

Yes, I can do it. I just kept both functions the same:

  static int append_taskname(struct netconsole_target *nt, int offset)
  static int append_cpu_nr(struct netconsole_target *nt, int offset)

Another option is to use extradata_len as the second argument, instead
of nt->userdata_length. That might(?) make the code easier to read? it
would look like the following:

          extradata_len = nt->userdata_length;
          if (nt->sysdata_fields & SYSDATA_CPU_NR)
                  extradata_len += append_cpu_nr(nt, extradata_len);
          if (nt->sysdata_fields & SYSDATA_TASKNAME)
                  extradata_len += append_taskname(nt, extradata_len);

What would you write yourself?

Thank you very much for the review,
--breno
Re: [PATCH net-next 2/7] netconsole: refactor CPU number formatting into separate function
Posted by Simon Horman 11 months, 2 weeks ago
On Tue, Feb 25, 2025 at 03:09:20AM -0800, Breno Leitao wrote:
> Hello Simon,
> 
> On Tue, Feb 25, 2025 at 10:17:48AM +0000, Simon Horman wrote:
> > On Fri, Feb 21, 2025 at 05:52:07AM -0800, Breno Leitao wrote:
> > > Extract CPU number formatting logic from prepare_extradata() into a new
> > > append_cpu_nr() function.
> > > 
> > > This refactoring improves code organization by isolating CPU number
> > > formatting into its own function while reducing the complexity of
> > > prepare_extradata().
> > > 
> > > The change prepares the codebase for the upcoming taskname feature by
> > > establishing a consistent pattern for handling sysdata features.
> > > 
> > > The CPU number formatting logic itself remains unchanged; only its
> > > location has moved to improve maintainability.
> > > 
> > > Signed-off-by: Breno Leitao <leitao@debian.org>
> > > ---
> > >  drivers/net/netconsole.c | 18 +++++++++++-------
> > >  1 file changed, 11 insertions(+), 7 deletions(-)
> > > 
> > > diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
> > > index c086e2fe51f874812379e6f89c421d7d32980f91..26ff2ed4de16bce58e9eeaf8b5b362dfaafaca0a 100644
> > > --- a/drivers/net/netconsole.c
> > > +++ b/drivers/net/netconsole.c
> > > @@ -1117,13 +1117,21 @@ static void populate_configfs_item(struct netconsole_target *nt,
> > >  	init_target_config_group(nt, target_name);
> > >  }
> > >  
> > > +static int append_cpu_nr(struct netconsole_target *nt, int offset)
> > > +{
> > > +	/* Append cpu=%d at extradata_complete after userdata str */
> > > +	return scnprintf(&nt->extradata_complete[offset],
> > > +			 MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
> > > +			 raw_smp_processor_id());
> > > +}
> > > +
> > >  /*
> > >   * prepare_extradata - append sysdata at extradata_complete in runtime
> > >   * @nt: target to send message to
> > >   */
> > >  static int prepare_extradata(struct netconsole_target *nt)
> > >  {
> > > -	int sysdata_len, extradata_len;
> > > +	int extradata_len;
> > >  
> > >  	/* userdata was appended when configfs write helper was called
> > >  	 * by update_userdata().
> > > @@ -1133,12 +1141,8 @@ static int prepare_extradata(struct netconsole_target *nt)
> > >  	if (!(nt->sysdata_fields & SYSDATA_CPU_NR))
> > >  		goto out;
> > >  
> > > -	/* Append cpu=%d at extradata_complete after userdata str */
> > > -	sysdata_len = scnprintf(&nt->extradata_complete[nt->userdata_length],
> > > -				MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
> > > -				raw_smp_processor_id());
> > > -
> > > -	extradata_len += sysdata_len;
> > > +	if (nt->sysdata_fields & SYSDATA_CPU_NR)
> > > +		extradata_len += append_cpu_nr(nt, nt->userdata_length);
> > 
> > Hi Breno,
> > 
> > As this is the only caller of append_cpu_nr() I'm wondering
> > if it would be nicer if nt was the only argument to append_cpu_nr().
> 
> Yes, I can do it. I just kept both functions the same:
> 
>   static int append_taskname(struct netconsole_target *nt, int offset)
>   static int append_cpu_nr(struct netconsole_target *nt, int offset)
> 
> Another option is to use extradata_len as the second argument, instead
> of nt->userdata_length. That might(?) make the code easier to read? it
> would look like the following:
> 
>           extradata_len = nt->userdata_length;
>           if (nt->sysdata_fields & SYSDATA_CPU_NR)
>                   extradata_len += append_cpu_nr(nt, extradata_len);
>           if (nt->sysdata_fields & SYSDATA_TASKNAME)
>                   extradata_len += append_taskname(nt, extradata_len);
> 
> What would you write yourself?

I think that I would reduce the number of parameters of append_cpu_nr() and
append_taskname(). But really, any of the options, including this patch
as-is, are fine. So please chose whichever you think is best.