Received: by 10.223.164.221 with SMTP id h29csp184275wrb; Fri, 3 Nov 2017 07:31:47 -0700 (PDT) X-Google-Smtp-Source: ABhQp+TJgXlcXbZtW91GBRjB7ye//oiUVbYasXs8GvjEvcqnlRJuHZGb7MKY0y6Xrk9llorKdntV X-Received: by 10.98.155.10 with SMTP id r10mr8052240pfd.66.1509719507101; Fri, 03 Nov 2017 07:31:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509719507; cv=none; d=google.com; s=arc-20160816; b=UzQX6OegJrLQmEMrNxAxlxAv1VFp3uMHT/9d2YRKpnlGr4o7wNV2vVvg+Mjoso8JZ2 +vrNR4KyCGd01xsXGf5n8R8oyo4SQqFDEVVzTIluRsxsISwJoeO93BAMborTzIe8Ha4e c8/+gku1LTYM9MqXlZtOhIbs8kvJ2EF016/391OEunsHGiO+1QN1eXqW2a/yu1ohCjTa OmOfdfrEhB9pBPXR4LjZfeTJsIlzwED76ajBcUUTtJv9YdHuBvB7I6O8UZNXtYDhhqop HTZudfV3BAMdOpGgETDvqO4dn7/3lFdlCIe+4g2KoJOUv0QtKbN2csMk1byLfkI+D4iZ 5dOA== 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:arc-authentication-results; bh=vffZhmRZQrkr5l4XHBEWSJ+p2qh5sSSBpWt5IHMzXqI=; b=YK2p9xgzxw3Eph7yPYhFJV+xn8A8ntHL3D8sWkvM2mcWV1bs1Yfjhi4uUjH6uHzZPL x0Yk4aHlB+qxRsH1CLeXEI/yq4L8qBRko6jmMYv9OXdUa0g7hJAz9DfUzlH0sAEaQ9jB /2f5qwjbZQikR59YUXSQtup2aY39wjR0lj0VQDdcpWkr3xyvqw1Q+z8AB7BFxB0Wvlvx W4/7xsBbAVVWjEl+q9rfkt2cU/RQK34XnM82/674/NPI8LVN1l1Ofhy+gfGiMO9Krl3P OqwKVjxvBeoVEGgqYhK1iSup32yD7XoUVHso7qs9XnDDl8xfKlb/1pqPBlsLG6jQSOAp dMaQ== 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 p3si6190175pgs.362.2017.11.03.07.31.33; Fri, 03 Nov 2017 07:31: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 S1755776AbdKCOYK (ORCPT + 95 others); Fri, 3 Nov 2017 10:24:10 -0400 Received: from mx2.suse.de ([195.135.220.15]:39059 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753855AbdKCOX4 (ORCPT ); Fri, 3 Nov 2017 10:23:56 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 92AAEAD09; Fri, 3 Nov 2017 14:23:54 +0000 (UTC) Date: Fri, 3 Nov 2017 07:22:54 -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: [PATCH v3] lib/dlock-list: Scale dlock_lists_empty() Message-ID: <20171103142254.d55bu2n44xe4aruf@linux-n805> References: <1509475860-16139-1-git-send-email-longman@redhat.com> <1509475860-16139-2-git-send-email-longman@redhat.com> <20171102170431.oq3i5mxtjcg53uot@linux-n805> <81bb3365-63f3-fea8-d238-e3880a4c8033@redhat.com> <20171103133420.pngmrsfmtimataz4@linux-n805> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20171103133420.pngmrsfmtimataz4@linux-n805> 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 Instead of the current O(N) implementation, at the cost of adding an atomic counter, we can convert the call to an atomic_read(). The counter only serves for accounting empty to non-empty transitions, and vice versa; therefore only modified twice for each of the lists during the lifetime of the dlock (while used). In addition, to be able to unaccount a list_del(), we add a dlist pointer to each head, thus minimizing the overall memory footprint. Signed-off-by: Davidlohr Bueso --- Changes from v2: Removed some bogus lines and an overoptimistic changelog. include/linux/dlock-list.h | 2 ++ lib/dlock-list.c | 52 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/include/linux/dlock-list.h b/include/linux/dlock-list.h index c00c7f92ada4..d176a2d00cd1 100644 --- a/include/linux/dlock-list.h +++ b/include/linux/dlock-list.h @@ -32,10 +32,12 @@ struct dlock_list_head { struct list_head list; spinlock_t lock; + struct dlock_list_heads *dlist; } ____cacheline_aligned_in_smp; struct dlock_list_heads { struct dlock_list_head *heads; + atomic_t waiters; }; /* diff --git a/lib/dlock-list.c b/lib/dlock-list.c index a4ddecc01b12..5814e42c5b81 100644 --- a/lib/dlock-list.c +++ b/lib/dlock-list.c @@ -122,8 +122,11 @@ int __alloc_dlock_list_heads(struct dlock_list_heads *dlist, INIT_LIST_HEAD(&head->list); head->lock = __SPIN_LOCK_UNLOCKED(&head->lock); + head->dlist = dlist; lockdep_set_class(&head->lock, key); } + + atomic_set(&dlist->waiters, 0); return 0; } EXPORT_SYMBOL(__alloc_dlock_list_heads); @@ -139,29 +142,36 @@ 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; + /* + * 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() + * + */ + smp_mb__before_atomic(); + return !atomic_read(&dlist->waiters); } EXPORT_SYMBOL(dlock_lists_empty); @@ -179,6 +189,16 @@ void dlock_lists_add(struct dlock_list_node *node, struct dlock_list_head *head = &dlist->heads[this_cpu_read(cpu2idx)]; /* + * 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. + */ + if (list_empty_careful(&head->list)) { + atomic_inc(&dlist->waiters); + smp_mb__after_atomic(); + } + + /* * There is no need to disable preemption */ spin_lock(&head->lock); @@ -212,6 +232,18 @@ void dlock_lists_del(struct dlock_list_node *node) spin_lock(&head->lock); if (likely(head == node->head)) { list_del_init(&node->list); + /* + * We still hold the head->lock, a normal list_empty() + * check will do. + */ + if (list_empty(&head->list)) { + struct dlock_list_heads *dlist; + dlist = node->head->dlist; + + atomic_dec(&dlist->waiters); + smp_mb__after_atomic(); + } + node->head = NULL; retry = false; } else { -- 2.13.6 From 1583697221242444994@xxx Fri Nov 10 16:29:24 +0000 2017 X-GM-THRID: 1583696838641474887 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread