Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1103620rwb; Sun, 6 Nov 2022 19:32:06 -0800 (PST) X-Google-Smtp-Source: AMsMyM5qbo2PL18NDjNBSA2+zCzPCYYNUsLiOShXJFegqqI9tVX6IMCP4In9zsEm0MCu6m06Yw6+ X-Received: by 2002:a05:6402:194d:b0:461:8982:52b8 with SMTP id f13-20020a056402194d00b00461898252b8mr48090811edz.49.1667791926647; Sun, 06 Nov 2022 19:32:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667791926; cv=none; d=google.com; s=arc-20160816; b=BqsQhFwStC5IxvePyRasgwvvfIiPj7h8bFJZyNaZtCZMblgEXc1+eRFttxceVGHF6c z8qT1rVH7VK/BAiAEWgp58zjbsA9ZrJaqgNdvayWoj08FbKHAClNtt/BEP0VhPpuwQZD /xUc12uY74Aum4RJqu2ahjsJEdpl4GLgSB3ujeXmmEU7A4LkJsKCnX1T2Qrx7BX27o2L wPsTIOE5NleNq7soepRzcHqPPUYTWOGqOEGFy7wSwOHFxhrrJWlpKClBx4gGQn3vWxzU BiQvMI8duxJgtCrcT7CucboFvSiGfpHPhMf52pRTY/V9w1cVQszuiAVIg0H8vVakvy9q BG4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=lknP2X8kYl/GPxh/Z2bYVUBQv2fIBaXao2E98IBeW00=; b=ngGo1ThJn8Xj8ci6Bm9EBg2u5FkMQ/F8vPoAoZHuRVWbzzle46FSQDLXboHMh3C0Bo /FFOR4N8CougpaL5T9hu3ZKu+Qo/jnzr0IB+eGCrohDspEA/SqZh8zaK446oyTJdPLbB xbc2WWay/e6Xdi0F1lGMjl49LaOUe1tz8P4DbH6oW7yZSMtmSsxRM9azG/WwEXNQ23+6 oNWAJ+MD9KIVkVVNBjZvsaXGLwou6fdhQRDjmx5A0dp4gFepTmi0vYbrgbtJ7YnMCh/i to3wd96VihG9ZxMlvfeRlHGIEOK7l4wY+p2naITpHna0hdjnFrGcyQLPtjhzHcdeyZrU gV5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=I7uDsJ5m; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n17-20020a1709065e1100b00787bacce740si6753861eju.537.2022.11.06.19.31.43; Sun, 06 Nov 2022 19:32:06 -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; dkim=pass header.i=@canonical.com header.s=20210705 header.b=I7uDsJ5m; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230363AbiKGDHN (ORCPT + 96 others); Sun, 6 Nov 2022 22:07:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230366AbiKGDHK (ORCPT ); Sun, 6 Nov 2022 22:07:10 -0500 Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8EF3DFB3 for ; Sun, 6 Nov 2022 19:07:07 -0800 (PST) Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id E0762423EF for ; Mon, 7 Nov 2022 03:07:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1667790425; bh=lknP2X8kYl/GPxh/Z2bYVUBQv2fIBaXao2E98IBeW00=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=I7uDsJ5mZPapEZT6GLcnZ46OuPNneA/F3yLBaqrBXwCtQJX96DrahrGVy4vfu+42t w/0Kg7OP4DaWWImLVNTg5uRIa8i55qpso6C3Twg0XRNXGgD1D/hApofHUK/aj8XCp2 1i0MLcebqO136RR0c9xMGfF/DFjQ818AXGJEVcuV0mdugtKqGqCKrrInGpTTVk/LP9 obPd26VPHSJA+vpdqRh5tJTDm93QASI9o6/UC/HR3BXiOpNWKDL+YiHi/nlGv2IEdr 1iQ5avXslPsk/NfDoI3An87AzxYhkVy8TElD+GN1Pk7Bi4hZO0VlbhisPm64MC9lmW +6d1O59VEyuxQ== Received: by mail-pf1-f199.google.com with SMTP id bq6-20020a056a000e0600b0056bcbc6720fso4848836pfb.22 for ; Sun, 06 Nov 2022 19:07:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lknP2X8kYl/GPxh/Z2bYVUBQv2fIBaXao2E98IBeW00=; b=5SEbDMVNchuB+fGl8q2Xz6/MmoAdqZ9JJMMpr9MChTG6+tgPLxhXZw9Xv2Vc97Z9mO Uznr0XM4CNbK3Vo75/dNId1X5N3/DdazxTsVsfnrSt25DyRH3IiN3Zjk87oU9i95IWuE niX67EhNBJ482BYbQ/dGX6Mvwh4yonj+u81WwvoB+E07RQ0GgElLRiVX6GeP5lelQ17k VYqvDQ3PS2f2h4hi2f6aGLUDjeOxULA5spa5uz9JnCg+6DJvVosM5Vg8JMw6rQlNN/Lo awCihUL2vtQIVs7K+bIkKDsl5o/8Bxa0R/ik3QWN4lJCoexKIciewRU192F4aGn/4BvU UkkA== X-Gm-Message-State: ACrzQf0XlGw1PJKoen1LVh9dLr4YxOB/nmoxOEfRABm8kK1v+asBqxeY F/JjRJ+Qza6D8u7Duy3bFNjp7hS8z2RrArpQXCXQQ5Czw8/ZEtfU/qeH97oYlKBUYEtpXUkEjec xpoxTKLqDlSkH6HUPwZwSjod8j35a6OjniulHePGgahIH2WuKaid6hJ0BOA== X-Received: by 2002:a17:90a:2a8b:b0:213:9ae5:b9ab with SMTP id j11-20020a17090a2a8b00b002139ae5b9abmr49893088pjd.110.1667790423974; Sun, 06 Nov 2022 19:07:03 -0800 (PST) X-Received: by 2002:a17:90a:2a8b:b0:213:9ae5:b9ab with SMTP id j11-20020a17090a2a8b00b002139ae5b9abmr49893050pjd.110.1667790423512; Sun, 06 Nov 2022 19:07:03 -0800 (PST) MIME-Version: 1.0 References: <9c41a4372b27420c732ff5599d823e363de00c6d.1657704829.git.geert+renesas@glider.be> In-Reply-To: From: Kai-Heng Feng Date: Mon, 7 Nov 2022 11:06:51 +0800 Message-ID: Subject: Re: [PATCH] PCI: Fix dropping valid root bus resources with .end = zero To: Geert Uytterhoeven Cc: Niklas Schnelle , Bjorn Helgaas , Johannes Berg , Rob Herring , Thierry Reding , linux-pci@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 On Tue, Nov 1, 2022 at 4:23 PM Geert Uytterhoeven wrote: > > Hi Niklas, > > On Mon, Oct 31, 2022 at 12:56 PM Niklas Schnelle wrote: > > On Wed, 2022-07-13 at 11:35 +0200, Geert Uytterhoeven wrote: > > > 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); > > > > Hi Geert, Hi Bjorn, Hi Kai-Heng, > > > > I just stumbled over this issue on s390 with the below kmemleak > > splat[0]. On s390 we currently always have a single PCI bus with bus > > number 00 per PCI domain so this is triggered whenever there are PCI > > devices attached to the system. > > > > Applying the patch from this mail makes the splat go away and the > > 'pci_bus 0002:00: root bus resource [bus 00]' message reappear. As this > > mail is from July I guess it got lost and this was never picked up ;-( > > Sorry, I still have to go over all patches submitted last summer that > didn't make it... > > > For now feel free to add my: > > > > Tested-by: Niklas Schnelle Acked-by: Kai-Heng Feng Yes I think maybe we should also free it, but I am not entirely sure it's safe to be freed in this context. Kai-Heng > > Thanks! > > Gr{oetje,eeting}s, > > Geert > > -- > Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org > > In personal conversations with technical people, I call myself a hacker. But > when I'm talking to journalists I just say "programmer" or something like that. > -- Linus Torvalds