Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp5641652imm; Mon, 27 Aug 2018 01:23:01 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdapgzu+DmksLl2hFHjYZK5NhnUTttJiXk58/LIekpnvK7ArxESzy+IIps8UpHMjfHU3ro1W X-Received: by 2002:a62:1d54:: with SMTP id d81-v6mr13372802pfd.139.1535358181818; Mon, 27 Aug 2018 01:23:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535358181; cv=none; d=google.com; s=arc-20160816; b=pjU8LsQl5UjLF/S73+94hUcZ7vscjXsrtGhRhKaSe/EDTiVeHDZJthF/3bo2P+BFU9 dC7N631eUUe/Ljr4hH4uqgu6a0Mju4MLaydE2EIDWVgWRXbola/Znk3QGTiRq2IJpMlB 6HT9mnw5C7cD4t9jtnwxgaAeFUPfu+nua64vWXTq+ivUD+wjHYnHdLwWOh/EVZBgIi5a pf1xW+p+668dJvBzHlQnf42IissCvZQ7VKvq5qGAdYNDsC+Ko6oP4xxxlc1jGdrnSpsZ KCuqNYgdcJ2sTdoH52p7vYTYrc3cC5miJlkJjRr9HmtM76939IOIzTOP7JvESfGKxB/p buKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=fE0L4qS4JAIcIeGVF5WHMr5nRwEMI4xXJIjWAI4oJwo=; b=TsHZYjBc0yTdHNhD/hpiE6sKerAumxbuKBu77oK6OqRjqdvR1uAJ3gGzInI93I2GRw PDQvuGlxhUrWXAGfsABAj6n9sVXSXnfOt8sYTflbAEMXJBwPJbGSGXQX6i8QYDWpEZKF +70EawmNztjbFX1Y2c6NkUuVRrOUin0xK5n12CBd0C0I98CWuHeD5TKz4TxyrhjlRVgm nV8LKhvqGfMimBVAOiv/b2EvQXaNCTqiT2gcUaxKC8zO6vySfNRhiisYLYUhsjJkJvyf jnNnfQsyXYocqzx9rD5D0JrbE8LEdawIMBsEFROoepjyt0+2wRqUlmiIP1mYEbHbMtT1 9L4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=bAKJB+6Z; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x1-v6si9746217plb.135.2018.08.27.01.22.46; Mon, 27 Aug 2018 01:23:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=bAKJB+6Z; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726953AbeH0MHP (ORCPT + 99 others); Mon, 27 Aug 2018 08:07:15 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:41001 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726785AbeH0MHO (ORCPT ); Mon, 27 Aug 2018 08:07:14 -0400 Received: by mail-wr1-f67.google.com with SMTP id z96-v6so12751480wrb.8 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=jajES9pdVjlgD2BYq7hkokIfrOrmlnyi7hopRF9cuHNweDoGEAiatBbeRJ3GtjNU+c e24hF6Rj+6othN8OOk8zbEtMcE2Qqv13bMSTzLRRAElamVL1OVdBWm1zjE6Jh5cFVnI4 4qij7JyeKBRigmeU1HFdIYdRz+N5Gxx5zHrIpop/oeNc/y94YqQoCmV1S+w7GKFh6iUm YHiBMt/RCsVIgkSGrOWFcG9gptfQ3C6kLK8uBsSMTmAlFEJuyFz8gfBbuwem09hxAlOd pFgehwbHN2uXhFMmzRhP/Zcqdh7hwZXO6KKuzhctq5SXqsZSW1ZX11cO4q0fxSNU9jbk wWag== X-Gm-Message-State: APzg51CyDXXd4tucueECyJEP+p28TC1PQlb/K0Jbr2zAQ63gJIajH2Ot cHTivBjLeE+vV9yTHFVy2miFzA== 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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 -- 2.18.0