Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4786675ybb; Tue, 24 Mar 2020 05:23:59 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuVuDri5AcDBCIcFHTvY2lBFOa0msTaYRkD9PwErYz53GYb7ltAbO36MKLR54qMnn1rYoA0 X-Received: by 2002:a9d:60b:: with SMTP id 11mr21476047otn.126.1585052639575; Tue, 24 Mar 2020 05:23:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585052639; cv=none; d=google.com; s=arc-20160816; b=wURNwXxFLu8bo2vX3ay8M4A/UY1aytiW2LrBVKynCm59238jIhJXlDUU1fMW4OXRxx RYa2fTQ8mSsLPSZ17e0uym5GTp8Mug2CoSy+28gAnp9iF5pfjgWVXEn8VZnlmz82vHCW eT04p38pphdrrqnQ3JBk8DYRRlKIHTSPjk1vWiaDWJwoiik7bCRcloMdjJsUWDRfKxJE 2eKHYEcgwBHsxFiLKW5K2D3EovucpNycYNCJf56MREnieo+6z9+dk04ieB811mq1+F3p 0iIsqeWlwYOk4hViBUHeiQ4LvZWxi915n2V97rHpXCeo6Zr9Nt8RxhpW2BWVq+OQbEYV Tj9w== 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=mufZG5/pHTT/BU7NPug772ycUZmf+Hdmb7Tdh23IbLY=; b=i1Hg6eJUrz4Z7kGcXIiPIC6mccUXA4FZxAd3WhJ1ZzvclhjgCY5dyhowBYuG1/7qKf Qo/Xe/XTHTIP9FHFdq9jDoX29Xuo3/tbo8DTI/20jaU7gVPOhWvfAM19o1fc7osrOa2m hq3DqzR8RVHcI2kHVhpjEQjeG00PGbXU1jZMon/CQPkTFXbeVFZ5QmFLc0BF8hZvzI9W 9UqPFIEkt+yU9Vuvc16hAhF23y4CTWRwdmokDiY/GH8N3eo09ofwgBBxmc4ZNUEHMlO6 0vJAdmrRSnmFGKgDL6MAr+OVLKYD3DdHG3vYJE1tqyqp1p83T9Rfe2JXNGkxwmhvbthz vM/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bhro6EjB; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v9si9271785otn.120.2020.03.24.05.23.46; Tue, 24 Mar 2020 05:23:59 -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=@linaro.org header.s=google header.b=bhro6EjB; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727405AbgCXMX1 (ORCPT + 99 others); Tue, 24 Mar 2020 08:23:27 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:37655 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727241AbgCXMX1 (ORCPT ); Tue, 24 Mar 2020 08:23:27 -0400 Received: by mail-wm1-f65.google.com with SMTP id d1so3229672wmb.2 for ; Tue, 24 Mar 2020 05:23:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=mufZG5/pHTT/BU7NPug772ycUZmf+Hdmb7Tdh23IbLY=; b=bhro6EjBkXlkItYYnrONWxE3yLRkV/9Ru6VZaTipS1qPzkuNggmuwscJyyu8h5wft6 b2qsEVSp9WAnyERCJloIcZVaWF8TDPo8nRLMjOqGdwDILl+rjksMuxexUbiatQMb/4Rc UP8iU52IwEZLQoBXoCnqWXxsE0ednzM/Cj45c0DZlL/eE92BX2OKQdu7DRhWm9YWJ15I ZwpN/m3k9xjEpJxZVZ+zecJ0oMm6tqfyh1zBt59YnfrVNYJEU513pQ8f5pE8FkWNj8Up PpkrxwG+/5MP33bmcpQTFQS8mCtgypTKsnxhrq6cZDbOvSFRyPjq0knxitnSLtse8gqv IFAA== 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=mufZG5/pHTT/BU7NPug772ycUZmf+Hdmb7Tdh23IbLY=; b=SvVGZ6zUql3gm7k7SyhQR/VR5LLPTBifYjq1A6v+nLCI2qOQY0xa2zeVHfg4l59HN6 SgH4tgTT7gxKONmVpHAM/LAtW9xSNo5lcTR02Cd1BsQ5qNg73fqbevdwC2tfsA80wL6a EozP08E9PfQndrD5xwZ0hpMTJ5qyY828jO1AhTnhNMzC4ErVN1WsvEi394hzeJJvt/Cu 1w+ELmyS0xzUVYZgGKrrJxObewiHcs3jCvnnF/2eXF752SNFK3NcO2Q5BPeT2E2WKm1a kR65SHEIryLSJdSguu8KAg76Jj2PCAatG34Ft6f+q9s6b90cGeSTU3EAuEQ8zHedJ5Vz W3fw== X-Gm-Message-State: ANhLgQ3XTXGQbiNpM2ELKZqsZf2ktPHMct/UNCBg7H67r0Ier78NIUec vqC1GaMyAECJ6ATmtG0NqvraYw== X-Received: by 2002:a7b:c359:: with SMTP id l25mr5369621wmj.149.1585052605119; Tue, 24 Mar 2020 05:23:25 -0700 (PDT) Received: from [192.168.86.34] (cpc89974-aztw32-2-0-cust43.18-1.cable.virginm.net. [86.30.250.44]) by smtp.googlemail.com with ESMTPSA id f9sm29463391wro.47.2020.03.24.05.23.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Mar 2020 05:23:24 -0700 (PDT) Subject: Re: [PATCH v5 1/1] nvmem: Add support for write-only instances To: Nicholas Johnson , "linux-kernel@vger.kernel.org" Cc: Mika Westerberg , Greg KH References: From: Srinivas Kandagatla Message-ID: <2565d557-0425-1a80-18c9-bab34355364c@linaro.org> Date: Tue, 24 Mar 2020 12:23:23 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed 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 Sorry for such late review, but I did endup finding two new issues with this patch. On 18/03/2020 15:20, Nicholas Johnson wrote: > There is at least one real-world use-case for write-only nvmem > instances. Refer to 03cd45d2e219 ("thunderbolt: Prevent crash if > non-active NVMem file is read"). > > Add support for write-only nvmem instances by adding attrs for 0200. > > Change nvmem_register() to abort if NULL group is returned from > nvmem_sysfs_get_groups(). > > Return NULL from nvmem_sysfs_get_groups() in invalid cases. > > Signed-off-by: Nicholas Johnson > --- > drivers/nvmem/core.c | 10 +++++-- > drivers/nvmem/nvmem-sysfs.c | 56 +++++++++++++++++++++++++++++++------ > 2 files changed, 56 insertions(+), 10 deletions(-) > > diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c > index 77d890d36..ddc7be514 100644 > --- a/drivers/nvmem/core.c > +++ b/drivers/nvmem/core.c > @@ -381,6 +381,14 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) > nvmem->type = config->type; > nvmem->reg_read = config->reg_read; > nvmem->reg_write = config->reg_write; > + nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config); > + if (!nvmem->dev.groups) { > + ida_simple_remove(&nvmem_ida, nvmem->id); > + gpiod_put(nvmem->wp_gpio); > + kfree(nvmem); > + return ERR_PTR(-EINVAL); > + } returning NULL is valid usecase here, consider it like not having CONFIG_NVMEM_SYSFS so no need to check the return value. But a warning/error message from nvmem_sysfs_get_groups would help debug in case of invalid configurations. > + > if (!config->no_of_node) > nvmem->dev.of_node = config->dev->of_node; > > @@ -395,8 +403,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) > nvmem->read_only = device_property_present(config->dev, "read-only") || > config->read_only || !nvmem->reg_write; > > - nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config); > - Moving this to before read_only flag is set would not work! as nvmem_sysfs_get_groups() is going to use read_only flag. Ideally you do not need to change anything in core.c file and just update nvmem-sysfs.c should be enough. > device_initialize(&nvmem->dev); > > dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); > diff --git a/drivers/nvmem/nvmem-sysfs.c b/drivers/nvmem/nvmem-sysfs.c > index 9e0c429cd..4ceac8680 100644 > --- a/drivers/nvmem/nvmem-sysfs.c > +++ b/drivers/nvmem/nvmem-sysfs.c > @@ -196,16 +196,49 @@ static const struct attribute_group *nvmem_ro_root_dev_groups[] = { > NULL, > }; > > +/* write only permission, root only */ > +static struct bin_attribute bin_attr_wo_root_nvmem = { > + .attr = { > + .name = "nvmem", > + .mode = 0200, > + }, > + .write = bin_attr_nvmem_write, > +}; > + > +static struct bin_attribute *nvmem_bin_wo_root_attributes[] = { > + &bin_attr_wo_root_nvmem, > + NULL, > +}; > + > +static const struct attribute_group nvmem_bin_wo_root_group = { > + .bin_attrs = nvmem_bin_wo_root_attributes, > + .attrs = nvmem_attrs, > +}; > + > +static const struct attribute_group *nvmem_wo_root_dev_groups[] = { > + &nvmem_bin_wo_root_group, > + NULL, > +}; > + > const struct attribute_group **nvmem_sysfs_get_groups( > struct nvmem_device *nvmem, > const struct nvmem_config *config) > { > - if (config->root_only) > - return nvmem->read_only ? > - nvmem_ro_root_dev_groups : > - nvmem_rw_root_dev_groups; > + /* Read-only */ > + if (nvmem->reg_read && (!nvmem->reg_write || nvmem->read_only)) > + return config->root_only ? > + nvmem_ro_root_dev_groups : nvmem_ro_dev_groups; > + > + /* Read-write */ > + if (nvmem->reg_read && nvmem->reg_write && !nvmem->read_only) > + return config->root_only ? > + nvmem_rw_root_dev_groups : nvmem_rw_dev_groups; > + > + /* Write-only, do not honour request for global writable entry */ > + if (!nvmem->reg_read && nvmem->reg_write && !nvmem->read_only) > + return config->root_only ? nvmem_wo_root_dev_groups : NULL; > > - return nvmem->read_only ? nvmem_ro_dev_groups : nvmem_rw_dev_groups; > + return NULL; > } > > /* > @@ -224,17 +257,24 @@ int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, > if (!config->base_dev) > return -EINVAL; > > - if (nvmem->read_only) { > + if (nvmem->reg_read && (!nvmem->reg_write || nvmem->read_only)) { > if (config->root_only) > nvmem->eeprom = bin_attr_ro_root_nvmem; > else > nvmem->eeprom = bin_attr_ro_nvmem; > - } else { > + } else if (!nvmem->reg_read && nvmem->reg_write && !nvmem->read_only) { > + if (config->root_only) > + nvmem->eeprom = bin_attr_wo_root_nvmem; > + else > + return -EINVAL; > + } else if (nvmem->reg_read && nvmem->reg_write && !nvmem->read_only) { > if (config->root_only) > nvmem->eeprom = bin_attr_rw_root_nvmem; > else > nvmem->eeprom = bin_attr_rw_nvmem; > - } > + } else > + return -EINVAL; > + > nvmem->eeprom.attr.name = "eeprom"; > nvmem->eeprom.size = nvmem->size; > #ifdef CONFIG_DEBUG_LOCK_ALLOC >