From patchwork Mon Aug 27 08:21:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 10576613 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 6D29F17DB for ; Mon, 27 Aug 2018 08:21:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5B38029938 for ; Mon, 27 Aug 2018 08:21:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 58CDE2994B; Mon, 27 Aug 2018 08:21:49 +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 0090629944 for ; Mon, 27 Aug 2018 08:21:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726945AbeH0MHP (ORCPT ); Mon, 27 Aug 2018 08:07:15 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:36927 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726918AbeH0MHO (ORCPT ); Mon, 27 Aug 2018 08:07:14 -0400 Received: by mail-wr1-f67.google.com with SMTP id u12-v6so11454480wrr.4 for ; Mon, 27 Aug 2018 01:21:38 -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; bh=fE0L4qS4JAIcIeGVF5WHMr5nRwEMI4xXJIjWAI4oJwo=; b=bAKJB+6ZeJaBDy0gRuAYKSpwqe8e7mxVfGS3uGmJ9XEnoHTA/g9z+ibiHWvvAt5We9 A0HA9PUmAlPtA4SoAn9CMXYZD2S/tgSqpZbGQzoH7/Q8PcxfnKZOXpnac9lU2GXqhCfW F7xIPrhcZ9D3e65SPVHmlwyG+JCuoxkT+rPeTF4x9XsOAgv6gyapsKrMWSjgWSIlRpBQ 0ddGzn3PSS0XCslxGwK0hDuoi/7RpAiQPH8/6w7ymGN/6MQI39yaIU7sm5h/XHtg9nUn /ejm45JtXTw5Ex0ntGhVoqQRbNsSa/tNjBpqMV/kkYQ/sQe0b2G6WQ4iJa/QgH89UOYp nUAw== 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; bh=fE0L4qS4JAIcIeGVF5WHMr5nRwEMI4xXJIjWAI4oJwo=; b=CkBZutmSR4uu8luY/NqsfB7P40RiaNDKMwwyjNYyHFXSoNtvMr8KqIR7X9f7gegvuR SZf/uz+MqAGe2n4N93AhHLoSfnLS40esRrI1C5p1Mlh6ssbP8UwNzTl6WZizoGQIKrD2 d3FkWFmNIXPiLw4CgMxZNfKOO52Mn7y1oAF0WHzPiAsuyL6EQdTa1jd2F88Pr2CwK9Ld Rqn1jRPod/YEUZAkUvrhmoPMwCHEQRPFfPQbiuViWf9iBXTEDl3JWlO/bB2B8kMpusO7 EmWNL6X3zYuR1zDGRrOLxzk4tFIHGCit7vl08lYaNcA5v9qSZohHzs7STRrQTaP58h5X Jv0g== X-Gm-Message-State: APzg51DWF2x+WQHe+gy7pj/Yq13OixV9XsfJoJd3mAKD2VlFSQd/ulX3 7afRoThPGWAR3x9wAgiWY9S+6w== X-Google-Smtp-Source: ANB0VdYVp6PH5lkGi93mUyekgo0jMNaTlitZiC7F1f47jSxuq+FzHEVKpHdnWQCwlydgRgnKfRA7nw== X-Received: by 2002:adf:b583:: with SMTP id c3-v6mr7953572wre.79.1535358097344; Mon, 27 Aug 2018 01:21:37 -0700 (PDT) Received: from brgl-bgdev.baylibre.local (AStLambert-681-1-34-229.w90-86.abo.wanadoo.fr. [90.86.153.229]) by smtp.gmail.com with ESMTPSA id b144-v6sm15402309wmd.23.2018.08.27.01.21.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Aug 2018 01:21:36 -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 Cc: linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Bartosz Golaszewski Subject: [PATCH 1/2] devres: provide devm_kstrdup_const() Date: Mon, 27 Aug 2018 10:21:00 +0200 Message-Id: <20180827082101.5036-1-brgl@bgdev.pl> X-Mailer: git-send-email 2.18.0 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. Also provide a corresponding version of devm_kfree(). Signed-off-by: Bartosz Golaszewski --- include/linux/device.h | 2 ++ mm/util.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/linux/device.h b/include/linux/device.h index 8f882549edee..f8f5982d26b2 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -693,7 +693,9 @@ 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, void *p); +extern void devm_kfree_const(struct device *dev, void *p); extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc; +extern 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); diff --git a/mm/util.c b/mm/util.c index d2890a407332..6d1f41b5775e 100644 --- a/mm/util.c +++ b/mm/util.c @@ -39,6 +39,20 @@ void kfree_const(const void *x) } EXPORT_SYMBOL(kfree_const); +/** + * 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, void *p) +{ + if (!is_kernel_rodata((unsigned long)p)) + devm_kfree(dev, p); +} +EXPORT_SYMBOL(devm_kfree_const); + /** * kstrdup - allocate space for and copy an existing string * @s: the string to duplicate @@ -78,6 +92,27 @@ const char *kstrdup_const(const char *s, gfp_t gfp) } EXPORT_SYMBOL(kstrdup_const); +/** + * 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 + * + * Function returns source string if it is in .rodata section otherwise it + * fallbacks to devm_kstrdup. + * + * Strings allocated by devm_kstrdup_const will be automatically freed when + * the associated device is detached. + */ +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); + /** * kstrndup - allocate space for and copy an existing string * @s: the string to duplicate