@@ -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 */
450463static 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+
476505static 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-
577592static 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);
635649static 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
644660static 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
662677static 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[] = {
12951311static 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 ;
0 commit comments