From patchwork Sat May 6 07:11:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 90686 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp900789vqo; Sat, 6 May 2023 00:12:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7bCgvwnn46LkTNQN5t7/Bpmd1uasUM9N3HMqEvGwacr+8OXN2oXy0oaAO2Vaw22PR7+6h2 X-Received: by 2002:a17:902:d507:b0:1ac:310d:872d with SMTP id b7-20020a170902d50700b001ac310d872dmr4754156plg.52.1683357129141; Sat, 06 May 2023 00:12:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683357129; cv=none; d=google.com; s=arc-20160816; b=czSUBA6uZXcrKb/6VB5BGTlA8P/5wBbw+7bej1ciajg+wcW3R44vJq+CZrazc25je1 u6cbRMg3c/q53GTEpgBU7k3AHbDsKRDYd32ADN9p8jY/5hdCIPaU8AeGD3wV/F6eg9c9 +ezkZr0OgeM3lMJnyEBv9aaDdQKWD/wN7IwSzJS5N0eSHm20yxaGzbTa2hbjCHNou7rM yWKioQfVD0qbMszmZjDO+Nh9NKx2QK6odt8V7NJh6JyOPaySDC4b7u6h74DJhR6kfEDl uRFO0wl29HWLOtIE4YXZxHJIZqy46jJ2O/nZM3oCA/zX2/7RFZU+DjYHDEk/tI7URaAW OkJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=LCwtFRx+YxY+EmtN+x8N93lmLUJnBu1GrjughYec7hk=; b=Uyu6EpZSRftn2kWcy7KwbAdvmNuwEqidGKBdLMv/29BR/2w4e9T5XKuJlCaPMKzP8Y uCGFC81nIUXytQgo3c5kVTa+Q7aOJT3LBvr0qwQURYxzV2JxJ2dAOHGw+jCSWnSLci9L BDBvMSfPYH79f25HqECaWP9Qg8b75xjmkDtp3AU03pgaBtsQnz7HtbcMulQ6x3FgRx+w 2XP9vTEtY1mJGNJPJ6rH0lhsS0LJq4EhbBt3j81ashqDh2OjhggG5lBWeoxVSe8ZpPzn GvgLq72bRSZwufMn5LrCqFCS+NcRHu8BMZ8nrXHA/VtQmocsjiH80bpipSuWHlD7BlKe GB5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=dPDRr9NJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id iw19-20020a170903045300b001a9b4bda723si3157150plb.421.2023.05.06.00.11.53; Sat, 06 May 2023 00:12:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=dPDRr9NJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230209AbjEFHLa (ORCPT + 99 others); Sat, 6 May 2023 03:11:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230132AbjEFHL2 (ORCPT ); Sat, 6 May 2023 03:11:28 -0400 Received: from smtp.smtpout.orange.fr (smtp-24.smtpout.orange.fr [80.12.242.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8F9E1156A for ; Sat, 6 May 2023 00:11:25 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id vC4bpnvtnGGqgvC4bpbPWL; Sat, 06 May 2023 09:11:23 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1683357083; bh=LCwtFRx+YxY+EmtN+x8N93lmLUJnBu1GrjughYec7hk=; h=From:To:Cc:Subject:Date; b=dPDRr9NJWUcumThQ3K8vmGoeFthoe40EmUuljc44kB6Db1/nur+r3j+irmyyDY6fJ FmKTLTebxollA+5WkjbG2hHYBh5hFwz3qxCz8BDkV87L2Wuu23WF7DKfRW1Ial90IE nbOfhNAHOwV+1ohcE2SbHWbX2vvLqqkeXTFS9yu0Rty0IZZgHs4RQpkMXHpBuPSZlQ EYiSdWajeq+wLrAAIe8ZbciQvBNHEWi02kTHh2743MgdarwyrNThgKMAXI8MQ5WFbx bpdXtTSUSISBEm98bDhfFG5cIyjrNgJIaKWxKHfQIhn1LYLohibYm2U8NrvfMspNZN gChnjeggQqtTw== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sat, 06 May 2023 09:11:23 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET To: Bjorn Helgaas , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , "Maciej W. Rozycki" Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , linux-pci@vger.kernel.org Subject: [PATCH 1/2] x86/PCI: Fix a sanity check in pirq_convert_irt_table() Date: Sat, 6 May 2023 09:11:09 +0200 Message-Id: X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765127884842915058?= X-GMAIL-MSGID: =?utf-8?q?1765127884842915058?= We compare the size in bytes of a struct (and its ending flexible array) with the number of elements in a flexible array. This is wrong and "ir->size < ir->used" is likely to be always false. Compute the number of possible entries instead, as already done in other places, and compare it to the number of used entries. Fixes: b584db0c84db ("x86/PCI: Add $IRT PIRQ routing table support") Signed-off-by: Christophe JAILLET --- /!\ This patch is speculative. Review with care /!\ I'm not sure that this sanity check is needed at all. If 'used' was the size of the flexible array, I think it would simplify the code in other places. It will also help scripts when __counted_by macro will be added. See [1]. [1]: https://lore.kernel.org/all/6453f739.170a0220.62695.7785@mx.google.com/ --- arch/x86/pci/irq.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/x86/pci/irq.c b/arch/x86/pci/irq.c index a498b847d740..e29b487cc069 100644 --- a/arch/x86/pci/irq.c +++ b/arch/x86/pci/irq.c @@ -128,12 +128,16 @@ static inline struct irq_routing_table *pirq_convert_irt_table(u8 *addr, { struct irt_routing_table *ir; struct irq_routing_table *rt; + int entries; u16 size; u8 sum; int i; ir = (struct irt_routing_table *)addr; - if (ir->signature != IRT_SIGNATURE || !ir->used || ir->size < ir->used) + entries = (ir->size - sizeof(struct irq_routing_table)) / + sizeof(struct irq_info); + + if (ir->signature != IRT_SIGNATURE || !ir->used || entries < ir->used) return NULL; size = sizeof(*ir) + ir->used * sizeof(ir->slots[0]); From patchwork Sat May 6 07:11:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 90687 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp906514vqo; Sat, 6 May 2023 00:29:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ70VCh4Op+AmXxTx3GlnimoHAd0BEr/3DmrcY+lISun1Ytw9rFM+qHl3aE43+BjkaPfx6uA X-Received: by 2002:a05:6a20:4429:b0:f2:ea8e:b130 with SMTP id ce41-20020a056a20442900b000f2ea8eb130mr1226335pzb.62.1683358166703; Sat, 06 May 2023 00:29:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683358166; cv=none; d=google.com; s=arc-20160816; b=HfwsRafqq9racQyJ+yP9cI09W/I1OteLNkocDrBrvdejfl4SZDhaoCiLZBy8chHI3h vP2PWsSnDBbgdrjt4W03namLgvnsaDrBviC9pglsGIwQuSIc8JP9S40WFIbWdoa5LkQf Albp/SrPV4Tvc6zbNDFvfYYu9bODzfZ8vZSAHtJKKR59eks+cwL5X59R6uBs20L3Ga4v U6fQ3hQ5kXSGQT5jzu9U2MR1TXIvLILCe/gvNCUBSU0vZnM71BSpIbrKbUK1GRFAo3wf tGqpHQdFDrTNCQUyNIcYqH4f7bhFqRiO54R2bNnyzWMRtw8VGEizrYOwfoKR1pDm0l6D bAFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hARn2U5Ck8gsaGekp7/R1Pqh66Ft8Q75GW6ULFlZglk=; b=SPRRsPHGVPpMTEa+rlcYs13NOzYsyqSI/71vGc+9FneBddhcG6UG1eaI5UyuSac/z4 DXLqElWXuRiivPpfdOYsrElG2gTiW3KuX9K4Jl4il3J4jBZkZ/voiSamJ7sddNEdFktX M76mOL9WL84WFGKJTTpmljT6Q1vArTO6wbCR1AA5SszRjPauPL5pNWQjkkEdPEOkLYAl nP1/4801mv2rfQWau9FSsE3MEuTniblRvUpJpG+anpVjnGranhtqFWCXr3cxTSTkGjIN RM+KxpGrbe2jSrQyAv8KrhtlkS1M+eANWSke+x53A3esd2L8Gthh9hj1gLA306nz66DP LUUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=jCzkfsS+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h123-20020a636c81000000b005289d0633d6si3588716pgc.498.2023.05.06.00.29.11; Sat, 06 May 2023 00:29:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=jCzkfsS+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230237AbjEFHLd (ORCPT + 99 others); Sat, 6 May 2023 03:11:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230136AbjEFHL3 (ORCPT ); Sat, 6 May 2023 03:11:29 -0400 Received: from smtp.smtpout.orange.fr (smtp-24.smtpout.orange.fr [80.12.242.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9908C11577 for ; Sat, 6 May 2023 00:11:28 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id vC4bpnvtnGGqgvC4kpbPX0; Sat, 06 May 2023 09:11:27 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1683357087; bh=hARn2U5Ck8gsaGekp7/R1Pqh66Ft8Q75GW6ULFlZglk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=jCzkfsS+JENGKpeRh4/VY6mz+f9xHy5jKatmWS04zuwGUv9WRlBqrvl/zlOZJYp7M zNnKHEUPsQopJd4vvX/Tn9tSHcr4HqzeDvGHRbRL8PpMx/HlhZNryCERxcM3pzfCtb mZxm19jDJyvK9C26UCXEW8nVgKEk+ve1wU4nx6Wgr7ezd6QzZ5NreCKmzrU/adE8qk Q3ylq+2ffYcHYFLTM7N8HGkQEqfFVU5dZJgdXym7MKnf7wgr5dWBj/+Bfh1wYC5ua+ 2d0MmoIyRMD4Z0GleiADFS++dIWnjc3g41YjJvVdv/yCKrrucTLC4ScWjbj4DEqPsR xZrPrPGglryxQ== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sat, 06 May 2023 09:11:27 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET To: Bjorn Helgaas , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , linux-pci@vger.kernel.org Subject: [PATCH 2/2] x86/PCI: Slightly simplify pirq_convert_irt_table() Date: Sat, 6 May 2023 09:11:10 +0200 Message-Id: <84bf047b01452a72642dbe355b02ef016c985a91.1683356951.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765128972903367656?= X-GMAIL-MSGID: =?utf-8?q?1765128972903367656?= 'size' if computed twice. *ir and *it being the same, the result is the same as well. While at it, also use struct_size() which is less verbose, more robust and more informative. Signed-off-by: Christophe JAILLET --- arch/x86/pci/irq.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/pci/irq.c b/arch/x86/pci/irq.c index e29b487cc069..6bc51f80eec2 100644 --- a/arch/x86/pci/irq.c +++ b/arch/x86/pci/irq.c @@ -140,14 +140,13 @@ static inline struct irq_routing_table *pirq_convert_irt_table(u8 *addr, if (ir->signature != IRT_SIGNATURE || !ir->used || entries < ir->used) return NULL; - size = sizeof(*ir) + ir->used * sizeof(ir->slots[0]); + size = struct_size(ir, slots, ir->used); if (size > limit - addr) return NULL; DBG(KERN_DEBUG "PCI: $IRT Interrupt Routing Table found at 0x%lx\n", __pa(ir)); - size = sizeof(*rt) + ir->used * sizeof(rt->slots[0]); rt = kzalloc(size, GFP_KERNEL); if (!rt) return NULL;