Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp11709849rwl; Tue, 3 Jan 2023 03:40:21 -0800 (PST) X-Google-Smtp-Source: AMrXdXszArxatdrg3EOszE15gjgQd0Wav0t7ps//kS9Bl7yPcjtumnnLkchCNxIMZDIfsWRl5jbI X-Received: by 2002:a17:907:8e93:b0:7bf:3627:df32 with SMTP id tx19-20020a1709078e9300b007bf3627df32mr39430809ejc.75.1672746021511; Tue, 03 Jan 2023 03:40:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672746021; cv=none; d=google.com; s=arc-20160816; b=oVmydpCHsPG4YmOGneXgKRcZ4r+FeS5fZNaA0Xf0tXX2uG6uWo/n/ZpoLUdTW/OuDl uim8dLN0xZVgaJpRgroTQSVSkw0+pJr/k2lF+/wW+Mzy88AypeFOQphLOJNH4xoB9Pxn VgqmPdYHyvS3T9CXz0V3WxxYnwGEBnHVEgo5bSaoaaqAJrnqIg9yPMvngQhlHFFkbPw/ HWqp9Gcin2DAnpmvr+lfudmzCCSVgRKgZ+Bk1VqeY7V0G95DxigsoOgduQbFW8YJQRxc PJ1rrfdKPwv7+aN9Rl8uyUx2mkWQFfkZHtSgi1xzHFVTfs5xZ3C4mGjG3UuN3hfsyK/a khng== 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=AaoqwLXNd+Nz9MWozTVAllt6QGl7h33IQzMR/AB+Kr0=; b=MvYNdcn2cSyKR/LIEyysIx+U1MZsMQ6EFDtlSny7enAfSXDC5SvPacYDGJnbYdFpBo YREGKIUboVFU5Q08khnwlLiwnQDhOpwt9f+dZ2TmZEu0u98jHsWnFVdf5/K1vSRMw/8p Ow30ETWkew/pjFxupVV/5d5AuPGE8Yq4K0rVVOSL3yO7VIe+5vkhZD2CjMgjVyNB9+EN tIKP6VWrbRfQIEPOvSVfiNJkVadJ7kMtfqWFmL2XPTYezH4dJDuyuqOlNyqQm/xh/SeX o15BT3Xiw/E7zSYzCmFM3JQDY93yu54JeeoDUmnWjkuEmAZwmtY/nObDpR/Z+1eyLAHN 7org== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HUPZMWds; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qk31-20020a1709077f9f00b007c16ba64edcsi27635308ejc.637.2023.01.03.03.40.07; Tue, 03 Jan 2023 03:40:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HUPZMWds; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237554AbjACLeZ (ORCPT + 61 others); Tue, 3 Jan 2023 06:34:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237607AbjACLdj (ORCPT ); Tue, 3 Jan 2023 06:33:39 -0500 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D90F311444 for ; Tue, 3 Jan 2023 03:30:43 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id g25-20020a7bc4d9000000b003d97c8d4941so17307788wmk.4 for ; Tue, 03 Jan 2023 03:30:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=AaoqwLXNd+Nz9MWozTVAllt6QGl7h33IQzMR/AB+Kr0=; b=HUPZMWdsi2AuvTdn8RF8dL21BsHzEAxCGBURaoDxxLk9hZAPD763lN/U3uQVwe8bdf 8wsbVG+tgxEkcw/6QZOhB9tiwBJfLaMYHERDDxv7Y/m9vWBBgFzHoXzOk7rLgpSZ8RJ/ xqwH92Jv7b9gxbibHPcSWvPPHr9psyDVnhX/qrF2zakUZ3Npu2rMff9xI98Jcp2/8DCY lky7MgAN+trCwX18jr0SRIjRyP5b3PneyWoBgicZfp23q9dXomp4EcrsjKWphftICaxw M7fzJFYXmX4UUL9IbPKmU9c8GIykYj63Rk/o8yl6fqKSmvcyiOrzCX843WCxLCmNgxlN qdMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=AaoqwLXNd+Nz9MWozTVAllt6QGl7h33IQzMR/AB+Kr0=; b=N0Oqs0S3PJj4rfLiqeTh+ncaHxGZs6aQiSFjPana8SCpGdfV2Nppl5I0TRf1oX7cCD lWlfziAzQBKpos9wLvHeM1GqxwbVPrzv7r6mvL5Qjy2lLUxoWYpVPqRrldtfa+NA6kgl nU6IZEFK3aIZZU/FSFr9uKjE0V2Aeg8ykj8P/mhrTI7sTNg8rgunE6ahHGH491IUtega 7rvie8OGUcYM3gEcKhirj0cXtQluB/EjIWseSjOqdAvSwjHOD25ZLnF8gh+jeF7hM4vH etpGpdJSPHO7CTuJLqkA3eiNkf/EFLhgeYEhmGC4XcdZIsiurXdt7PY8lkbMEeAqeWq3 3Mgw== X-Gm-Message-State: AFqh2krJKGllXg50elC4mpRbs8cJdKgt/AlHo8Hmvg6AkwFUrz486+LJ tU6aUQkHL9rtkm/MutWir8w8JA== X-Received: by 2002:a05:600c:43ca:b0:3d0:73f5:b2c0 with SMTP id f10-20020a05600c43ca00b003d073f5b2c0mr32698726wmn.20.1672745438153; Tue, 03 Jan 2023 03:30:38 -0800 (PST) Received: from [192.168.1.195] ([5.133.47.210]) by smtp.googlemail.com with ESMTPSA id m28-20020a05600c3b1c00b003d1e3b1624dsm50704696wms.2.2023.01.03.03.30.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 03 Jan 2023 03:30:37 -0800 (PST) Message-ID: Date: Tue, 3 Jan 2023 11:30:36 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: [PATCH] nvmem: fix registration vs use race Content-Language: en-US To: "Russell King (Oracle)" Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Hector Martin References: From: Srinivas Kandagatla In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham 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 03/01/2023 09:42, Russell King (Oracle) wrote: > The i.MX6 CPU frequency driver sometimes fails to register at boot time > due to nvmem_cell_read_u32() sporadically returning -ENOENT. > > This happens because there is a window where __nvmem_device_get() in > of_nvmem_cell_get() is able to return the nvmem device, but as cells > have been setup, nvmem_find_cell_entry_by_node() returns NULL. > > The occurs because the nvmem core registration code violates one of the > fundamental principles of kernel programming: do not publish data > structures before their setup is complete. > > Fix this by making nvmem core code conform with this principle. > how about a Fixes tag and Cc stable? > Signed-off-by: Russell King (Oracle) > --- > drivers/nvmem/core.c | 18 ++++++++---------- > 1 file changed, 8 insertions(+), 10 deletions(-) > > diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c > index 321d7d63e068..6b89fb6fa582 100644 > --- a/drivers/nvmem/core.c > +++ b/drivers/nvmem/core.c > @@ -835,22 +835,16 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) > nvmem->dev.groups = nvmem_dev_groups; > #endif > > - dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); > - > - rval = device_register(&nvmem->dev); > - if (rval) > - goto err_put_device; > - > if (nvmem->nkeepout) { > rval = nvmem_validate_keepouts(nvmem); > if (rval) > - goto err_device_del; > + goto err_put_device; AFAIU, as we never did a get_device/kobject_get, calling put_device at this point will not invoke a release callback which can potentially leak both nvmem and ida. --srini > } > > if (config->compat) { > rval = nvmem_sysfs_setup_compat(nvmem, config); > if (rval) > - goto err_device_del; > + goto err_put_device; > } > > if (config->cells) { > @@ -867,6 +861,12 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) > if (rval) > goto err_remove_cells; > > + dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); > + > + rval = device_register(&nvmem->dev); > + if (rval) > + goto err_remove_cells; > + > blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); > > return nvmem; > @@ -876,8 +876,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) > err_teardown_compat: > if (config->compat) > nvmem_sysfs_remove_compat(nvmem, config); > -err_device_del: > - device_del(&nvmem->dev); > err_put_device: > put_device(&nvmem->dev); >