From: Leon Romanovsky <leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
To: Jason Gunthorpe
<jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
Cc: linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
Doug Ledford <dledford-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>,
Yishai Hadas
<yishaih-LDSdmyG8hGV8YrgS2mwiifqBs+8SCbDb@public.gmane.org>
Subject: Re: [PATCH rdma-core 4/5] verbs: Tidy up ibverbs_get_device_list
Date: Sun, 3 Sep 2017 17:03:51 +0300 [thread overview]
Message-ID: <20170903140351.GO10539@mtr-leonro.local> (raw)
In-Reply-To: <1504212659-9674-5-git-send-email-jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
[-- Attachment #1: Type: text/plain, Size: 6121 bytes --]
On Thu, Aug 31, 2017 at 02:50:58PM -0600, Jason Gunthorpe wrote:
> Now that we have ccan lists the logic here can be simplified greatly.
> Eliminate the confusing used and have_driver values, instead just
> delete items from the sysfs list as we run down the process. This
> directly guarantees discovered sysfs items are handled only once,
> and makes the lifetime of the sysfs pointers much clearer.
>
> Signed-off-by: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
> ---
> libibverbs/init.c | 93 +++++++++++++++++++++++++++++--------------------------
> 1 file changed, 49 insertions(+), 44 deletions(-)
>
> diff --git a/libibverbs/init.c b/libibverbs/init.c
> index 91ce8b4e378458..04583bb5137c96 100644
> --- a/libibverbs/init.c
> +++ b/libibverbs/init.c
> @@ -60,9 +60,7 @@ struct ibv_sysfs_dev {
> char sysfs_path[IBV_SYSFS_PATH_MAX];
> char ibdev_path[IBV_SYSFS_PATH_MAX];
> int abi_ver;
> - int have_driver;
> struct timespec time_created;
> - int used;
> };
>
> struct ibv_driver_name {
> @@ -148,8 +146,6 @@ static int find_sysfs_devs(struct list_head *tmp_sysfs_dev_list)
>
> sysfs_dev->time_created = buf.st_mtim;
>
> - sysfs_dev->have_driver = 0;
> - sysfs_dev->used = 0;
> if (ibv_read_sysfs_file(sysfs_dev->sysfs_path, "abi_version",
> value, sizeof value) > 0)
> sysfs_dev->abi_ver = strtol(value, NULL, 10);
> @@ -412,7 +408,6 @@ static struct verbs_device *try_driver(struct ibv_driver *driver,
> strcpy(dev->name, sysfs_dev->ibdev_name);
> strcpy(dev->ibdev_path, sysfs_dev->ibdev_path);
> vdev->sysfs = sysfs_dev;
> - sysfs_dev->used = 1;
>
> return vdev;
> }
> @@ -481,25 +476,51 @@ static int same_sysfs_dev(struct ibv_sysfs_dev *sysfs1,
> return 0;
> }
>
> -int ibverbs_get_device_list(struct list_head *list)
> +/* Match every ibv_sysfs_dev in the sysfs_list to a driver and add a new entry
> + * to device_list. Once matched to a driver the entry in sysfs_list is
> + * removed.
> + */
> +static void try_all_drivers(struct list_head *sysfs_list,
> + struct list_head *device_list,
> + unsigned int *num_devices)
> +{
> + struct ibv_sysfs_dev *sysfs_dev;
> + struct ibv_sysfs_dev *tmp;
> + struct verbs_device *vdev;
> +
> + list_for_each_safe(sysfs_list, sysfs_dev, tmp, entry) {
> + vdev = try_drivers(sysfs_dev);
> + if (vdev) {
> + list_del(&sysfs_dev->entry);
> + // Ownership of sysfs_dev moves into vdev->sysfs
Please don't use "//" C++ style in C files.
Thanks
> + list_add(device_list, &vdev->entry);
> + (*num_devices)++;
> + }
> + }
> +}
> +
> +int ibverbs_get_device_list(struct list_head *device_list)
> {
> - LIST_HEAD(tmp_sysfs_dev_list);
> + LIST_HEAD(sysfs_list);
> struct ibv_sysfs_dev *sysfs_dev, *next_dev;
> struct verbs_device *vdev, *tmp;
> static int drivers_loaded;
> - int num_devices = 0;
> + unsigned int num_devices = 0;
> int statically_linked = 0;
> - int no_driver = 0;
> int ret;
>
> - ret = find_sysfs_devs(&tmp_sysfs_dev_list);
> + ret = find_sysfs_devs(&sysfs_list);
> if (ret)
> return -ret;
>
> - list_for_each_safe(list, vdev, tmp, entry) {
> + /* Remove entries from the sysfs_list that are already preset in the
> + * device_list, and remove entries from the device_list that are not
> + * present in the sysfs_list.
> + */
> + list_for_each_safe(device_list, vdev, tmp, entry) {
> struct ibv_sysfs_dev *old_sysfs = NULL;
>
> - list_for_each(&tmp_sysfs_dev_list, sysfs_dev, entry) {
> + list_for_each(&sysfs_list, sysfs_dev, entry) {
> if (same_sysfs_dev(vdev->sysfs, sysfs_dev)) {
> old_sysfs = sysfs_dev;
> break;
> @@ -507,7 +528,8 @@ int ibverbs_get_device_list(struct list_head *list)
> }
>
> if (old_sysfs) {
> - sysfs_dev->have_driver = 1;
> + list_del(&old_sysfs->entry);
> + free(old_sysfs);
> num_devices++;
> } else {
> list_del(&vdev->entry);
> @@ -515,19 +537,9 @@ int ibverbs_get_device_list(struct list_head *list)
> }
> }
>
> - list_for_each(&tmp_sysfs_dev_list, sysfs_dev, entry) {
> - if (sysfs_dev->have_driver)
> - continue;
> - vdev = try_drivers(sysfs_dev);
> - if (vdev) {
> - sysfs_dev->have_driver = 1;
> - list_add(list, &vdev->entry);
> - num_devices++;
> - } else
> - no_driver = 1;
> - }
> + try_all_drivers(&sysfs_list, device_list, &num_devices);
>
> - if (!no_driver || drivers_loaded)
> + if (list_empty(&sysfs_list) || drivers_loaded)
> goto out;
>
> /*
> @@ -553,29 +565,22 @@ int ibverbs_get_device_list(struct list_head *list)
> load_drivers();
> drivers_loaded = 1;
>
> - list_for_each(&tmp_sysfs_dev_list, sysfs_dev, entry) {
> - if (sysfs_dev->have_driver)
> - continue;
> -
> - vdev = try_drivers(sysfs_dev);
> - if (vdev) {
> - sysfs_dev->have_driver = 1;
> - list_add(list, &vdev->entry);
> - num_devices++;
> - }
> - }
> + try_all_drivers(&sysfs_list, device_list, &num_devices);
>
> out:
> - list_for_each_safe(&tmp_sysfs_dev_list, sysfs_dev, next_dev, entry) {
> - if (!sysfs_dev->have_driver && getenv("IBV_SHOW_WARNINGS")) {
> - fprintf(stderr, PFX "Warning: no userspace device-specific "
> - "driver found for %s\n", sysfs_dev->sysfs_path);
> + /* Anything left in sysfs_list was not assoicated with a
> + * driver.
> + */
> + list_for_each_safe(&sysfs_list, sysfs_dev, next_dev, entry) {
> + if (getenv("IBV_SHOW_WARNINGS")) {
> + fprintf(stderr, PFX
> + "Warning: no userspace device-specific driver found for %s\n",
> + sysfs_dev->sysfs_path);
> if (statically_linked)
> - fprintf(stderr, " When linking libibverbs statically, "
> - "driver must be statically linked too.\n");
> + fprintf(stderr,
> + " When linking libibverbs statically, driver must be statically linked too.\n");
> }
> - if (!sysfs_dev->used)
> - free(sysfs_dev);
> + free(sysfs_dev);
> }
>
> return num_devices;
> --
> 2.7.4
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
> the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2017-09-03 14:03 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-31 20:50 [PATCH rdma-core 0/5] Small fixes for verbs Jason Gunthorpe
[not found] ` <1504212659-9674-1-git-send-email-jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2017-08-31 20:50 ` [PATCH rdma-core 1/5] verbs: Remove diagnostic ignored "-Wmissing-prototypes" Jason Gunthorpe
2017-08-31 20:50 ` [PATCH rdma-core 2/5] util: Fix check_snprintf to use __rc__ for local Jason Gunthorpe
[not found] ` <1504212659-9674-3-git-send-email-jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2017-09-01 4:56 ` Leon Romanovsky
[not found] ` <20170901045632.GJ10539-U/DQcQFIOTAAJjI8aNfphQ@public.gmane.org>
2017-09-01 7:00 ` Nicolas Morey-Chaisemartin
[not found] ` <e10f536a-bad3-55b4-f3ef-207404209c89-l3A5Bk7waGM@public.gmane.org>
2017-09-01 9:35 ` Leon Romanovsky
[not found] ` <20170901093553.GK10539-U/DQcQFIOTAAJjI8aNfphQ@public.gmane.org>
2017-09-01 9:46 ` Nicolas Morey-Chaisemartin
[not found] ` <dfa51066-0d39-5d31-c7e6-b2af1a6089a5-l3A5Bk7waGM@public.gmane.org>
2017-09-01 14:13 ` Leon Romanovsky
2017-09-01 15:00 ` Jason Gunthorpe
[not found] ` <20170901150013.GA21378-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2017-09-01 15:13 ` Bart Van Assche
[not found] ` <1504278799.14386.1.camel-Sjgp3cTcYWE@public.gmane.org>
2017-09-01 15:47 ` Jason Gunthorpe
2017-08-31 20:50 ` [PATCH rdma-core 3/5] verbs: Use ccan list.h instead of open coding lists Jason Gunthorpe
[not found] ` <1504212659-9674-4-git-send-email-jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2017-09-01 7:05 ` Nicolas Morey-Chaisemartin
[not found] ` <b4255929-e0bc-d779-7190-f38fea24395e-l3A5Bk7waGM@public.gmane.org>
2017-09-02 1:47 ` Jason Gunthorpe
2017-08-31 20:50 ` [PATCH rdma-core 4/5] verbs: Tidy up ibverbs_get_device_list Jason Gunthorpe
[not found] ` <1504212659-9674-5-git-send-email-jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2017-09-03 14:03 ` Leon Romanovsky [this message]
[not found] ` <20170903140351.GO10539-U/DQcQFIOTAAJjI8aNfphQ@public.gmane.org>
2017-09-03 14:48 ` Jason Gunthorpe
[not found] ` <20170903144839.GA20230-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2017-09-03 17:25 ` Leon Romanovsky
2017-08-31 20:50 ` [PATCH rdma-core 5/5] ocrdma: Remove ocrdma_dev_list Jason Gunthorpe
2017-09-04 12:07 ` [PATCH rdma-core 0/5] Small fixes for verbs Yishai Hadas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170903140351.GO10539@mtr-leonro.local \
--to=leon-dgejt+ai2ygdnm+yrofe0a@public.gmane.org \
--cc=dledford-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
--cc=jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org \
--cc=linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=yishaih-LDSdmyG8hGV8YrgS2mwiifqBs+8SCbDb@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox