Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2475927ybl; Sat, 14 Dec 2019 13:08:52 -0800 (PST) X-Google-Smtp-Source: APXvYqyWxPRl77hBg8R0/IhrUAw9viZFUba4gmeq2FAhwEdoEWgcutag8vPbXAoJbhu8rFzxzKbI X-Received: by 2002:aca:4f48:: with SMTP id d69mr9154366oib.103.1576357732097; Sat, 14 Dec 2019 13:08:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576357732; cv=none; d=google.com; s=arc-20160816; b=CGDkZsEGcm/F6xmcgCU3goB32WGq9k8zDzg1fE6r6DVc+ygwcoyAj2BHTtfz1Xz45h 3pTdnaQET4YHN3nSOtNa9aqAH/hPAi4i9JpkBowzMY8gNpWgFtQFoGxPvhnd2JG4D0fB JjbXHMOmvh21GtlJi1e3UYUGECXOyaqRUqD1jZDl49fgjwLqbMV1dCss/xLnUMqrfxea uGBKR/q06mCl5dbqjTMqUCqVPEKWqUzw91cFVXpJl3j5Ai02qlCnVlrxa5fbapD+UMbT Hg8oJ61JU8+gPdT+6k2si0E918d5KIgXxRodHUoUzVFXjZBOhPV+Y2ut4OuKrI3RULW7 OhAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:date:from:dkim-signature; bh=h4m24kjjVELGBf67T3Sqeabi8zZPBxcF9/3vyN0xZq0=; b=Bzc6a1mbmGpHB/TWG30P4BsUJbk36szdFl/7cBgL9JHhlfuEsakPs9wfHPKFjpXEry zxid8T8f3fyZTadV+gESGWmNaWRDFA2PGXPd83IV3AMjmmhp8Nti3M4FFluZuysW8PeA 3OfTIiumwl8JDk8pONYKD+HYArPMFgaEb+pMhPStFJ3nhl8gq6vU2dJq+YzvwdNWhEjB 68EIwkFasqzAXacPQdDe1FO/7OlECu1rdf/jNRIBGWaxpIiCwkUSOk931OaksJnV+Etg BSAmwBkrNAEkasHSxRPoUtiDRQigYN8vxGcEKH/K0xYiiyCq4jSia2onGQflYt1vig6r 1N8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=IryrQR+I; 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 v76si7370898oif.207.2019.12.14.13.08.39; Sat, 14 Dec 2019 13:08:52 -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=fail header.i=@gmail.com header.s=20161025 header.b=IryrQR+I; 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 S1726823AbfLNVIA (ORCPT + 99 others); Sat, 14 Dec 2019 16:08:00 -0500 Received: from mail-qk1-f193.google.com ([209.85.222.193]:36552 "EHLO mail-qk1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726687AbfLNVIA (ORCPT ); Sat, 14 Dec 2019 16:08:00 -0500 Received: by mail-qk1-f193.google.com with SMTP id a203so1173026qkc.3; Sat, 14 Dec 2019 13:07:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:date:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=h4m24kjjVELGBf67T3Sqeabi8zZPBxcF9/3vyN0xZq0=; b=IryrQR+IH5+iX7wsZIFOqz9y2HqsFeXaIoOVLELOlnEgNyooteQJdC9TRfUUNNgWne bD0X6AjoGH/nn4WGKE/mX/qubAFXjoCl/W88bLoXVECIkBSAxXVDfZ28tafXUADV81aD qgmLXZWNRRkTFJosyOLegs5Z44uCXPKIlzRKLoyOR0NQW7qYUFdXyG2dYYtlJdEF0AVX jpgjC7xQv3EairSpifNjQ1ftHCt9Mt6bP2W6+gMVA/hiYqlSoW51UBZqf/uvR5Myh25l fjGN0KtaTjFy0AVtEMZugcaf/PFBKOjsiDBuOoKyCJCSGXCReRYmXJfjg63I7WZ7sPu0 JglA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:date:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to:user-agent; bh=h4m24kjjVELGBf67T3Sqeabi8zZPBxcF9/3vyN0xZq0=; b=gEdQj2EgCx6NolwQPFvslBNiQgZWoxLcNDHIOIFOmg66xG6oliNnAhVsJ+tokRP7iL cRR1Cgm9rdtIG9ltkEfzoqiCfuxAjNLhKZWRc05A6feC7DrcOVUT9li675h2spEeSwNq zZEUKA7WqWFmGD3Bdu1EvZuBORcV3q84ljUIDHCGH7AT4P72X0ZdmvcIOhGW/FEnYERz z6Oqct71GBmIQpH+5fRTlg5pDXrAUfKtZ9dxKBV+GbkioqeSg9PAt1c5rmRRFzFlWqqm rhxqoK/tNdMrRoYqwzZ3lwgu7srTB8ONbns9djc3tP/YuFqYV93fUeUt++uZpbdnGown jAFQ== X-Gm-Message-State: APjAAAWlKwr82OhepUjLTaJMJxr4cIfAjruFxPDdO4AyEtPXKzpTLmuk hUay2She/Ssr17qsvfUFmWM= X-Received: by 2002:a05:620a:149b:: with SMTP id w27mr19561271qkj.229.1576357679354; Sat, 14 Dec 2019 13:07:59 -0800 (PST) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id v125sm4252515qka.47.2019.12.14.13.07.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Dec 2019 13:07:59 -0800 (PST) From: Arvind Sankar X-Google-Original-From: Arvind Sankar Date: Sat, 14 Dec 2019 16:07:57 -0500 To: Ard Biesheuvel Cc: Arvind Sankar , Ard Biesheuvel , Linux Kernel Mailing List , linux-efi , Hans de Goede , Matthew Garrett , Ingo Molnar , Andy Lutomirski , Thomas Gleixner Subject: Re: [PATCH 03/10] efi/libstub: use a helper to iterate over a EFI handle array Message-ID: <20191214210756.GE140998@rani.riverdale.lan> References: <20191214175735.22518-1-ardb@kernel.org> <20191214175735.22518-4-ardb@kernel.org> <20191214203257.GD140998@rani.riverdale.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Dec 14, 2019 at 08:40:57PM +0000, Ard Biesheuvel wrote: > 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) :) The idiom of the && with , operator is copied from for_each_bvec in bvec.h. Perhaps more readable with helper macros: #define __efi_handle_size (efi_is_64bit() ? sizeof(efi_handle_t) \ : sizeof(u32)) #define __efi_handle_elem(array, i) \ (efi_is_64bit() ? ((efi_handle_t *)(array))[i] \ : (efi_handle_t)(uintptr_t)((u32 *)(array))[i]) #define for_each_efi_handle(handle, array, size, i) \ for (i = 0; i < (size) / __efi_handle_size && \ (handle = __efi_handle_elem(array, i), 1); i++)