Skip to contentSkip to conversation

ptp: Move from simple ida to xarray

PATCH Superseded Kory Maincent 2 participants 2 messages Mar 11, 2024

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ptp: Move from simple ida to xarray

Move from simple ida to xarray for storing and loading the ptp_clock pointer. This prepares support for future hardware timestamp selection by being able to link the ptp clock index to its pointer.

---

Change in v2:
- Update an err value missing

---
 drivers/ptp/ptp_clock.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/ptp/ptp_clock.c +8 -8
index 3aaf1a3430c5..9339fd475df0 100644
@@ -31,7 +31,7 @@ struct class *ptp_class;
 
 static dev_t ptp_devt;
 
-static DEFINE_IDA(ptp_clocks_map);
+static DEFINE_XARRAY_ALLOC(ptp_clocks_map);
 
 /* time stamp event queue operations */
 
@@ -201,7 +201,7 @@ static void ptp_clock_release(struct device *dev)
 	bitmap_free(tsevq->mask);
 	kfree(tsevq);
 	debugfs_remove(ptp->debugfs_root);
-	ida_free(&ptp_clocks_map, ptp->index);
+	xa_erase(&ptp_clocks_map, ptp->index);
 	kfree(ptp);
 }
 
@@ -246,12 +246,12 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
 	if (ptp == NULL)
 		goto no_memory;
 
-	index = ida_alloc_max(&ptp_clocks_map, MINORMASK, GFP_KERNEL);
-	if (index < 0) {
-		err = index;
+	err = xa_alloc(&ptp_clocks_map, &index, ptp, xa_limit_31b,
+		       GFP_KERNEL);
+	if (err)
 		goto no_slot;
-	}
 
+	err = -ENOMEM;
 	ptp->clock.ops = ptp_clock_ops;
 	ptp->info = info;
 	ptp->devid = MKDEV(major, index);
@@ -378,7 +378,7 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
 	list_del(&queue->qlist);
 	kfree(queue);
 no_memory_queue:
-	ida_free(&ptp_clocks_map, index);
+	xa_erase(&ptp_clocks_map, index);
 no_slot:
 	kfree(ptp);
 no_memory:
@@ -511,7 +511,7 @@ static void __exit ptp_exit(void)
 {
 	class_destroy(ptp_class);
 	unregister_chrdev_region(ptp_devt, MINORMASK + 1);
-	ida_destroy(&ptp_clocks_map);
+	xa_destroy(&ptp_clocks_map);
 }
 
 static int __init ptp_init(void)
Signature · 1 line
2.25.1
#1 lore raw
Quoted 39 lines · Kory Maincent

Move from simple ida to xarray for storing and loading the ptp_clock pointer. This prepares support for future hardware timestamp selection by being able to link the ptp clock index to its pointer.

---

Change in v2:
- Update an err value missing

---
  drivers/ptp/ptp_clock.c | 16 ++++++++--------
  1 file changed, 8 insertions(+), 8 deletions(-)
drivers/ptp/ptp_clock.c +2 -2
index 3aaf1a3430c5..9339fd475df0 100644
@@ -31,7 +31,7 @@ struct class *ptp_class;
 
  static dev_t ptp_devt;
 
-static DEFINE_IDA(ptp_clocks_map);
+static DEFINE_XARRAY_ALLOC(ptp_clocks_map);
 
  /* time stamp event queue operations */
 
@@ -201,7 +201,7 @@ static void ptp_clock_release(struct device *dev)
  	bitmap_free(tsevq->mask);
  	kfree(tsevq);
  	debugfs_remove(ptp->debugfs_root);
-	ida_free(&ptp_clocks_map, ptp->index);
+	xa_erase(&ptp_clocks_map, ptp->index);
  	kfree(ptp);
  }
 
@@ -246,12 +246,12 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
  	if (ptp == NULL)
  		goto no_memory;
 

drivers/ptp/ptp_clock.c struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, · line 254

Quoted 10 lines
drivers/ptp/ptp_clock.c +4 -4
-	index = ida_alloc_max(&ptp_clocks_map, MINORMASK, GFP_KERNEL);
-	if (index < 0) {
-		err = index;
+	err = xa_alloc(&ptp_clocks_map, &index, ptp, xa_limit_31b,
+		       GFP_KERNEL);
+	if (err)
  		goto no_slot;
-	}
 
+	err = -ENOMEM;

here it looks weird (even if technically correct),
I would either move the err assignement down, just above next kzalloc() call, or refactor function to use the following idiom:
ptr = kzalloc(...)

  if (!ptr) {
      err = -ENOMEM;
      goto somewhere;

}

(it's already is for @vclock_index field init)

Otherwise context is too small to tell what is the reason.

Quoted 21 lines
drivers/ptp/ptp_clock.c +2 -2
  	ptp->clock.ops = ptp_clock_ops;
  	ptp->info = info;
  	ptp->devid = MKDEV(major, index);
@@ -378,7 +378,7 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
  	list_del(&queue->qlist);
  	kfree(queue);
  no_memory_queue:
-	ida_free(&ptp_clocks_map, index);
+	xa_erase(&ptp_clocks_map, index);
  no_slot:
  	kfree(ptp);
  no_memory:
@@ -511,7 +511,7 @@ static void __exit ptp_exit(void)
  {
  	class_destroy(ptp_class);
  	unregister_chrdev_region(ptp_devt, MINORMASK + 1);
-	ida_destroy(&ptp_clocks_map);
+	xa_destroy(&ptp_clocks_map);
  }
 
  static int __init ptp_init(void)