diff mbox series

[net-next] ptp: Move from simple ida to xarray

Message ID 20240307100327.887758-1-kory.maincent@bootlin.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net-next] ptp: Move from simple ida to xarray | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 940 this patch: 940
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 2 of 2 maintainers
netdev/build_clang success Errors and warnings before: 956 this patch: 956
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 956 this patch: 956
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 46 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-03-08--21-00 (tests: 887)

Commit Message

Kory Maincent March 7, 2024, 10:03 a.m. UTC
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.

Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>
---
---
 drivers/ptp/ptp_clock.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

Comments

Simon Horman March 8, 2024, 9:07 p.m. UTC | #1
On Thu, Mar 07, 2024 at 11:03:26AM +0100, Kory Maincent wrote:
> 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.
> 
> Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>

...

> @@ -246,11 +246,10 @@ 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;
> -	}
>  
>  	ptp->clock.ops = ptp_clock_ops;
>  	ptp->info = info;

Hi Kory,

Prior to this change err was -ENOMEM at this point. Now it is 0.

And The immediately following code is:

	ptp->devid = MKDEV(major, index);
	ptp->index = index;
	INIT_LIST_HEAD(&ptp->tsevqs);
	queue = kzalloc(sizeof(*queue), GFP_KERNEL);
	if (!queue)
		goto no_memory_queue;

The goto above results in:

	return ERR_PTR(err);

But here err is 0. This does not seem correct.

Flagged by Smatch.

...
Kory Maincent March 11, 2024, 9:08 a.m. UTC | #2
On Fri, 8 Mar 2024 21:07:45 +0000
Simon Horman <horms@kernel.org> wrote:

> On Thu, Mar 07, 2024 at 11:03:26AM +0100, Kory Maincent wrote:
> > 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.
> > 
> > Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>  
> 
> ...
> 
> > @@ -246,11 +246,10 @@ 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;
> > -	}
> >  
> >  	ptp->clock.ops = ptp_clock_ops;
> >  	ptp->info = info;  
> 
> Hi Kory,
> 
> Prior to this change err was -ENOMEM at this point. Now it is 0.

You are totally right. Thanks for reporting the issue!

Regards,
diff mbox series

Patch

diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c
index 3aaf1a3430c5..392c880d9f34 100644
--- a/drivers/ptp/ptp_clock.c
+++ b/drivers/ptp/ptp_clock.c
@@ -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,11 +246,10 @@  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;
-	}
 
 	ptp->clock.ops = ptp_clock_ops;
 	ptp->info = info;
@@ -378,7 +377,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 +510,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)