Skip to contentSkip to conversation

libnvdimm, pfn: not allocate nd_pfn->pfn_sb if already allocated

PATCH In review Wei Yang 2 participants 10 messages Jan 18, 2019 – Jan 21, 2019 libnvdimm: non-volatile memory device

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 libnvdimm, pfn: not allocate nd_pfn->pfn_sb if already allocated

In current implementation, we might re-allocate nd_pfn->pfn_sb.

For example:

    nd_dax_probe()
        nd_pfn->pfn_sb = devm_kzalloc()

    dax_pmem_probe()
        nvdimm_setup_pfn()
            nd_pfn_init()
                nd_pfn->pfn_sb = devm_kzalloc()

This patch checks nd_pfn->pfn_sb before allocating it in nd_pfn_init().

---
 drivers/nvdimm/pfn_devs.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/nvdimm/pfn_devs.c +9 -5
index 6f22272e8d80..1f4f07007483 100644
@@ -687,21 +687,25 @@ static int nd_pfn_init(struct nd_pfn *nd_pfn)
 	u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0;
 	struct nd_namespace_common *ndns = nd_pfn->ndns;
 	struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
+	struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
 	resource_size_t start, size;
 	struct nd_region *nd_region;
 	u32 start_pad, end_trunc;
-	struct nd_pfn_sb *pfn_sb;
 	unsigned long npfns;
 	phys_addr_t offset;
 	const char *sig;
 	u64 checksum;
 	int rc;
 
-	pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
-	if (!pfn_sb)
-		return -ENOMEM;
+	if (!pfn_sb) {
+		pfn_sb = devm_kzalloc(&nd_pfn->dev,
+				      sizeof(*pfn_sb), GFP_KERNEL);
+		if (!pfn_sb)
+			return -ENOMEM;
+
+		nd_pfn->pfn_sb = pfn_sb;
+	}
 
-	nd_pfn->pfn_sb = pfn_sb;
 	if (is_nd_dax(&nd_pfn->dev))
 		sig = DAX_SIG;
 	else
Signature · 6 lines
2.19.1

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
#1 lore raw
Quoted 13 lines · Wei Yang

In current implementation, we might re-allocate nd_pfn->pfn_sb.

For example:

    nd_dax_probe()
        nd_pfn->pfn_sb = devm_kzalloc()

    dax_pmem_probe()
        nvdimm_setup_pfn()
            nd_pfn_init()
                nd_pfn->pfn_sb = devm_kzalloc()
Quoted 1 line

This patch checks nd_pfn->pfn_sb before allocating it in nd_pfn_init().

Ugh, this patch is unfortunately uncovering a deeper problem. nvdimm_setup_pfn() should not be calling nd_pfn_init(). This effectively is always re-initializing the info-block, benign but unnecessary. Instead it needs to be using nd_pfn_validate() followed by an __nvdimm_setup_pfn() in the dax_pmem_probe() path.

Good find!
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

author
#2 lore raw
Quoted 16 lines · Dan Williams
Quoted 13 lines · Wei Yang

In current implementation, we might re-allocate nd_pfn->pfn_sb.

For example:

    nd_dax_probe()
        nd_pfn->pfn_sb = devm_kzalloc()

    dax_pmem_probe()
        nvdimm_setup_pfn()
            nd_pfn_init()
                nd_pfn->pfn_sb = devm_kzalloc()
Quoted 1 line

This patch checks nd_pfn->pfn_sb before allocating it in nd_pfn_init().

Quoted 7 lines

Ugh, this patch is unfortunately uncovering a deeper problem. nvdimm_setup_pfn() should not be calling nd_pfn_init(). This effectively is always re-initializing the info-block, benign but unnecessary. Instead it needs to be using nd_pfn_validate() followed by an __nvdimm_setup_pfn() in the dax_pmem_probe() path.

Good find!

So we should fix this like:

dax_pmem_probe()

    nd_pfn_validate()
    __nvdimm_setup_pfn()

Is my understanding correct?

Signature · 6 lines
Wei Yang
Help you, Help me
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
#3 lore raw
Quoted 26 lines · Wei Yang
Quoted 16 lines · Dan Williams
Quoted 13 lines · Wei Yang

In current implementation, we might re-allocate nd_pfn->pfn_sb.

For example:

    nd_dax_probe()
        nd_pfn->pfn_sb = devm_kzalloc()

    dax_pmem_probe()
        nvdimm_setup_pfn()
            nd_pfn_init()
                nd_pfn->pfn_sb = devm_kzalloc()
Quoted 1 line

This patch checks nd_pfn->pfn_sb before allocating it in nd_pfn_init().

Quoted 7 lines

