From patchwork Tue Aug 28 09:33:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 10578101 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AB099920 for ; Tue, 28 Aug 2018 09:42:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9BF0329A0C for ; Tue, 28 Aug 2018 09:42:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8E51B29A22; Tue, 28 Aug 2018 09:42:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1C18229A0C for ; Tue, 28 Aug 2018 09:42:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727238AbeH1Ndi (ORCPT ); Tue, 28 Aug 2018 09:33:38 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:35703 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727162AbeH1Ndh (ORCPT ); Tue, 28 Aug 2018 09:33:37 -0400 Received: by mail-wr1-f65.google.com with SMTP id j26-v6so938356wre.2 for ; Tue, 28 Aug 2018 02:42:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZhPS8aKWZHYYGJDbC8vwHCt0WY7YA5vftpevadBx97w=; b=K7DgoVj8B2c94KzxsFLciUUUVbJs9GG+54y9l44xJv0cIGKEsWtmJ6asfgtAuJRNWG 3YWTqiKNWfRuzi4MGrLExpWGQh33pnpPC31fZLOE1iAYaZnvNBt/3EJXVAqNj7ZSE+GI BMJhuQNbkhunybgsu80wI2MVHZF+0+S2fKypURC6G8Pc77I+SuMexD717Ca3+aF+LLbe UzJnEY249o+9lwmLaJUX6IZQ3YbgBPFpZpqVHYRbpKuZno3BH9GupI4N9Roz4FVRGDCH 96bpEf6CsVsmyV6piYYOMmj0WwEH7xTECiwsKNNBE6PoyKyKGeFQ9Ba7YdipTSodR1f+ fs9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZhPS8aKWZHYYGJDbC8vwHCt0WY7YA5vftpevadBx97w=; b=b+aViKQ6D0nbbKO2Ge1oT6F5Nf4aGnxVZI8mW0AXPic89pSWue0nLdSgXIPeLm2gWE w9GIHt79hNPE8dTHhuUbhDygI9agOt4LWva4i9JtFIOa5YYM9cxwTwgQfc12N9DvEflb 3p55lwWCYb8AKl4u+Ws2b4SlM2FEjTDEqB+dT74LNS95gj2HvHsodD5lXeHoXluBnkms SwV9pdN3iXyhVkcAlqN8F3vND81sbMqaAJZ+m/oauulz6fTd0RSwzjOg+2dxpZ8WlwKw +7vnQscW0mozlrvGhV2pvdgLBvPuZzMd2AvI1yunHaVym1vgUXwDROvqAYxu7uW389Z8 OMnA== X-Gm-Message-State: APzg51D6V+tpW5gJFkPLvwtuVCdISQD8fnuW0ovG2vHOrBpDDPGgBx15 3qoZFzQ5UDLtMi0PEhSoYI4Vrw== X-Google-Smtp-Source: ANB0VdazQ1b4GJa2MqkcHdXAY4E48XRwkQi/Vh5haOo3iQRppCTAdopcVEtiX1xS13EEr5bEkgPC0A== X-Received: by 2002:adf:e8c1:: with SMTP id k1-v6mr539276wrn.43.1535448849697; Tue, 28 Aug 2018 02:34:09 -0700 (PDT) Received: from brgl-bgdev.home ([2a01:cb1d:af:5b00:e837:b8d5:48c1:571b]) by smtp.gmail.com with ESMTPSA id l18-v6sm952276wru.75.2018.08.28.02.34.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Aug 2018 02:34:09 -0700 (PDT) From: Bartosz Golaszewski To: Michael Turquette , Stephen Boyd , Greg Kroah-Hartman , "Rafael J . Wysocki" , Arend van Spriel , Ulf Hansson , Bjorn Helgaas , Vivek Gautam , Robin Murphy , Joe Perches , Heikki Krogerus , Andrew Morton , Mike Rapoport , Michal Hocko , Al Viro , Jonathan Corbet , Roman Gushchin , Huang Ying , Kees Cook , Bjorn Andersson , Arnd Bergmann Cc: linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Bartosz Golaszewski Subject: [PATCH v2 3/4] devres: provide devm_kstrdup_const() Date: Tue, 28 Aug 2018 11:33:31 +0200 Message-Id: <20180828093332.20674-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180828093332.20674-1-brgl@bgdev.pl> References: <20180828093332.20674-1-brgl@bgdev.pl> Sender: linux-clk-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Provide a resource managed version of kstrdup_const(). This variant internally calls devm_kstrdup() on pointers that are outside of .rodata section and returns the string as is otherwise. Also provide a corresponding version of devm_kfree(). Signed-off-by: Bartosz Golaszewski Reviewed-by: Bjorn Andersson --- drivers/base/devres.c | 38 ++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 3 +++ 2 files changed, 41 insertions(+) diff --git a/drivers/base/devres.c b/drivers/base/devres.c index 438c91a43508..48185d57bc5b 100644 --- a/drivers/base/devres.c +++ b/drivers/base/devres.c @@ -11,6 +11,8 @@ #include #include +#include + #include "base.h" struct devres_node { @@ -822,6 +824,28 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) } EXPORT_SYMBOL_GPL(devm_kstrdup); +/** + * devm_kstrdup_const - resource managed conditional string duplication + * @dev: device for which to duplicate the string + * @s: the string to duplicate + * @gfp: the GFP mask used in the kmalloc() call when allocating memory + * + * Strings allocated by devm_kstrdup_const will be automatically freed when + * the associated device is detached. + * + * RETURNS: + * Source string if it is in .rodata section otherwise it falls back to + * devm_kstrdup. + */ +const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp) +{ + if (is_kernel_rodata((unsigned long)s)) + return s; + + return devm_kstrdup(dev, s, gfp); +} +EXPORT_SYMBOL(devm_kstrdup_const); + /** * devm_kvasprintf - Allocate resource managed space and format a string * into that. @@ -895,6 +919,20 @@ void devm_kfree(struct device *dev, const void *p) } EXPORT_SYMBOL_GPL(devm_kfree); +/** + * devm_kfree_const - Resource managed conditional kfree + * @dev: device this memory belongs to + * @p: memory to free + * + * Function calls devm_kfree only if @p is not in .rodata section. + */ +void devm_kfree_const(struct device *dev, const void *p) +{ + if (!is_kernel_rodata((unsigned long)p)) + devm_kfree(dev, p); +} +EXPORT_SYMBOL(devm_kfree_const); + /** * devm_kmemdup - Resource-managed kmemdup * @dev: Device this memory belongs to diff --git a/include/linux/device.h b/include/linux/device.h index 33f7cb271fbb..79ccc6eb0975 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -693,7 +693,10 @@ static inline void *devm_kcalloc(struct device *dev, return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO); } extern void devm_kfree(struct device *dev, const void *p); +extern void devm_kfree_const(struct device *dev, const void *p); extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc; +extern const char *devm_kstrdup_const(struct device *dev, + const char *s, gfp_t gfp); extern void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp);