2024-02-05 15:40:04

by Robin Murphy

[permalink] [raw]
Subject: [PATCH v3 0/3] iommu/iova: use named kmem_cache for iova magazines

Hi all,

I decided that Pasha's patch[1] was a good excuse to tackle a bit more
cleanup of the existing IOVA code, so it can fit in more neatly. Thus
to save time and effort I've taken the liberty of putting together this
mini-series as the version I'd like to merge.

Cheers,
Robin.

[1] https://lore.kernel.org/linux-iommu/[email protected]/


Pasha Tatashin (1):
iommu/iova: use named kmem_cache for iova magazines

Robin Murphy (2):
iommu/iova: Tidy up iova_cache_get() failure
iommu/iova: Reorganise some code

drivers/iommu/iova.c | 143 +++++++++++++++++++++++--------------------
1 file changed, 76 insertions(+), 67 deletions(-)

--
2.39.2.101.g768bb238c484.dirty



2024-02-05 15:40:35

by Robin Murphy

[permalink] [raw]
Subject: [PATCH v3 1/3] iommu/iova: Tidy up iova_cache_get() failure

Failure handling in iova_cache_get() is a little messy, and we'd like
to add some more to it, so let's tidy up a bit first. By leaving the
hotplug handler until last we can take advantage of kmem_cache_destroy()
being NULL-safe to have a single cleanup label. We can also improve the
error reporting, noting that kmem_cache_create() already screams if it
fails, so that one is redundant.

Signed-off-by: Robin Murphy <[email protected]>
---
drivers/iommu/iova.c | 33 ++++++++++++++++-----------------
1 file changed, 16 insertions(+), 17 deletions(-)

diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c
index d30e453d0fb4..cf95001d85c0 100644
--- a/drivers/iommu/iova.c
+++ b/drivers/iommu/iova.c
@@ -254,26 +254,20 @@ static void free_iova_mem(struct iova *iova)

int iova_cache_get(void)
{
+ int err = -ENOMEM;
+
mutex_lock(&iova_cache_mutex);
if (!iova_cache_users) {
- int ret;
+ iova_cache = kmem_cache_create("iommu_iova", sizeof(struct iova), 0,
+ SLAB_HWCACHE_ALIGN, NULL);
+ if (!iova_cache)
+ goto out_err;

- ret = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead", NULL,
- iova_cpuhp_dead);
- if (ret) {
- mutex_unlock(&iova_cache_mutex);
- pr_err("Couldn't register cpuhp handler\n");
- return ret;
- }
-
- iova_cache = kmem_cache_create(
- "iommu_iova", sizeof(struct iova), 0,
- SLAB_HWCACHE_ALIGN, NULL);
- if (!iova_cache) {
- cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
- mutex_unlock(&iova_cache_mutex);
- pr_err("Couldn't create iova cache\n");
- return -ENOMEM;
+ err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead",
+ NULL, iova_cpuhp_dead);
+ if (err) {
+ pr_err("IOVA: Couldn't register cpuhp handler: %pe\n", ERR_PTR(err));
+ goto out_err;
}
}

@@ -281,6 +275,11 @@ int iova_cache_get(void)
mutex_unlock(&iova_cache_mutex);

return 0;
+
+out_err:
+ kmem_cache_destroy(iova_cache);
+ mutex_unlock(&iova_cache_mutex);
+ return err;
}
EXPORT_SYMBOL_GPL(iova_cache_get);

--
2.39.2.101.g768bb238c484.dirty


2024-02-05 18:15:53

by David Rientjes

[permalink] [raw]
Subject: Re: [PATCH v3 1/3] iommu/iova: Tidy up iova_cache_get() failure

On Mon, 5 Feb 2024, Robin Murphy wrote:

> Failure handling in iova_cache_get() is a little messy, and we'd like
> to add some more to it, so let's tidy up a bit first. By leaving the
> hotplug handler until last we can take advantage of kmem_cache_destroy()
> being NULL-safe to have a single cleanup label. We can also improve the
> error reporting, noting that kmem_cache_create() already screams if it
> fails, so that one is redundant.
>
> Signed-off-by: Robin Murphy <[email protected]>

