Received: by 10.213.65.68 with SMTP id h4csp250684imn; Sat, 17 Mar 2018 02:34:17 -0700 (PDT) X-Google-Smtp-Source: AG47ELt/THAxdly4a3Lb1cI/kWb2kzg5V4vM+ia8p0oCUby8512mnUpeZwt0SDbFZnCXlfNZ1Mla X-Received: by 2002:a17:902:b10c:: with SMTP id q12-v6mr5224463plr.197.1521279257914; Sat, 17 Mar 2018 02:34:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521279257; cv=none; d=google.com; s=arc-20160816; b=C9ATyjhTtx+Esg/JSocKAq6o20qs7B+E7YYAosh3f/7zz/6I/cIARSzCHY+DEbsq0H pVTiOEUeCObFcHj9v4LaucuTao4PxzSG3EK+xyTmEKQyDnA3G4o1wcW5VPhnYt3IKihj zoU1dVCv05IgVdeUZpm7CciEE1D1UuMWl50dT9yPCSMTa2sdFdxfxbeiWfjXXqYuAnhZ gb7UOlpHVEBbwPbhnMfID9f0WQU4s9juiMkjYuL6sAWE4rqU0Xmfmyd8btfziYmtdSyw cq2xso032VFzEw7cxNUcZ8p+X4EihiUREMOHGxBHZYnyuauTStvrNFXy5d4gaWCTC3r2 kYVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:organization:from:references:cc:to:subject :dkim-signature:arc-authentication-results; bh=uD4iztECnSmpUx3AGWZO3rYe6Ei3cDSaatdSUY6QkdA=; b=eiJCA7AXtplbm3iL01Vhr34CUZ17oGrcdp6nh2UYD7I6loozSacAFMO3dFps7FJNz5 7iVS9zKKGJcM9VctpJAk7QUCOtq50NOVHyHDiVLCfzGswGsamirCipHml5eLGOAyj+6y UZtPcNOeoctH8sy2rpOjlnf9SSkkSIvuphlQlNGY3oxABcvoE7yJPXukMmUBloFefZ0b L6Kbs70qbe+q/KYH8SHcKrOEk6WK2EJE1Fl86yOaMo8uJat8AyInn+MReMap0VojSMdf mO+rUmVpe6dHMYZJ0PvQrpIQaP3mBNj2BBkEI5bkiXZWkpjvhBTXxSlBSgocPcozHtQW GmvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=trNlsUg7; 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 r62si6495016pgr.77.2018.03.17.02.34.01; Sat, 17 Mar 2018 02:34:17 -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 (test mode) header.i=@ideasonboard.com header.s=mail header.b=trNlsUg7; 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 S1751743AbeCQJdH (ORCPT + 99 others); Sat, 17 Mar 2018 05:33:07 -0400 Received: from galahad.ideasonboard.com ([185.26.127.97]:51739 "EHLO galahad.ideasonboard.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750778AbeCQJdF (ORCPT ); Sat, 17 Mar 2018 05:33:05 -0400 Received: from [192.168.43.222] (unknown [149.254.234.195]) by galahad.ideasonboard.com (Postfix) with ESMTPSA id 1FCDA200AD; Sat, 17 Mar 2018 10:30:45 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1521279047; bh=2k2kaS21j2I2sZFy3/d42S5fDdFqu8YIn2kXcQVxbGc=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=trNlsUg70tjRNDetA7ldHRm5b9athKPNd7tgAfmiJNNvYjm3qpKYrzzRXS+Qcpkp7 IqeNW3W8hBGmYYHx52nBFdvYtXDPSLcTTZkFnruFv6cba3iCmaGCtKrhlziqtCrlVG tq0vqaPFmZ3l4OqZzl1fWs7AeJTwIu1Tzo5vCMqs= Subject: Re: [PATCH][RFC] kernel.h: provide array iterator To: Rasmus Villemoes , linux-kernel@vger.kernel.org Cc: Laurent Pinchart , linux-media@vger.kernel.org, Andrew Morton , Ingo Molnar , Thomas Gleixner , Kees Cook , Herbert Xu , Masahiro Yamada , Greg Kroah-Hartman , Krzysztof Kozlowski , Randy Dunlap , Ian Abbott References: <1521108052-26861-1-git-send-email-kieran.bingham@ideasonboard.com> From: Kieran Bingham Organization: Ideas on Board Message-ID: <30d9c547-a53b-8c3f-f185-7152bd302c8b@ideasonboard.com> Date: Sat, 17 Mar 2018 10:32:57 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Rasmus, On 16/03/18 16:27, Rasmus Villemoes wrote: > On 2018-03-15 11:00, Kieran Bingham wrote: >> Simplify array iteration with a helper to iterate each entry in an array. >> Utilise the existing ARRAY_SIZE macro to identify the length of the array >> and pointer arithmetic to process each item as a for loop. >> >> Signed-off-by: Kieran Bingham >> --- >> include/linux/kernel.h | 10 ++++++++++ >> 1 file changed, 10 insertions(+) >> >> The use of static arrays to store data is a common use case throughout the >> kernel. Along with that is the obvious need to iterate that data. >> >> In fact there are just shy of 5000 instances of iterating a static array: >> git grep "for .*ARRAY_SIZE" | wc -l >> 4943 >> >> When working on the UVC driver - I found that I needed to split one such >> iteration into two parts, and at the same time felt that this could be >> refactored to be cleaner / easier to read. > > About that, it would be helpful if you first converted to the new > iterator, so that one can more easily see they are equivalent. And then > split in two, adding the flush_workqueue call. Or do it the other way > around. But please don't mix the two in one patch, especially not if > it's supposed to act as an example of how to use the new helper. > My apologies - in the example below I was trying to show the usage and reason for the macro. This was not meant to be a change to be integrated - the 'example change' is not how the change will be committed, or included in the patch - but was added here purely to show the usage / reason for the new macro and promote the discussion. (So I'll already call that a success) But that is a good point - the example usage could be much simplified here, and then included in the commit message. >> I do however worry that this simple short patch might not be desired or could >> also be heavily bikeshedded due to it's potential wide spread use (though >> perhaps that would be a good thing to have more users) ... but here it is, >> along with an example usage below which is part of a separate series. > > I think it can be useful, and it does have the must_be_array protection > built in, so code doesn't silently break if one changes from a > fixed-size allocation to e.g. a kmalloc-based one. Just don't attempt a > tree-wide mass conversion, but obviously starting to make use of it when > refactoring code anyway is fine. Well it had already been suggested to try to make a coccinelle patch - but I suspect time and effort required may delay or postpone that currently. I'll focus on seeing if I can actually get this macro in before expending effort on a full conversion :-D I originally anticipated that this would be a 'convert or use as required' style change. > And now, the bikeshedding you expected :) It wouldn't be a discussion without it :D >> diff --git a/include/linux/kernel.h b/include/linux/kernel.h >> index ce51455e2adf..95d7dae248b7 100644 >> --- a/include/linux/kernel.h >> +++ b/include/linux/kernel.h >> @@ -70,6 +70,16 @@ >> */ >> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) >> >> +/** >> + * for_each_array_element - Iterate all items in an array >> + * @elem: pointer of array type for iteration cursor > > Hm, "pointer of array type" sounds wrong; it's not a "pointer to array". > But "pointer of array elements' type" is clumsy. Maybe just "@elem: > iteration cursor" is clear enough. "@elem: iteration cursor" sounds good to me. Depending on how the other conversations go here - I will likely make this change. (I see there was a previous attempt at including a very similar macro) >> + * @array: array to be iterated >> + */ >> +#define for_each_array_element(elem, array) \ >> + for (elem = &(array)[0]; \ >> + elem < &(array)[ARRAY_SIZE(array)]; \ >> + ++elem) >> + > > Please parenthesize elem as well. That's certainly a good point! Thanks :D > Rasmus Regards Kieran Bingham