Received: by 10.223.164.221 with SMTP id h29csp2387902wrb; Thu, 2 Nov 2017 10:06:47 -0700 (PDT) X-Google-Smtp-Source: ABhQp+TRk1Wiua0WU6Ihm6iedQajSG4du7yIF9ccb8KXZq2okkmZJ2YbnW55Ttq9QuKQY12rhTp8 X-Received: by 10.98.34.131 with SMTP id p3mr4581203pfj.1.1509642407098; Thu, 02 Nov 2017 10:06:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509642407; cv=none; d=google.com; s=arc-20160816; b=QFsHG6Jq/Uxt1c0I8kl3Yh+vGYYZAGfx9fIPkOOEURsbFShtl/pu7pMSL/nHt0W4zu ig6LY6QpsVpDG5UqxxTLhJjiu0O3z1fxk7Pj+X7p/37vW7Bewd0Xp6shqRt1hwxbnGIr mCx0w+vtS+EaNcTOmXfRGR5SSbKkC6ycgGTfhdewSsXPWOfaqEtTGPGtholPsaQKIb+R rnlxuqya6LTnwPFGA+dp/xXmerBqp9hMvORdjrQDu3MhHkXjBQLTvvUpUj0PrsxAS/aT +j1cSRoOS3V9rUf+9/klCi7ZYjEOoNUvZ2cfsVP6lry7CsQ5d7cmNf7azgrUvMOOg6Et S+HA== 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-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date :arc-authentication-results; bh=DIwLAB5E/BGGuFtEvDTHMb/7U6S9BmO9tAEC81APR6Y=; b=fcfl0wZEKrUaGy9OpQmc6X7aNt+Jcflih1SA1w1JjwuGlyHjlneBUVMAsiNpUvY4LN ht4CCfH+gz3S5kCh400cuP1DKHWJT5tsRpZaSe9hKV0JFfpXLHSgBmNYGfw9WXhw4uf5 wLpKTqEtw1c3tFlyoR9f1SV4/MLsNRHs5AKszs0WKTDYD4fvB2wIdNmrcOjTfDAtGZfE N9Zu/vK9N+152PxFjq89tbyHpTHgbF3c4hs8ATwBNuw+HJ+cN96ld3shf1WYDlIAGUlO W1I32jpSWmeDN7CjDQzuPoJPV507CHLh16/f0Ap4VBbJadMy31W+31BNOltAgfcEAf9N P8Pg== 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 i8si4097647pgf.567.2017.11.02.10.06.33; Thu, 02 Nov 2017 10:06:47 -0700 (PDT) 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 S1755468AbdKBRFh convert rfc822-to-8bit (ORCPT + 97 others); Thu, 2 Nov 2017 13:05:37 -0400 Received: from mx2.suse.de ([195.135.220.15]:56592 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752042AbdKBRFg (ORCPT ); Thu, 2 Nov 2017 13:05:36 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 43212AC7D; Thu, 2 Nov 2017 17:05:34 +0000 (UTC) Date: Thu, 2 Nov 2017 10:04:31 -0700 From: Davidlohr Bueso To: Waiman Long Cc: Alexander Viro , Jan Kara , Jeff Layton , "J. Bruce Fields" , Tejun Heo , Christoph Lameter , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Ingo Molnar , Peter Zijlstra , Andi Kleen , Dave Chinner , Boqun Feng Subject: Re: [PATCH v8 1/6] lib/dlock-list: Distributed and lock-protected lists Message-ID: <20171102170431.oq3i5mxtjcg53uot@linux-n805> References: <1509475860-16139-1-git-send-email-longman@redhat.com> <1509475860-16139-2-git-send-email-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8BIT In-Reply-To: <1509475860-16139-2-git-send-email-longman@redhat.com> User-Agent: NeoMutt/20170421 (1.8.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 31 Oct 2017, Waiman Long wrote: >+/** >+ * dlock_lists_empty - Check if all the dlock lists are empty >+ * @dlist: Pointer to the dlock_list_heads structure >+ * Return: true if list is empty, false otherwise. >+ * >+ * This can be a pretty expensive function call. If this function is required >+ * in a performance critical path, we may have to maintain a global count >+ * of the list entries in the global dlock_list_heads structure instead. >+ */ I vote for doing this in the original version. How about the following? >+bool dlock_lists_empty(struct dlock_list_heads *dlist) >+{ >+ int idx; >+ >+ for (idx = 0; idx < nr_cpu_ids; idx++) >+ if (!list_empty(&dlist->heads[idx].list)) >+ return false; >+ return true; >+} >+EXPORT_SYMBOL(dlock_lists_empty); ----------8<----------------------------------------------- From: Davidlohr Bueso Subject: [PATCH] lib/dlock-list: Scale dlock_lists_empty() Instead of the current O(N) implementation; at the cost of adding an atomic counter. We also need to add a heads pointer to the node structure such that we can unaccount a thread doing list_del(). Signed-off-by: Davidlohr Bueso --- include/linux/dlock-list.h | 2 ++ lib/dlock-list.c | 40 ++++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/include/linux/dlock-list.h b/include/linux/dlock-list.h index c00c7f92ada4..dd73d5787885 100644 --- a/include/linux/dlock-list.h +++ b/include/linux/dlock-list.h @@ -36,6 +36,7 @@ struct dlock_list_head { struct dlock_list_heads { struct dlock_list_head *heads; + atomic_t waiters; }; /* @@ -44,6 +45,7 @@ struct dlock_list_heads { struct dlock_list_node { struct list_head list; struct dlock_list_head *head; + struct dlock_list_heads *heads; }; /* diff --git a/lib/dlock-list.c b/lib/dlock-list.c index a4ddecc01b12..bd11fc0da254 100644 --- a/lib/dlock-list.c +++ b/lib/dlock-list.c @@ -124,6 +124,8 @@ int __alloc_dlock_list_heads(struct dlock_list_heads *dlist, head->lock = __SPIN_LOCK_UNLOCKED(&head->lock); lockdep_set_class(&head->lock, key); } + + atomic_set(&dlist->waiters, 0); return 0; } EXPORT_SYMBOL(__alloc_dlock_list_heads); @@ -139,29 +141,23 @@ void free_dlock_list_heads(struct dlock_list_heads *dlist) { kfree(dlist->heads); dlist->heads = NULL; + atomic_set(&dlist->waiters, 0); } EXPORT_SYMBOL(free_dlock_list_heads); /** * dlock_lists_empty - Check if all the dlock lists are empty * @dlist: Pointer to the dlock_list_heads structure - * Return: true if list is empty, false otherwise. * - * This can be a pretty expensive function call. If this function is required - * in a performance critical path, we may have to maintain a global count - * of the list entries in the global dlock_list_heads structure instead. + * Return: true if all dlock lists are empty, false otherwise. */ bool dlock_lists_empty(struct dlock_list_heads *dlist) { - int idx; - /* Shouldn't be called before nr_dlock_lists is initialized */ WARN_ON_ONCE(!nr_dlock_lists); - for (idx = 0; idx < nr_dlock_lists; idx++) - if (!list_empty(&dlist->heads[idx].list)) - return false; - return true; + smp_mb__before_atomic(); + return !atomic_read(&dlist->waiters); } EXPORT_SYMBOL(dlock_lists_empty); @@ -179,10 +175,30 @@ void dlock_lists_add(struct dlock_list_node *node, struct dlock_list_head *head = &dlist->heads[this_cpu_read(cpu2idx)]; /* + * Serialize dlist->waiters such that a 0->1 transition is not missed, + * by another thread checking if any of the dlock lists are used. + * + * CPU0 CPU1 + * dlock_list_add() dlock_lists_empty() + * [S] atomic_inc(waiters); + * smp_mb__after_atomic(); + * smp_mb__before_atomic(); + * [L] atomic_read(waiters) + * list_add() + * + * Bump the waiters counter _before_ taking the head->lock such that we + * don't miss a thread adding itself to a list while spinning for the + * lock. + */ + atomic_inc(&dlist->waiters); + smp_mb__after_atomic(); + + /* * There is no need to disable preemption */ spin_lock(&head->lock); node->head = head; + node->heads = dlist; list_add(&node->list, &head->list); spin_unlock(&head->lock); } @@ -199,8 +215,7 @@ EXPORT_SYMBOL(dlock_lists_add); * a bug. */ void dlock_lists_del(struct dlock_list_node *node) -{ - struct dlock_list_head *head; +{ struct dlock_list_head *head; bool retry; do { @@ -214,6 +229,7 @@ void dlock_lists_del(struct dlock_list_node *node) list_del_init(&node->list); node->head = NULL; retry = false; + atomic_dec(&node->heads->waiters); } else { /* * The lock has somehow changed. Retry again if it is -- 2.13.6 From 1582890380697345801@xxx Wed Nov 01 18:45:01 +0000 2017 X-GM-THRID: 1582800381820199218 X-Gmail-Labels: Inbox,Category Forums