From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D573EC433E0 for ; Sat, 25 Jul 2020 19:20:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B1DDE20674 for ; Sat, 25 Jul 2020 19:20:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="Xhf417/R" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727951AbgGYTU5 (ORCPT ); Sat, 25 Jul 2020 15:20:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726681AbgGYTU4 (ORCPT ); Sat, 25 Jul 2020 15:20:56 -0400 Received: from mail-io1-xd42.google.com (mail-io1-xd42.google.com [IPv6:2607:f8b0:4864:20::d42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D72AC08C5C1 for ; Sat, 25 Jul 2020 12:20:55 -0700 (PDT) Received: by mail-io1-xd42.google.com with SMTP id q75so4977357iod.1 for ; Sat, 25 Jul 2020 12:20:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=BAKD43YZW+XuTJpWzZ9VYyNMQYro/UXMGluF3fF4RYs=; b=Xhf417/RwG3NeXd9PMir029lydvfWTPF/8jJlWtZ2kMEl6apVCGbNy22lOShc2CrJ3 6fnIVRmhU2MH8kyyigQzB6BYr4f+E9VuXQ6oP7TySgZkjacZl34d4lC4uoSJu2X+1hFG PD/Amz7tkEbGbbrMctrB8Ux5faMcAaurEl/N72CcGFuk98JoYpRMEgCv4qHsTHcqo0wZ ZQxQYyDYayi+9wtQZ4UwHACQyPddT2V2GsU+SgMaqmOojihG6eO/Anl8mxsIQYRSrMDo jlDWC2/3MbLFSliD3ej5bAfhCbvVO/8/PYfVbRZw4b61VKvCvrAAK4Zva6pD1WTHc67C +dng== 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=BAKD43YZW+XuTJpWzZ9VYyNMQYro/UXMGluF3fF4RYs=; b=oky1YLs6wryueH0I8Wmfah6sOEHhBy64F4OQqIBuCIllwcr1/7+uMQsi6z3SlcWkCf 6i5lYu9h4ur3A+p3N+4y3uAL9NnM7iP9w7mIqaEl3VK7rkto7k4J5M9F/bxoB6yluVfu KWWu4QwZU+pUtNC7+eqzQ/RflawBsqMTglFHhWPB9Ke7gROxRpL9wywF+FLBxfXnXNIN CLCMU7AoUtmLDc3m+uFl4RoXE7i10hqjxVxRqTpLb0S58mPLp4Rpv/QN57ztrQSxZgxN FUq5bONV6LeQJy2WVJuXMftojpWSfs6cspZFqrGXVZk3o/jMZuvEyw2feVwP8KStW1lg KJnw== X-Gm-Message-State: AOAM532RAtemYABDJRWrUKm3xKb4uf5t12G2t07iThbgs5LTy1bHbviq u5bAGndjgrPx2VUNmwd31tS8P5PqyYlMc/deIqbBNA== X-Google-Smtp-Source: ABdhPJx6JxsimzPomfM2WuKjX7IWCzlyMb7jOINulu+5K1XUglCEFIKyVyJiLS58iWiQ3f7gDzo6FNe8rcOgwXN1sCs= X-Received: by 2002:a05:6602:2c01:: with SMTP id w1mr16805753iov.130.1595704854695; Sat, 25 Jul 2020 12:20:54 -0700 (PDT) MIME-Version: 1.0 References: <20200715092528.8136-1-brgl@bgdev.pl> <20200715092528.8136-2-brgl@bgdev.pl> In-Reply-To: <20200715092528.8136-2-brgl@bgdev.pl> From: Bartosz Golaszewski Date: Sat, 25 Jul 2020 21:20:43 +0200 Message-ID: Subject: Re: [PATCH v5 1/3] devres: provide devm_krealloc() To: Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , Michal Simek , Greg Kroah-Hartman , Guenter Roeck , Andy Shevchenko Cc: linux-iio , Linux ARM , 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 Wed, Jul 15, 2020 at 11:25 AM 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(). > > Signed-off-by: Bartosz Golaszewski > --- [snip!] > +void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp) > +{ > + struct devres *old_dr, *new_dr; > + struct list_head old_head; > + unsigned long flags; > + size_t total_size; > + void *ret = NULL; > + > + if (unlikely(!new_size)) { > + devm_kfree(dev, ptr); > + return ZERO_SIZE_PTR; > + } > + > + 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, &total_size)) > + return NULL; > + > + spin_lock_irqsave(&dev->devres_lock, flags); > + > + old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr); > + if (!old_dr) { > + spin_unlock_irqrestore(&dev->devres_lock, flags); > + WARN(1, "Memory chunk not managed or managed by a different device."); > + return NULL; > + } > + > + old_head = old_dr->node.entry; > + > + new_dr = krealloc(old_dr, total_size, gfp); Ugh, I wanted to check up on this patch and, after looking at it now, realized it's wrong. If the user calls devm_krealloc() with GFP_KERNEL we may end up sleeping with spinlock taken. Let me prepare another version. Bartosz From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B8AAC433E0 for ; Sat, 25 Jul 2020 19:22:24 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 651BB20674 for ; Sat, 25 Jul 2020 19:22:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="AV930dkG"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="Xhf417/R" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 651BB20674 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=7Ub1rlRffSs4GjMA2hvM8DeojQMpzB7Wdk+g/J0s3sU=; b=AV930dkGZ5WJjbothvnYuz3uI 1uQOtBJlnW86e+Wwftix9HMncSQTkPryVOu5D4uwJIK7o01aEa9/PO7jgPUWnttimhmxkZKcoDC3m n7jRVs0wQustEX1bzjT/q4k/T31gRKouC4Is7xuN2p2CykDZMKR6ozaLqQ2w8tldFLJigpLcrhKsB jSiZUQM2ZrHFZQOBZGos5veKi438Ibt4fSTxzC6UPhAE2bgqkvY4gdMDn+FOsISukXY4N0uEvDtnS u4SwMsEh59DJ5GW1O0a9j1ilnv3e42RGN54tJGAs1B1sKo8SA/KkLc2FoZPaPVHR1bifCvsnUAf/s kZoYYQ94w==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jzPjC-0005mx-8O; Sat, 25 Jul 2020 19:21:02 +0000 Received: from mail-io1-xd44.google.com ([2607:f8b0:4864:20::d44]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jzPj8-0005mJ-Eu for linux-arm-kernel@lists.infradead.org; Sat, 25 Jul 2020 19:20:59 +0000 Received: by mail-io1-xd44.google.com with SMTP id v6so13150366iob.4 for ; Sat, 25 Jul 2020 12:20:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=BAKD43YZW+XuTJpWzZ9VYyNMQYro/UXMGluF3fF4RYs=; b=Xhf417/RwG3NeXd9PMir029lydvfWTPF/8jJlWtZ2kMEl6apVCGbNy22lOShc2CrJ3 6fnIVRmhU2MH8kyyigQzB6BYr4f+E9VuXQ6oP7TySgZkjacZl34d4lC4uoSJu2X+1hFG PD/Amz7tkEbGbbrMctrB8Ux5faMcAaurEl/N72CcGFuk98JoYpRMEgCv4qHsTHcqo0wZ ZQxQYyDYayi+9wtQZ4UwHACQyPddT2V2GsU+SgMaqmOojihG6eO/Anl8mxsIQYRSrMDo jlDWC2/3MbLFSliD3ej5bAfhCbvVO/8/PYfVbRZw4b61VKvCvrAAK4Zva6pD1WTHc67C +dng== 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=BAKD43YZW+XuTJpWzZ9VYyNMQYro/UXMGluF3fF4RYs=; b=BPikGlFZGgn/sGUhPlvgCySh9WRGns+eyPweNro78DbJy88k7GXh9upN9Q9vJdBFXg M6TOfTkhPbxDNkIpkxey4dm6mqsHAi+6N5rItb3jW0LNcIi79Epw2teHyPY7zduUOjSL AoApQWzzi+97Y+nRqtBzY+xsAL1Iroh9JFL5KJ1OUjN3brAkCMQA+QtIMcdj8zGU0Cli pdLPwyo+VOchtRnWYuE2wZMAusInCsIIC1MPCPLxYHkt14BAAQGA+GBu9n8RSgOmWl2j 1xZouWL8d+SUgMU/IzAyk+Ff3+kwEkhPu+03TEJ89o4fvp0T6dhW5L5CTuIQB+lhfpWf olNg== X-Gm-Message-State: AOAM530iDcEOQDi/dzD0T28xrS2vuVo5RT3zO5+sKhOoZ6T9ZFs0be8+ yCyipwDjtEF1v5LMzuqYjchDGVUj5xzMMFmzJnk6IA== X-Google-Smtp-Source: ABdhPJx6JxsimzPomfM2WuKjX7IWCzlyMb7jOINulu+5K1XUglCEFIKyVyJiLS58iWiQ3f7gDzo6FNe8rcOgwXN1sCs= X-Received: by 2002:a05:6602:2c01:: with SMTP id w1mr16805753iov.130.1595704854695; Sat, 25 Jul 2020 12:20:54 -0700 (PDT) MIME-Version: 1.0 References: <20200715092528.8136-1-brgl@bgdev.pl> <20200715092528.8136-2-brgl@bgdev.pl> In-Reply-To: <20200715092528.8136-2-brgl@bgdev.pl> From: Bartosz Golaszewski Date: Sat, 25 Jul 2020 21:20:43 +0200 Message-ID: Subject: Re: [PATCH v5 1/3] devres: provide devm_krealloc() To: Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , Michal Simek , Greg Kroah-Hartman , Guenter Roeck , Andy Shevchenko X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200725_152058_643640_A0424CE8 X-CRM114-Status: GOOD ( 15.16 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-iio , Bartosz Golaszewski , Linux Kernel Mailing List , Linux ARM Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, Jul 15, 2020 at 11:25 AM 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(). > > Signed-off-by: Bartosz Golaszewski > --- [snip!] > +void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp) > +{ > + struct devres *old_dr, *new_dr; > + struct list_head old_head; > + unsigned long flags; > + size_t total_size; > + void *ret = NULL; > + > + if (unlikely(!new_size)) { > + devm_kfree(dev, ptr); > + return ZERO_SIZE_PTR; > + } > + > + 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, &total_size)) > + return NULL; > + > + spin_lock_irqsave(&dev->devres_lock, flags); > + > + old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr); > + if (!old_dr) { > + spin_unlock_irqrestore(&dev->devres_lock, flags); > + WARN(1, "Memory chunk not managed or managed by a different device."); > + return NULL; > + } > + > + old_head = old_dr->node.entry; > + > + new_dr = krealloc(old_dr, total_size, gfp); Ugh, I wanted to check up on this patch and, after looking at it now, realized it's wrong. If the user calls devm_krealloc() with GFP_KERNEL we may end up sleeping with spinlock taken. Let me prepare another version. Bartosz _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel