Skip to content

Commit 95bf6df

Browse files
committed
Merge branch 'for-6.5/dax-cleanups' into nvdimm-for-next
The reference counting of dax_region objects is needlessly complicated, has lead to confusion [1], and has hidden a bug [2]. While testing the cleanup for those issues, a CONFIG_DEBUG_KOBJECT_RELEASE test run uncovered a use-after-free in dax_mapping_release(). Clean all of that up. Thanks to Yongqiang, Paul, and Ira for their analysis. Additionally, clean up a redundant variable in fsdax, and fix memory hotplug registration in the kmem driver. [1]: http://lore.kernel.org/r/20221203095858.612027-1-liuyongqiang13@huawei.com [2]: http://lore.kernel.org/r/3cf0890b-4eb0-e70e-cd9c-2ecc3d496263@hpe.com
2 parents 0e796e3 + 46e66da commit 95bf6df

8 files changed

Lines changed: 46 additions & 51 deletions

File tree

drivers/dax/bus.c

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -446,18 +446,33 @@ static void unregister_dev_dax(void *dev)
446446
put_device(dev);
447447
}
448448

449+
static void dax_region_free(struct kref *kref)
450+
{
451+
struct dax_region *dax_region;
452+
453+
dax_region = container_of(kref, struct dax_region, kref);
454+
kfree(dax_region);
455+
}
456+
457+
static void dax_region_put(struct dax_region *dax_region)
458+
{
459+
kref_put(&dax_region->kref, dax_region_free);
460+
}
461+
449462
/* a return value >= 0 indicates this invocation invalidated the id */
450463
static int __free_dev_dax_id(struct dev_dax *dev_dax)
451464
{
452-
struct dax_region *dax_region = dev_dax->region;
453465
struct device *dev = &dev_dax->dev;
466+
struct dax_region *dax_region;
454467
int rc = dev_dax->id;
455468

456469
device_lock_assert(dev);
457470

458-
if (is_static(dax_region) || dev_dax->id < 0)
471+
if (!dev_dax->dyn_id || dev_dax->id < 0)
459472
return -1;
473+
dax_region = dev_dax->region;
460474
ida_free(&dax_region->ida, dev_dax->id);
475+
dax_region_put(dax_region);
461476
dev_dax->id = -1;
462477
return rc;
463478
}
@@ -473,6 +488,20 @@ static int free_dev_dax_id(struct dev_dax *dev_dax)
473488
return rc;
474489
}
475490

