linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] device-dax: avoid an unnecessary check in alloc_dev_dax_range()
@ 2020-11-20  9:22 Zhen Lei
  2020-12-18  3:10 ` Dan Williams
  2020-12-18  6:09 ` Leizhen (ThunderTown)
  0 siblings, 2 replies; 4+ messages in thread
From: Zhen Lei @ 2020-11-20  9:22 UTC (permalink / raw)
  To: Dan Williams, Vishal Verma, Dave Jiang, linux-nvdimm, linux-kernel
  Cc: Zhen Lei

Swap the calling sequence of krealloc() and __request_region(), call the
latter first. In this way, the value of dev_dax->nr_range does not need to
be considered when __request_region() failed.

Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
---
 drivers/dax/bus.c | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
index 27513d311242..1efae11d947a 100644
--- a/drivers/dax/bus.c
+++ b/drivers/dax/bus.c
@@ -763,23 +763,15 @@ static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
 		return 0;
 	}
 
-	ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
-			* (dev_dax->nr_range + 1), GFP_KERNEL);
-	if (!ranges)
-		return -ENOMEM;
-
 	alloc = __request_region(res, start, size, dev_name(dev), 0);
-	if (!alloc) {
-		/*
-		 * If this was an empty set of ranges nothing else
-		 * will release @ranges, so do it now.
-		 */
-		if (!dev_dax->nr_range) {
-			kfree(ranges);
-			ranges = NULL;
-		}
-		dev_dax->ranges = ranges;
+	if (!alloc)
 		return -ENOMEM;
+
+	ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
+			* (dev_dax->nr_range + 1), GFP_KERNEL);
+	if (!ranges) {
+		rc = -ENOMEM;
+		goto err;
 	}
 
 	for (i = 0; i < dev_dax->nr_range; i++)
@@ -808,11 +800,14 @@ static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
 		dev_dbg(dev, "delete range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
 				&alloc->start, &alloc->end);
 		dev_dax->nr_range--;
-		__release_region(res, alloc->start, resource_size(alloc));
-		return rc;
+		goto err;
 	}
 
 	return 0;
+
+err:
+	__release_region(res, alloc->start, resource_size(alloc));
+	return rc;
 }
 
 static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
-- 
2.26.0.106.g9fadedd



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] device-dax: avoid an unnecessary check in alloc_dev_dax_range()
  2020-11-20  9:22 [PATCH 1/1] device-dax: avoid an unnecessary check in alloc_dev_dax_range() Zhen Lei
@ 2020-12-18  3:10 ` Dan Williams
  2020-12-18  6:02   ` Leizhen (ThunderTown)
  2020-12-18  6:09 ` Leizhen (ThunderTown)
  1 sibling, 1 reply; 4+ messages in thread
From: Dan Williams @ 2020-12-18  3:10 UTC (permalink / raw)
  To: Zhen Lei; +Cc: Vishal Verma, Dave Jiang, linux-nvdimm, linux-kernel, Jane Chu

On Fri, Nov 20, 2020 at 1:23 AM Zhen Lei <thunder.leizhen@huawei.com> wrote:
>
> Swap the calling sequence of krealloc() and __request_region(), call the
> latter first. In this way, the value of dev_dax->nr_range does not need to
> be considered when __request_region() failed.

This looks ok, but I think I want to see another cleanup go in first
before this to add a helper for trimming the last range off the set of
ranges:

static void dev_dax_trim_range(struct dev_dax *dev_dax)
{
        int i = dev_dax->nr_range - 1;
        struct range *range = &dev_dax->ranges[i].range;
        struct dax_region *dax_region = dev_dax->region;

        dev_dbg(dev, "delete range[%d]: %#llx:%#llx\n", i,
                (unsigned long long)range->start,
                (unsigned long long)range->end);

        __release_region(&dax_region->res, range->start, range_len(range));
        if (--dev_dax->nr_range == 0) {
                kfree(dev_dax->ranges);
                dev_dax->ranges = NULL;
        }
}

Care to do a lead in patch with that cleanup, then do this one?

I think that might also cleanup a memory leak report from Jane in
addition to not needing the "goto" as well.

http://lore.kernel.org/r/c8a8a260-34c6-dbfc-1f19-25c23d01cb45@oracle.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] device-dax: avoid an unnecessary check in alloc_dev_dax_range()
  2020-12-18  3:10 ` Dan Williams
