Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp912635pxp; Wed, 16 Mar 2022 21:04:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzYuK+qdtVfs4b3rYa7zmZ0CnpLi7q7G7o0WbH9Br+2jC86oSSc0JsrPHpkA3jgq/c1Wvra X-Received: by 2002:a17:902:e54c:b0:151:ead7:b8bb with SMTP id n12-20020a170902e54c00b00151ead7b8bbmr2762145plf.5.1647489897574; Wed, 16 Mar 2022 21:04:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647489897; cv=none; d=google.com; s=arc-20160816; b=lim9HSelZavCqBhgv/TnYei96Imk+nC/Iss4qrXbcf1qmxTI4uzgBBgBpCsN+SYzh/ Xtv0d/hW9LRGMjhrPt+XK9mOQLaa/IfaDLi57Xu6VuKOBKqPOIcUq/gtCq7SMizP01HQ lNImcSl1x/aOzZWvRKKYeC+q5ftsjsKeLe5tMw9KjHkqv+b7LeSEK3g8Vn+wH1JTmC8y c8wDr+/3afVU+8N/BOS8dXylH9qGzZBfeVrhX5kacYdDGqIpxJqmIbc21s4s+WJA+ove XQz/WQEE9w8m15ZyIgx/k0tOPhTXCSfCESLMkjHRH3M/RgoZzjx847UvZcmJDVjYVpDv nDBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=WEml57iQP8ABbI+mDA7s3AB7OtRvXlB4GSsR8dRD3Dc=; b=ZDfhmlAMHfcZGm07CewNeBFEEUGW/4iBq4fHmcNLG9UB7zJec0sjcW+k4C4ez9YHxM ccxfFdm9eR/hrk4kvf/v4Y2bPKgREYPusjhipBJScdJRPomf4ru7nl+AJKEqVKPjunzr TmQA05zxjdxv9a00+LpgRdo8SdB0258UoueSJWwNm1HyJQCX3ynebhwJKuHXYrvjC+BA BD+T7FUn7WTgHsa/XxGXQWHXx41FIFmPkQoQmnQ0ezi2VRtbybeHPVPaMkSpzKyldxjo 4EVnTLMuiJlbXRqUqoVLp2NDlodKC4R/wCh7eeuUqyUiAnvBKEBrV4YaIaKKErXWK0jW FBUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=Q8imu69a; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id e22-20020a17090ab39600b001bd14e0303csi1125796pjr.20.2022.03.16.21.04.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 21:04:57 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=Q8imu69a; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5A048954BD; Wed, 16 Mar 2022 20:46:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355609AbiCPMOh (ORCPT + 99 others); Wed, 16 Mar 2022 08:14:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355596AbiCPMOf (ORCPT ); Wed, 16 Mar 2022 08:14:35 -0400 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 535C633EA4 for ; Wed, 16 Mar 2022 05:13:21 -0700 (PDT) Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 0FBDD3F4C1 for ; Wed, 16 Mar 2022 12:13:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1647432800; bh=WEml57iQP8ABbI+mDA7s3AB7OtRvXlB4GSsR8dRD3Dc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Q8imu69aXqZ6upMRhXGqxg2AJ/IvlPXVySEhBS6qsc2Bk3jWq/dzn4pLK+htHA2Z8 kZTHYiFzd4FK/TJeXKwPz1xlUpCdNr3tLcH9l96M3zeKRu5s34yAxq1yMH7/t/PUlI B/pB4CbX4TyHXXQ1iC11Bnk5NRZvDkVJ4hnk9+FPNmENYxizehOAdPsw/p8V2zowm5 wbcAlXeRqGyb1uQURSmhfxpop6nw4dNtvWODfKg0CbYgI7y71fRDd2fCyuhFQXWu6K X8BDwQGLZMwnRxpTBmF/gzYpDFMNLJMIhUTYE/dtw6UW27hswq7dypoOkW3zIi5S1d pt77Ll3bQZEjg== Received: by mail-ed1-f69.google.com with SMTP id i5-20020a056402054500b00415ce7443f4so1202115edx.12 for ; Wed, 16 Mar 2022 05:13:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=WEml57iQP8ABbI+mDA7s3AB7OtRvXlB4GSsR8dRD3Dc=; b=JN+L1okMgTxCLAUJBudzuGrZhagzLbrhBp6qcj6CkZj1bGHnCxvBjgwFCmrn6NvOO3 Z+4KuGfmVUripb0KFXq4R3Zz0ysn8DL1l3Gw/UnNuPmuK7Q+M8B8ffO2u797uuC/624f hAu58zeE+uqggamZG9USKH0OkRrynX0nHZT+sPWtnOCxMScjL9xu6HtuhDwFQ0tVoYHI DyexXHBOZKmz28zKiwgbsMwaKOku0O6LppNfpJAVFBAH18NtNWzTbJSohQtRYWbsrNRu +nFEoza4FvLjgSnu5oSp4QLL0/mKzbfKA0GF3NlZeYOxNoUe3ADxfy+dsCfuTI2nWLyG yTww== X-Gm-Message-State: AOAM5327qK0UKizksYu3XMlFiH4WJll9TLDZQUjjNcuNbI5uVZU/uTxu h4pnssBtOsO9tBDCkK3PDfPsg3sgUfm0fHX0KiXb3ikdXjAgsUfdocjaNdwKVYfQesuLMSDj1CB dkejPhttIZjKLGf/QSkO9anh5UjI97HsIQlWMWHMxMA== X-Received: by 2002:adf:8063:0:b0:1ef:78e3:330 with SMTP id 90-20020adf8063000000b001ef78e30330mr23936028wrk.424.1647432789105; Wed, 16 Mar 2022 05:13:09 -0700 (PDT) X-Received: by 2002:adf:8063:0:b0:1ef:78e3:330 with SMTP id 90-20020adf8063000000b001ef78e30330mr23935984wrk.424.1647432788803; Wed, 16 Mar 2022 05:13:08 -0700 (PDT) Received: from [192.168.0.17] (78-11-189-27.static.ip.netia.com.pl. [78.11.189.27]) by smtp.googlemail.com with ESMTPSA id o11-20020adf9d4b000000b001f0077ea337sm1541013wre.22.2022.03.16.05.13.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Mar 2022 05:13:08 -0700 (PDT) Message-ID: Date: Wed, 16 Mar 2022 13:13:06 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH v4 01/11] driver: platform: Add helper for safer setting of driver_override Content-Language: en-US To: Andy Shevchenko Cc: Greg Kroah-Hartman , "Rafael J. Wysocki" , Stuart Yoder , Laurentiu Tudor , Abel Vesa , Shawn Guo , Sascha Hauer , Fabio Estevam , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Bjorn Helgaas , Bjorn Andersson , Mathieu Poirier , Vineeth Vijayan , Peter Oberparleiter , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Andy Gross , Srinivas Kandagatla , Mark Brown , "Michael S. Tsirkin" , Jason Wang , Linux Kernel Mailing List , linux-clk , NXP Linux Team , linux-arm Mailing List , Linux on Hyper-V List , linux-pci , linux-remoteproc@vger.kernel.org, linux-s390@vger.kernel.org, linux-arm-msm@vger.kernel.org, ALSA Development Mailing List , linux-spi , virtualization@lists.linux-foundation.org, Linus Torvalds , Rasmus Villemoes References: <20220312132856.65163-1-krzysztof.kozlowski@canonical.com> <20220312132856.65163-2-krzysztof.kozlowski@canonical.com> From: Krzysztof Kozlowski In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 15/03/2022 18:59, Andy Shevchenko wrote: > On Sat, Mar 12, 2022 at 5:16 PM Krzysztof Kozlowski > wrote: >> >> Several core drivers and buses expect that driver_override is a >> dynamically allocated memory thus later they can kfree() it. >> >> However such assumption is not documented, there were in the past and >> there are already users setting it to a string literal. This leads to >> kfree() of static memory during device release (e.g. in error paths or >> during unbind): >> >> kernel BUG at ../mm/slub.c:3960! >> Internal error: Oops - BUG: 0 [#1] PREEMPT SMP ARM >> ... >> (kfree) from [] (platform_device_release+0x88/0xb4) >> (platform_device_release) from [] (device_release+0x2c/0x90) >> (device_release) from [] (kobject_put+0xec/0x20c) >> (kobject_put) from [] (exynos5_clk_probe+0x154/0x18c) >> (exynos5_clk_probe) from [] (platform_drv_probe+0x6c/0xa4) >> (platform_drv_probe) from [] (really_probe+0x280/0x414) >> (really_probe) from [] (driver_probe_device+0x78/0x1c4) >> (driver_probe_device) from [] (bus_for_each_drv+0x74/0xb8) >> (bus_for_each_drv) from [] (__device_attach+0xd4/0x16c) >> (__device_attach) from [] (bus_probe_device+0x88/0x90) >> (bus_probe_device) from [] (device_add+0x3dc/0x62c) >> (device_add) from [] (of_platform_device_create_pdata+0x94/0xbc) >> (of_platform_device_create_pdata) from [] (of_platform_bus_create+0x1a8/0x4fc) >> (of_platform_bus_create) from [] (of_platform_bus_create+0x20c/0x4fc) >> (of_platform_bus_create) from [] (of_platform_populate+0x84/0x118) >> (of_platform_populate) from [] (of_platform_default_populate_init+0xa0/0xb8) >> (of_platform_default_populate_init) from [] (do_one_initcall+0x8c/0x404) > >> (do_one_initcall) from [] (kernel_init_freeable+0x3d0/0x4d8) >> (kernel_init_freeable) from [] (kernel_init+0x8/0x114) >> (kernel_init) from [] (ret_from_fork+0x14/0x20) > > I believe you may remove these three. Sure (for this and later comments). > >> Provide a helper which clearly documents the usage of driver_override. >> This will allow later to reuse the helper and reduce amount of > > the amount > >> duplicated code. > >> Convert the platform driver to use new helper and make the > > a new > >> driver_override field const char (it is not modified by the core). > > ... > >> +/** >> + * driver_set_override() - Helper to set or clear driver override. >> + * @dev: Device to change >> + * @override: Address of string to change (e.g. &device->driver_override); >> + * The contents will be freed and hold newly allocated override. >> + * @s: NUL terminated string, new driver name to force a match, pass empty > > NUL-terminated? (44 vs 115 occurrences) > >> + * string to clear it >> + * @len: length of @s >> + * >> + * Helper to set or clear driver override in a device, intended for the cases >> + * when the driver_override field is allocated by driver/bus code. >> + * >> + * Returns: 0 on success or a negative error code on failure. >> + */ >> +int driver_set_override(struct device *dev, const char **override, >> + const char *s, size_t len) >> +{ >> + const char *new, *old; >> + char *cp; >> + >> + if (!dev || !override || !s) >> + return -EINVAL; >> + >> + /* >> + * The stored value will be used in sysfs show callback (sysfs_emit()), >> + * which has a length limit of PAGE_SIZE and adds a trailing newline. >> + * Thus we can store one character less to avoid truncation during sysfs >> + * show. >> + */ >> + if (len >= (PAGE_SIZE - 1)) >> + return -EINVAL; >> + >> + new = kstrndup(s, len, GFP_KERNEL); >> + if (!new) >> + return -ENOMEM; >> + >> + cp = strchr(new, '\n'); >> + if (cp) >> + *cp = '\0'; > > AFAIU you may reduce memory footprint by > > cp = strnchr(new, len, '\n'); > if (cp) > len = s - cp; > > new = kstrndup(...); > Indeed, thanks. Best regards, Krzysztof