Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1114994ybh; Wed, 18 Mar 2020 15:24:08 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvgIzxECqt9IwUFxGRIPf49IZqKad30VMouXmRXHqy3Lr/AN10rr61nKTNqE7rGErDZQaAl X-Received: by 2002:a9d:4f0:: with SMTP id 103mr5696030otm.336.1584570248164; Wed, 18 Mar 2020 15:24:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584570248; cv=none; d=google.com; s=arc-20160816; b=sE+2KmsCcOb7ssg4m2MgWnKuC+fTYH9GRdONbQT431HaLs4VtiMEXkaGn0fUIcAWV/ 503iL1uJwZLyRjFNUfNEqD6IqVieDsX0oK3b089mOaaf+SftBVlQ7ZkYuxvMutOYtb7j xYFRBCJGyAMtQTHm/ZiAgw/XofizZ9NbtB4BPxvV3EKGnp+L74lhLbMsnsN2Cp4iJs+D 21HftvyHJCDO8gpp6khC1kzSTymcdtDemcKeOT6lHQindeutgpuf2TvqHU0rM5ei4O8z gR6nJ1OqRn/ItI9KZF5fq9mFQU7E08IXzEFD//NssxAgsOI43eeIwM2qws6mmvxC3yiv UZ2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:message-id:subject:cc:to:from:date :dkim-signature; bh=UgNUegwqfFuJmj2rtgqvAuwQrF1e5/5Dxf1hHSBC2x8=; b=tbMBObSiOudN5WRkiQFqaBTam3Yzmh2r0rEWnZ1btfVd9AuqZvH92hxYSDXIADqL/0 GmJn7rHnR8K/Wx1y9T0M49NAfH3I1Ez3xuMuW4PQB4uFkLkxV7Sw+TkXPgwxsiowMrZ+ aBLFsxVqhosGf1frmIZpfTI+0SxzeWz6guol5nuhZmi05mJlR291yr5lOT8qbkKjk4nR wLFfxsCixpvcDJLD4qEw/zxGhBAOqa593zjCT/Xu1SF+MPixpkcqPgshrPN+WVVZdmAq K4pxZcVAlt8a4uEHpuvtlcRZYLXS0JGOCJBxVZEP2NeG3HExa1M7tQ2iHQAc2fNfoXfI pZpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=TeaROXeq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g19si123893otn.219.2020.03.18.15.23.54; Wed, 18 Mar 2020 15:24:08 -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=pass header.i=@kernel.org header.s=default header.b=TeaROXeq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726897AbgCRWWl (ORCPT + 99 others); Wed, 18 Mar 2020 18:22:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:47450 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726663AbgCRWWl (ORCPT ); Wed, 18 Mar 2020 18:22:41 -0400 Received: from localhost (mobile-166-175-186-165.mycingular.net [166.175.186.165]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6804D20714; Wed, 18 Mar 2020 22:22:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1584570160; bh=Xz/0EvZI1ZYMODHoYRMl9J+dOM/tsbfHznYvsE+DY4A=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=TeaROXeqtIiOuPZEJEeYWnIPHrh1Ask7srNIxIYhwTpKsUqP2jP8EVgIYJM33HfwF 1jFMhhapjJedYXzGwqqv6NFwF3Nch7XWO1iNLYfMzXCjEK5TU34+g7gYssJdkFOM1b DGF8UtEi5MxA8pPt6Acsc8nHRD8W/OlpKBw98Rb4= Date: Wed, 18 Mar 2020 17:22:38 -0500 From: Bjorn Helgaas To: Thomas Gleixner Cc: Marc Gonzalez , Aman Sharma , Lorenzo Pieralisi , Thomas Petazzoni , Andrew Murray , Linus Walleij , Ryder Lee , Karthikeyan Mitran , Hou Zhiqiang , Mans Rullgard , Matthias Brugger , linux-pci@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org, Marc Zyngier Subject: Re: [PATCH 4/5] pci: handled return value of platform_get_irq correctly Message-ID: <20200318222238.GA247500@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <874kulbwyv.fsf@nanos.tec.linutronix.de> User-Agent: Mutt/1.12.2 (2019-09-21) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 18, 2020 at 02:42:48PM +0100, Thomas Gleixner wrote: > Bjorn Helgaas writes: > > On Fri, Mar 13, 2020 at 04:56:42PM -0500, Bjorn Helgaas wrote: > >> On Fri, Mar 13, 2020 at 10:05:58PM +0100, Thomas Gleixner wrote: > >> > > I think the best pattern is: > >> > > > >> > > irq = platform_get_irq(pdev, i); > >> > > if (irq < 0) > >> > > return irq; > >> > > >> > Careful. 0 is not a valid interrupt. > >> > >> Should callers of platform_get_irq() check for a 0 return value? > >> About 900 of them do not. > > I don't know what I was looking at. > > platform_get_irq() does the right thing already, so checking for irq < 0 > is sufficient. > > Sorry for the confusion! Thanks, I was indeed confused! Maybe we could reduce future confusion by strengthening the comments slightly, e.g., - * Return: IRQ number on success, negative error number on failure. + * Return: non-zero IRQ number on success, negative error number on failure. I don't want to push my luck, but it's pretty hard to prove that platform_get_irq() never returns 0. What would you think of something like the following? @@ -133,23 +133,24 @@ EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname); * if (irq < 0) * return irq; * - * Return: IRQ number on success, negative error number on failure. + * Return: non-zero IRQ number on success, negative error number on failure. */ int platform_get_irq_optional(struct platform_device *dev, unsigned int num) { + int ret; #ifdef CONFIG_SPARC /* sparc does not have irqs represented as IORESOURCE_IRQ resources */ if (!dev || num >= dev->archdata.num_irqs) return -ENXIO; - return dev->archdata.irqs[num]; + ret = dev->archdata.irqs[num]; + goto out; #else struct resource *r; - int ret; if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) { ret = of_irq_get(dev->dev.of_node, num); if (ret > 0 || ret == -EPROBE_DEFER) - return ret; + goto out; } r = platform_get_resource(dev, IORESOURCE_IRQ, num); @@ -157,7 +158,7 @@ int platform_get_irq_optional(struct platform_device *dev, unsigned int num) if (r && r->flags & IORESOURCE_DISABLED) { ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r); if (ret) - return ret; + goto out; } } @@ -171,13 +172,17 @@ int platform_get_irq_optional(struct platform_device *dev, unsigned int num) struct irq_data *irqd; irqd = irq_get_irq_data(r->start); - if (!irqd) - return -ENXIO; + if (!irqd) { + ret = -ENXIO; + goto out; + } irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS); } - if (r) - return r->start; + if (r) { + ret = r->start; + goto out; + } /* * For the index 0 interrupt, allow falling back to GpioInt @@ -190,11 +195,14 @@ int platform_get_irq_optional(struct platform_device *dev, unsigned int num) ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num); /* Our callers expect -ENXIO for missing IRQs. */ if (ret >= 0 || ret == -EPROBE_DEFER) - return ret; + goto out; } - return -ENXIO; + ret = -ENXIO; #endif +out: + WARN(ret == 0, "0 is an invalid IRQ number\n"); + return ret; } EXPORT_SYMBOL_GPL(platform_get_irq_optional); @@ -212,7 +220,7 @@ EXPORT_SYMBOL_GPL(platform_get_irq_optional); * if (irq < 0) * return irq; * - * Return: IRQ number on success, negative error number on failure. + * Return: non-zero IRQ number on success, negative error number on failure. */ int platform_get_irq(struct platform_device *dev, unsigned int num) { @@ -284,8 +292,10 @@ static int __platform_get_irq_byname(struct platform_device *dev, } r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name); - if (r) + if (r) { + WARN(r->start == 0, "0 is an invalid IRQ number\n"); return r->start; + } return -ENXIO; } @@ -297,7 +307,7 @@ static int __platform_get_irq_byname(struct platform_device *dev, * * Get an IRQ like platform_get_irq(), but then by name rather then by index. * - * Return: IRQ number on success, negative error number on failure. + * Return: non-zero IRQ number on success, negative error number on failure. */ int platform_get_irq_byname(struct platform_device *dev, const char *name) { @@ -319,7 +329,7 @@ EXPORT_SYMBOL_GPL(platform_get_irq_byname); * Get an optional IRQ by name like platform_get_irq_byname(). Except that it * does not print an error message if an IRQ can not be obtained. * - * Return: IRQ number on success, negative error number on failure. + * Return: non-zero IRQ number on success, negative error number on failure. */ int platform_get_irq_byname_optional(struct platform_device *dev, const char *name)