From patchwork Tue Jan 31 10:31:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geert Uytterhoeven X-Patchwork-Id: 50772 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2677481wrn; Tue, 31 Jan 2023 02:44:35 -0800 (PST) X-Google-Smtp-Source: AK7set9Qm/iYBesQ8vTBES6wKgaXk2QY9qHmuSoUBG/yfc35/OoU9NvMQtBqFpeJon2BSydpHh+O X-Received: by 2002:a17:907:8b95:b0:887:7871:2b2f with SMTP id tb21-20020a1709078b9500b0088778712b2fmr8712072ejc.61.1675161875394; Tue, 31 Jan 2023 02:44:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675161875; cv=none; d=google.com; s=arc-20160816; b=xt8O5SoowwNuzo9P9tiE2O9YO/rXsZYfF4B4Dofx6uT+IWU6N6uwFWeY7nX7zk/MWr b7uVaP1V13BsW0q/b2USCTeURDB+n/1pebOG9tvquUVNf+e+V8xsureNTdwLsofceEhC rsD98yVIbw2/xgAuGYxSDSupuily35hnI/2yeONPVQbaLFNCLpZB4UD6w13Wpw8IyR5B vghzMPSaanTq4BWe7H0u8kNRxUoLTUCQpWR0AVDxmc7FPn491K+4jGY7jc5LSjC31XcH A4Fhfl+zxMannewCbTF5N5ScFvpYl47Cv4rto5hAAJ4G2Rc8foH5AiepvWest/sTxS+s oR3Q== 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=n00VL1EsVJIayKzRCOyazD8EdVEmFurkroejJ97hz8w=; b=z3FDioHLp/krsUQKr6VxjtFlFgHEBnzhY/iIXNqb54OdGRDNvd5ptZGS0KAXmja1bE 8/CZI5f28luP4VtYh/uV8pOlYyle95MXOJtGDXcIQZpVSsfwqaTr/m78q60fMb4vES4L kPcFv1amPjITyFTes7cGa31tPiHlqLOw5Vms0u1nL//YiIZhZxlbuzY5eL1FZvw59/HH VTC/1upTIcF0Kbk21Ga6MuCDltGHHt/LtRpy6uL2drH9w99XNk+up2GIX1gwsfEz5nc2 br+NrFmQ0DWbpEwsx+g6r/P1jzl0c+iwBaNdxiINeX2lhLDIdebUQJJQ8YxTCCRoQXRB j0OA== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 19-20020a17090602d300b008725c668f2asi17671355ejk.590.2023.01.31.02.44.11; Tue, 31 Jan 2023 02:44:35 -0800 (PST) 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 S230484AbjAaKbq (ORCPT + 99 others); Tue, 31 Jan 2023 05:31:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230215AbjAaKbn (ORCPT ); Tue, 31 Jan 2023 05:31:43 -0500 Received: from laurent.telenet-ops.be (laurent.telenet-ops.be [IPv6:2a02:1800:110:4::f00:19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00956392B4 for ; Tue, 31 Jan 2023 02:31:41 -0800 (PST) Received: from ramsan.of.borg ([IPv6:2a02:1810:ac12:ed50:fd54:3eff:f16a:8c82]) by laurent.telenet-ops.be with bizsmtp id FNXf2900b3oGUMV01NXf6P; Tue, 31 Jan 2023 11:31:40 +0100 Received: from rox.of.borg ([192.168.97.57]) by ramsan.of.borg with esmtp (Exim 4.95) (envelope-from ) id 1pMnvB-007vjk-Pw; Tue, 31 Jan 2023 11:31:39 +0100 Received: from geert by rox.of.borg with local (Exim 4.95) (envelope-from ) id 1pMnvP-000NWt-NJ; Tue, 31 Jan 2023 11:31:39 +0100 From: Geert Uytterhoeven To: Bjorn Helgaas , Kai-Heng Feng Cc: Niklas Schnelle , Johannes Berg , linux-pci@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-kernel@vger.kernel.org, Geert Uytterhoeven Subject: [PATCH v2 resend] PCI: Fix dropping valid root bus resources with .end = zero Date: Tue, 31 Jan 2023 11:31:36 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE 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?1756534538246576120?= X-GMAIL-MSGID: =?utf-8?q?1756534538246576120?= 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 Tested-by: Niklas Schnelle Acked-by: Kai-Heng Feng --- 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/ v2: - Add Tested-by, Acked-by. --- 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 1779582fb5007cd1..5988584825482e9f 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -996,7 +996,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);