Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1374142ybt; Sat, 11 Jul 2020 08:09:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzv/jd4kzIRYMw9LAFFZYtJyezlGFGWoiwK50KzgumbDb9vkq3rrlstTbLbdV4LvbTnJXK6 X-Received: by 2002:a17:906:60d0:: with SMTP id f16mr60342128ejk.17.1594480171315; Sat, 11 Jul 2020 08:09:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594480171; cv=none; d=google.com; s=arc-20160816; b=juZBnUlNugoOFnfE2ob2FIF5eh2/BP5t0Ni1Y8KLBaDRZaDrJ2UKB9d0DK8U4yLoSk CCi1eesK2TelkGpe9B2ffMJGDPRo+A4viIFd5D3JvDQ6fvVXttu1u0deRvd1w+zGUO2F R9If9QMTzgC2rc9e0OA5qvlzEFr3LReH7D+1msrLhOCLyZerKB1g026lXAvUWKRRt3j/ +oiNeQ8IJiiwISgHMm5nFAjSnPS/rZ/MWcJaUU8ww5ouQ3078eWX1dDgTapL4/aw7iNR q8xcYXzQC/Nhqa1oF0zmQo4iCQMgj61UUpNpWKh10jJqdL9sZNPF4PHXSXuuT+VP/F5s xGRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=IQvs3fHvv9Fkjtgdt9Tsxnn3h6ei6m8hcchByCZR5HY=; b=Rxo4+DKyXs2/BCoo2IbT1UA0bYHwWBDMh62Vh1a7JqCVG4tt4t1TIwSrO0JTNBjlEh 0TbSevfhAXcl6o/TKVJuy7BxdZnjfbhO1joEGJMaAkT8hyyVuiuEZ/rqFlLekherP5Vx TuB+LJo1jD1hfUPQytsrGMnDbN9uBIByLm4CqMZRXua1wOZKZNnUpCqp20SgnXcfKZYu X9jo1+NW4QKPleoUHQ/BIYRpvaY6rdUnHGcJQbRxEKv8GuJIGVAlZEyvKXT81ewT2ZA/ vAPHAd36drSSpYkRxR1wbEboFgzIBLR2+461v0u5V3bM9o9iBtnKxhIxxUk64pUT91S6 /8gA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="epAoZmB/"; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n7si6037688ejk.154.2020.07.11.08.09.08; Sat, 11 Jul 2020 08:09:31 -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=@gmail.com header.s=20161025 header.b="epAoZmB/"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728508AbgGKPIl (ORCPT + 99 others); Sat, 11 Jul 2020 11:08:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728330AbgGKPIl (ORCPT ); Sat, 11 Jul 2020 11:08:41 -0400 Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C328C08C5DD; Sat, 11 Jul 2020 08:08:41 -0700 (PDT) Received: by mail-pl1-x642.google.com with SMTP id x11so3441285plo.7; Sat, 11 Jul 2020 08:08:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=IQvs3fHvv9Fkjtgdt9Tsxnn3h6ei6m8hcchByCZR5HY=; b=epAoZmB/f07JzI9Yc+QctLwKc0hvk4AvWpFa/BT9ZouzIYPP9i8z2n5NrA65EKv/ce alBjQSbjOiB+jqnI+cxqp1TMFlqAP3VUWEe090EruPIhzL37jfFkvHgBiwL/p5+FJWOt fHp9T37FE1Zcv3RMB9tP8hk692s9V3v4l+1K9BM6Jnjh49lpyaJ3X2qg5Rv813tHQ7Ne xPLVV/UZ6yb8wXsbEWlqKroZNzQrWeUwwpY/DJ5OoURTpxvPvQ+203SexbO8wSW8MaAB 0dmn4p42I3uuQotJAAuy5tO4SgYKJmv0VfyHYCUOLINc/h2mWrpDfeJz6vy0rTfUf6GX AtRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=IQvs3fHvv9Fkjtgdt9Tsxnn3h6ei6m8hcchByCZR5HY=; b=ao746FefwpbugGw2Hde4Y55rTx81sAwT7SI16UFyioxiqKFCntVokFujToEIbpqw2w juxX3bDlh69xPC0lqdz8LrdjrlFWIdLcpPcSw6Oom6RkX5dPXMdwoGz6x7MnMdKuxBDl OyKnGcR+VK+WAVYZCXecN23Smvv2UWKoSRQ8kfUzd1YWXfevGh/JMAlLqRpKnpy5wN1E UK4TVC2M0igDvhKUUJdCnlS2+fqLBJIEJ63MgkQh0NYLIQJozHt54lTku3ide2x98isa isyBBY65xSemy/0SlWaoDPp7B2xdRuv7IRLJjzJtyDVETMC+U0fbY5RByzssU6Xn7TlP vSgQ== X-Gm-Message-State: AOAM532Y9TtHtkxMd/TCrBbgGau53FPjV5jgdQThEZ+F5kx1LPVawVKh NdbX1PtaEQRKfIYDkEPRBxrdI7uMhX/B6ThrmmM= X-Received: by 2002:a17:902:7611:: with SMTP id k17mr63884975pll.255.1594480120624; Sat, 11 Jul 2020 08:08:40 -0700 (PDT) MIME-Version: 1.0 References: <20200710161516.11625-1-brgl@bgdev.pl> <20200710161516.11625-2-brgl@bgdev.pl> In-Reply-To: <20200710161516.11625-2-brgl@bgdev.pl> From: Andy Shevchenko Date: Sat, 11 Jul 2020 18:08:24 +0300 Message-ID: Subject: Re: [PATCH v3 1/3] devres: provide devm_krealloc() To: Bartosz Golaszewski Cc: Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , Michal Simek , Greg Kroah-Hartman , Guenter Roeck , linux-iio , linux-arm Mailing List , Linux Kernel Mailing List , Bartosz Golaszewski Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 10, 2020 at 7:17 PM Bartosz Golaszewski wrote: > > From: Bartosz Golaszewski > > Implement the managed variant of krealloc(). This function works with > all memory allocated by devm_kmalloc() (or devres functions using it > implicitly like devm_kmemdup(), devm_kstrdup() etc.). > > Managed realloc'ed chunks can be manually released with devm_kfree(). ... > devm_kfree() > devm_kmalloc() > devm_kmalloc_array() > + devm_krealloc() > devm_kmemdup() > devm_kstrdup() > devm_kvasprintf() Order? ... > +void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp) Do we really need the 'new_' prefix in the parameter? > +{ > + struct devres *old_dr, *new_dr; > + struct list_head old_head; > + unsigned long flags; > + void *ret = NULL; > + size_t tot_size; tot -> total. > + > + if (unlikely(!new_size)) { > + devm_kfree(dev, ptr); > + return ZERO_SIZE_PTR; > + } I guess here we need a comment of the possibilities below to have ZERO_SIZE_PTR as input. > + if (unlikely(ZERO_OR_NULL_PTR(ptr))) > + return devm_kmalloc(dev, new_size, gfp); > + > + if (WARN_ON(is_kernel_rodata((unsigned long)ptr))) > + /* > + * We cannot reliably realloc a const string returned by > + * devm_kstrdup_const(). > + */ > + return NULL; > + > + if (!check_dr_size(new_size, &tot_size)) > + return NULL; > + > + spin_lock_irqsave(&dev->devres_lock, flags); > + > + old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr); > + if (WARN_ON(!old_dr)) Under spin lock? I would rather see spin unlock followed by WARN. > + /* Memory chunk not managed or managed by a different device. */ > + goto out; > + > + old_head = old_dr->node.entry; > + > + new_dr = krealloc(old_dr, tot_size, gfp); > + if (!new_dr) > + goto out; > + > + if (new_dr != old_dr) > + list_replace(&old_head, &new_dr->node.entry); > + > + ret = new_dr->data; > + > +out: > + spin_unlock_irqrestore(&dev->devres_lock, flags); > + return ret; > +} -- With Best Regards, Andy Shevchenko