Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2472304ybl; Sat, 14 Dec 2019 13:05:17 -0800 (PST) X-Google-Smtp-Source: APXvYqzXjLfvaw5hVfQsazJ8SRTM3VQUkpMJZwADyEToc86TnS9++2FsT77/UTecNb24mxC+dEqN X-Received: by 2002:a54:4485:: with SMTP id v5mr8145754oiv.144.1576357517882; Sat, 14 Dec 2019 13:05:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576357517; cv=none; d=google.com; s=arc-20160816; b=eCRNs2feprukWSEXDFmyr/leq9YJHv0N7FpL/mjzSu7e1Nnf0sNF/gRMEFbypFA/vJ KwtJL8T+wkOEZJhI/RxiPsTcNPdSeBPJoWGxqbeRGJQAYp3bTyYmhW4Mz3FzMJPRLy+p YOEPAgQDY+vxqByTtdziqkopKCHP8ZvG6Nk7R6VEUDOeLIBd2/H2PbbFxhTGIZYSQkzX kU7Dpjdsl25+uCkKFG3gX7/TqU6XY3jiJgTsR1uzYs9L7gWG287M45FiFwrdomJd0swr 2RhqbmIbLwOW+O8MtZcyUhGqOUprx6d9mkm0eomZN/DkJKWvrPLkoWoDMA2xact/x1lw O+EQ== 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=5wnKTPr8/3r2YII52ChVOnxqx80JfjbUnrxVzdXCZKo=; b=aM2YPgoISSGQrjDfCJKeXHIYje7t/W7kdKXUUExs8w9hzWchzaDKdfAZRB/TOv7wvO jouMvEY9sG3tzyJUsS1/keu4bbIDvTPUl3zeTMDHPc8OA8J1iC+gW7hQujx6UXEMxUQB TTvasOd192zroxz0yEHVDOXUFbU1F7h2j+nX3Ye3Qdx23FlDcJmKb+Q+/cKT8GL/LIbQ /UBiZS1g5vytu+R8K25lyn2G0fifFAOzKgAQHbeR6Bk4SXbLRGzLYG8AIBDVN6QUJ75T cTL0p4tGal1GGmdnuLPFts/QslZ+3m0GLFkEj2FJf+9D6HYKQ5c7x1FngDXCbu/Xiade 8rhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="FlyBr3/0"; 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 w131si7215477oie.47.2019.12.14.13.05.04; Sat, 14 Dec 2019 13:05:17 -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="FlyBr3/0"; 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 S1727006AbfLNVEO (ORCPT + 99 others); Sat, 14 Dec 2019 16:04:14 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:37149 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726907AbfLNVEN (ORCPT ); Sat, 14 Dec 2019 16:04:13 -0500 Received: by mail-wr1-f68.google.com with SMTP id w15so2613753wru.4 for ; Sat, 14 Dec 2019 13:04:12 -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=5wnKTPr8/3r2YII52ChVOnxqx80JfjbUnrxVzdXCZKo=; b=FlyBr3/0YeOz6HxsMr93QGnPD1w8kqOok1nJA0Dmnyt3d9CFyynf6P73phyvsB4hxd 8ItZ3oryn7X7ExW415Av8R+8Mu2KsXnkfF9la+2bTfsj2UJ23izzcFZL8/iQCck3QST0 PimHnixHc5WRSdxALLRfUkELA6wV46XKQvZ4TTGiRBfdnwZ495w550nAtNeYAbn7xMvN At5ScgnTN5n8htA1RshUQHB0zMVabsA0LLQrppQ63s2k5eAK2nVia7jy9UWVlvRAeV74 +mlMz/22GIuBYKLfhB+mV6oUpvpYK0cRwyasbJdzbvUlkMUN+Gykge7QkUPF+vcf/Jxk nNYg== 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=5wnKTPr8/3r2YII52ChVOnxqx80JfjbUnrxVzdXCZKo=; b=VZs+fuRHiXWL/gZ6fJHIG6pyr93sdAWXb7jAozTcxfJVxceo8BV1QqVpkw8mCLHUP1 00hpNAuIRl4ub6eKdd2zOZ6//2ktzf0LWv3Vv9whdPgOJhb25UWCTmYKR8b/5dvUyeJE 6OmOLnPgxb0f5FJqFmWk4qhc3Eps9vadlXf0+aKb+tc/qJADWOioSe3F2FCGmiIUqKHj 0gnmXdf4RPjeEA6DyBS4XOW8X94S9UBwqfZeOb/PKSJQRKBZB0ZrL8GXoBN31lUwnIRt 8o3q7jOLV+X1epxS46lp5UBLmZdWDLNRrvf9GhAUIx406DPJlFtMQjmplD+xzAE6STtx jXIw== X-Gm-Message-State: APjAAAXGmsInEZu4OdfHk0nq5JCc7rhxDsz7+rGYULtkEj/Ax5fCg/VV HYpRTJUHZk9ExYFWoFxk5+i6YLjXo/Ww2G+jYyrqTQ== X-Received: by 2002:a5d:46c1:: with SMTP id g1mr20198121wrs.200.1576357451503; Sat, 14 Dec 2019 13:04:11 -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: From: Ard Biesheuvel Date: Sat, 14 Dec 2019 21:04:10 +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: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++)