Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp675338ybt; Fri, 10 Jul 2020 09:31:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQ/X/DfMMpn1eBrEhfVbsqJvI8CRsFn89yb8ZtKkFyA8GmMQ9WlpVofcSGMiMTkk//pu6Z X-Received: by 2002:a05:6402:2c5:: with SMTP id b5mr63125540edx.316.1594398698672; Fri, 10 Jul 2020 09:31:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594398698; cv=none; d=google.com; s=arc-20160816; b=bTdYpF2v2ikl3MXxf1tM3ChkhxkQ+rbsnvySW6ZEONqgC9/gNTCaceHQs6ZL2ArGst 9SrmuGi5fxfuMjsfQ3aEkE2iZZKxKLGEfwC6k4o9imeCzvFrnZSWhBIqDdDnZeIqlcTi PG3L7JHFMD0pyTzX7jQk/ug1fnHYm1yQP9xjJh30o5lJmNDUyaG2iu2DiiR2M9iPByZn HDt77LV0jTofiYuuvlSpJqlPkp6+T2EnJ+jRfcLcUxIcpDzzU3/EJw8WoJAMYFgLWSC1 Pb9dL77bN4I6xii6uB+SN96NZG2+3m8cBzb5q8EzJUsHsEBw0/h64c67T8/gaYu6S5f6 P79g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:dkim-signature:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=wPbMvPXTwmmZyNSlsW+QBsjbAUy/OrrNRVHRg/UgtC4=; b=jGbldVOuIi/estEIlqmnWrGI9hCCZDGqVAHlp0OGPQs7TGJSHR3jwaLH8KStE4oacf zUS2RPkwoK6rhio5LopsOFOpyxZwjm8hoGKW/YJjFSuY3g8rFIT6vNIdsnpoMQBtkcJb ZZOmcISVoaHYGpc9VJTUir/lQa26hRhXSnam3Z+HL9ni2LQ440nw5pUi8Wr8TA5dwa9g 1J3qxa+A1UOKx4jwZMe4xtldO1VFXlmn9NBwutY6rsZfQJ/VW+71pMzn81cHaUKuPuDc U2hJWBppPzJM1QhgQNCzj9hAhFSVzwHLrnw18tCvoQDbzPqsIvqMkfz90mg+s0MwZBxM UZlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=OashMJfM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mj26si4198175ejb.416.2020.07.10.09.31.15; Fri, 10 Jul 2020 09:31:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=OashMJfM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728142AbgGJQa7 (ORCPT + 99 others); Fri, 10 Jul 2020 12:30:59 -0400 Received: from hqnvemgate24.nvidia.com ([216.228.121.143]:3136 "EHLO hqnvemgate24.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726920AbgGJQa6 (ORCPT ); Fri, 10 Jul 2020 12:30:58 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate24.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Fri, 10 Jul 2020 09:29:09 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Fri, 10 Jul 2020 09:30:58 -0700 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Fri, 10 Jul 2020 09:30:58 -0700 Received: from [10.26.72.135] (172.20.13.39) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Fri, 10 Jul 2020 16:30:56 +0000 Subject: Re: [PATCH v2 4/6] devres: handle zero size in devm_kmalloc() To: Bartosz Golaszewski CC: Jonathan Corbet , Greg Kroah-Hartman , "Rafael J . Wysocki" , Guenter Roeck , Jean Delvare , linux-doc , Linux Kernel Mailing List , , Bartosz Golaszewski References: <20200629065008.27620-1-brgl@bgdev.pl> <20200629065008.27620-5-brgl@bgdev.pl> <5c2e7514-b6d0-1331-37b0-d17a0cdb9693@nvidia.com> <9cd4521b-aba0-616b-8957-8f21b9ba3068@nvidia.com> From: Jon Hunter Message-ID: Date: Fri, 10 Jul 2020 17:30:54 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: X-Originating-IP: [172.20.13.39] X-ClientProxiedBy: HQMAIL107.nvidia.com (172.20.187.13) To HQMAIL107.nvidia.com (172.20.187.13) Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1594398549; bh=wPbMvPXTwmmZyNSlsW+QBsjbAUy/OrrNRVHRg/UgtC4=; h=X-PGP-Universal:Subject:To:CC:References:From:Message-ID:Date: User-Agent:MIME-Version:In-Reply-To:X-Originating-IP: X-ClientProxiedBy:Content-Type:Content-Language: Content-Transfer-Encoding; b=OashMJfMHNodUZ63liXAW3lX8xNoFwNA2F0TimvpIET1HqnGobjCjh6h87sXKCJql tDhnHi1dCFssHAeIFR5AHL/q3c37gKZ//xE+w12I6lP8mUC/zpJcTK6aGhcTLaHQmI uIlhILBmtYgv0x3rTVPkBghDJFD5j2S5nLKktfY+G3nCpS/BdW/eUXhXNMgJx7TY0V ulE96GaL52cPLA5o64y0xhHsfpCvXHzPuCw1iCWn5E8qhM2MPwgjUSCHq6IJ+JYVTf 1Gx1Bs8swFAWDSWul6nQAZi84ZRGcRumhRM/M7mlfRXvMLBv9ZaCyIpb5WXruCMI7X JkfNQG7rQAxPQ== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/07/2020 17:24, Bartosz Golaszewski wrote: > On Fri, Jul 10, 2020 at 6:11 PM Jon Hunter wrote: >> >> >> On 10/07/2020 17:03, Bartosz Golaszewski wrote: >>> On Fri, Jul 10, 2020 at 3:46 PM Jon Hunter wrote: >>>> >>>> Hi Bartosz, >>>> >>>> On 29/06/2020 07:50, Bartosz Golaszewski wrote: >>>>> From: Bartosz Golaszewski >>>>> >>>>> Make devm_kmalloc() behave similarly to non-managed kmalloc(): return >>>>> ZERO_SIZE_PTR when requested size is 0. Update devm_kfree() to handle >>>>> this case. >>>>> >>>>> Signed-off-by: Bartosz Golaszewski >>>>> --- >>>>> drivers/base/devres.c | 9 ++++++--- >>>>> 1 file changed, 6 insertions(+), 3 deletions(-) >>>>> >>>>> diff --git a/drivers/base/devres.c b/drivers/base/devres.c >>>>> index 1df1fb10b2d9..ed615d3b9cf1 100644 >>>>> --- a/drivers/base/devres.c >>>>> +++ b/drivers/base/devres.c >>>>> @@ -819,6 +819,9 @@ void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) >>>>> { >>>>> struct devres *dr; >>>>> >>>>> + if (unlikely(!size)) >>>>> + return ZERO_SIZE_PTR; >>>>> + >>>>> /* use raw alloc_dr for kmalloc caller tracing */ >>>>> dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev)); >>>>> if (unlikely(!dr)) >>>>> @@ -950,10 +953,10 @@ void devm_kfree(struct device *dev, const void *p) >>>>> int rc; >>>>> >>>>> /* >>>>> - * Special case: pointer to a string in .rodata returned by >>>>> - * devm_kstrdup_const(). >>>>> + * Special cases: pointer to a string in .rodata returned by >>>>> + * devm_kstrdup_const() or NULL/ZERO ptr. >>>>> */ >>>>> - if (unlikely(is_kernel_rodata((unsigned long)p))) >>>>> + if (unlikely(is_kernel_rodata((unsigned long)p) || ZERO_OR_NULL_PTR(p))) >>>>> return; >>>>> >>>>> rc = devres_destroy(dev, devm_kmalloc_release, >>>> >>>> >>>> This change caught a bug in one of our Tegra drivers, which I am in the >>>> process of fixing. Once I bisected to this commit it was easy to track >>>> down, but I am wondering if there is any reason why we don't add a >>>> WARN_ON() if size is 0 in devm_kmalloc? It was essentially what I ended >>>> up doing to find the bug. >>>> >>>> Jon >>>> >>>> -- >>>> nvpublic >>> >>> Hi Jon, >>> >>> this is in line with what the regular kmalloc() does. If size is zero, >>> it returns ZERO_SIZE_PTR. It's not an error condition. Actually in >>> user-space malloc() does a similar thing: for size == 0 it allocates >>> one-byte and returns a pointer to it (at least in glibc). >> >> >> Yes that's fine, I was just wondering if there is any reason not to WARN >> as well? >> >> Cheers >> Jon >> > > Why? Nothing bad happens. Regular kmalloc() doesn't warn, why should > devm_kmalloc() do? Simply because it is easier to track down a bug. In my case the NULL pointer crash did not occur until entering suspend when the memory, that was allocated at probe time, was first actually accessed. So it was not immediately obvious which call to devm_kmalloc caused the problem. Anyway, if kmalloc does not warn either, then fine, it was purely a question. Jon -- nvpublic