Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp733827pxb; Wed, 6 Oct 2021 14:18:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBY8BkC6KXeEU6DnpiA28+wB8lBrO9hP8VpzAbE2PdsaU/GT2ETLV9+z4AOMUu/Dl7oZQG X-Received: by 2002:a50:da06:: with SMTP id z6mr711720edj.355.1633555139407; Wed, 06 Oct 2021 14:18:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633555139; cv=none; d=google.com; s=arc-20160816; b=IocPJtlEQwQ+sMcfyHCFast9DTnyWhcsCWog6+Pd5NKU+kzME8cmY12k8rlqAXViMH Pp5T9g7UFlT9BSrlUzhz63xnPzVAcXaL8TKVoUDqpCRN0I8QD33guLHoJgFy+meAR6XM MZujTADNuea7hdaOT/S26OWLnAXfqPrnHKtYEjsUiprqs2/7a0i/v5ytyWfK346F8KSF UyMDhfsMw9TVQ3gYxkirQWld9F31OMNazkCNYppQNEj5myC2GPbHDB4itU58HajxeBwQ on51Q27g7iCQ6cXy01OsbeO6fPUCL+KFTC/Bl61jCOZpzGIyxUYZPRNVpeNsiAHAEugC 9c2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:references:in-reply-to:subject :cc:to:from:mime-version:content-transfer-encoding:dkim-signature :dkim-signature; bh=A5n7l6aClaDhnZd/TVwd1w5RuwnsRNMzKDPibEDS/6M=; b=BPNJyG9Sq+wRcavTk26PZgxoaxaEYUIQJNNGO5FrCZLl+4jivo2bGiyvziS3mc0V50 q66IX3I5HKxHLmRkDhmaMR6vAUl7jmK+FSVHTefETLr/wKA8+Y1WY6HkPIdkTKlmMPO6 istP2nrmHfMEa18evEmkJ47+hEhLzcUSPRoOiy894a3HkQFZmwe1I2R70nn9W7CHqK5Z /TCvLVUWwiaWz/nKkwYrdclTO3Wil/OVsUanlUZBXcBrwE3TlWUJUVGSOGJt9G/fmArA Vc2ckN4PbvMszCeoj5wCDw6XSarODwxl1lkRp4pBD6TsIknEp0NGGLH36wCTk9e8x6tp HAmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=hMflkvTc; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b="zn/vItLq"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j24si20120071ejt.152.2021.10.06.14.18.34; Wed, 06 Oct 2021 14:18:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=hMflkvTc; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b="zn/vItLq"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239633AbhJFVSN (ORCPT + 99 others); Wed, 6 Oct 2021 17:18:13 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:51702 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230161AbhJFVSK (ORCPT ); Wed, 6 Oct 2021 17:18:10 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id B761E1FEFF; Wed, 6 Oct 2021 21:16:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1633554976; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=A5n7l6aClaDhnZd/TVwd1w5RuwnsRNMzKDPibEDS/6M=; b=hMflkvTcTYsQs1jGLh6aN3/4X5VMsW5QJA1+Xib8QYHPYvGsLew6dv/1GDqoq6ukJZ2p+b GtbW8g6pLI45khIv1j7uhwDd4L1svW4YIgVB1MlTJjcqEEQdEci6Z4Phw/WRriHiPlxbxq KwJo9AX88ZSur4ZwzxPoRmNqllTUgjM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1633554976; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=A5n7l6aClaDhnZd/TVwd1w5RuwnsRNMzKDPibEDS/6M=; b=zn/vItLqHD0lJu/zctTfyhQiZtUP6LYD0YZHWo9t7iG93rTEbSPuJ3w22jGyynXO5n7Q+U +2G1MrcN4VrVfzDQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id DDFAD13E7C; Wed, 6 Oct 2021 21:16:14 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 0SvNJh4SXmFjMgAAMHmgww (envelope-from ); Wed, 06 Oct 2021 21:16:14 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit MIME-Version: 1.0 From: "NeilBrown" To: "Wei Yang" Cc: kuba@kernel.org, gregkh@linuxfoundation.org, mojha@codeaurora.org, jkosina@suse.cz, linux-kernel@vger.kernel.org, "Wei Yang" Subject: Re: [PATCH] hashtable: remove a redundant check in hash_for_each_xxx() In-reply-to: <20211006152100.17795-1-richard.weiyang@gmail.com> References: <20211006152100.17795-1-richard.weiyang@gmail.com> Date: Thu, 07 Oct 2021 08:16:11 +1100 Message-id: <163355497171.31063.8329134032738647570@noble.neil.brown.name> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 07 Oct 2021, Wei Yang wrote: > The three hash_for_each_xxx() helper iterate the hash table with help > of hlist_for_each_entry_xxx(), which breaks the loop only when obj is > NULL. > > This means the check during each iteration is redundant. This patch > removes it. > > Signed-off-by: Wei Yang > --- > include/linux/hashtable.h | 9 +++------ > 1 file changed, 3 insertions(+), 6 deletions(-) > > diff --git a/include/linux/hashtable.h b/include/linux/hashtable.h > index f6c666730b8c..a15719ed303f 100644 > --- a/include/linux/hashtable.h > +++ b/include/linux/hashtable.h > @@ -124,8 +124,7 @@ static inline void hash_del_rcu(struct hlist_node *node) > * @member: the name of the hlist_node within the struct > */ > #define hash_for_each(name, bkt, obj, member) \ > - for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\ > - (bkt)++)\ > + for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++) \ > hlist_for_each_entry(obj, &name[bkt], member) I think you are missing an important property of this code. What we have here is a new loop command (hash_for_each()) that is constructed from 2 nested loops. This sort of construct is in general difficult to use because in C it is common to use "break" to exit a loop early. 'break' cannot exit two levels of loop though. So if you aren't careful, doing something like hash_for_each() { do something if (some test) break; } might not do what you expect. The 'break' will exit the inner loop, but not the outer loop. That could easily lead to buggy code. But this macro *is* careful. If the loop body *does* use break, then the inner loop will abort but 'obj' will still be non-NULL. The test for NULL in the outer loop causes the outer loop to abort too - as the programmer probably expected. So by removing the 'obj == NULL' test, you would cause any usage which breaks out of the loop to now be incorrect. I recommend that instead of this patch, you provide a patch which improves the documentation to make this clear. e.g. Note: it is safe to 'break' out of this loop even though it is a two nested loops. The 'obj == NULL' test ensures that when the inner loop is broken, the outer loop will break too. Thanks, NeilBrown > > /** > @@ -136,8 +135,7 @@ static inline void hash_del_rcu(struct hlist_node *node) > * @member: the name of the hlist_node within the struct > */ > #define hash_for_each_rcu(name, bkt, obj, member) \ > - for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\ > - (bkt)++)\ > + for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++) \ > hlist_for_each_entry_rcu(obj, &name[bkt], member) > > /** > @@ -150,8 +148,7 @@ static inline void hash_del_rcu(struct hlist_node *node) > * @member: the name of the hlist_node within the struct > */ > #define hash_for_each_safe(name, bkt, tmp, obj, member) \ > - for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\ > - (bkt)++)\ > + for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++) \ > hlist_for_each_entry_safe(obj, tmp, &name[bkt], member) > > /** > -- > 2.23.0 > >