Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp513109imw; Wed, 13 Jul 2022 02:55:47 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vmTj2vhw/mrmv8jIXH2uotkwdm7C7pstyt+e/WaDMJIh2SLWj3rekAuwP4TWVZHgAQNAHG X-Received: by 2002:a17:903:2cf:b0:151:a932:f1f0 with SMTP id s15-20020a17090302cf00b00151a932f1f0mr2452876plk.130.1657706147767; Wed, 13 Jul 2022 02:55:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657706147; cv=none; d=google.com; s=arc-20160816; b=l+49Yp8g5Dc7MaCQbCRZsonhXXc97EsEMcvW/Y7e1lvI9//eVndr02te3O1rsFkOPt UmQHBbu4p7qylOIyr/gjqBpq4X8jQCzMMnfv0ilwR+0t3nIFYWLMeYo/MacaPVV5KeCW Y//iOorkXnWq9TREHm1gflESA3DEfSr5MsdZ6I8qSHWqJZz2VIZhOFYTDBPeNiortEL3 PVNwnC2i6fj1AEgwVPtJvzlwYcfM4GUD3gaK5XzO6O1w78mcGVEBSdFt2yy3ISs58ZYX h6u2g/n8aFFus+c9u03PIx8lJim3UQKFXeSHK+IMnlVmoYnoQKC73ft8MglZcgA8vMwb V64A== 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; bh=ZUIejn6RPuhB6OMQ8OnEgvoPxzraklGHQMCN+NfI774=; b=pu9uQTMU4ALI8mB9tAr8eMkW5Lsmoq4cOjMFYWTMHG3t/cHYmcEqOOSk8pcow7VEwk HOhrAeymUbCQTwncILRCaTZLq/S05rWFRnAJqHjcECZDmakQmzIigxPIRqUDUU3xLdAX Kp69sLKUoGeXm11xVLQIWgZLAdW28orsKkIJgkeTjgLlgZkPWKePTTfbOykLItBCDoDb nX13uavN2NR4n4xdcoE497HW4n0zHDO0cvFMrSRgeLmX41MUge6q5hcCjQq5NAOBL0a6 8nKKEAXQJ7RHi43rnVn8948iFP8zFk+HJebtCOFORl3gXVQc3Tir5/H9EpbReSXd47DU j7oQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d14-20020a656b8e000000b00415c679f6dcsi16953223pgw.180.2022.07.13.02.55.34; Wed, 13 Jul 2022 02:55:47 -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; 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 S236113AbiGMJgY (ORCPT + 99 others); Wed, 13 Jul 2022 05:36:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236156AbiGMJgB (ORCPT ); Wed, 13 Jul 2022 05:36:01 -0400 Received: from xavier.telenet-ops.be (xavier.telenet-ops.be [IPv6:2a02:1800:120:4::f00:14]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D568E222AC for ; Wed, 13 Jul 2022 02:35:56 -0700 (PDT) Received: from ramsan.of.borg ([84.195.186.194]) by xavier.telenet-ops.be with bizsmtp id uZbt2700R4C55Sk01Zbtd4; Wed, 13 Jul 2022 11:35:54 +0200 Received: from rox.of.borg ([192.168.97.57]) by ramsan.of.borg with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1oBYmf-003LRy-7N; Wed, 13 Jul 2022 11:35:53 +0200 Received: from geert by rox.of.borg with local (Exim 4.93) (envelope-from ) id 1oBYme-00B78G-Ol; Wed, 13 Jul 2022 11:35:52 +0200 From: Geert Uytterhoeven To: Bjorn Helgaas , Kai-Heng Feng Cc: Johannes Berg , Rob Herring , Thierry Reding , linux-pci@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-kernel@vger.kernel.org, Geert Uytterhoeven Subject: [PATCH] PCI: Fix dropping valid root bus resources with .end = zero Date: Wed, 13 Jul 2022 11:35:50 +0200 Message-Id: <9c41a4372b27420c732ff5599d823e363de00c6d.1657704829.git.geert+renesas@glider.be> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE, 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 On r8a7791/koelsch: kmemleak: 1 new suspected memory leaks (see /sys/kernel/debug/kmemleak) # cat /sys/kernel/debug/kmemleak unreferenced object 0xc3a34e00 (size 64): comm "swapper/0", pid 1, jiffies 4294937460 (age 199.080s) hex dump (first 32 bytes): b4 5d 81 f0 b4 5d 81 f0 c0 b0 a2 c3 00 00 00 00 .]...].......... 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [] __kmalloc+0xf0/0x140 [<34bd6bc0>] resource_list_create_entry+0x18/0x38 [<767046bc>] pci_add_resource_offset+0x20/0x68 [] devm_of_pci_get_host_bridge_resources.constprop.0+0xb0/0x390 When coalescing two resources for a contiguous aperture, the first resource is enlarged to cover the full contiguous range, while the second resource is marked invalid. This invalidation is done by clearing the flags, start, and end members. When adding the initial resources to the bus later, invalid resources are skipped. Unfortunately, the check for an invalid resource considers only the end member, causing false positives. E.g. on r8a7791/koelsch, root bus resource 0 ("bus 00") is skipped, and no longer registered with pci_bus_insert_busn_res() (causing the memory leak), nor printed: pci-rcar-gen2 ee090000.pci: host bridge /soc/pci@ee090000 ranges: pci-rcar-gen2 ee090000.pci: MEM 0x00ee080000..0x00ee08ffff -> 0x00ee080000 pci-rcar-gen2 ee090000.pci: PCI: revision 11 pci-rcar-gen2 ee090000.pci: PCI host bridge to bus 0000:00 -pci_bus 0000:00: root bus resource [bus 00] pci_bus 0000:00: root bus resource [mem 0xee080000-0xee08ffff] Fix this by only skipping resources where all of the flags, start, and end members are zero. Fixes: 7c3855c423b17f6c ("PCI: Coalesce host bridge contiguous apertures") Signed-off-by: Geert Uytterhoeven --- Is there any side effect of not registering the root bus resource with pci_bus_insert_busn_res()? This is the resource created by of_pci_parse_bus_range(), and thus affects any DT platforms using "bus-range = <0 0>". Perhaps checking for "!res->flags" would be sufficient? I assume this still causes memory leaks on systems where resources are coalesced, as the second resource of a contiguous aperture is no longer referenced? Perhaps instead of clearing the resource, it should be removed from the list (and freed? is it actually safe to do that?)? Apparently Johannes had identified the bug before, but didn't realize the full impact... https://lore.kernel.org/r/5331e942ff28bb191d62bb403b03ceb7d750856c.camel@sipsolutions.net/ --- drivers/pci/probe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c index 17a969942d37033a..be628798d279ada0 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -994,7 +994,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge) resource_list_for_each_entry_safe(window, n, &resources) { offset = window->offset; res = window->res; - if (!res->end) + if (!res->flags && !res->start && !res->end) continue; list_move_tail(&window->node, &bridge->windows); -- 2.25.1