491+
static int alloc_dev_dax_id(struct dev_dax *dev_dax)
492+
{
493+
struct dax_region *dax_region = dev_dax->region;
494+
int id;
495+
496+
id = ida_alloc(&dax_region->ida, GFP_KERNEL);
497+
if (id < 0)
498+
return id;
499+
kref_get(&dax_region->kref);
500+
dev_dax->dyn_id = true;
501+
dev_dax->id = id;
502+
return id;
503+
}
504+
476505
static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
477506
const char *buf, size_t len)
478507
{
@@ -560,20 +589,6 @@ static const struct attribute_group *dax_region_attribute_groups[] = {
560589
NULL,
561590
};
562591

563-
static void dax_region_free(struct kref *kref)
564-
{
565-
struct dax_region *dax_region;
566-
567-
dax_region = container_of(kref, struct dax_region, kref);
568-
kfree(dax_region);
569-
}
570-
571-
void dax_region_put(struct dax_region *dax_region)
572-
{
573-
kref_put(&dax_region->kref, dax_region_free);
574-
}
575-
EXPORT_SYMBOL_GPL(dax_region_put);
576-
577592
static void dax_region_unregister(void *region)
578593
{
579594
struct dax_region *dax_region = region;
@@ -625,7 +640,6 @@ struct dax_region *alloc_dax_region(struct device *parent, int region_id,
625640
return NULL;
626641
}
627642

628-
kref_get(&dax_region->kref);
629643
if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
630644
return NULL;
631645
return dax_region;
@@ -635,10 +649,12 @@ EXPORT_SYMBOL_GPL(alloc_dax_region);
635649
static void dax_mapping_release(struct device *dev)
636650
{
637651
struct dax_mapping *mapping = to_dax_mapping(dev);
638-
struct dev_dax *dev_dax = to_dev_dax(dev->parent);
652+
struct device *parent = dev->parent;
653+
struct dev_dax *dev_dax = to_dev_dax(parent);
639654

640655
ida_free(&dev_dax->ida, mapping->id);
641656
kfree(mapping);
657+
put_device(parent);
642658
}
643659

644660
static void unregister_dax_mapping(void *data)
@@ -655,8 +671,7 @@ static void unregister_dax_mapping(void *data)
655671
dev_dax->ranges[mapping->range_id].mapping = NULL;
656672
mapping->range_id = -1;
657673

658-
device_del(dev);
659-
put_device(dev);
674+
device_unregister(dev);
660675
}
661676

662677
static struct dev_dax_range *get_dax_range(struct device *dev)
@@ -778,6 +793,7 @@ static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
778793
dev = &mapping->dev;
779794
device_initialize(dev);
780795
dev->parent = &dev_dax->dev;
796+
get_device(dev->parent);
781797
dev->type = &dax_mapping_type;
782798
dev_set_name(dev, "mapping%d", mapping->id);
783799
rc = device_add(dev);
@@ -1295,12 +1311,10 @@ static const struct attribute_group *dax_attribute_groups[] = {
12951311
static void dev_dax_release(struct device *dev)
12961312
{
12971313
struct dev_dax *dev_dax = to_dev_dax(dev);
1298-
struct dax_region *dax_region = dev_dax->region;
12991314
struct dax_device *dax_dev = dev_dax->dax_dev;
13001315

13011316
put_dax(dax_dev);
13021317
free_dev_dax_id(dev_dax);
1303-
dax_region_put(dax_region);
13041318
kfree(dev_dax->pgmap);
13051319
kfree(dev_dax);
13061320
}
@@ -1324,6 +1338,7 @@ struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
13241338
if (!dev_dax)
13251339
return ERR_PTR(-ENOMEM);
13261340

1341+
dev_dax->region = dax_region;
13271342
if (is_static(dax_region)) {
13281343
if (dev_WARN_ONCE(parent, data->id < 0,
13291344
"dynamic id specified to static region\n")) {
@@ -1339,13 +1354,11 @@ struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
13391354
goto err_id;
13401355
}
13411356

1342-
rc = ida_alloc(&dax_region->ida, GFP_KERNEL);
1357+
rc = alloc_dev_dax_id(dev_dax);
13431358
if (rc < 0)
13441359
goto err_id;
1345-
dev_dax->id = rc;
13461360
}
13471361

1348-
dev_dax->region = dax_region;
13491362
dev = &dev_dax->dev;
13501363
device_initialize(dev);
13511364
dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
@@ -1386,7 +1399,6 @@ struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
13861399
dev_dax->target_node = dax_region->target_node;
13871400
dev_dax->align = dax_region->align;
13881401
ida_init(&dev_dax->ida);
1389-
kref_get(&dax_region->kref);
13901402

13911403
inode = dax_inode(dax_dev);
13921404
dev->devt = inode->i_rdev;

drivers/dax/bus.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ struct dev_dax;
99
struct resource;
1010
struct dax_device;
1111
struct dax_region;
12-
void dax_region_put(struct dax_region *dax_region);
1312

1413
/* dax bus specific ioresource flags */
1514
#define IORESOURCE_DAX_STATIC BIT(0)

drivers/dax/cxl.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ static int cxl_dax_region_probe(struct device *dev)
1313
struct cxl_region *cxlr = cxlr_dax->cxlr;
1414
struct dax_region *dax_region;
1515
struct dev_dax_data data;
16-
struct dev_dax *dev_dax;
1716

1817
if (nid == NUMA_NO_NODE)
1918
nid = memory_add_physaddr_to_nid(cxlr_dax->hpa_range.start);
@@ -28,13 +27,8 @@ static int cxl_dax_region_probe(struct device *dev)
2827
.id = -1,
2928
.size = range_len(&cxlr_dax->hpa_range),
3029
};
31-
dev_dax = devm_create_dev_dax(&data);
32-
if (IS_ERR(dev_dax))
33-
return PTR_ERR(dev_dax);
3430

35-
/* child dev_dax instances now own the lifetime of the dax_region */
36-
dax_region_put(dax_region);
37-
return 0;
31+
return PTR_ERR_OR_ZERO(devm_create_dev_dax(&data));
3832
}
3933