Ugh, this patch is unfortunately uncovering a deeper problem. nvdimm_setup_pfn() should not be calling nd_pfn_init(). This effectively is always re-initializing the info-block, benign but unnecessary. Instead it needs to be using nd_pfn_validate() followed by an __nvdimm_setup_pfn() in the dax_pmem_probe() path.

Good find!

Quoted 7 lines

So we should fix this like:

dax_pmem_probe()

    nd_pfn_validate()
    __nvdimm_setup_pfn()

Is my understanding correct?

Yes, at first glance, but it would need a deeper review and testing. The usage of nvdimm_setup_pfn() in drivers/nvdimm/pmem.c needs a similar fixup.
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

author
#4 lore raw
Quoted 33 lines · Dan Williams
Quoted 32 lines · Wei Yang
Quoted 16 lines · Dan Williams
Quoted 13 lines · Wei Yang

In current implementation, we might re-allocate nd_pfn->pfn_sb.

For example:

    nd_dax_probe()
        nd_pfn->pfn_sb = devm_kzalloc()

    dax_pmem_probe()
        nvdimm_setup_pfn()
            nd_pfn_init()
                nd_pfn->pfn_sb = devm_kzalloc()
Quoted 1 line

This patch checks nd_pfn->pfn_sb before allocating it in nd_pfn_init().

Quoted 7 lines

Ugh, this patch is unfortunately uncovering a deeper problem. nvdimm_setup_pfn() should not be calling nd_pfn_init(). This effectively is always re-initializing the info-block, benign but unnecessary. Instead it needs to be using nd_pfn_validate() followed by an __nvdimm_setup_pfn() in the dax_pmem_probe() path.

Good find!

So we should fix this like:

dax_pmem_probe()

    nd_pfn_validate()
    __nvdimm_setup_pfn()
Quoted 5 lines
Quoted 1 line

Is my understanding correct?

Yes, at first glance, but it would need a deeper review and testing. The usage of nvdimm_setup_pfn() in drivers/nvdimm/pmem.c needs a similar fixup.

I went through the code again and found I may remove the allocation in nd_pfn_init().

Below is my analysis.

nvdimm_setup_pfn() is invoked in two places:

* dax_pmem_probe()
* pmem_attach_disk(), when it is_nd_pfn()

And before these two function called, corresponding device should be allocated and created. And we can see these two corresponding places to create the devices are:

* nd_dax_probe()
* nd_pfn_probe()

Both of them allocate pfn_sb. This means it is not necessary to allocate it again when we do the probe function.

Do you think it looks good to you?

Signature · 6 lines
Wei Yang
Help you, Help me
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
#5 lore raw
Quoted 55 lines · Wei Yang
Quoted 33 lines · Dan Williams
Quoted 32 lines · Wei Yang
Quoted 16 lines · Dan Williams
Quoted 13 lines · Wei Yang

In current implementation, we might re-allocate nd_pfn->pfn_sb.

For example:

    nd_dax_probe()
        nd_pfn->pfn_sb = devm_kzalloc()

    dax_pmem_probe()
        nvdimm_setup_pfn()
            nd_pfn_init()
                nd_pfn->pfn_sb = devm_kzalloc()
Quoted 1 line

This patch checks nd_pfn->pfn_sb before allocating it in nd_pfn_init().

Quoted 7 lines

Ugh, this patch is unfortunately uncovering a deeper problem. nvdimm_setup_pfn() should not be calling nd_pfn_init(). This effectively is always re-initializing the info-block, benign but unnecessary. Instead it needs to be using nd_pfn_validate() followed by an __nvdimm_setup_pfn() in the dax_pmem_probe() path.

Good find!

So we should fix this like:

dax_pmem_probe()

    nd_pfn_validate()
    __nvdimm_setup_pfn()
Quoted 5 lines
Quoted 1 line

Is my understanding correct?

Yes, at first glance, but it would need a deeper review and testing. The usage of nvdimm_setup_pfn() in drivers/nvdimm/pmem.c needs a similar fixup.

I went through the code again and found I may remove the allocation in nd_pfn_init().

Below is my analysis.

nvdimm_setup_pfn() is invoked in two places:

* dax_pmem_probe()
* pmem_attach_disk(), when it is_nd_pfn()

And before these two function called, corresponding device should be allocated and created. And we can see these two corresponding places to create the devices are:

Quoted 8 lines

* nd_dax_probe()
* nd_pfn_probe()

Both of them allocate pfn_sb. This means it is not necessary to allocate it again when we do the probe function.

Do you think it looks good to you?

No it doesn't because the real issue is that we should not be recreating the info block. So the fix is not avoiding the allocation, the fix is avoiding nd_pfn_init() in the probe path altogether.
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

