diff mbox series

[1/2] net/iucv: Improve unlocking in iucv_enable()

Message ID 81f7db31-a258-4dc8-b6e1-c1ef1844a9d2@web.de (mailing list archive)
State Rejected
Delegated to: Netdev Maintainers
Headers show
Series net/iucv: Adjustments for iucv_enable() | expand

Checks

Context Check Description
netdev/series_format warning Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
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: 8 this patch: 8
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
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: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 19 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Markus Elfring Jan. 1, 2024, 8:58 p.m. UTC
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Mon, 1 Jan 2024 21:15:11 +0100

* Add a label so that a call of the function “cpus_read_unlock”
  is stored only once in this function implementation.

* Replace one call at the end by a goto statement.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 net/iucv/iucv.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

--
2.43.0

Comments

Suman Ghosh Jan. 2, 2024, 6:44 a.m. UTC | #1
@@ -555,13 +555,16 @@ static int iucv_enable(void)
> 	if (cpumask_empty(&iucv_buffer_cpumask))
> 		/* No cpu could declare an iucv buffer. */
> 		goto out;
>+
>+	rc = 0;
>+unlock:
> 	cpus_read_unlock();
>-	return 0;
>+	return rc;
>+
> out:
> 	kfree(iucv_path_table);
> 	iucv_path_table = NULL;
>-	cpus_read_unlock();
>-	return rc;
>+	goto unlock;
[Suman] This looks confusing. What is the issue with retaining the original change?
> }
>
> /*
>--
>2.43.0
>
Markus Elfring Jan. 2, 2024, 7:38 a.m. UTC | #2
> @@ -555,13 +555,16 @@ static int iucv_enable(void)
>> 	if (cpumask_empty(&iucv_buffer_cpumask))
>> 		/* No cpu could declare an iucv buffer. */
>> 		goto out;
>> +
>> +	rc = 0;
>> +unlock:
>> 	cpus_read_unlock();
>> -	return 0;
>> +	return rc;
>> +
>> out:
>> 	kfree(iucv_path_table);
>> 	iucv_path_table = NULL;
>> -	cpus_read_unlock();
>> -	return rc;
>> +	goto unlock;
> [Suman] This looks confusing. What is the issue with retaining the original change?

I propose to reduce the number of cpus_read_unlock() calls
(in the source code).

Regards,
Markus
Suman Ghosh Jan. 2, 2024, 8:27 a.m. UTC | #3
>>> 	if (cpumask_empty(&iucv_buffer_cpumask))
>>> 		/* No cpu could declare an iucv buffer. */
>>> 		goto out;
>>> +
>>> +	rc = 0;
>>> +unlock:
>>> 	cpus_read_unlock();
>>> -	return 0;
>>> +	return rc;
>>> +
>>> out:
>>> 	kfree(iucv_path_table);
>>> 	iucv_path_table = NULL;
>>> -	cpus_read_unlock();
>>> -	return rc;
>>> +	goto unlock;
>> [Suman] This looks confusing. What is the issue with retaining the
>original change?
>
>I propose to reduce the number of cpus_read_unlock() calls (in the
>source code).
>
>Regards,
>Markus
[Suman] Then I think we should do something like this. Changing the code flow back-and-forth using "goto" does not seem correct.

static int iucv_enable(void)
{
        size_t alloc_size;
        int cpu, rc = 0;

        cpus_read_lock();
        alloc_size = iucv_max_pathid * sizeof(struct iucv_path);
        iucv_path_table = kzalloc(alloc_size, GFP_KERNEL);
        if (!iucv_path_table) {
                rc = -ENOMEM;
                goto out;
        }

        /* Declare per cpu buffers. */
        for_each_online_cpu(cpu)
                smp_call_function_single(cpu, iucv_declare_cpu, NULL, 1);
        if (cpumask_empty(&iucv_buffer_cpumask))
                /* No cpu could declare an iucv buffer. */
                rc = -EIO;

out:
        if (rc) {
                kfree(iucv_path_table); //kfree is itself NULL protected. So, kzalloc failure should also be handled.
                iucv_path_table = NULL;
        }

        cpus_read_unlock();
        return rc;
}
Alexandra Winter Jan. 2, 2024, 9:53 a.m. UTC | #4
On 02.01.24 09:27, Suman Ghosh wrote:
>>> [Suman] This looks confusing. What is the issue with retaining the
>> original change?
>>
>> I propose to reduce the number of cpus_read_unlock() calls (in the
>> source code).
>>
>> Regards,
>> Markus
> [Suman] Then I think we should do something like this. Changing the code flow back-and-forth using "goto" does not seem correct.

