Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1875527imm; Thu, 27 Sep 2018 04:03:46 -0700 (PDT) X-Google-Smtp-Source: ACcGV61gzfERtEmjwbOfMUC8G9r0WZIZObNrfF9t1ua7fiz1nQjex8Pwd8hS/dHCzRAopv+sWCco X-Received: by 2002:a17:902:a606:: with SMTP id u6-v6mr2989665plq.69.1538046226271; Thu, 27 Sep 2018 04:03:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538046226; cv=none; d=google.com; s=arc-20160816; b=lJHo2YLyocNHPNfjMIFXEHokAyGmNc0Bl0a6WPrLBUsmSaFmG74ttUEJznEd1fDiUz 9aqvjFRxsg14fkh+fbAW1GJKYP8dWimLnAWjhsgYDbxaC6rcu5eVuKSmdlS/EiXyXrOe SoFpzLXpkG5q0muCBzC9RNbp9BfhoMBZWsGRSKptFvSd1RW02j8UG8lhsqhWljn0AFos OynVckxW8TaVJwuzqZpoyMJGpEzX6d8NwY1/PjACMDYGfF6ivYHC8EDzdolZTbKaghse D26inpE5a8iJftGjURb2byje6RgnxJCYIeMIH8TY7rcbvd6tzRocFekGhZ7yi6QPqlfu jQtA== 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 :in-reply-to:references:mime-version; bh=7HJUfQz7O6e48xTUuJGMgp+ZT+lbPUT9KkUOBt+vzK0=; b=IT6NfpEccsAfpouclXA9l/VlWHKtTz/nXbXp2DZcI4rgVbilyzr8Ql4JwVEDtXo9rw +PRkZnTFyiQcMW1JMvO6UHX3QnJsSaO208FGZkfjm2eA9nqqNtYl+wOkZxPv4jxUSg50 HZmGXS8owRZVlCFNJ3HMNcREDDIlVa7MdRo1y1+bJhCEdXpPLHyOw+lE49Ua9bzklgwJ MGPu880wcDrbXehiBvLQ5Xrg5cv0XBGe5PLPTxv8ffJZSU5kkPkB3crLr0qP8xFDHF2I QjrXCq+nEq3IArEcxRfCHMwh3jfBMTfLHrTe9HNNI7NSo71uMuPiKCgLHASfBaqnOPCQ Vf+Q== ARC-Authentication-Results: i=1; mx.google.com; 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 x19-v6si1599358pln.465.2018.09.27.04.03.31; Thu, 27 Sep 2018 04:03:46 -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; 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 S1727297AbeI0RTs (ORCPT + 99 others); Thu, 27 Sep 2018 13:19:48 -0400 Received: from mail-vk1-f193.google.com ([209.85.221.193]:34986 "EHLO mail-vk1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726820AbeI0RTr (ORCPT ); Thu, 27 Sep 2018 13:19:47 -0400 Received: by mail-vk1-f193.google.com with SMTP id w193-v6so487158vke.2; Thu, 27 Sep 2018 04:02:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=7HJUfQz7O6e48xTUuJGMgp+ZT+lbPUT9KkUOBt+vzK0=; b=dr0x7cURRh99Vt4Emp7EFYWhoHtbJnvkf4M8JljbbfLocIt90p3sEeRnQHQpZhLCv0 hpsp36xu9DlzVauOrmCPrL+ZojmE/EAIzYUconvK8G3P9FNofECgcT5UZx2NIyCrC1+L scocFjcPVhq2uoOBB9l69BoRAikjvaZSO7rVZSN+XH3eZSXpFzTTUCQUILrLvvavgmMf wx//Fm2Wf1v4x7QlzANf1/XCUVwPNkYEk3zl+2pm31RGPefs5GpWKbPAcCgrUhsslzyN x+SWdcdt1pBrPJJ16Jd0rKxN05Lltee9vYSkSk+idv51Odm6yAC+4czp9dweYmtnn9c+ CfIQ== X-Gm-Message-State: ABuFfohQF0J8z6H8yTtZuz/tWBDp6Gpb38IDZ7qFBkvHzTDkjgeqaHSZ 32bqaAGMYOGM/qEA/u0peWJqilWFDJLUZxw5mm0= X-Received: by 2002:a1f:2ad6:: with SMTP id q205-v6mr3318912vkq.83.1538046123270; Thu, 27 Sep 2018 04:02:03 -0700 (PDT) MIME-Version: 1.0 References: <20180924101150.23349-1-brgl@bgdev.pl> <20180924101150.23349-4-brgl@bgdev.pl> <9ad301dc-47ef-cd7d-699d-e51716d1703f@rasmusvillemoes.dk> In-Reply-To: <9ad301dc-47ef-cd7d-699d-e51716d1703f@rasmusvillemoes.dk> From: Geert Uytterhoeven Date: Thu, 27 Sep 2018 13:01:50 +0200 Message-ID: Subject: Re: [PATCH v3 3/4] devres: provide devm_kstrdup_const() To: Rasmus Villemoes Cc: Kees Cook , Bartosz Golaszewski , Michael Turquette , Stephen Boyd , Greg KH , "Rafael J. Wysocki" , Arend van Spriel , Ulf Hansson , Bjorn Helgaas , vivek.gautam@codeaurora.org, Robin Murphy , Joe Perches , Heikki Krogerus , Andrew Morton , Mike Rapoport , Michal Hocko , Al Viro , Jonathan Corbet , guro@fb.com, Huang Ying , =?UTF-8?Q?Bj=C3=B6rn_Andersson?= , Arnd Bergmann , Andy Shevchenko , linux-clk , Linux Kernel Mailing List , 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 Hi Rasmus, On Thu, Sep 27, 2018 at 12:55 PM Rasmus Villemoes wrote: > 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. I guess we can also teach kfree() to ignore is_kernel_rodata(), and drop kfree_const()? Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds