Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1342149imm; Wed, 26 Sep 2018 16:21:12 -0700 (PDT) X-Google-Smtp-Source: ACcGV62CZECDxq1rseDn1FyQ3xLAF418NWYNA+I23KJscv7K6S+vLYqZLJqw6664mGzRTQ8jvFGH X-Received: by 2002:a63:531a:: with SMTP id h26-v6mr2257564pgb.441.1538004072761; Wed, 26 Sep 2018 16:21:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538004072; cv=none; d=google.com; s=arc-20160816; b=tm7//rd7E/yu8TByQDSb7uNOJ22NzPED9jxXNgrnEMOfcWeHJBq3C2Hkkpp4forB4J G94r4ivnIC44iOsp0IpuDAREf4tbD3YQtRjDmo0iqkjcguEhibY6oepCUgigXyK/5OWK I1Gl/M8ciSjk2pDH1eXePxMdztl0nj0aUL1b+yqiE++caXSFmZRKdVNYCncNEUsjK05d VIsCDSZF/pXSvujLWBz+heZV9mH5buumXP/OSo4nxpzv24lp7SZOYCkq/b3gzIb2TxtL AdHvL1uLmKImVa2gWMF3y3wwSOYwW3XLdIt+ILdHQWqd/6D05C9aOGG4F6oONg7X2gQh dxRQ== 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 :references:in-reply-to:mime-version:dkim-signature; bh=J4hnax56MWeBuquh4FUCFu79TGgJ3rZfC7klKDFSzWE=; b=kg8lZVC8fh7MSc5bNbUNDPeMecwqtRFeL5I7/zJ8BNIQAduUvX2oTEdKmLycAc2ve9 AFOf+xZ7VN+/+O22LB3Rt820Ldc5h4v0hJEXf9zSa8tGPTVAU6V9TKH1bt3NETGWGxdS q6S+Niz53rK/W8LOsQdBzOjIrxC5XGL0VKyauJtS8icvxM/gI6oWVewdB3jro9F+01Fp 8t+IiJ13bvNXgvLdnMYsI2Bsq+0lxdS07Z6kqm9rUnFHA/lfUpVhwtNpLGBFs3R5NU87 ATIl7oVHJkgfL3JyRrQCnzHtcsE/fI7bjDtXsrYuebh5gpB8/b454nm3BAi8OiB0OoXb ktSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Hp8K5uiS; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e185-v6si344230pgc.318.2018.09.26.16.20.46; Wed, 26 Sep 2018 16:21:12 -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=@chromium.org header.s=google header.b=Hp8K5uiS; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726566AbeI0Ffs (ORCPT + 99 others); Thu, 27 Sep 2018 01:35:48 -0400 Received: from mail-yw1-f66.google.com ([209.85.161.66]:41754 "EHLO mail-yw1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726107AbeI0Ffs (ORCPT ); Thu, 27 Sep 2018 01:35:48 -0400 Received: by mail-yw1-f66.google.com with SMTP id q129-v6so310022ywg.8 for ; Wed, 26 Sep 2018 16:20:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=J4hnax56MWeBuquh4FUCFu79TGgJ3rZfC7klKDFSzWE=; b=Hp8K5uiSwrR07FSa5kIMrD64+v3ydwDDZ2hHUpzBVrelF6bUxasuumC8ylGESdh1fX ewl2tE3sCv0In1Hv7VxIo9Rw219sASTc0Oh0xbkLVWpKyci3+2ysVWnYeJMQ/1R+AqI7 hQCtKsY0XS2cuhZvOfIUynlnzWD3HNXPra0hM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=J4hnax56MWeBuquh4FUCFu79TGgJ3rZfC7klKDFSzWE=; b=qlFI3DkakCR5lM50SbtSBiRhybaaHhbtBNjTuL1WwsmWdqISO9XPReeUDCa8/qjyma hnTm8DPodyz7LB63yIsjmqOfoW4MrZukWTwKBoUiMF+n6/JGmh4z9NGm7pQQwWj9DYxH xcTMR7y2cBRI5mQ1pv9TllFZbpyqfOfgD97zk1u1Q9oW/l6FtqOx3+0k/5OtGsxpJTIX hNqe/E5j7V6Pjyj6W1x0Z7CSIRgKiJUf0P4DC30WSaV8a6wNEQ0vE5dvei5HXw8g38Aj fJPOQped0vCpp7j82TvDyr6bF1FtcyMpAc7e6qFm+v4ddLCINB+3LSiOdvNHLN3zrpTU wIQA== X-Gm-Message-State: ABuFfoj6qP49QwB1dcxZlYyqVMf7qRMK1TSP/9vRbyGje+ZSiMbN68/9 27LDN0/HyCOEYBrNXK/GMr4hhyOAcq4= X-Received: by 2002:a0d:e20d:: with SMTP id l13-v6mr4352608ywe.216.1538004029248; Wed, 26 Sep 2018 16:20:29 -0700 (PDT) Received: from mail-yw1-f43.google.com (mail-yw1-f43.google.com. [209.85.161.43]) by smtp.gmail.com with ESMTPSA id m1-v6sm146303ywf.96.2018.09.26.16.20.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Sep 2018 16:20:29 -0700 (PDT) Received: by mail-yw1-f43.google.com with SMTP id p206-v6so296748ywg.12 for ; Wed, 26 Sep 2018 16:20:28 -0700 (PDT) X-Received: by 2002:a81:2288:: with SMTP id i130-v6mr4508506ywi.288.1538003620110; Wed, 26 Sep 2018 16:13:40 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:d116:0:0:0:0:0 with HTTP; Wed, 26 Sep 2018 16:13:39 -0700 (PDT) In-Reply-To: <20180924101150.23349-4-brgl@bgdev.pl> References: <20180924101150.23349-1-brgl@bgdev.pl> <20180924101150.23349-4-brgl@bgdev.pl> From: Kees Cook Date: Wed, 26 Sep 2018 16:13:39 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 3/4] devres: provide devm_kstrdup_const() To: Bartosz Golaszewski Cc: 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 , Bjorn Andersson , Arnd Bergmann , Andy Shevchenko , linux-clk@vger.kernel.org, LKML , Linux-MM 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 Mon, Sep 24, 2018 at 3:11 AM, Bartosz Golaszewski wrote: > 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); With devm_kfree and devm_kfree_const both taking "const", how are devm_kstrdup_const() and devm_kfree_const() going to be correctly paired at compile time? (i.e. I wasn't expecting the prototype change to devm_kfree()) -Kees > 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); > > -- > 2.18.0 > -- Kees Cook Pixel Security