Much easier to follow :)

Acked-by: David Rientjes <[email protected]>

2024-02-06 11:01:55

by John Garry

[permalink] [raw]
Subject: Re: [PATCH v3 1/3] iommu/iova: Tidy up iova_cache_get() failure

On 05/02/2024 15:32, Robin Murphy wrote:
> Failure handling in iova_cache_get() is a little messy, and we'd like
> to add some more to it, so let's tidy up a bit first. By leaving the
> hotplug handler until last we can take advantage of kmem_cache_destroy()
> being NULL-safe to have a single cleanup label. We can also improve the
> error reporting, noting that kmem_cache_create() already screams if it
> fails, so that one is redundant.
>
> Signed-off-by: Robin Murphy <[email protected]>

Regardless of a couple of minor comments, below, FWIW:
Reviewed-by: John Garry <[email protected]>

> ---
> drivers/iommu/iova.c | 33 ++++++++++++++++-----------------
> 1 file changed, 16 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c
> index d30e453d0fb4..cf95001d85c0 100644
> --- a/drivers/iommu/iova.c
> +++ b/drivers/iommu/iova.c
> @@ -254,26 +254,20 @@ static void free_iova_mem(struct iova *iova)
>
> int iova_cache_get(void)
> {
> + int err = -ENOMEM;
> +
> mutex_lock(&iova_cache_mutex);
> if (!iova_cache_users) {
> - int ret;
> + iova_cache = kmem_cache_create("iommu_iova", sizeof(struct iova), 0,
> + SLAB_HWCACHE_ALIGN, NULL);

Maybe can use KMEM_CACHE(), but it would mean that the name would change.

> + if (!iova_cache)
> + goto out_err;
>
> - ret = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead", NULL,
> - iova_cpuhp_dead);
> - if (ret) {
> - mutex_unlock(&iova_cache_mutex);
> - pr_err("Couldn't register cpuhp handler\n");
> - return ret;
> - }
> -
> - iova_cache = kmem_cache_create(
> - "iommu_iova", sizeof(struct iova), 0,
> - SLAB_HWCACHE_ALIGN, NULL);
> - if (!iova_cache) {
> - cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
> - mutex_unlock(&iova_cache_mutex);
> - pr_err("Couldn't create iova cache\n");
> - return -ENOMEM;
> + err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead",
> + NULL, iova_cpuhp_dead);
> + if (err) {
> + pr_err("IOVA: Couldn't register cpuhp handler: %pe\n", ERR_PTR(err));

Maybe can use pr_fmt with "iova".

> + goto out_err;
> }
> }
>
> @@ -281,6 +275,11 @@ int iova_cache_get(void)
> mutex_unlock(&iova_cache_mutex);
>
> return 0;
> +
> +out_err:
> + kmem_cache_destroy(iova_cache);
> + mutex_unlock(&iova_cache_mutex);
> + return err;
> }
> EXPORT_SYMBOL_GPL(iova_cache_get);
>


2024-02-06 11:26:14

by Robin Murphy

[permalink] [raw]
Subject: Re: [PATCH v3 1/3] iommu/iova: Tidy up iova_cache_get() failure

On 06/02/2024 11:01 am, John Garry wrote:
> On 05/02/2024 15:32, Robin Murphy wrote:
>> Failure handling in iova_cache_get() is a little messy, and we'd like
>> to add some more to it, so let's tidy up a bit first. By leaving the
>> hotplug handler until last we can take advantage of kmem_cache_destroy()
>> being NULL-safe to have a single cleanup label. We can also improve the
>> error reporting, noting that kmem_cache_create() already screams if it
>> fails, so that one is redundant.
>>
>> Signed-off-by: Robin Murphy <[email protected]>
>
> Regardless of a couple of minor comments, below, FWIW:
> Reviewed-by: John Garry <[email protected]>

Thanks!

