Received: by 10.213.65.68 with SMTP id h4csp154089imn; Thu, 15 Mar 2018 21:22:36 -0700 (PDT) X-Google-Smtp-Source: AG47ELuQwEPeoqbRFnlQetN/IpEhQZM5UZRv5mWy8Mwnhe8MztJDxlluCMa0lOrAu7t+fZSNraUT X-Received: by 2002:a17:902:71cf:: with SMTP id t15-v6mr481474plm.107.1521174156747; Thu, 15 Mar 2018 21:22:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521174156; cv=none; d=google.com; s=arc-20160816; b=Dv4YPTy1nMuJzhgNdzvCjTXd9otr190u40joU8SaYga2VsnHen3g0WayFlCEEfMc2e 2tNWvQEQwOqRJmkVDa+4tXv0bpHgktXtucEpV6qPbCGO6kmilcMSZ3fPKhOk/xU4F0f3 LvKTl/FVtKhIZjO1gnGEOf+TIi8n2NijeZtuND5di4W5RRn5oWm7iTUnKDvndvmVswNF mHRPKhpuHL0xjz+qjDsJUdkLN0tCEERWclO/azEM6AWc42Bhqg4cPUsGXX2NESWLTHy3 UtWXiM54jmTbYGybcCV+rzIjmBAh1+NMKmvRaZjGqWbxodRDL3AeQWxFUehjuHu7p5tk t0pA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature:dkim-signature :arc-authentication-results; bh=414MW60eI6NmcE76+qRENIsZQMki6/23Kw2kxQvowtE=; b=AePTEx+ToQm0It9iTfKWIN0BTm1qeotgqwfcSToB0omHxubUx4Mo1fh1BFv5hfNO0j qPMBM4A3He8JjcJ+dSwVq8mjyLehAfMsjSTZxD5+6nCSptvRMW9eYDsdTBvxAS+niYkI gyggQvLU5XsZNPgs28fVayPLEgdKfB0BAT9Mb1WEg6IYBR48BNisyrTiiG8is+ZTTTww /GlZc5cJIZZ4AI8bgKogIL0cBAwfHOrCzJZ7Uz89praSWYEourSGWe1IJ0EcCQzTOmvM 5Me7F0wY4n/pUoHH1Rnz10gcU7aunkan5Lu+pc7J4Z1r70YA1y6JSSNaBBfpy/xsP2Jx TibQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=eS0dC0M/; dkim=fail header.i=@chromium.org header.s=google header.b=l738poce; 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=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g26si569766pgv.384.2018.03.15.21.22.22; Thu, 15 Mar 2018 21:22:36 -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=fail header.i=@google.com header.s=20161025 header.b=eS0dC0M/; dkim=fail header.i=@chromium.org header.s=google header.b=l738poce; 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=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751485AbeCPEVU (ORCPT + 99 others); Fri, 16 Mar 2018 00:21:20 -0400 Received: from mail-ua0-f193.google.com ([209.85.217.193]:41413 "EHLO mail-ua0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751153AbeCPEVS (ORCPT ); Fri, 16 Mar 2018 00:21:18 -0400 Received: by mail-ua0-f193.google.com with SMTP id i21so2560523uak.8 for ; Thu, 15 Mar 2018 21:21:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=414MW60eI6NmcE76+qRENIsZQMki6/23Kw2kxQvowtE=; b=eS0dC0M/41RZ0GnhQy01qoxG+ol4fEJVDl0160wjX9V5gMyOkdro8nFJFoUQQNXE9J clUvxd6mUT0ECT/qZlY6kGNFXZam199gdRozz3GDq4PAY0QZ/n7wafCZUT6alGXaFlYP uSmckPF7/jCWerN8XLkftn4Vt0SqM+GlEVasYUrgiOfQ1KpcXGNsEXb1BDCmNF5VozAW e4jjAMtomxQ3pPXjlKBz/FmzqVMmCzT0rbXUV+uSHyvUc5POAonwubefcE8i+ld28PwV NpcocoJJ+kDK4c/3f4WI/VxKWVVcnMHi2nE8xxkn1kLf7C1jlE/nnKLQ5aVMl42vlUIj 5Y5w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=414MW60eI6NmcE76+qRENIsZQMki6/23Kw2kxQvowtE=; b=l738poce7bgPILZC/mibeFiU8WQCa4AV5T0DDzrlW4FolNp77IPlwbpKQCl/j6XlmY xQqwZWsxAMCojCEfuNN41D6za42/fBX2d/OEU7FCejoYTgOT9phsFySfnMwr9XhXd+Rt 7i6ZMFOXVAFskGyCJP4tmdZ51Mq4d1jagGDLc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=414MW60eI6NmcE76+qRENIsZQMki6/23Kw2kxQvowtE=; b=e1Ietln+KM0ywnE7zP+C1KPCX0II3yome3c/nPeQrER2LvSm6npiFW+YHDvvzfTGXL Gev8XJbjUoAU6Fu9IFU6hTxeD4tm2skyBKKWj9dnFsCU3uX2qMJ8iRXzxlpXmyZEThPY yt4uo9GBz6+JoG5bzvhRUhwMHKQkwW/8ogmKoHf3xWLXnWWJ9I2oYfD2GGn2wmovqotN 7wRDXeJzySpW1zWJlSvWF32x+TDKb0X3iWAJkXYjyCUIxnmcTuqyvcceQsS24ga6rwiW aDy+o4s/5egQcDsEknp2QkgknEKnTmdFsM5gzxk/YdlnyMkGjxcuL9kWFmGdt94XLcEV NjFQ== X-Gm-Message-State: AElRT7EtWX/RTBB8/caCQN6UIQtzqPY72QPmIsq0DBgfUeb9nbHuEwBo e2UaR092xW7TLG0z8h4eBxSgNMcKBn3kaTbOSWYpKGeQ X-Received: by 10.176.78.167 with SMTP id l39mr213356uah.193.1521174076927; Thu, 15 Mar 2018 21:21:16 -0700 (PDT) MIME-Version: 1.0 Received: by 10.31.172.6 with HTTP; Thu, 15 Mar 2018 21:21:15 -0700 (PDT) In-Reply-To: <1521108052-26861-1-git-send-email-kieran.bingham@ideasonboard.com> References: <1521108052-26861-1-git-send-email-kieran.bingham@ideasonboard.com> From: Kees Cook Date: Thu, 15 Mar 2018 21:21:15 -0700 X-Google-Sender-Auth: Xo-BvB32mssHr27Szwg0qDNDd2U Message-ID: Subject: Re: [PATCH][RFC] kernel.h: provide array iterator To: Kieran Bingham Cc: LKML , Laurent Pinchart , linux-media@vger.kernel.org, Andrew Morton , Ingo Molnar , Thomas Gleixner , Herbert Xu , Masahiro Yamada , Greg Kroah-Hartman , Krzysztof Kozlowski , Randy Dunlap , Ian Abbott , Julia Lawall , cocci@systeme.lip6.fr Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 15, 2018 at 3:00 AM, 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 I suspect the main question is "Does this macro make the code easier to read?" I think it does, and we have other kinds of iterators like this in the kernel already. Would it be worth building a Coccinelle script to do the 5000 replacements? -Kees > > 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. > > 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. > > The aim is to simplify iteration on static arrays, in the same way that we have > iterators for lists. The use of the ARRAY_SIZE macro, provides all the > protections given by "__must_be_array(arr)" to this macro too. > > Regards > > Kieran > > ============================================================================= > Example Usage from a pending UVC development: > > +#define for_each_uvc_urb(uvc_urb, uvc_streaming) \ > + for_each_array_element(uvc_urb, uvc_streaming->uvc_urb) > > /* > * Uninitialize isochronous/bulk URBs and free transfer buffers. > */ > static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers) > { > - struct urb *urb; > - unsigned int i; > + struct uvc_urb *uvc_urb; > > uvc_video_stats_stop(stream); > > - for (i = 0; i < UVC_URBS; ++i) { > - struct uvc_urb *uvc_urb = &stream->uvc_urb[i]; > + for_each_uvc_urb(uvc_urb, stream) > + usb_kill_urb(uvc_urb->urb); > > - urb = uvc_urb->urb; > - if (urb == NULL) > - continue; > + flush_workqueue(stream->async_wq); > > - usb_kill_urb(urb); > - usb_free_urb(urb); > + for_each_uvc_urb(uvc_urb, stream) { > + usb_free_urb(uvc_urb->urb); > uvc_urb->urb = NULL; > } > > if (free_buffers) > uvc_free_urb_buffers(stream); > } > ============================================================================= > > > > > 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 > + * @array: array to be iterated > + */ > +#define for_each_array_element(elem, array) \ > + for (elem = &(array)[0]; \ > + elem < &(array)[ARRAY_SIZE(array)]; \ > + ++elem) > + > #define u64_to_user_ptr(x) ( \ > { \ > typecheck(u64, x); \ > -- > 2.7.4 > -- Kees Cook Pixel Security