Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp2596184ybh; Mon, 9 Mar 2020 09:03:26 -0700 (PDT) X-Google-Smtp-Source: ADFU+vurMhB0kNO88mg5FxF2hmoVQOCM7YcqhEOtA6FtLu6gJuABNN0f+UG4vbn1rZXwiuhsqtxu X-Received: by 2002:a05:6830:143:: with SMTP id j3mr13508165otp.355.1583769806541; Mon, 09 Mar 2020 09:03:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583769806; cv=none; d=google.com; s=arc-20160816; b=UW+WlQObXKc+eg0vBhZuaEQrkYFOTDyeLGTIxCg5jP9WkE7mt/MluiMZUtcC69zcms RVAMIbltQyN2uwHwFO+haQyeRRYqxXKuaowjsRES2jQx7KEIt2wdU3spjdOAjiXF5C10 6e2R7ZWwM2YE53AD1EXIigc8vINS4BQua/2rLgprNz00W81FYSgYbt0Yh8BabQ4vd4qa +s6RU6brs98d7Pg6b8gNkPrF/NPjNs5cVMKYirR9lcKIInVrU7R8REJkLEdFeWmKWE2Z jXA1m60FXGoCoA7LSEZu6E0iDjHKnpH8SFOlrAbKtWYMwDyT/FUUkqksY/Q2ESBFgNEg esAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=FPahBlx9BxgYf27cCynxoigTpGq1oNMafhDT1t5E2i0=; b=AnRFcHrNLgiiWvwngVt2r2xLd2/o6akegWodOHl9PlO1GAn5cAfp7iUQ6zAsuakrno V1DYULHO0X3+e7biCtD2DQzsQaZyk7WTs9H0abn3qKv5vLaODlUpqUGWdspwXniH61bz Qy0qa6oEmmHsSbpoADGWFbrrzb6Bgkc3056YmkB50UIqlMMa0EsBQSgSidWCXEqC8xEn 6IZ/izwr+qUOMGNt9imqegX6dAEp25O8Ag/ONo1adXaTzskUSqW4aBDPuOgf7d6QMljJ qDHg5OXgPcfryxNDZUQgb7H0Nnbsdh2WO86zbgvpPylsuZ3iCKYaw/4/HGjlWd5WRAt+ lRIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=po9U227B; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l14si6660421ots.115.2020.03.09.09.03.05; Mon, 09 Mar 2020 09:03:26 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=po9U227B; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727032AbgCIQCV (ORCPT + 99 others); Mon, 9 Mar 2020 12:02:21 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:44154 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726758AbgCIQCV (ORCPT ); Mon, 9 Mar 2020 12:02:21 -0400 Received: by mail-pl1-f195.google.com with SMTP id d9so4137752plo.11 for ; Mon, 09 Mar 2020 09:02:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=FPahBlx9BxgYf27cCynxoigTpGq1oNMafhDT1t5E2i0=; b=po9U227B3G8D+omYOHGTuf/4x0/W09eJkeNJg4vLPzb4Rd5ucAh7Z21VTV2lLryz0I rHFfoxh7V+x1VQOAC9k8tgGB2zDS/nkEEZ83Btpw9Ch6e2PytlZqxUbUD2HLdIYPY71w pvtjSoyW6mUBooxIseZ94yeg8ejfVUHsSlgJxdzFvgJEwsmv5QO9WkhyGXv4TEB1TMcz q+aQKW2Qs5vlLv4e1vkQMh0/rsTGBJfBoi/gt1QTUi8/DyZ3PGsmaOPGLsw/3yLgB3eD G6ImskgVSMfs12GhCwE1Ge/4SUoJJ9UuhPGzpqae1vYOG/0Zm4GvRM21CLEeDuT0kYIg dPdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=FPahBlx9BxgYf27cCynxoigTpGq1oNMafhDT1t5E2i0=; b=QnbNVKWTu9pK6Nq2hnvzVviRnDudsU9t5PVWY0VMI5oHLbiZWicOqxT+vN0H3wCfkm fEUxuHE+rV4yxkHKBu5ogbOjzR9iD9xihrkb7ltKe7KJoBCCHCp1PkS4wwRiI0A6KXWm ln0f+QvRqaIc8YjjxxGzLl30pP5DNTuCzFUKQfXc8QHSrB8SOwAQqR0nm0gQqgHjzGrc Bvp2xL2yRQ9POhofsfW4alu8DUoZIuBnp8hyzKgFHR0YLGZNBFJ1Dx0yq19Vp9++VbR2 adT71XQNPRbWwrag4tTiTQ9xWnWBT8PuREp+XsR8w0Z80kwKPq0bkdAh2QoHjoHcDc60 LcNA== X-Gm-Message-State: ANhLgQ1odImggpkydnhu2/9kTItCneNyDpIXI34ewfYutCmftAnOwxV8 uAu2PkvfWkPdqmuVZctoqpE= X-Received: by 2002:a17:90a:8:: with SMTP id 8mr18953pja.130.1583769740092; Mon, 09 Mar 2020 09:02:20 -0700 (PDT) Received: from localhost ([43.224.245.179]) by smtp.gmail.com with ESMTPSA id q12sm45104429pfh.158.2020.03.09.09.02.18 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Mon, 09 Mar 2020 09:02:19 -0700 (PDT) From: qiwuchen55@gmail.com To: akpm@linux-foundation.org, peterz@infradead.org, walken@google.com, rfontana@redhat.com, dbueso@suse.de Cc: tglx@linutronix.de, linux-kernel@vger.kernel.org, chenqiwu Subject: [PATCH] rbtree: introduce three helpers about sort-order iteration Date: Tue, 10 Mar 2020 00:02:14 +0800 Message-Id: <1583769734-1311-1-git-send-email-qiwuchen55@gmail.com> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: chenqiwu I noticed that there are many relatively common usages about the sort-order iteration for rbtree, like this: [drivers/android/binder.c] for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { struct binder_node *node = rb_entry(n, struct binder_node, rb_node); ...... } This patch introduced three helpers to simplify sort-order iteration for rbtree. Signed-off-by: chenqiwu --- include/linux/rbtree.h | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h index 1fd61a9..26b4359 100644 --- a/include/linux/rbtree.h +++ b/include/linux/rbtree.h @@ -90,11 +90,52 @@ static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent }) /** + * rbtree_for_each - iterate in sort-order over a rbtree. + * @pos: the 'rb_node *' to use as a loop cursor. + * @root: 'rb_root *' of the rbtree. + */ +#define rbtree_for_each(pos, root) \ + for (pos = rb_first(root); pos; pos = rb_next(pos)) + +/** + * rbtree_for_each_entry - iterate in sort-order over rb_root of given type. + * @pos: the 'type *' to use as a loop cursor. + * @root: 'rb_root *' of the rbtree. + * @field: the name of the rb_node field within 'type'. + */ +#define rbtree_for_each_entry(pos, root, field) \ + for (pos = rb_entry_safe(rb_first(root), typeof(*pos), field); \ + pos; \ + pos = rb_entry_safe(rb_next(&pos->field), typeof(*pos), field)) + +/** + * rbtree_for_each_entry_safe - iterate in sort-order over of given type + * allowing the backing memory of @pos to be invalidated. + * @pos: the 'type *' to use as a loop cursor. + * @n: another 'type *' to use as temporary storage. + * @root: 'rb_root *' of the rbtree. + * @field: the name of the rb_node field within 'type'. + * + * rbtree_order_for_each_entry_safe() provides a similar guarantee as + * list_for_each_entry_safe() and allows the sort-order iteration to + * continue independent of changes to @pos by the body of the loop. + * + * Note, however, that it cannot handle other modifications that re-order the + * rbtree it is iterating over. This includes calling rb_erase() on @pos, as + * rb_erase() may rebalance the tree, causing us to miss some nodes. + */ +#define rbtree_for_each_entry_safe(pos, n, root, field) \ + for (pos = rb_entry_safe(rb_first(root), typeof(*pos), field); \ + pos && ({ n = rb_entry_safe(rb_next(&pos->field), \ + typeof(*pos), field); 1; }); \ + pos = n) + +/** * rbtree_postorder_for_each_entry_safe - iterate in post-order over rb_root of - * given type allowing the backing memory of @pos to be invalidated + * given type allowing the backing memory of @pos to be invalidated. * * @pos: the 'type *' to use as a loop cursor. - * @n: another 'type *' to use as temporary storage + * @n: another 'type *' to use as temporary storage. * @root: 'rb_root *' of the rbtree. * @field: the name of the rb_node field within 'type'. * -- 1.9.1