10
10
11
11
#include <linux/acpi.h>
12
12
#include <linux/blkdev.h>
13
+ #include <linux/cleanup.h>
13
14
#include <linux/cpufreq.h>
14
15
#include <linux/device.h>
15
16
#include <linux/dma-map-ops.h> /* for dma_default_coherent */
@@ -563,24 +564,11 @@ static struct class devlink_class = {
563
564
564
565
static int devlink_add_symlinks (struct device * dev )
565
566
{
567
+ char * buf_con __free (kfree ) = NULL , * buf_sup __free (kfree ) = NULL ;
566
568
int ret ;
567
- size_t len ;
568
569
struct device_link * link = to_devlink (dev );
569
570
struct device * sup = link -> supplier ;
570
571
struct device * con = link -> consumer ;
571
- char * buf ;
572
-
573
- len = max (strlen (dev_bus_name (sup )) + strlen (dev_name (sup )),
574
- strlen (dev_bus_name (con )) + strlen (dev_name (con )));
575
- len += strlen (":" );
576
- /*
577
- * we kzalloc() memory for symlink name of both supplier and
578
- * consumer, so explicitly take into account both prefix.
579
- */
580
- len += max (strlen ("supplier:" ), strlen ("consumer:" )) + 1 ;
581
- buf = kzalloc (len , GFP_KERNEL );
582
- if (!buf )
583
- return - ENOMEM ;
584
572
585
573
ret = sysfs_create_link (& link -> link_dev .kobj , & sup -> kobj , "supplier" );
586
574
if (ret )
@@ -590,58 +578,64 @@ static int devlink_add_symlinks(struct device *dev)
590
578
if (ret )
591
579
goto err_con ;
592
580
593
- snprintf (buf , len , "consumer:%s:%s" , dev_bus_name (con ), dev_name (con ));
594
- ret = sysfs_create_link (& sup -> kobj , & link -> link_dev .kobj , buf );
581
+ buf_con = kasprintf (GFP_KERNEL , "consumer:%s:%s" , dev_bus_name (con ), dev_name (con ));
582
+ if (!buf_con ) {
583
+ ret = - ENOMEM ;
584
+ goto err_con_dev ;
585
+ }
586
+
587
+ ret = sysfs_create_link (& sup -> kobj , & link -> link_dev .kobj , buf_con );
595
588
if (ret )
596
589
goto err_con_dev ;
597
590
598
- snprintf (buf , len , "supplier:%s:%s" , dev_bus_name (sup ), dev_name (sup ));
599
- ret = sysfs_create_link (& con -> kobj , & link -> link_dev .kobj , buf );
591
+ buf_sup = kasprintf (GFP_KERNEL , "supplier:%s:%s" , dev_bus_name (sup ), dev_name (sup ));
592
+ if (!buf_sup ) {
593
+ ret = - ENOMEM ;
594
+ goto err_sup_dev ;
595
+ }
596
+
597
+ ret = sysfs_create_link (& con -> kobj , & link -> link_dev .kobj , buf_sup );
600
598
if (ret )
601
599
goto err_sup_dev ;
602
600
603
601
goto out ;
604
602
605
603
err_sup_dev :
606
- snprintf (buf , len , "consumer:%s:%s" , dev_bus_name (con ), dev_name (con ));
607
- sysfs_remove_link (& sup -> kobj , buf );
604
+ sysfs_remove_link (& sup -> kobj , buf_con );
608
605
err_con_dev :
609
606
sysfs_remove_link (& link -> link_dev .kobj , "consumer" );
610
607
err_con :
611
608
sysfs_remove_link (& link -> link_dev .kobj , "supplier" );
612
609
out :
613
- kfree (buf );
614
610
return ret ;
615
611
}
616
612
617
613
static void devlink_remove_symlinks (struct device * dev )
618
614
{
615
+ char * buf_con __free (kfree ) = NULL , * buf_sup __free (kfree ) = NULL ;
619
616
struct device_link * link = to_devlink (dev );
620
- size_t len ;
621
617
struct device * sup = link -> supplier ;
622
618
struct device * con = link -> consumer ;
623
- char * buf ;
624
619
625
620
sysfs_remove_link (& link -> link_dev .kobj , "consumer" );
626
621
sysfs_remove_link (& link -> link_dev .kobj , "supplier" );
627
622
628
- len = max (strlen (dev_bus_name (sup )) + strlen (dev_name (sup )),
629
- strlen (dev_bus_name (con )) + strlen (dev_name (con )));
630
- len += strlen (":" );
631
- len += max (strlen ("supplier:" ), strlen ("consumer:" )) + 1 ;
632
- buf = kzalloc (len , GFP_KERNEL );
633
- if (!buf ) {
634
- WARN (1 , "Unable to properly free device link symlinks!\n" );
635
- return ;
636
- }
637
-
638
623
if (device_is_registered (con )) {
639
- snprintf (buf , len , "supplier:%s:%s" , dev_bus_name (sup ), dev_name (sup ));
640
- sysfs_remove_link (& con -> kobj , buf );
624
+ buf_sup = kasprintf (GFP_KERNEL , "supplier:%s:%s" , dev_bus_name (sup ), dev_name (sup ));
625
+ if (!buf_sup )
626
+ goto out ;
627
+ sysfs_remove_link (& con -> kobj , buf_sup );
641
628
}
642
- snprintf (buf , len , "consumer:%s:%s" , dev_bus_name (con ), dev_name (con ));
643
- sysfs_remove_link (& sup -> kobj , buf );
644
- kfree (buf );
629
+
630
+ buf_con = kasprintf (GFP_KERNEL , "consumer:%s:%s" , dev_bus_name (con ), dev_name (con ));
631
+ if (!buf_con )
632
+ goto out ;
633
+ sysfs_remove_link (& sup -> kobj , buf_con );
634
+
635
+ return ;
636
+
637
+ out :
638
+ WARN (1 , "Unable to properly free device link symlinks!\n" );
645
639
}
646
640
647
641
static struct class_interface devlink_class_intf = {
0 commit comments