Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1867717imm; Thu, 27 Sep 2018 03:56:17 -0700 (PDT) X-Google-Smtp-Source: ACcGV62D+h7HQ+V+KnyseFkIlK67cLOWE8H2BL+GFVzNROEWeEbAhc+Bv/phIBWcCMuV114xR2y5 X-Received: by 2002:aa7:83cd:: with SMTP id j13-v6mr10723802pfn.236.1538045777191; Thu, 27 Sep 2018 03:56:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538045777; cv=none; d=google.com; s=arc-20160816; b=tO/L3fUsuK1odli5Q2a7PinleWBqyW/JCo8JlPIomnmrVlMFCLwkdryJv+NDV+4qHl 3gf1nJMTsKz3FpzNUw4jdPSn3veCL5/4JUmwPHN/PPjR0PtZImpNMXJLdN+GYCnsyFf8 dRaZ+/7K8hN5ttRLMH7VsF4s4uj7nkL73v9inaEBivOCvsbHDjyF8cCDfFUe5B4wAWvz SlKKAGT2QdfMreF3Swp3xvQzTKPkpOllSPnTnbKkDHXTXR8ey70t5U/HszjDFwDdo5Tw r4EWSyQGIC+B502/POdlN8Sky3HP6jj4Xbx/GPGF/L+Jh4ShHg4CHuB9dYrmPcURDRur Fwhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=e5dSaq6nqL0GUtqykX2UvAZc43uMIT6lNzwgjsWybkU=; b=Ftd1iDv6CGSzzmIpTzdiWSjOmBGpLbYHKBln76aPlWLoimdQbBGlzCLEXkARC5wW9x 5C+q/jpD7dPGBMhPjrttB56qlqrI1OnnIe5EDUsjDUqf5u4OJrfM/d2HaL4E0RsLlb8a c/fg0mvnUasCc0OfC189VJ5IfhuOssvpbsJCGkgAKBRpxjMiqGo54izdpHcpZyyi8KLI MNZOOctRbB3uxU5GDavHU14aA/6QJsNdiPLvxfPhAfWc3ZYGBNBXWuk+6xmi+RD7y9Nv Zt8WKplzmG3YkTtiRUnr7bOBG+tWVi5SfrrAy5G9JDtB8y6OsWyY7SCete9I40UTMe/b ww4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=UUw97vf3; 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 n2-v6si1633860plp.276.2018.09.27.03.56.02; Thu, 27 Sep 2018 03:56:17 -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=@rasmusvillemoes.dk header.s=google header.b=UUw97vf3; 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 S1727428AbeI0RM5 (ORCPT + 99 others); Thu, 27 Sep 2018 13:12:57 -0400 Received: from mail-lj1-f194.google.com ([209.85.208.194]:33229 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726948AbeI0RM4 (ORCPT ); Thu, 27 Sep 2018 13:12:56 -0400 Received: by mail-lj1-f194.google.com with SMTP id l19-v6so1979297ljb.0 for ; Thu, 27 Sep 2018 03:55:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=e5dSaq6nqL0GUtqykX2UvAZc43uMIT6lNzwgjsWybkU=; b=UUw97vf3XWC6fThzrgdIA965omDm3VsX8HyhKSka/R2ZIHZDQXewVaKmdf7GLZ/Uri SixfNaYwAJb0Atlsu7v0ia5tar7npk1edKAh996xoWcXPtn1APA86DsI6En9hltOB8TP C/kNgGtbPFL6QhYFXxpdKn6lisDKaLW5VKiLA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=e5dSaq6nqL0GUtqykX2UvAZc43uMIT6lNzwgjsWybkU=; b=nUOfymwchx5B9a+nBo/eYyzAua6OBPinXX6ztVlm2CAE52+gNloUU11tsWvk2P+8rT YN0H6LW+QX8ha02I5pyHx9raAydWpjX+lPh5iQm2Z1y2kcGORnejhYMqcL3raCq9nhWO cYQq96vi024j2Lnc9QgtxUyOFTsdZXE5Xdc79cYbFmr8ApV8yfzGAjwoSaS+tILP+5rZ IWkWNLYJJMCqB9XaSJzjmusdb1GNmotKZDpOcFx6kz7bPPkjDqDrZKOOqsBJUoF/Joys O0kIfcUTXGHkLoAG+KyBccxviJThIalleLx39aR8Fmp6ZDJwwFdovyC33l+t+7dHSI29 fYDQ== X-Gm-Message-State: ABuFfohK3YashS4g1K5t9YgqXdBtwjN+kRIs3pmCGgxQ2cmG32Dyn10q xjUUAC7JQzH+HTS9n1WQihkvUQ== X-Received: by 2002:a2e:2c15:: with SMTP id s21-v6mr161813ljs.162.1538045713061; Thu, 27 Sep 2018 03:55:13 -0700 (PDT) Received: from [172.16.11.40] ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id a15-v6sm357194ljf.92.2018.09.27.03.55.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Sep 2018 03:55:12 -0700 (PDT) Subject: Re: [PATCH v3 3/4] devres: provide devm_kstrdup_const() To: Kees Cook , 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 References: <20180924101150.23349-1-brgl@bgdev.pl> <20180924101150.23349-4-brgl@bgdev.pl> From: Rasmus Villemoes Message-ID: <9ad301dc-47ef-cd7d-699d-e51716d1703f@rasmusvillemoes.dk> Date: Thu, 27 Sep 2018 12:55:10 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018-09-27 01:13, Kees Cook wrote: > 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(). >> >> +/** >> + * 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()) Just drop devm_kfree_const and teach devm_kfree to ignore is_kernel_rodata(). That avoids the 50-100 bytes of overhead for adding yet another EXPORT_SYMBOL and makes it easier to port drivers to devm_kstrdup_const (and avoids the bugs Kees is worried about). devm managed resources are almost never freed explicitly, so that single extra comparison in devm_kfree shouldn't matter for performance. Rasmus