Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp4603191imm; Mon, 30 Jul 2018 18:45:22 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdoN8ivp+uLn+PwgHcKr75JogH+ylOvV0Xq1qe7dtdEbpme8rysFgx3r2bRVAwZp/UExBC+ X-Received: by 2002:a62:5290:: with SMTP id g138-v6mr20206872pfb.46.1533001522563; Mon, 30 Jul 2018 18:45:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533001522; cv=none; d=google.com; s=arc-20160816; b=uWVUN0O/VY1iGBkpNnsaKmLmv+TxQl9oXVLbD0vwDziu/ZZa9i0SBvptIPaYcXfZl+ jigv0sTXTzSvRav5BKHyWTF1k30amgmt0ZgK3jRQcH+VbN8+kA0016z39YBAsvxe5ZIh kHBGd2SoJENoPEq5h4Vn3uv8GU4QR1KzUQd1kH2iyx82gkZEXvalI7I8b6V9ChnKeSKv Gz+dJaGe+YfTABodhduFRfU3bLy8jYatMWtxiKb+XT4BQcJARhIpPOgkLvJf5aMsmmvc JXhp9JhlhT+iRqrPQ/7zivsDWksqBHu7sk4wNm2/WdL2wbYTXb2ChlLv75BWeVKRcrDY Px0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=MnneWn2uhd5uHBwBcWdjlf16tJNVllBH0oFR9iV7/a0=; b=Jtk2HiRQr+ZoYbW5/N/DKPMfDx4zi5/L22cFaUFE2hc6cEkKBrVHczncHw3ZXxkRI8 2PTtkkuhckKpDuXHMoaYP2aWaElboHEzUD7DfZfhssFUtlVjXKXyos3l1RKx2njxpt+i bAluRM1rtgp5Qo/lsGSR2ZAT2gnIt34+AFSyarVmsv18yi66IXndxC4qSixXpYy3KJZP 0zQJvpsvymI/3Q2dgtyeJc87/IcKH/N/7T3OvL1c+wmhkygEr2OwANJRspJOu2OWrUaA uX9UfMjGVBDQlKp5S6W6Pjq29x/lHMHrWJvMh/OmpzKQXkx6ZnwVlHnDIr14o8AR1lqQ smJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=kjo87Or6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a33-v6si8650486plc.15.2018.07.30.18.45.07; Mon, 30 Jul 2018 18:45:22 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=kjo87Or6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731511AbeGaDWF (ORCPT + 99 others); Mon, 30 Jul 2018 23:22:05 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:39520 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727714AbeGaDWF (ORCPT ); Mon, 30 Jul 2018 23:22:05 -0400 Received: by mail-pg1-f193.google.com with SMTP id a11-v6so8213453pgw.6 for ; Mon, 30 Jul 2018 18:44:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id; bh=MnneWn2uhd5uHBwBcWdjlf16tJNVllBH0oFR9iV7/a0=; b=kjo87Or6wzPh/6eIgMDYLL/OnifKyIj+sE+T7WrNanwU/ylTzo+0bkhXuzsoNQPOi8 nmGP6kTKmoaWB0id2/hgK06uqd9nMVOWnFi8l+nvmCFEaK+Mx1nwuIs+lXJ78KVlcXpi ttKoefEk4M7rXH2FXCu8LOUTBeq/TyfuCAjUbW05fpIJhniEsDYTlUrFOZfetQQsRVM/ 2/yCPLulZStRe2uxBydX2QoM8NNB9U+wedQeSDBJzbTTBiRM3g/97XKXFI372AKIltRT H2so1r4BAOac0uQ+57arUsohCbn3MDCEED9xn6Njy+xTwZtZja4/Tft5fEzn8wGvsDQQ g9sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=MnneWn2uhd5uHBwBcWdjlf16tJNVllBH0oFR9iV7/a0=; b=dcZAFyShqRXPeggWbEzcXC3o23RjMdMpiGZq0SmqdXDC9qIg+iyTP+k3lGcycfnDcC MhJJ9mJozY48SAe8KQ2IZZ3l1nSsu8PBpwRy5HxKRLaFG4NoUKppdtLo+lxrT3VYRdma ECfU1CHvbrQaVnxaO9GEysQaTmQIEeeWgmtNioSoYhO5l8KMoiYEtcpKdoH28dRT+/aA SBeg1hw6betcL96ISKoMM5VHf29ecicENxugX3TWB4aVbd8JgeqXcL0Tl/NLFsUjBEMs zyNhszCfJXafpGKPf5GlyDV1AVHdMpkWvM8X0Bz6djGj+ulg7EufqbGW1A0HMJAqZ9hm pICw== X-Gm-Message-State: AOUpUlHkOx5loCGAUvk6Q8VdXNdCK2jiXiTilX+iP5w/zSdQPKuyoEXo 8GK5hBfSjAue9VWW9nHjAiM= X-Received: by 2002:a62:2459:: with SMTP id r86-v6mr20024250pfj.31.1533001457374; Mon, 30 Jul 2018 18:44:17 -0700 (PDT) Received: from localhost (108-223-40-66.lightspeed.sntcca.sbcglobal.net. [108.223.40.66]) by smtp.gmail.com with ESMTPSA id x24-v6sm18835338pfh.67.2018.07.30.18.44.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Jul 2018 18:44:16 -0700 (PDT) From: Guenter Roeck To: Michael Ellerman Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Robin Murphy , Christoph Hellwig , Guenter Roeck Subject: [PATCH] powerpc/4xx: Fix error return path in ppc4xx_msi_probe() Date: Mon, 30 Jul 2018 18:44:14 -0700 Message-Id: <1533001454-8751-1-git-send-email-linux@roeck-us.net> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org An arbitrary error in ppc4xx_msi_probe() quite likely results in a crash similar to the following, seen after dma_alloc_coherent() returned an error. Unable to handle kernel paging request for data at address 0x00000000 Faulting instruction address: 0xc001bff0 Oops: Kernel access of bad area, sig: 11 [#1] BE Canyonlands Modules linked in: CPU: 0 PID: 1 Comm: swapper Tainted: G W 4.18.0-rc6-00010-gff33d1030a6c #1 NIP: c001bff0 LR: c001c418 CTR: c01faa7c REGS: cf82db40 TRAP: 0300 Tainted: G W (4.18.0-rc6-00010-gff33d1030a6c) MSR: 00029000 CR: 28002024 XER: 00000000 DEAR: 00000000 ESR: 00000000 GPR00: c001c418 cf82dbf0 cf828000 cf8de400 00000000 00000000 000000c4 000000c4 GPR08: c0481ea4 00000000 00000000 000000c4 22002024 00000000 c00025e8 00000000 GPR16: 00000000 00000000 00000000 00000000 00000000 00000000 c0492380 0000004a GPR24: 00029000 0000000c 00000000 cf8de410 c0494d60 c0494d60 cf8bebc0 00000001 NIP [c001bff0] ppc4xx_of_msi_remove+0x48/0xa0 LR [c001c418] ppc4xx_msi_probe+0x294/0x3b8 Call Trace: [cf82dbf0] [00029000] 0x29000 (unreliable) [cf82dc10] [c001c418] ppc4xx_msi_probe+0x294/0x3b8 [cf82dc70] [c0209fbc] platform_drv_probe+0x40/0x9c [cf82dc90] [c0208240] driver_probe_device+0x2a8/0x350 [cf82dcc0] [c0206204] bus_for_each_drv+0x60/0xac [cf82dcf0] [c0207e88] __device_attach+0xe8/0x160 [cf82dd20] [c02071e0] bus_probe_device+0xa0/0xbc [cf82dd40] [c02050c8] device_add+0x404/0x5c4 [cf82dd90] [c0288978] of_platform_device_create_pdata+0x88/0xd8 [cf82ddb0] [c0288b70] of_platform_bus_create+0x134/0x220 [cf82de10] [c0288bcc] of_platform_bus_create+0x190/0x220 [cf82de70] [c0288cf4] of_platform_bus_probe+0x98/0xec [cf82de90] [c0449650] __machine_initcall_canyonlands_ppc460ex_device_probe+0x38/0x54 [cf82dea0] [c0002404] do_one_initcall+0x40/0x188 [cf82df00] [c043daec] kernel_init_freeable+0x130/0x1d0 [cf82df30] [c0002600] kernel_init+0x18/0x104 [cf82df40] [c000c23c] ret_from_kernel_thread+0x14/0x1c Instruction dump: 90010024 813d0024 2f890000 83c30058 41bd0014 48000038 813d0024 7f89f800 409d002c 813e000c 57ea103a 3bff0001 <7c69502e> 2f830000 419effe0 4803b26d ---[ end trace 8cf551077ecfc42a ]--- Fix it up. Specifically, - Return valid error codes from ppc4xx_setup_pcieh_hw(), have it clean up after itself, and only access hardware after all possible error conditions have been handled. - Use devm_kzalloc() instead of kzalloc() in ppc4xx_msi_probe() Signed-off-by: Guenter Roeck --- arch/powerpc/platforms/4xx/msi.c | 51 +++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/arch/powerpc/platforms/4xx/msi.c b/arch/powerpc/platforms/4xx/msi.c index 81b2cbce7df8..7c324eff2f22 100644 --- a/arch/powerpc/platforms/4xx/msi.c +++ b/arch/powerpc/platforms/4xx/msi.c @@ -146,13 +146,19 @@ static int ppc4xx_setup_pcieh_hw(struct platform_device *dev, const u32 *sdr_addr; dma_addr_t msi_phys; void *msi_virt; + int err; sdr_addr = of_get_property(dev->dev.of_node, "sdr-base", NULL); if (!sdr_addr) - return -1; + return -EINVAL; - mtdcri(SDR0, *sdr_addr, upper_32_bits(res.start)); /*HIGH addr */ - mtdcri(SDR0, *sdr_addr + 1, lower_32_bits(res.start)); /* Low addr */ + msi_data = of_get_property(dev->dev.of_node, "msi-data", NULL); + if (!msi_data) + return -EINVAL; + + msi_mask = of_get_property(dev->dev.of_node, "msi-mask", NULL); + if (!msi_mask) + return -EINVAL; msi->msi_dev = of_find_node_by_name(NULL, "ppc4xx-msi"); if (!msi->msi_dev) @@ -160,30 +166,30 @@ static int ppc4xx_setup_pcieh_hw(struct platform_device *dev, msi->msi_regs = of_iomap(msi->msi_dev, 0); if (!msi->msi_regs) { - dev_err(&dev->dev, "of_iomap problem failed\n"); - return -ENOMEM; + dev_err(&dev->dev, "of_iomap failed\n"); + err = -ENOMEM; + goto node_put; } dev_dbg(&dev->dev, "PCIE-MSI: msi register mapped 0x%x 0x%x\n", (u32) (msi->msi_regs + PEIH_TERMADH), (u32) (msi->msi_regs)); msi_virt = dma_alloc_coherent(&dev->dev, 64, &msi_phys, GFP_KERNEL); - if (!msi_virt) - return -ENOMEM; + if (!msi_virt) { + err = -ENOMEM; + goto iounmap; + } msi->msi_addr_hi = upper_32_bits(msi_phys); msi->msi_addr_lo = lower_32_bits(msi_phys & 0xffffffff); dev_dbg(&dev->dev, "PCIE-MSI: msi address high 0x%x, low 0x%x\n", msi->msi_addr_hi, msi->msi_addr_lo); + mtdcri(SDR0, *sdr_addr, upper_32_bits(res.start)); /*HIGH addr */ + mtdcri(SDR0, *sdr_addr + 1, lower_32_bits(res.start)); /* Low addr */ + /* Progam the Interrupt handler Termination addr registers */ out_be32(msi->msi_regs + PEIH_TERMADH, msi->msi_addr_hi); out_be32(msi->msi_regs + PEIH_TERMADL, msi->msi_addr_lo); - msi_data = of_get_property(dev->dev.of_node, "msi-data", NULL); - if (!msi_data) - return -1; - msi_mask = of_get_property(dev->dev.of_node, "msi-mask", NULL); - if (!msi_mask) - return -1; /* Program MSI Expected data and Mask bits */ out_be32(msi->msi_regs + PEIH_MSIED, *msi_data); out_be32(msi->msi_regs + PEIH_MSIMK, *msi_mask); @@ -191,6 +197,12 @@ static int ppc4xx_setup_pcieh_hw(struct platform_device *dev, dma_free_coherent(&dev->dev, 64, msi_virt, msi_phys); return 0; + +iounmap: + iounmap(msi->msi_regs); +node_put: + of_node_put(msi->msi_dev); + return err; } static int ppc4xx_of_msi_remove(struct platform_device *dev) @@ -209,7 +221,6 @@ static int ppc4xx_of_msi_remove(struct platform_device *dev) msi_bitmap_free(&msi->bitmap); iounmap(msi->msi_regs); of_node_put(msi->msi_dev); - kfree(msi); return 0; } @@ -223,18 +234,16 @@ static int ppc4xx_msi_probe(struct platform_device *dev) dev_dbg(&dev->dev, "PCIE-MSI: Setting up MSI support...\n"); - msi = kzalloc(sizeof(*msi), GFP_KERNEL); - if (!msi) { - dev_err(&dev->dev, "No memory for MSI structure\n"); + msi = devm_kzalloc(&dev->dev, sizeof(*msi), GFP_KERNEL); + if (!msi) return -ENOMEM; - } dev->dev.platform_data = msi; /* Get MSI ranges */ err = of_address_to_resource(dev->dev.of_node, 0, &res); if (err) { dev_err(&dev->dev, "%pOF resource error!\n", dev->dev.of_node); - goto error_out; + return err; } msi_irqs = of_irq_count(dev->dev.of_node); @@ -243,7 +252,7 @@ static int ppc4xx_msi_probe(struct platform_device *dev) err = ppc4xx_setup_pcieh_hw(dev, res, msi); if (err) - goto error_out; + return err; err = ppc4xx_msi_init_allocator(dev, msi); if (err) { @@ -256,7 +265,7 @@ static int ppc4xx_msi_probe(struct platform_device *dev) phb->controller_ops.setup_msi_irqs = ppc4xx_setup_msi_irqs; phb->controller_ops.teardown_msi_irqs = ppc4xx_teardown_msi_irqs; } - return err; + return 0; error_out: ppc4xx_of_msi_remove(dev); -- 2.7.4