I share Suman's concern that jumping backwards goto is confusing.
But I think the Coccinelle finding of freeing a null-pointer should be addressed (see patch 2/2)
Thank you Markus for reporting it.

The allocation does require holding the cpus_read_lock. 
For some reason Markus wants to reduce the number of cpus_read_unlock() calls (why?),
so what about something like this for both issues:

diff --git a/net/iucv/iucv.c b/net/iucv/iucv.c
index 0ed6e34d6edd..1030403b826b 100644
--- a/net/iucv/iucv.c
+++ b/net/iucv/iucv.c
@@ -542,24 +542,22 @@ static int iucv_enable(void)
        size_t alloc_size;
        int cpu, rc;

-       cpus_read_lock();
-       rc = -ENOMEM;
        alloc_size = iucv_max_pathid * sizeof(struct iucv_path);
        iucv_path_table = kzalloc(alloc_size, GFP_KERNEL);
        if (!iucv_path_table)
-               goto out;
+               return -ENOMEM;
        /* Declare per cpu buffers. */
-       rc = -EIO;
+       cpus_read_lock();
        for_each_online_cpu(cpu)
                smp_call_function_single(cpu, iucv_declare_cpu, NULL, 1);
-       if (cpumask_empty(&iucv_buffer_cpumask))
+       if (cpumask_empty(&iucv_buffer_cpumask)) {
                /* No cpu could declare an iucv buffer. */
-               goto out;
-       cpus_read_unlock();
-       return 0;
-out:
-       kfree(iucv_path_table);
-       iucv_path_table = NULL;
+               kfree(iucv_path_table);
+               iucv_path_table = NULL;
+               rc = -EIO;
+       } else {
+               rc = 0;
+       }
        cpus_read_unlock();
        return rc;
 }
Markus Elfring Jan. 2, 2024, 10:31 a.m. UTC | #5
> I share Suman's concern that jumping backwards goto is confusing.
> But I think the Coccinelle finding of freeing a null-pointer should be addressed (see patch 2/2)
> Thank you Markus for reporting it.
>
> The allocation does require holding the cpus_read_lock.

How does this information fit to your following suggestion to adjust the lock scope?


> For some reason Markus wants to reduce the number of cpus_read_unlock() calls (why?),

One cpus_read_unlock() call is required here.
Would you like to benefit more from a smaller executable code size?


> so what about something like this for both issues:
>
> diff --git a/net/iucv/iucv.c b/net/iucv/iucv.c
> index 0ed6e34d6edd..1030403b826b 100644
> --- a/net/iucv/iucv.c
> +++ b/net/iucv/iucv.c
> @@ -542,24 +542,22 @@ static int iucv_enable(void)
>         size_t alloc_size;
>         int cpu, rc;
>
> -       cpus_read_lock();
> -       rc = -ENOMEM;
>         alloc_size = iucv_max_pathid * sizeof(struct iucv_path);
>         iucv_path_table = kzalloc(alloc_size, GFP_KERNEL);
>         if (!iucv_path_table)
> -               goto out;
> +               return -ENOMEM;
>         /* Declare per cpu buffers. */
> -       rc = -EIO;
> +       cpus_read_lock();
>         for_each_online_cpu(cpu)
>                 smp_call_function_single(cpu, iucv_declare_cpu, NULL, 1);
> -       if (cpumask_empty(&iucv_buffer_cpumask))
> +       if (cpumask_empty(&iucv_buffer_cpumask)) {
>                 /* No cpu could declare an iucv buffer. */
> -               goto out;
> -       cpus_read_unlock();
> -       return 0;
> -out:
> -       kfree(iucv_path_table);
> -       iucv_path_table = NULL;
> +               kfree(iucv_path_table);
> +               iucv_path_table = NULL;
> +               rc = -EIO;
> +       } else {
> +               rc = 0;
> +       }
>         cpus_read_unlock();
>         return rc;
>  }


I suggest to reconsider patch squashing a bit more.

Regards,
Markus
diff mbox series

Patch

diff --git a/net/iucv/iucv.c b/net/iucv/iucv.c
index 0ed6e34d6edd..71ba309e05ee 100644
--- a/net/iucv/iucv.c
+++ b/net/iucv/iucv.c
@@ -555,13 +555,16 @@  static int iucv_enable(void)
 	if (cpumask_empty(&iucv_buffer_cpumask))
 		/* No cpu could declare an iucv buffer. */
 		goto out;
+
+	rc = 0;
+unlock:
 	cpus_read_unlock();
-	return 0;
+	return rc;
+
 out:
 	kfree(iucv_path_table);
 	iucv_path_table = NULL;
-	cpus_read_unlock();
-	return rc;
+	goto unlock;
 }

 /*