#6 lore raw
Quoted 63 lines · Dan Williams
Quoted 61 lines · Wei Yang
Quoted 33 lines · Dan Williams
Quoted 32 lines · Wei Yang
Quoted 16 lines · Dan Williams
Quoted 13 lines · Wei Yang

In current implementation, we might re-allocate nd_pfn->pfn_sb.

For example:

    nd_dax_probe()
        nd_pfn->pfn_sb = devm_kzalloc()

    dax_pmem_probe()
        nvdimm_setup_pfn()
            nd_pfn_init()
                nd_pfn->pfn_sb = devm_kzalloc()
Quoted 1 line

This patch checks nd_pfn->pfn_sb before allocating it in nd_pfn_init().

Quoted 7 lines

Ugh, this patch is unfortunately uncovering a deeper problem. nvdimm_setup_pfn() should not be calling nd_pfn_init(). This effectively is always re-initializing the info-block, benign but unnecessary. Instead it needs to be using nd_pfn_validate() followed by an __nvdimm_setup_pfn() in the dax_pmem_probe() path.

Good find!

So we should fix this like:

dax_pmem_probe()

    nd_pfn_validate()
    __nvdimm_setup_pfn()
Quoted 5 lines
Quoted 1 line

Is my understanding correct?

Yes, at first glance, but it would need a deeper review and testing. The usage of nvdimm_setup_pfn() in drivers/nvdimm/pmem.c needs a similar fixup.

I went through the code again and found I may remove the allocation in nd_pfn_init().

Below is my analysis.

nvdimm_setup_pfn() is invoked in two places:

* dax_pmem_probe()
* pmem_attach_disk(), when it is_nd_pfn()

And before these two function called, corresponding device should be allocated and created. And we can see these two corresponding places to create the devices are:

* nd_dax_probe()
* nd_pfn_probe()

Both of them allocate pfn_sb. This means it is not necessary to allocate it again when we do the probe function.

Quoted 6 lines
Quoted 2 lines

Do you think it looks good to you?

No it doesn't because the real issue is that we should not be recreating the info block. So the fix is not avoiding the allocation, the fix is avoiding nd_pfn_init() in the probe path altogether.

I was wrong, thankfully! nd_pfn_init() *does* call nd_pfn_validate(), I was just overlooking it.

I also don't see that this fix is needed. nd_dax_probe() devm-allocates pfn_sb and performs a validation. If the validation succeeds nd_dax_probe() returns 0 which creates a new device and then causes nd_pmem_probe() to fail. At that point devm frees the pfn_sb allocation. The allocation is also freed if the validation step failed.
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

author
#7 lore raw
Quoted 71 lines · Dan Williams
Quoted 63 lines · Dan Williams
Quoted 61 lines · Wei Yang
Quoted 33 lines · Dan Williams
Quoted 32 lines · Wei Yang
Quoted 16 lines · Dan Williams
Quoted 13 lines · Wei Yang

In current implementation, we might re-allocate nd_pfn->pfn_sb.

For example:

    nd_dax_probe()
        nd_pfn->pfn_sb = devm_kzalloc()

    dax_pmem_probe()
        nvdimm_setup_pfn()
            nd_pfn_init()
                nd_pfn->pfn_sb = devm_kzalloc()
Quoted 1 line

This patch checks nd_pfn->pfn_sb before allocating it in nd_pfn_init().

Quoted 7 lines

Ugh, this patch is unfortunately uncovering a deeper problem. nvdimm_setup_pfn() should not be calling nd_pfn_init(). This effectively is always re-initializing the info-block, benign but unnecessary. Instead it needs to be using nd_pfn_validate() followed by an __nvdimm_setup_pfn() in the dax_pmem_probe() path.

Good find!

So we should fix this like:

dax_pmem_probe()

    nd_pfn_validate()
    __nvdimm_setup_pfn()
Quoted 5 lines
Quoted 1 line

Is my understanding correct?

Yes, at first glance, but it would need a deeper review and testing. The usage of nvdimm_setup_pfn() in drivers/nvdimm/pmem.c needs a similar fixup.

I went through the code again and found I may remove the allocation in nd_pfn_init().

Below is my analysis.

nvdimm_setup_pfn() is invoked in two places:

* dax_pmem_probe()
* pmem_attach_disk(), when it is_nd_pfn()

And before these two function called, corresponding device should be allocated and created. And we can see these two corresponding places to create the devices are:

* nd_dax_probe()
* nd_pfn_probe()

Both of them allocate pfn_sb. This means it is not necessary to allocate it again when we do the probe function.

Quoted 6 lines
Quoted 2 lines

Do you think it looks good to you?

No it doesn't because the real issue is that we should not be recreating the info block. So the fix is not avoiding the allocation, the fix is avoiding nd_pfn_init() in the probe path altogether.