4034
static struct cxl_driver cxl_dax_region_driver = {

drivers/dax/dax-private.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ struct dax_mapping {
5252
* @region - parent region
5353
* @dax_dev - core dax functionality
5454
* @target_node: effective numa node if dev_dax memory range is onlined
55-
* @id: ida allocated id
55+
* @dyn_id: is this a dynamic or statically created instance
56+
* @id: ida allocated id when the dax_region is not static
5657
* @ida: mapping id allocator
5758
* @dev - device core
5859
* @pgmap - pgmap for memmap setup / lifetime (driver owned)
@@ -64,6 +65,7 @@ struct dev_dax {
6465
struct dax_device *dax_dev;
6566
unsigned int align;
6667
int target_node;
68+
bool dyn_id;
6769
int id;
6870
struct ida ida;
6971
struct device dev;

drivers/dax/hmem/hmem.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ static int dax_hmem_probe(struct platform_device *pdev)
1616
struct dax_region *dax_region;
1717
struct memregion_info *mri;
1818
struct dev_dax_data data;
19-
struct dev_dax *dev_dax;
2019

2120
/*
2221
* @region_idle == true indicates that an administrative agent
@@ -38,13 +37,8 @@ static int dax_hmem_probe(struct platform_device *pdev)
3837
.id = -1,
3938
.size = region_idle ? 0 : range_len(&mri->range),
4039
};
41-
dev_dax = devm_create_dev_dax(&data);
42-
if (IS_ERR(dev_dax))
43-
return PTR_ERR(dev_dax);
4440

45-
/* child dev_dax instances now own the lifetime of the dax_region */
46-
dax_region_put(dax_region);
47-
return 0;
41+
return PTR_ERR_OR_ZERO(devm_create_dev_dax(&data));
4842
}
4943

5044
static struct platform_driver dax_hmem_driver = {

drivers/dax/kmem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
9999
if (!data->res_name)
100100
goto err_res_name;
101101

102-
rc = memory_group_register_static(numa_node, total_len);
102+
rc = memory_group_register_static(numa_node, PFN_UP(total_len));
103103
if (rc < 0)
104104
goto err_reg_mgid;
105105
data->mgid = rc;

drivers/dax/pmem.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ static struct dev_dax *__dax_pmem_probe(struct device *dev)
1313
int rc, id, region_id;
1414
resource_size_t offset;
1515
struct nd_pfn_sb *pfn_sb;
16-
struct dev_dax *dev_dax;
1716
struct dev_dax_data data;
1817
struct nd_namespace_io *nsio;
1918
struct dax_region *dax_region;
@@ -65,12 +64,8 @@ static struct dev_dax *__dax_pmem_probe(struct device *dev)
6564
.pgmap = &pgmap,
6665
.size = range_len(&range),
6766
};
68-
dev_dax = devm_create_dev_dax(&data);
6967

70-
/* child dev_dax instances now own the lifetime of the dax_region */
71-
dax_region_put(dax_region);
72-
73-
return dev_dax;
68+
return devm_create_dev_dax(&data);
7469
}
7570

7671
static int dax_pmem_probe(struct device *dev)

fs/dax.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,6 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
18301830
vm_fault_t ret = VM_FAULT_FALLBACK;
18311831
pgoff_t max_pgoff;
18321832
void *entry;
1833-
int error;
18341833

18351834
if (vmf->flags & FAULT_FLAG_WRITE)
18361835
iter.flags |= IOMAP_WRITE;
@@ -1877,7 +1876,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
18771876
}
18781877

18791878
iter.pos = (loff_t)xas.xa_index << PAGE_SHIFT;
1880-
while ((error = iomap_iter(&iter, ops)) > 0) {
1879+
while (iomap_iter(&iter, ops) > 0) {
18811880
if (iomap_length(&iter) < PMD_SIZE)
18821881
continue; /* actually breaks out of the loop */
18831882

0 commit comments

Comments
 (0)