Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2481647ybl; Sat, 14 Dec 2019 13:13:50 -0800 (PST) X-Google-Smtp-Source: APXvYqy4Fy1r0VRCxXqKfcqw8SSlVxj8Z+mnTQL728BU3y0E0rZCTydxgI/NrTPOCRtBFr4eMDc1 X-Received: by 2002:a9d:7d8e:: with SMTP id j14mr21552791otn.227.1576358030182; Sat, 14 Dec 2019 13:13:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576358030; cv=none; d=google.com; s=arc-20160816; b=kIKksoqd0YsBlmz1paNIo7HtJJ8dv/6Vy1m+n76LDUIFFVDUgSBA/tA50ZMTnWU6uK Ka7kjAG8tiIxT8IH3YnvJyn1Yv6/g9pg1lyblQ/kAlnF2kCWA5+eY+/hCuU3gFDMD34x kZZ7H+ndpHyME0sXymp+svm8SsRy5/NHb8ix9IVljhi3qXnouF7gCoH6Jco2YNhCHo/s zt+ToyL/4poyQ2jiZDwQjQfGySgTGO1y2e+BXP6B7QxCt9VfgHloTX4DHsIyOan7yroe XdTf0zPfpw3qq8uWaUTXMJruiyKHFPFvBiIpu3zYmGfPfCymQx1Ar9pciZ6Ru1LmNvjG LfGw== 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=LDgrFmqwBbruyvbEPNvS6lroKGpuFewbY+qcvevRshA=; b=GygBsflRDwpp9xwO8LAIV5nXRom8jbIV/2LA5S8CCQb04AeT/goZ2lliLWnzO685UF FYlC6wAGbZJU0NyiL54IanP0G9UFWPG7/kU87qvOzMk057ql11KzIw2BxLmqO1IwaVA9 IcxmFIYYqSO8S7bG8aZlcmDJvTdGVCZ+jtcIXDvL8BhEI+eIi3haGoXsydaGXMnik82Z SGh9CtMqz8oW9oXFk1fVsyL1wwQhYY0XcZv+nq/VC/G3BEzo5OBCnhSz0nldl2d2Siw4 p5HvbCCtjtRSa2ivC18BwI7BOzwyPGhVu+O+PoX+7hkZGJiQAO1UVL6ca5YIv+f+XoEo CIaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=BPscf8un; 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 m4si7425909otq.42.2019.12.14.13.13.38; Sat, 14 Dec 2019 13:13:50 -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=BPscf8un; 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 S1726994AbfLNVLx (ORCPT + 99 others); Sat, 14 Dec 2019 16:11:53 -0500 Received: from mail-qk1-f193.google.com ([209.85.222.193]:33480 "EHLO mail-qk1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726781AbfLNVLx (ORCPT ); Sat, 14 Dec 2019 16:11:53 -0500 Received: by mail-qk1-f193.google.com with SMTP id d71so320022qkc.0; Sat, 14 Dec 2019 13:11:52 -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=LDgrFmqwBbruyvbEPNvS6lroKGpuFewbY+qcvevRshA=; b=BPscf8unKDMk9/9oIHbHvBOJKH0tk1PilMYY7IWC8e/wmylNMy3jw8l7hP79ws3vtr VjpOBVUFczhc3wlpZHibWn6UZKF6l5d+zkQGhAglLdT9V/vFOm29GlQ3ZtnmBXMybXLT lExwL7h2RWGoe2SlsvYYTI1PaZ278DFREdiUXNAhfP4flqPvq7LMRbuA8k5qCCpOrtEq tYgR62CSqBxxs+M1aonbZwu5TRErIdMgBznyFNmgouhVFcUiqlQxr13pZLdm3C4V63h5 yhczXyRKJod8Ph7FQlPSQI54rrQ1tQj2GtQx7ogjUGd7O60TgjH8ZFcDH/2wAmxJJlWN 3nIw== 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=LDgrFmqwBbruyvbEPNvS6lroKGpuFewbY+qcvevRshA=; b=mNl4qcISM6goAfXKteR5uxXF0n9DNYfNyR1MF50WlZvazf8Bjj7nkD+7T+AK7D3mlM VymYas9e+XTv2u+KBLuDsg5XFfIWjORtfe0i1rR3UDmVqDGwf9BAbjNYiVgfJ7ZPbLz8 N23tw+QEjLaaJ0Bl9ZTeJNWEP1j/ChffqlXZjYUOYeuBQHYzoXAZYuSQrHRqpHT3XOoP NNNMnAtdHKJgVJmZfyGmZ26dRMUP0biVK34VlQMm6YJaelaLpZZiw9+nTv1wKxa8h483 mDKpKIApsYiMpmaJBi9Lbvi2saJvGJNSU/xkGbSwpXu+UpjrJQJdfGnKm0AYrhefTdJV /LlQ== X-Gm-Message-State: APjAAAXsVpf+V+aCMUEu49xSSWbsfwFMvMjcNKxaHgReoDBNFAKfuDB+ 4/WMLeIU5qSiXSI3Sm6MeZS6A6rZkTo= X-Received: by 2002:a05:620a:1114:: with SMTP id o20mr19709799qkk.128.1576357911445; Sat, 14 Dec 2019 13:11:51 -0800 (PST) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id n190sm4279637qke.90.2019.12.14.13.11.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Dec 2019 13:11:51 -0800 (PST) From: Arvind Sankar X-Google-Original-From: Arvind Sankar Date: Sat, 14 Dec 2019 16:11:49 -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: <20191214211149.GF140998@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 09:04:10PM +0000, Ard Biesheuvel wrote: > On Sat, 14 Dec 2019 at 21:40, 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) > > How about > > #define efi_get_handle_at(array, idx) \ > (efi_is_64bit() ? (efi_handle_t)(unsigned long)((u64 *)(array))[idx] \ > : (efi_handle_t)(unsigned long)((u32 *)(array))[i]) > > > #define efi_get_handle_num(size) \ > ((size) / (efi_is_64bit() ? sizeof(u64) : sizeof(u32))) > > #define for_each_efi_handle(handle, array, size, i) \ > for (i = 0; \ > i < efi_get_handle_num(size) && \ > ((handle = efi_get_handle_at((array), i)) || true); \ > i++) Heh, I came up with almost the same thing, but yours is slightly better, You have a typo in efi_get_handle_at (i instead of idx in the second line).