Quoted 7 lines

I was wrong, thankfully! nd_pfn_init() *does* call nd_pfn_validate(), I was just overlooking it.

I also don't see that this fix is needed. nd_dax_probe() devm-allocates pfn_sb and performs a validation. If the validation succeeds nd_dax_probe() returns 0 which creates a new device and then causes nd_pmem_probe() to fail. At that point devm frees the pfn_sb

If I am correct, nd_dax_probe() creates a device whose driver is dax_pmem_driver. So the probe function is dax_pmem_probe.

Based on my test, one more pfn_sb is allocated in dax_pmem_probe and the driver attached to the device successfully.

Quoted 2 lines

allocation. The allocation is also freed if the validation step failed.

nd_pmem_probe() will be called on device created by nd_btt_probe() and nd_pfn_probe(), if I am right.

So in which case these two driver will fail to to attach the device and release the memory?

Signature · 6 lines
Wei Yang
Help you, Help me
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
#8 lore raw

On Sun, Jan 20, 2019 at 4:51 PM Wei Yang <[email protected]> wrote:
[..]

Quoted 11 lines
Quoted 4 lines

I also don't see that this fix is needed. nd_dax_probe() devm-allocates pfn_sb and performs a validation. If the validation succeeds nd_dax_probe() returns 0 which creates a new device and then causes nd_pmem_probe() to fail. At that point devm frees the pfn_sb

If I am correct, nd_dax_probe() creates a device whose driver is dax_pmem_driver. So the probe function is dax_pmem_probe.

Based on my test, one more pfn_sb is allocated in dax_pmem_probe and the driver attached to the device successfully.

Quoted 8 lines
Quoted 2 lines

allocation. The allocation is also freed if the validation step failed.

nd_pmem_probe() will be called on device created by nd_btt_probe() and nd_pfn_probe(), if I am right.

So in which case these two driver will fail to to attach the device and release the memory?

See nd_pmem_probe():

        /* if we find a valid info-block we'll come back as that personality */
        if (nd_btt_probe(dev, ndns) == 0 || nd_pfn_probe(dev, ndns) == 0
                        || nd_dax_probe(dev, ndns) == 0)
                return -ENXIO;

...all devm allocations are released due to that "return -ENXIO".
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

author
#9 lore raw
In reply to Dan Williams
Quoted 62 lines · Dan Williams
Quoted 61 lines · Wei Yang
Quoted 33 lines · Dan Williams
Quoted 32 lines · Wei Yang
Quoted 16 lines · Dan Williams
Quoted 13 lines · Wei Yang

In current implementation, we might re-allocate nd_pfn->pfn_sb.

For example:

    nd_dax_probe()
        nd_pfn->pfn_sb = devm_kzalloc()

    dax_pmem_probe()
        nvdimm_setup_pfn()
            nd_pfn_init()
                nd_pfn->pfn_sb = devm_kzalloc()
Quoted 1 line

This patch checks nd_pfn->pfn_sb before allocating it in nd_pfn_init().

Quoted 7 lines

Ugh, this patch is unfortunately uncovering a deeper problem. nvdimm_setup_pfn() should not be calling nd_pfn_init(). This effectively is always re-initializing the info-block, benign but unnecessary. Instead it needs to be using nd_pfn_validate() followed by an __nvdimm_setup_pfn() in the dax_pmem_probe() path.

Good find!

So we should fix this like:

dax_pmem_probe()

    nd_pfn_validate()
    __nvdimm_setup_pfn()
Quoted 5 lines
Quoted 1 line

Is my understanding correct?

Yes, at first glance, but it would need a deeper review and testing. The usage of nvdimm_setup_pfn() in drivers/nvdimm/pmem.c needs a similar fixup.

I went through the code again and found I may remove the allocation in nd_pfn_init().

Below is my analysis.

nvdimm_setup_pfn() is invoked in two places:

* dax_pmem_probe()
* pmem_attach_disk(), when it is_nd_pfn()

And before these two function called, corresponding device should be allocated and created. And we can see these two corresponding places to create the devices are:

* nd_dax_probe()
* nd_pfn_probe()

Both of them allocate pfn_sb. This means it is not necessary to allocate it again when we do the probe function.

Quoted 6 lines
Quoted 2 lines

Do you think it looks good to you?

No it doesn't because the real issue is that we should not be recreating the info block. So the fix is not avoiding the allocation, the fix is avoiding nd_pfn_init() in the probe path altogether.

Hmm... I may lost here. Which info block we recreated?

For one particular device type, nvdimm_setup_pfn() is just called once. By preventing re-allocation here is enough to me. I may not get your point here.

Would you mind share some light on this place?

Signature · 6 lines
Wei Yang
Help you, Help me
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm