From patchwork Fri May 27 16:22:07 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roland Dreier X-Patchwork-Id: 824842 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.3) with ESMTP id p4RGMVFw002474 for ; Fri, 27 May 2011 16:22:32 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752138Ab1E0QWb (ORCPT ); Fri, 27 May 2011 12:22:31 -0400 Received: from na3sys010aog110.obsmtp.com ([74.125.245.88]:45935 "HELO na3sys010aog110.obsmtp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1751440Ab1E0QWa (ORCPT ); Fri, 27 May 2011 12:22:30 -0400 Received: from mail-pv0-f182.google.com ([74.125.83.182]) (using TLSv1) by na3sys010aob110.postini.com ([74.125.244.12]) with SMTP ID DSNKTd/Pxaj616GnOlBUowdFCMYz9hFBfgvf@postini.com; Fri, 27 May 2011 09:22:30 PDT Received: by mail-pv0-f182.google.com with SMTP id 11so1069170pvg.41 for ; Fri, 27 May 2011 09:22:29 -0700 (PDT) Received: by 10.142.151.20 with SMTP id y20mr362154wfd.220.1306513347313; Fri, 27 May 2011 09:22:27 -0700 (PDT) MIME-Version: 1.0 Received: by 10.143.79.19 with HTTP; Fri, 27 May 2011 09:22:07 -0700 (PDT) In-Reply-To: <1306493434.1835.4.camel@deela.quest-ce.net> References: <1306493434.1835.4.camel@deela.quest-ce.net> From: Roland Dreier Date: Fri, 27 May 2011 09:22:07 -0700 X-Google-Sender-Auth: w8nVsm7SAwsNPJqeIFdw-dtMbz8 Message-ID: Subject: Re: [PATCH] Fixing segfault in libibverbs To: Yann Droneaud Cc: Animesh K Trivedi1 , linux-rdma@vger.kernel.org, Bernard Metzler Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Fri, 27 May 2011 16:22:32 +0000 (UTC) >> This patch fixes SEGFAULT in libibverbs in case when there are no user >> space drivers found. I've cloned the libibverbs from >> git://git.openfabrics.org/ofed_1_1_5/libibverbs.git (I hope this is >> correct) Thanks for the patches. I think I would prefer to fix this more like the following (sorry for the attachment, using the gmail web interface ATM). This actually simplifies things I think, and sticks to the original idea that we should only be calling ibverbs_init once. I only compile tested this, can one of you guys confirm this also fixes the problem? Thanks! Roland Tested-by: Yann Droneaud src/device.c | 20 ++++++++++---------- 1 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/device.c b/src/device.c index 185f4a6..5798895 100644 --- a/src/device.c +++ b/src/device.c @@ -49,32 +49,34 @@ #include "ibverbs.h" -static pthread_mutex_t device_list_lock = PTHREAD_MUTEX_INITIALIZER; +static pthread_once_t device_list_once = PTHREAD_ONCE_INIT; static int num_devices; static struct ibv_device **device_list; +static void count_devices(void) +{ + num_devices = ibverbs_init(&device_list); +} + struct ibv_device **__ibv_get_device_list(int *num) { - struct ibv_device **l = 0; + struct ibv_device **l; int i; if (num) *num = 0; - pthread_mutex_lock(&device_list_lock); - - if (!num_devices) - num_devices = ibverbs_init(&device_list); + pthread_once(&device_list_once, count_devices); if (num_devices < 0) { errno = -num_devices; - goto out; + return NULL; } l = calloc(num_devices + 1, sizeof (struct ibv_device *)); if (!l) { errno = ENOMEM; - goto out; + return NULL; } for (i = 0; i < num_devices; ++i) @@ -82,8 +84,6 @@ struct ibv_device **__ibv_get_device_list(int *num) if (num) *num = num_devices; -out: - pthread_mutex_unlock(&device_list_lock); return l; } default_symver(__ibv_get_device_list, ibv_get_device_list);