From patchwork Tue Mar 1 07:58:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaomeng Tong X-Patchwork-Id: 12764275 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6BF90C433F5 for ; Tue, 1 Mar 2022 07:59:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DDAAA8D0005; Tue, 1 Mar 2022 02:59:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D63A88D0001; Tue, 1 Mar 2022 02:59:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C04CD8D0005; Tue, 1 Mar 2022 02:59:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0053.hostedemail.com [216.40.44.53]) by kanga.kvack.org (Postfix) with ESMTP id ADC5F8D0001 for ; Tue, 1 Mar 2022 02:59:04 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 589048249980 for ; Tue, 1 Mar 2022 07:59:04 +0000 (UTC) X-FDA: 79195066608.26.72654EE Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf24.hostedemail.com (Postfix) with ESMTP id D9CFE180006 for ; Tue, 1 Mar 2022 07:59:03 +0000 (UTC) Received: by mail-pl1-f172.google.com with SMTP id z11so4426057pla.7 for ; Mon, 28 Feb 2022 23:59:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=YmXLEDuShcOW3JnBcgY++d4c0kg2D36rSz3AenVtmRY=; b=P+Lo9i6QowRAx0EEntsNvNUxCt1I6HaXTfmY+33mqDxWWil2KSfipziM4Nn/14h0Oq z3ypRqxwexLrfYlQJoFd5+rVz7UslgYwUmHKWvJwvuKairaWtKd3veo5qsliUZ77MFPs 4HnpB+JqFuavKk7cpK2Xv26KsS2ayZZif2Pr2d/FgeJA2JDTyHKjs7+K7Ws+K0zgWKRI ruFI6zDEScMjqU+vBM3+W3qJPu1gsLFAzxWdPEOww2yB+9dZENlLnBl8qdXU/rKNsiTR Eg+MOzpR1yqVnUoWa8XtPdBWxpwcFODjLHq/RzIc9anKNpZ0JutOuz4IzAjjjND7Pu74 wYFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=YmXLEDuShcOW3JnBcgY++d4c0kg2D36rSz3AenVtmRY=; b=GbVC3nS05kO1SioQtUUMZyOiVQo+QFTWspSHCsMjyRKje7tBoAQzl6nl5uz4Z6mNYq H5GGt2ENbLVhbbvJhBHASi8ZTeNAjt6OTr4N0Ky19W0L/Qcy0Z7H/lH6eAd3mZjuDaJe ZAONIMX8Z3E09+WsUFyXiYDKg/IKuG2fqP8Kkp2gQqs3WG2PsGn5TR2K4huIrbIGUzXB GiPu2MhGVSq8JF+VoRgkzhKPgtgU1nUI+lBxHMfUSTLjGdEegCQBvPL3rPLDW1XxP55Q RhvIj7hBbEj6VOhXg3ZExdAyhdBq1nECW0u0J0PlXBrh3PU64LOk0JxbfU0rnxQ8PTzj 2C9w== X-Gm-Message-State: AOAM532kNNK4GPzTkyi37pgkT/6ffxI4ewOZjZ1xw/CVF0oeSJaYPk0L kkbe5Ioy9o6fALMG15KMlWY= X-Google-Smtp-Source: ABdhPJzyKErM8pnzRaH8ojeB27k6eMiFHyMnxjPUJP6pxbk0TO4CP5X8i6d+VfvO4iKjFenzpV33QA== X-Received: by 2002:a17:902:7d83:b0:14e:f2f4:743 with SMTP id a3-20020a1709027d8300b0014ef2f40743mr24258960plm.107.1646121542543; Mon, 28 Feb 2022 23:59:02 -0800 (PST) Received: from ubuntu.huawei.com ([119.3.119.18]) by smtp.googlemail.com with ESMTPSA id o12-20020a17090aac0c00b001b9e5286c90sm1662745pjq.0.2022.02.28.23.58.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 23:59:02 -0800 (PST) From: Xiaomeng Tong To: torvalds@linux-foundation.org Cc: arnd@arndb.de, jakobkoschel@gmail.com, linux-kernel@vger.kernel.org, gregkh@linuxfoundation.org, keescook@chromium.org, jannh@google.com, linux-kbuild@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, Xiaomeng Tong Subject: [PATCH 2/6] list: add new MACROs to make iterator invisiable outside the loop Date: Tue, 1 Mar 2022 15:58:35 +0800 Message-Id: <20220301075839.4156-3-xiam0nd.tong@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220301075839.4156-1-xiam0nd.tong@gmail.com> References: <20220301075839.4156-1-xiam0nd.tong@gmail.com> X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: D9CFE180006 X-Stat-Signature: zx95jhfxhmq4ekuzyndc81ir755jc541 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=P+Lo9i6Q; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of xiam0nd.tong@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=xiam0nd.tong@gmail.com X-HE-Tag: 1646121543-12078 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: For each list_for_each_entry* macros(10 variants), implements a respective new *_inside one. Such as the new macro list_for_each_entry_inside for list_for_each_entry. The idea is to be as compatible with the original interface as possible and to minimize code changes. Here are 2 examples: list_for_each_entry_inside: - declare the iterator-variable pos inside the loop. Thus, the origin declare of the inputed *pos* outside the loop should be removed. In other words, the inputed *pos* now is just a string name. - add a new "type" argument as the type of the container struct this is embedded in, and should be inputed when calling the macro. list_for_each_entry_safe_continue_inside: - declare the iterator-variable pos and n inside the loop. Thus, the origin declares of the inputed *pos* and *n* outside the loop should be removed. In other words, the inputed *pos* and *n* now are just string name. - add a new "start" argument as the given iterator to start with and can be used to get the container struct *type*. This should be inputed when calling the macro. Signed-off-by: Xiaomeng Tong Reported-by: kernel test robot --- include/linux/list.h | 156 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/include/linux/list.h b/include/linux/list.h index dd6c2041d..1595ce865 100644 --- a/include/linux/list.h +++ b/include/linux/list.h @@ -639,6 +639,19 @@ static inline void list_splice_tail_init(struct list_head *list, !list_entry_is_head(pos, head, member); \ pos = list_next_entry(pos, member)) +/** + * list_for_each_entry_inside + * - iterate over list of given type and keep iterator inside the loop + * @pos: the type * to use as a loop cursor. + * @type: the type of the container struct this is embedded in. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + */ +#define list_for_each_entry_inside(pos, type, head, member) \ + for (type * pos = list_first_entry(head, type, member); \ + !list_entry_is_head(pos, head, member); \ + pos = list_next_entry(pos, member)) + /** * list_for_each_entry_reverse - iterate backwards over list of given type. * @pos: the type * to use as a loop cursor. @@ -650,6 +663,19 @@ static inline void list_splice_tail_init(struct list_head *list, !list_entry_is_head(pos, head, member); \ pos = list_prev_entry(pos, member)) +/** + * list_for_each_entry_reverse_inside + * - iterate backwards over list of given type and keep iterator inside the loop. + * @pos: the type * to use as a loop cursor. + * @type: the type of the container struct this is embedded in. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + */ +#define list_for_each_entry_reverse_inside(pos, type, head, member) \ + for (type * pos = list_last_entry(head, type, member); \ + !list_entry_is_head(pos, head, member); \ + pos = list_prev_entry(pos, member)) + /** * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() * @pos: the type * to use as a start point @@ -675,6 +701,22 @@ static inline void list_splice_tail_init(struct list_head *list, !list_entry_is_head(pos, head, member); \ pos = list_next_entry(pos, member)) +/** + * list_for_each_entry_continue_inside + * - continue iteration over list of given type and keep iterator inside the loop + * @pos: the type * to use as a loop cursor. + * @start: the given iterator to start with. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Continue to iterate over list of given type, continuing after + * the current position. + */ +#define list_for_each_entry_continue_inside(pos, start, head, member) \ + for (typeof(*start) *pos = list_next_entry(start, member); \ + !list_entry_is_head(pos, head, member); \ + pos = list_next_entry(pos, member)) + /** * list_for_each_entry_continue_reverse - iterate backwards from the given point * @pos: the type * to use as a loop cursor. @@ -689,6 +731,22 @@ static inline void list_splice_tail_init(struct list_head *list, !list_entry_is_head(pos, head, member); \ pos = list_prev_entry(pos, member)) +/** + * list_for_each_entry_continue_reverse_inside + * - iterate backwards from the given point and keep iterator inside the loop + * @pos: the type * to use as a loop cursor. + * @start: the given iterator to start with. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Start to iterate over list of given type backwards, continuing after + * the current position. + */ +#define list_for_each_entry_continue_reverse_inside(pos, start, head, member) \ + for (typeof(*start) *pos = list_prev_entry(start, member); \ + !list_entry_is_head(pos, head, member); \ + pos = list_prev_entry(pos, member)) + /** * list_for_each_entry_from - iterate over list of given type from the current point * @pos: the type * to use as a loop cursor. @@ -701,6 +759,20 @@ static inline void list_splice_tail_init(struct list_head *list, for (; !list_entry_is_head(pos, head, member); \ pos = list_next_entry(pos, member)) +/** + * list_for_each_entry_from_inside + * - iterate over list of given type from the current point and keep iterator inside the loop + * @pos: the type * to use as a loop cursor. + * @start: the given iterator to start with. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Iterate over list of given type, continuing from current position. + */ +#define list_for_each_entry_from_inside(pos, start, head, member) \ + for (typeof(*start) *pos = start; !list_entry_is_head(pos, head, member); \ + pos = list_next_entry(pos, member)) + /** * list_for_each_entry_from_reverse - iterate backwards over list of given type * from the current point @@ -714,6 +786,21 @@ static inline void list_splice_tail_init(struct list_head *list, for (; !list_entry_is_head(pos, head, member); \ pos = list_prev_entry(pos, member)) +/** + * list_for_each_entry_from_reverse_inside + * - iterate backwards over list of given type from the current point + * and keep iterator inside the loop + * @pos: the type * to use as a loop cursor. + * @start: the given iterator to start with. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Iterate backwards over list of given type, continuing from current position. + */ +#define list_for_each_entry_from_reverse_inside(pos, start, head, member) \ + for (typeof(*start) *pos = start; !list_entry_is_head(pos, head, member); \ + pos = list_prev_entry(pos, member)) + /** * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry * @pos: the type * to use as a loop cursor. @@ -727,6 +814,22 @@ static inline void list_splice_tail_init(struct list_head *list, !list_entry_is_head(pos, head, member); \ pos = n, n = list_next_entry(n, member)) +/** + * list_for_each_entry_safe_inside + * - iterate over list of given type safe against removal of list entry + * and keep iterator inside the loop + * @pos: the type * to use as a loop cursor. + * @n: another type * to use as temporary storage + * @type: the type of the container struct this is embedded in. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + */ +#define list_for_each_entry_safe_inside(pos, n, type, head, member) \ + for (type * pos = list_first_entry(head, type, member), \ + *n = list_next_entry(pos, member); \ + !list_entry_is_head(pos, head, member); \ + pos = n, n = list_next_entry(n, member)) + /** * list_for_each_entry_safe_continue - continue list iteration safe against removal * @pos: the type * to use as a loop cursor. @@ -743,6 +846,24 @@ static inline void list_splice_tail_init(struct list_head *list, !list_entry_is_head(pos, head, member); \ pos = n, n = list_next_entry(n, member)) +/** + * list_for_each_entry_safe_continue_inside + * - continue list iteration safe against removal and keep iterator inside the loop + * @pos: the type * to use as a loop cursor. + * @n: another type * to use as temporary storage + * @start: the given iterator to start with. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Iterate over list of given type, continuing after current point, + * safe against removal of list entry. + */ +#define list_for_each_entry_safe_continue_inside(pos, n, start, head, member) \ + for (typeof(*start) *pos = list_next_entry(start, member), \ + *n = list_next_entry(pos, member); \ + !list_entry_is_head(pos, head, member); \ + pos = n, n = list_next_entry(n, member)) + /** * list_for_each_entry_safe_from - iterate over list from current point safe against removal * @pos: the type * to use as a loop cursor. @@ -758,6 +879,23 @@ static inline void list_splice_tail_init(struct list_head *list, !list_entry_is_head(pos, head, member); \ pos = n, n = list_next_entry(n, member)) +/** + * list_for_each_entry_safe_from_inside + * - iterate over list from current point safe against removal and keep iterator inside the loop + * @pos: the type * to use as a loop cursor. + * @n: another type * to use as temporary storage + * @start: the given iterator to start with. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Iterate over list of given type from current point, safe against + * removal of list entry. + */ +#define list_for_each_entry_safe_from_inside(pos, n, start, head, member) \ + for (typeof(*start) *pos = start, *n = list_next_entry(pos, member); \ + !list_entry_is_head(pos, head, member); \ + pos = n, n = list_next_entry(n, member)) + /** * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal * @pos: the type * to use as a loop cursor. @@ -774,6 +912,24 @@ static inline void list_splice_tail_init(struct list_head *list, !list_entry_is_head(pos, head, member); \ pos = n, n = list_prev_entry(n, member)) +/** + * list_for_each_entry_safe_reverse_insde + * - iterate backwards over list safe against removal and keep iterator inside the loop + * @pos: the type * to use as a loop cursor. + * @n: another type * to use as temporary storage + * @type: the type of the struct this is enmbeded in. + * @head: the head for your list. + * @member: the name of the list_head within the struct. + * + * Iterate backwards over list of given type, safe against removal + * of list entry. + */ +#define list_for_each_entry_safe_reverse_inside(pos, n, type, head, member) \ + for (type * pos = list_last_entry(head, type, member), \ + *n = list_prev_entry(pos, member); \ + !list_entry_is_head(pos, head, member); \ + pos = n, n = list_prev_entry(n, member)) + /** * list_safe_reset_next - reset a stale list_for_each_entry_safe loop * @pos: the loop cursor used in the list_for_each_entry_safe loop