>> ---
>>   drivers/iommu/iova.c | 33 ++++++++++++++++-----------------
>>   1 file changed, 16 insertions(+), 17 deletions(-)
>>
>> diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c
>> index d30e453d0fb4..cf95001d85c0 100644
>> --- a/drivers/iommu/iova.c
>> +++ b/drivers/iommu/iova.c
>> @@ -254,26 +254,20 @@ static void free_iova_mem(struct iova *iova)
>>   int iova_cache_get(void)
>>   {
>> +    int err = -ENOMEM;
>> +
>>       mutex_lock(&iova_cache_mutex);
>>       if (!iova_cache_users) {
>> -        int ret;
>> +        iova_cache = kmem_cache_create("iommu_iova", sizeof(struct
>> iova), 0,
>> +                           SLAB_HWCACHE_ALIGN, NULL);
>
> Maybe can use KMEM_CACHE(), but it would mean that the name would change.

Oh, I never came across that before. On reflection, I am inclined to
think that the "iommu_" names are usefully informative to userspace, and
it's not worth churning the structure names themselves just to save a
couple of lines here.

>> +        if (!iova_cache)
>> +            goto out_err;
>> -        ret = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD,
>> "iommu/iova:dead", NULL,
>> -                    iova_cpuhp_dead);
>> -        if (ret) {
>> -            mutex_unlock(&iova_cache_mutex);
>> -            pr_err("Couldn't register cpuhp handler\n");
>> -            return ret;
>> -        }
>> -
>> -        iova_cache = kmem_cache_create(
>> -            "iommu_iova", sizeof(struct iova), 0,
>> -            SLAB_HWCACHE_ALIGN, NULL);
>> -        if (!iova_cache) {
>> -            cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
>> -            mutex_unlock(&iova_cache_mutex);
>> -            pr_err("Couldn't create iova cache\n");
>> -            return -ENOMEM;
>> +        err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD,
>> "iommu/iova:dead",
>> +                          NULL, iova_cpuhp_dead);
>> +        if (err) {
>> +            pr_err("IOVA: Couldn't register cpuhp handler: %pe\n",
>> ERR_PTR(err));
>
> Maybe can use pr_fmt with "iova".

That one I did consider, but since we now have just this one print and
no imminent reason to add more, I figured I'd just stick it inline, and
we can factor out a pr_fmt in future if we ever have cause to.

Cheers,
Robin.

>
>> +            goto out_err;
>>           }
>>       }
>> @@ -281,6 +275,11 @@ int iova_cache_get(void)
>>       mutex_unlock(&iova_cache_mutex);
>>       return 0;
>> +
>> +out_err:
>> +    kmem_cache_destroy(iova_cache);
>> +    mutex_unlock(&iova_cache_mutex);
>> +    return err;
>>   }
>>   EXPORT_SYMBOL_GPL(iova_cache_get);
>

2024-02-07 16:41:34

by Jerry Snitselaar

[permalink] [raw]
Subject: Re: [PATCH v3 1/3] iommu/iova: Tidy up iova_cache_get() failure

On Mon, Feb 05, 2024 at 03:32:39PM +0000, Robin Murphy wrote:
> Failure handling in iova_cache_get() is a little messy, and we'd like
> to add some more to it, so let's tidy up a bit first. By leaving the
> hotplug handler until last we can take advantage of kmem_cache_destroy()
> being NULL-safe to have a single cleanup label. We can also improve the
> error reporting, noting that kmem_cache_create() already screams if it
> fails, so that one is redundant.
>
> Signed-off-by: Robin Murphy <[email protected]>
> ---
> drivers/iommu/iova.c | 33 ++++++++++++++++-----------------
> 1 file changed, 16 insertions(+), 17 deletions(-)
>

Reviewed-by: Jerry Snitselaar <[email protected]>


2024-02-09 10:46:36

by Joerg Roedel

[permalink] [raw]
Subject: Re: [PATCH v3 0/3] iommu/iova: use named kmem_cache for iova magazines

On Mon, Feb 05, 2024 at 03:32:38PM +0000, Robin Murphy wrote:
> Pasha Tatashin (1):
> iommu/iova: use named kmem_cache for iova magazines
>
> Robin Murphy (2):
> iommu/iova: Tidy up iova_cache_get() failure
> iommu/iova: Reorganise some code
>
> drivers/iommu/iova.c | 143 +++++++++++++++++++++++--------------------
> 1 file changed, 76 insertions(+), 67 deletions(-)

Applied, thanks Robin.