Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1385819rdb; Wed, 6 Dec 2023 18:27:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHaQWK+us4je8IaimHXgao0YD7Nun0k73eL2E2M5MYhy5VSnEP2L0rPXJO75Cz9Sun9pU6w X-Received: by 2002:a05:6870:9e47:b0:1fa:1c89:c656 with SMTP id pt7-20020a0568709e4700b001fa1c89c656mr2238192oab.56.1701916042772; Wed, 06 Dec 2023 18:27:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701916042; cv=none; d=google.com; s=arc-20160816; b=CHmJMYFmWtjHeRV+aSXAG8PnyjcP+oi//xnxXwLRzOewiRGuZ896nID1gp+ipZDWAw iPS7LHvWzeNUP8UNOt1kF5hOfq9ODydD6hAezqRZKtKHwKQN1hjocz6Uy4xUQeY4ewZG IZpPeMv8eiRoj+FFXo1f5Etxc0Mve3EUa+G/v6faqlkYgRh0rTWT8WHhsG9+8TqrxRqm OI6HqJ8ce4ELVrBuBqnvA+YKD7fjXrtoy6S+labnvfVqcMCw2tZRcATNuYMhFKWq04z2 3RFqF4TXDL0IvQTb2MjcRpAbb/KwaLRSKknDLbXKoTIvQFzEbw51eaPDSRMI9Pz0RHnF p7Pw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=VnNxPtULTHkI7+FJBFkxvuhGf4itUHcJaix/h4kMFWk=; fh=OvCUO7VSi2qhFSf1pAk0DSxAfqIFciTB3JbkL+4/94M=; b=H58pjd2qI2qDSFabNZQl45Pr5Q2Ff/1vh9q472B5HsEh9YqYjOjQCOll9QsHXEhtrs FKYR0s4QglU1HEzfBNAKPhhS5Dy2ffumh/d4CdV4f3+cdUbafUHhRnaafRgksI0399fQ ZNOAZ+zjET/l2CDdOf72NPl6vHDpFS8dZRM9wJNiwykJpg8Q9ebckK/3mBCr/ZgQM96c U4izWPwcQjHgkigeSomW34FplC8VDBw4x+0kYMLxoWnicnli3PfEP3rc8aLsAu568Ztv kjcHVOWMZIGF6fA7tmqISaU+3vG00S962NzvZ8eSNUKpyk6VUwEkK/qTUOczEYxrOfaD xmLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b=Ar1MDqmX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id x6-20020a63cc06000000b00564c67e66fbsi284643pgf.842.2023.12.06.18.27.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 18:27:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b=Ar1MDqmX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 04FF681F3335; Wed, 6 Dec 2023 18:27:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231238AbjLGCXz (ORCPT + 99 others); Wed, 6 Dec 2023 21:23:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230385AbjLGCXy (ORCPT ); Wed, 6 Dec 2023 21:23:54 -0500 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [IPv6:2a03:a000:7:0:5054:ff:fe1c:15ff]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03294D4B; Wed, 6 Dec 2023 18:24:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=VnNxPtULTHkI7+FJBFkxvuhGf4itUHcJaix/h4kMFWk=; b=Ar1MDqmXci7Shf383eNOiYpJEG Xcy3OFqNU4GSO5sHlexxo6Frj3D0bdK7t+mK8aor2Jpel1HIT2ag9M92CZVM0cOjQo9Wnom0g3UGp qx76DkqvBHRVUEP1ZUJKw+GvsKnrHxL5YjJ6SUVJMaeIIsGd5JuJlFvnpM0RIYLRjuRROrME0JNen Amge3ULWWLEFE2Mh5jcjAbSVWVNTsgHByvrf/xDNoMfbt62eVJXxNcbY7h2IUvONyfrwyI6Nl0SLP enz2SulFtL40+bB9YOQ9gbkdJzFPm8Zup2rbNIfhNAkSwgxkLHPAQuHR2xO0Sv6mD34zqUsZTX975 vhdOn4gg==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.96 #2 (Red Hat Linux)) id 1rB43R-00831U-1R; Thu, 07 Dec 2023 02:23:57 +0000 Date: Thu, 7 Dec 2023 02:23:57 +0000 From: Al Viro To: Dave Chinner Cc: linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-cachefs@redhat.com, dhowells@redhat.com, gfs2@lists.linux.dev, dm-devel@lists.linux.dev, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 01/11] lib/dlock-list: Distributed and lock-protected lists Message-ID: <20231207022357.GS1674809@ZenIV> References: <20231206060629.2827226-1-david@fromorbit.com> <20231206060629.2827226-2-david@fromorbit.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231206060629.2827226-2-david@fromorbit.com> Sender: Al Viro X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 06 Dec 2023 18:27:20 -0800 (PST) On Wed, Dec 06, 2023 at 05:05:30PM +1100, Dave Chinner wrote: > +static inline struct dlock_list_node * > +__dlock_list_next_entry(struct dlock_list_node *curr, > + struct dlock_list_iter *iter) > +{ > + /* > + * Find next entry > + */ > + if (curr) > + curr = list_next_entry(curr, list); > + > + if (!curr || (&curr->list == &iter->entry->list)) { Hmm... hlist, perhaps? I mean, that way the thing becomes if (curr) curr = hlist_entry_safe(curr->node.next, struct dlock_list_node, node); if (!curr) curr = __dlock_list_next_list(iter); return curr; BTW, does anybody have objections against #define hlist_first_entry(head, type, member) hlist_entry_safe((head)->first, type, member) #define hlist_next_entry(pos, member) hlist_entry_safe((pos)->member.next, typeof(*pos), member) added in list.h? > +static int __init cpu2idx_init(void) > +{ > + int idx, cpu; > + > + idx = 0; > + for_each_possible_cpu(cpu) > + per_cpu(cpu2idx, cpu) = idx++; > + return 0; > +} > +postcore_initcall(cpu2idx_init); Is it early enough? Feels like that ought to be done from smp_init() or right after it... > +/** > + * 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. > + */ > +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; > +} Umm... How would one use it, anyway? You'd need to stop all insertions first, wouldn't you? > + */ > +struct dlock_list_node *__dlock_list_next_list(struct dlock_list_iter *iter) > +{ > + struct dlock_list_node *next; > + struct dlock_list_head *head; > + > +restart: > + if (iter->entry) { > + spin_unlock(&iter->entry->lock); > + iter->entry = NULL; > + } > + > +next_list: > + /* > + * Try next list > + */ > + if (++iter->index >= nr_cpu_ids) > + return NULL; /* All the entries iterated */ > + > + if (list_empty(&iter->head[iter->index].list)) > + goto next_list; > + > + head = iter->entry = &iter->head[iter->index]; > + spin_lock(&head->lock); > + /* > + * There is a slight chance that the list may become empty just > + * before the lock is acquired. So an additional check is > + * needed to make sure that a valid node will be returned. > + */ > + if (list_empty(&head->list)) > + goto restart; > + > + next = list_entry(head->list.next, struct dlock_list_node, > + list); > + WARN_ON_ONCE(next->head != head); > + > + return next; > +} Perhaps something like if (iter->entry) { spin_unlock(&iter->entry->lock); iter->entry = NULL; } while (++iter->index < nr_cpu_ids) { struct dlock_list_head *head = &iter->head[iter->index]; if (list_empty(head->list)) continue; spin_lock(&head->lock); // recheck under lock if (unlikely(list_empty(&head->list))) { spin_unlock(&head->lock); continue; } iter->entry = head; next = list_first_entry(&head->list, struct dlock_list_node, list); WARN_ON_ONCE(next->head != head); return next; } return NULL;