Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp290160imu; Mon, 10 Dec 2018 22:03:13 -0800 (PST) X-Google-Smtp-Source: AFSGD/UuC07H3tVcV7+kzgPGCD4nD4K+gOeGbMHLAMVr1meTm49AnZ9Le6WxVS6nBizpGK3Ey0iR X-Received: by 2002:a17:902:820f:: with SMTP id x15mr14306314pln.224.1544508193460; Mon, 10 Dec 2018 22:03:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544508193; cv=none; d=google.com; s=arc-20160816; b=emK5roIgFzXBNGEvybmDPyP2hWTf0G+Xpoe60fzpVr7PMUK/7GWZvhi1nAzb7pI8Bs +SN9XJQSWVAKETLAmIDVp9ALx2d2oPWJ2+jLCRU9oHHvVeF8GjI25WMMljRI/9Xf9qRj TTy9n9JQ8V6CHFzx2II2U6ydmomENCy6kKNIeOsUfOoJf2za8AcapKeK2QduQQw9hcz8 y+xRUPnum74QUJRIr5BgMBIpv2ZHn0j4FfTIr3BzATj1PWNnCia3tj4YjSDFVbldIsl8 DPPdosWvwUvnBESxmX90rhjOao0PRhmT0nGEHqRd1FJTFPlfFZ09JA/paQLVuIo2A14b O6kA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=6MbmCp1O651eRLlDYu6Uvi1rhMgDpHIf4P6vldohk7Y=; b=Ss1pEVBudFUn6HoVee4k3OlZl3umF/ryyVyaBZINI8GEIaGWXLfykONYcmaUXFc4k7 2nvJe2pmqLOwJpLvRG8c7eXb3R3mFEoENF+QAVCvU9RmX9C3P6KioOVdgz4AauifdkeZ VtH58lO3e29Y972vWMB6sqL3zQg3mGoLCPcE//jtqhR14Ob3dlT4atF7O0FusMTH9Dkn p4oFNoyKoI8Jy7Zf6iy9ll4XWqEfD1lyWsgjX9ku+OsXbFQuNd+lT6OZRWOpo3TuQlNH LeZaeLvqQbF3ckO9IuX8FUuXI4drEJkMsoAQ1wldDyqZ5royIOGgu9bSi3RiOtytA7cr 2XZA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c3si11216627plr.178.2018.12.10.22.02.55; Mon, 10 Dec 2018 22:03:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728256AbeLKFqN (ORCPT + 99 others); Tue, 11 Dec 2018 00:46:13 -0500 Received: from orcrist.hmeau.com ([104.223.48.154]:43296 "EHLO deadmen.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726562AbeLKFqM (ORCPT ); Tue, 11 Dec 2018 00:46:12 -0500 Received: from gondobar.mordor.me.apana.org.au ([192.168.128.4] helo=gondobar) by deadmen.hmeau.com with esmtps (Exim 4.89 #2 (Debian)) id 1gWarp-0004jY-5U; Tue, 11 Dec 2018 13:46:01 +0800 Received: from herbert by gondobar with local (Exim 4.89) (envelope-from ) id 1gWarg-0002Z5-H4; Tue, 11 Dec 2018 13:45:52 +0800 Date: Tue, 11 Dec 2018 13:45:52 +0800 From: Herbert Xu To: Arnd Bergmann Cc: Thomas Graf , "David S. Miller" , NeilBrown , Tom Herbert , Kees Cook , Paul Blakey , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Phil Sutter Subject: Re: [PATCH] test_rhashtable: remove semaphore usage Message-ID: <20181211054552.peone47xl5czpfar@gondor.apana.org.au> References: <20181210211957.210189-1-arnd@arndb.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181210211957.210189-1-arnd@arndb.de> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 10, 2018 at 10:17:20PM +0100, Arnd Bergmann wrote: > This is one of only two files that initialize a semaphore to a negative > value. We don't really need the two semaphores here at all, but can do > the same thing in more conventional and more effient way, by using a > single waitqueue and an atomic thread counter. > > This gets us a little bit closer to eliminating classic semaphores from > the kernel. It also fixes a corner case where we fail to continue after > one of the threads fails to start up. > > An alternative would be to use a split kthread_create()+wake_up_process() > and completely eliminate the separate synchronization. > > Signed-off-by: Arnd Bergmann > --- > This is part of a longer, untested, series to remove semaphores > from the kernel, please review as such before applying. > --- > lib/test_rhashtable.c | 28 ++++++++++++++++------------ > 1 file changed, 16 insertions(+), 12 deletions(-) This was created by Phil Sutter so I am adding him to the cc list. > diff --git a/lib/test_rhashtable.c b/lib/test_rhashtable.c > index 18de5ff1255b..12bdea4f6c20 100644 > --- a/lib/test_rhashtable.c > +++ b/lib/test_rhashtable.c > @@ -20,11 +20,11 @@ > #include > #include > #include > -#include > #include > #include > #include > #include > +#include > > #define MAX_ENTRIES 1000000 > #define TEST_INSERT_FAIL INT_MAX > @@ -112,7 +112,8 @@ static struct rhashtable_params test_rht_params_dup = { > .automatic_shrinking = false, > }; > > -static struct semaphore prestart_sem, startup_sem; > +static atomic_t startup_count; > +static DECLARE_WAIT_QUEUE_HEAD(startup_wait); > > static int insert_retry(struct rhashtable *ht, struct test_obj *obj, > const struct rhashtable_params params) > @@ -635,8 +636,9 @@ static int threadfunc(void *data) > int i, step, err = 0, insert_retries = 0; > struct thread_data *tdata = data; > > - up(&prestart_sem); > - if (down_interruptible(&startup_sem)) > + if (atomic_dec_and_test(&startup_count)) > + wake_up(&startup_wait); > + if (wait_event_interruptible(startup_wait, atomic_read(&startup_count) == -1)) > pr_err(" thread[%d]: down_interruptible failed\n", tdata->id); > > for (i = 0; i < tdata->entries; i++) { > @@ -756,8 +758,7 @@ static int __init test_rht_init(void) > > pr_info("Testing concurrent rhashtable access from %d threads\n", > tcount); > - sema_init(&prestart_sem, 1 - tcount); > - sema_init(&startup_sem, 0); > + atomic_set(&startup_count, tcount); > tdata = vzalloc(array_size(tcount, sizeof(struct thread_data))); > if (!tdata) > return -ENOMEM; > @@ -783,15 +784,18 @@ static int __init test_rht_init(void) > tdata[i].objs = objs + i * entries; > tdata[i].task = kthread_run(threadfunc, &tdata[i], > "rhashtable_thrad[%d]", i); > - if (IS_ERR(tdata[i].task)) > + if (IS_ERR(tdata[i].task)) { > pr_err(" kthread_run failed for thread %d\n", i); > - else > + atomic_dec(&startup_count); > + } else { > started_threads++; > + } > } > - if (down_interruptible(&prestart_sem)) > - pr_err(" down interruptible failed\n"); > - for (i = 0; i < tcount; i++) > - up(&startup_sem); > + if (wait_event_interruptible(startup_wait, atomic_read(&startup_count) == 0)) > + pr_err(" wait_event interruptible failed\n"); > + /* count is 0 now, set it to -1 and wake up all threads together */ > + atomic_dec(&startup_count); > + wake_up_all(&startup_wait); > for (i = 0; i < tcount; i++) { > if (IS_ERR(tdata[i].task)) > continue; > -- > 2.20.0 > -- Email: Herbert Xu Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt