Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2450027ybl; Sat, 14 Dec 2019 12:42:00 -0800 (PST) X-Google-Smtp-Source: APXvYqwtuUL3GkSI94d3pR17TkqYRU1XtGPIoXW+9dgyxp8FXoTzSzgczAXH1vmxMKFYcpn0202p X-Received: by 2002:a9d:6745:: with SMTP id w5mr21340411otm.221.1576356120393; Sat, 14 Dec 2019 12:42:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576356120; cv=none; d=google.com; s=arc-20160816; b=Bc3L5r+JOgdPnuIxP1HYZkAVFNq/+ttg4Ka5jSjXip0NhmIzlMZbWZmK+t8GPjWG0o XAtnCRPlcY59B5XxmaArO9+ZIC6joF4AslIj5AeVKgn5UnQAaab7Bm6lBAM8dRymeXcp IwbXMA/Fz4MHsneqEUPbfZBOe8lhSfCaXVywf3otvXT7pJDtq/kkfxhztEJsSZBDckZ8 iAm5N+oXhyDKALHq4q8+sKYMCl2AufLd2ZGc96H99RZiZZjWWziym/EuuFZCZEfTIleN ObU1OEWhGeyhEjTsZQUoXrT868HbuMnQ9Mq0UjMN490gSuiPt1kpnJPTSAejPfsCxoC2 KOjQ== 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 :in-reply-to:references:mime-version:dkim-signature; bh=VIrmPdhUQoEFUpl9pVyfp9mVBMFH/es2QUeZpBBMI0M=; b=CN00M2hG/0yjhe6Kw5UNTOBjQ+ijGsg7JCYEfVy55p5cAWS3wgO8LKN8VSExsvPvcM KaVnIyLFaWNKCGI2xsAN5tKvkBw1hXAImDd+Ip/e9yswQ3gMk9hcw8zbz0RgJ3yoy2vQ HfYlSNVl+oq2EQWeCSj7MYntAx3qTXDASRpXTYGQ/Guti5L9LZYPdUY1UKm3IXb979gz ncWW/GhO9LXHV2xt6tPyWSuQfk9xYvCoSzic9HvmWiE53It6Wq2jDWi86mRaISpbTiZv 0OkGehEFz9hUeEB2C5q+M0JnyWzSt0x3VXceoRtLmRECrtUods91TLdbfsGOBXZpF4+W DNuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="hN/H9UkJ"; 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=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h22si7500473otn.279.2019.12.14.12.41.48; Sat, 14 Dec 2019 12:42:00 -0800 (PST) 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=@linaro.org header.s=google header.b="hN/H9UkJ"; 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=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726907AbfLNUlB (ORCPT + 99 others); Sat, 14 Dec 2019 15:41:01 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:40802 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726814AbfLNUlA (ORCPT ); Sat, 14 Dec 2019 15:41:00 -0500 Received: by mail-wm1-f66.google.com with SMTP id t14so2385132wmi.5 for ; Sat, 14 Dec 2019 12:40:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=VIrmPdhUQoEFUpl9pVyfp9mVBMFH/es2QUeZpBBMI0M=; b=hN/H9UkJbqHG3eIyw7LV32khWF14F7HuwFEWwgG+fPhtjj90pFkDouO8IZ+uHLexMr px9Ad5kTt6nq9QMiRdtDunkh01u8oFtXfIpRovKtSvDSZF50GIammzIQRv1kGUuT9Inn Sl4DXE7MgLb1NWvBq9JL1TLYihdL4vB/XBjCzMMtuckA3XEo0CtNIubp/QFpI8bqM4xC Rx2HT3wKA2RCrCVprkqdNeerjCk4YIgOT7pHkyD2+cOQF6JoGcd9VEQCqznrq4GP8IDf JolQwhYPtA/GjJ7BUiuE4Qsw6pn0oOhr5Or8RmSGD3R+mPncu2BxnHbkYtNe3qZvcF4z wMFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=VIrmPdhUQoEFUpl9pVyfp9mVBMFH/es2QUeZpBBMI0M=; b=lVnqqkA6RF2jAl2YyOLpjKyESs7UuxNegwoIa+JxWXKKHHPo/jDxM6FI3rYuErBoiq Qe1Ctt3ChusdnH7L/y0sPuem/I6ZWq2bhqBzqrcLWv6u04mL7spotWSJD1RGI+VHHb+j oIp6/fvebtkmHdmQSPfeMTjeYbOaKS+C4hHxaBu2vhLt8/3QvTMY4vLxNo9Ju4XguQm3 jiUL3MO84nJHPZXp5+1tiZHUNJGUuWkx2VJFWN/f9NIstEs323JCGwolO7UmwUXD2OZz FeAa3Wo1MgsQFtLY29anAjeCq3buvBOdkJ1QNFST+LRU/iOGBCBQrx8M8xV+g/2ex5Ri BGRQ== X-Gm-Message-State: APjAAAXCLWtupfsM4xM5pXZhnba0RruEk6Vxl/zNgnquMfsq4w8RDkk6 i+/jzuChrShek6P0Sgwj9itk8ewoNXjgH2bihFv1Tg== X-Received: by 2002:a1c:7205:: with SMTP id n5mr21792417wmc.9.1576356058556; Sat, 14 Dec 2019 12:40:58 -0800 (PST) MIME-Version: 1.0 References: <20191214175735.22518-1-ardb@kernel.org> <20191214175735.22518-4-ardb@kernel.org> <20191214203257.GD140998@rani.riverdale.lan> In-Reply-To: <20191214203257.GD140998@rani.riverdale.lan> From: Ard Biesheuvel Date: Sat, 14 Dec 2019 20:40:57 +0000 Message-ID: Subject: Re: [PATCH 03/10] efi/libstub: use a helper to iterate over a EFI handle array To: Arvind Sankar Cc: Ard Biesheuvel , Linux Kernel Mailing List , linux-efi , Hans de Goede , Matthew Garrett , Ingo Molnar , Andy Lutomirski , Thomas Gleixner 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 Sat, 14 Dec 2019 at 21:33, Arvind Sankar wrote: > > On Sat, Dec 14, 2019 at 06:57:28PM +0100, Ard Biesheuvel wrote: > > Iterating over a EFI handle array is a bit finicky, since we have > > to take mixed mode into account, where handles are only 32-bit > > while the native efi_handle_t type is 64-bit. > > > > So introduce a helper, and replace the various occurrences of > > this pattern. > > > > Signed-off-by: Ard Biesheuvel > > --- > > > > +#define for_each_efi_handle(handle, array, size, i) \ > > + for (i = 1, handle = efi_is_64bit() \ > > + ? (efi_handle_t)(unsigned long)((u64 *)(array))[0] \ > > + : (efi_handle_t)(unsigned long)((u32 *)(array))[0]; \ > > + i++ <= (size) / (efi_is_64bit() ? sizeof(efi_handle_t) \ > > + : sizeof(u32)); \ > > + handle = efi_is_64bit() \ > > + ? (efi_handle_t)(unsigned long)((u64 *)(array))[i] \ > > + : (efi_handle_t)(unsigned long)((u32 *)(array))[i]) > > + > > /* > > * The UEFI spec and EDK2 reference implementation both define EFI_GUID as > > * struct { u32 a; u16; b; u16 c; u8 d[8]; }; and so the implied alignment > > -- > > 2.17.1 > > > > This would access one past the array, no? Eg if the array has one > handle, i is incremented to 2 the first time the condition is checked, > then the loop increment will access array[2] before the condition is > checked again. There seem to be at least a couple of other for_each > macros that might have similar issues. > Indeed. > How about the below instead? > > #define for_each_efi_handle(handle, array, size, i) \ > for (i = 0; \ > (i < (size) / (efi_is_64bit() ? sizeof(efi_handle_t) \ > : sizeof(u32))) && \ > ((handle = efi_is_64bit() \ > ? ((efi_handle_t *)(array))[i] \ > : (efi_handle_t)(unsigned long)((u32 *)(array))[i]), 1);\ > i++) > Yeah, that looks correct to me, but perhaps we can come up with something slightly more readable? :-) (Not saying my code was better in that respect)