@ 2020-12-18  6:02   ` Leizhen (ThunderTown)
  0 siblings, 0 replies; 4+ messages in thread
From: Leizhen (ThunderTown) @ 2020-12-18  6:02 UTC (permalink / raw)
  To: Dan Williams
  Cc: Vishal Verma, Dave Jiang, linux-nvdimm, linux-kernel, Jane Chu



On 2020/12/18 11:10, Dan Williams wrote:
> On Fri, Nov 20, 2020 at 1:23 AM Zhen Lei <thunder.leizhen@huawei.com> wrote:
>>
>> Swap the calling sequence of krealloc() and __request_region(), call the
>> latter first. In this way, the value of dev_dax->nr_range does not need to
>> be considered when __request_region() failed.
> 
> This looks ok, but I think I want to see another cleanup go in first
> before this to add a helper for trimming the last range off the set of
> ranges:
> 
> static void dev_dax_trim_range(struct dev_dax *dev_dax)
> {
>         int i = dev_dax->nr_range - 1;
>         struct range *range = &dev_dax->ranges[i].range;
>         struct dax_region *dax_region = dev_dax->region;
> 
>         dev_dbg(dev, "delete range[%d]: %#llx:%#llx\n", i,
>                 (unsigned long long)range->start,
>                 (unsigned long long)range->end);
> 
>         __release_region(&dax_region->res, range->start, range_len(range));
>         if (--dev_dax->nr_range == 0) {
>                 kfree(dev_dax->ranges);
>                 dev_dax->ranges = NULL;
>         }
> }
> 
> Care to do a lead in patch with that cleanup, then do this one?

I don't mind! You can add above helper first. After that, I'll update
and send this patch again.

> 
> I think that might also cleanup a memory leak report from Jane in
> addition to not needing the "goto" as well.
> 
> http://lore.kernel.org/r/c8a8a260-34c6-dbfc-1f19-25c23d01cb45@oracle.com
> 
> .
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] device-dax: avoid an unnecessary check in alloc_dev_dax_range()
  2020-11-20  9:22 [PATCH 1/1] device-dax: avoid an unnecessary check in alloc_dev_dax_range() Zhen Lei
  2020-12-18  3:10 ` Dan Williams
@ 2020-12-18  6:09 ` Leizhen (ThunderTown)
  1 sibling, 0 replies; 4+ messages in thread
From: Leizhen (ThunderTown) @ 2020-12-18  6:09 UTC (permalink / raw)
  To: Dan Williams, Vishal Verma, Dave Jiang, linux-nvdimm, linux-kernel



On 2020/11/20 17:22, Zhen Lei wrote:
> Swap the calling sequence of krealloc() and __request_region(), call the
> latter first. In this way, the value of dev_dax->nr_range does not need to
> be considered when __request_region() failed.
> 
> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> ---
>  drivers/dax/bus.c | 29 ++++++++++++-----------------
>  1 file changed, 12 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
> index 27513d311242..1efae11d947a 100644
> --- a/drivers/dax/bus.c
> +++ b/drivers/dax/bus.c
> @@ -763,23 +763,15 @@ static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
>  		return 0;
>  	}
>  
> -	ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
> -			* (dev_dax->nr_range + 1), GFP_KERNEL);
> -	if (!ranges)
> -		return -ENOMEM;
> -
>  	alloc = __request_region(res, start, size, dev_name(dev), 0);
> -	if (!alloc) {
> -		/*
> -		 * If this was an empty set of ranges nothing else
> -		 * will release @ranges, so do it now.
> -		 */
> -		if (!dev_dax->nr_range) {
> -			kfree(ranges);
> -			ranges = NULL;
> -		}
> -		dev_dax->ranges = ranges;
> +	if (!alloc)
>  		return -ENOMEM;
> +
> +	ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
> +			* (dev_dax->nr_range + 1), GFP_KERNEL);
> +	if (!ranges) {
> +		rc = -ENOMEM;
> +		goto err;

Hi, Dan Williams:
In fact, after adding the new helper dev_dax_trim_range(), we can
directly call __release_region() and return error code at here. Replace goto.

>  	}
>  
>  	for (i = 0; i < dev_dax->nr_range; i++)
> @@ -808,11 +800,14 @@ static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
>  		dev_dbg(dev, "delete range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
>  				&alloc->start, &alloc->end);
>  		dev_dax->nr_range--;
> -		__release_region(res, alloc->start, resource_size(alloc));
> -		return rc;
> +		goto err;
>  	}
>  
>  	return 0;
> +
> +err:
> +	__release_region(res, alloc->start, resource_size(alloc));
> +	return rc;
>  }
>  
>  static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-12-18  6:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-20  9:22 [PATCH 1/1] device-dax: avoid an unnecessary check in alloc_dev_dax_range() Zhen Lei
2020-12-18  3:10 ` Dan Williams
2020-12-18  6:02   ` Leizhen (ThunderTown)
2020-12-18  6:09 ` Leizhen (ThunderTown)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).