Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8594FC47253 for ; Fri, 1 May 2020 12:31:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 68CBA208D6 for ; Fri, 1 May 2020 12:31:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728746AbgEAMbm (ORCPT ); Fri, 1 May 2020 08:31:42 -0400 Received: from foss.arm.com ([217.140.110.172]:39474 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728742AbgEAMbm (ORCPT ); Fri, 1 May 2020 08:31:42 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D84D71FB; Fri, 1 May 2020 05:31:41 -0700 (PDT) Received: from [10.57.39.240] (unknown [10.57.39.240]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 74A123F305; Fri, 1 May 2020 05:31:40 -0700 (PDT) Subject: Re: [PATCH RFC 2/2] ACPI/IORT: work around num_ids ambiguity To: Ard Biesheuvel Cc: Linux ARM , ACPI Devel Maling List , Hanjun Guo , Lorenzo Pieralisi , Pankaj Bansal , Will Deacon , Sudeep Holla , Catalin Marinas References: <20200501095843.25401-1-ardb@kernel.org> <20200501095843.25401-3-ardb@kernel.org> From: Robin Murphy Message-ID: <18e01ac7-974e-8308-c18c-67aa3fd7ad4e@arm.com> Date: Fri, 1 May 2020 13:31:38 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org On 2020-05-01 12:41 pm, Ard Biesheuvel wrote: > On Fri, 1 May 2020 at 12:55, Robin Murphy wrote: >> >> On 2020-05-01 10:58 am, Ard Biesheuvel wrote: >>> The ID mapping table structure of the IORT table describes the size of >>> a range using a num_ids field carrying the number of IDs in the region >>> minus one. This has been misinterpreted in the past in the parsing code, >>> and firmware is known to have shipped where this results in an ambiguity, >>> where regions that should be adjacent have an overlap of one value. >>> >>> So let's work around this by detecting this case specifically: when >>> resolving an ID translation, allow one that matches right at the end of >>> a multi-ID region to be superseded by a subsequent one. >>> >>> Signed-off-by: Ard Biesheuvel >>> --- >>> drivers/acpi/arm64/iort.c | 23 +++++++++++++++----- >>> 1 file changed, 18 insertions(+), 5 deletions(-) >>> >>> diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c >>> index 98be18266a73..d826dd9dc4c5 100644 >>> --- a/drivers/acpi/arm64/iort.c >>> +++ b/drivers/acpi/arm64/iort.c >>> @@ -316,10 +316,19 @@ static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in, >>> } >>> >>> if (rid_in < map->input_base || >>> - (rid_in >= map->input_base + map->id_count)) >>> + (rid_in > map->input_base + map->id_count)) >>> return -ENXIO; >>> >>> *rid_out = map->output_base + (rid_in - map->input_base); >>> + >>> + /* >>> + * Due to confusion regarding the meaning of the id_count field (which >>> + * carries the number of IDs *minus 1*), we may have to disregard this >>> + * match if it is at the end of the range, and overlaps with the start >>> + * of another one. >>> + */ >>> + if (map->id_count > 0 && rid_in == map->input_base + map->id_count) >>> + return -EAGAIN; >>> return 0; >>> } >>> >>> @@ -404,7 +413,8 @@ static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node, >>> /* Parse the ID mapping tree to find specified node type */ >>> while (node) { >>> struct acpi_iort_id_mapping *map; >>> - int i, index; >>> + int i, index, rc = 0; >>> + u32 out_ref = 0, map_id = id; >>> >>> if (IORT_TYPE_MASK(node->type) & type_mask) { >>> if (id_out) >>> @@ -438,15 +448,18 @@ static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node, >>> if (i == index) >>> continue; >>> >>> - if (!iort_id_map(map, node->type, id, &id)) >>> + rc = iort_id_map(map, node->type, map_id, &id); >>> + if (!rc) >>> break; >> >> This needs a big FW_BUG splat in the case where it did find an overlap. > > Sure, although we did help create the problem in the first place. > >> Ideally we'd also enforce that the other half of must be the first entry >> of another range, but perhaps we're into diminishing returns by that point. >> > > That would mean the regions overlap regardless of whether you > interpret num_ids correctly or not, which means we'll be doing > validation of general well-formedness of the table rather than > providing a workaround for this particular issue. The point was to limit any change in behaviour to the specific case that we need to work around. Otherwise a table that was entirely malformed rather than just off-by-one on the sizes might go from happening-to-work to not working, or vice versa; the diminishing return is in how much we care about that. > I think the fact that we got it wrong initially justifies treating the > off-by-one case specially, but beyond that, we should make it robust > without being pedantic imo. As the #1 search engine hit for "Linux is not a firmware validation suite", I can reassure you that we're on the same page in that regard ;) >> If we silently fix things up, then people will continue to write broken >> tables without even realising, new OSes will have to implement the same >> mechanism because vendors will have little interest in changing things >> that have worked "correctly" with Linux for years, and we've effectively >> achieved a de-facto redefinition of the spec. Making our end of the >> interface robust is obviously desirable, but there still needs to be >> *some* incentive for the folks on the other end to get it right. >> > > Agreed. But at least we'll be able to detect it and flag it in the > general case, rather than having a special case for D05/06 only > (although I suppose splitting the output ranges like those platforms > do is rather unusual) Yup, in principle the fixed quirk list gives a nice reassuring sense of "we'll work around these early platforms and everyone from now on will get it right", but whether reality plays out that way is another matter entirely... Robin.