Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3639177ybv; Tue, 25 Feb 2020 04:52:03 -0800 (PST) X-Google-Smtp-Source: APXvYqy5/7/6bj4u5oOo0hx3foRyeTwWShE9J8qYwJHtpFg/F1jLYU5hi24PYzvnLz8SzuugmMaS X-Received: by 2002:a9d:6005:: with SMTP id h5mr46624250otj.153.1582635123048; Tue, 25 Feb 2020 04:52:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582635123; cv=none; d=google.com; s=arc-20160816; b=s23L1JclRsR+XXY6HN/Maex8N95WI638xEZOTdum2CQjX3JM7ktuftY73o5aFk3IaX ZTdJgx4EHENMvJSF9mvY0J9AcswVxCAu0TSNOe7SWobiUOyw1UFXMcvWZIUk+xSTVH0s WoKp8mmsmzjsvLCCFOX/cyPxGPYgPMM1VTGCnVin4/HEjeScTaTfa+neqQ4RvTb9NvnA YUGSun7eZ51gVr6Tj2rLxM3/mvQO086vqd8hiTayjxTNJ6Kn15zVUF4Bv+dBrjUPPXZK JLvDvMhsFILnMsu6hdUKlbYYc2Bj6iGeKAbOgNXn0ViklEuEt32j72GZNqh0sq9qtyHQ ws+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:organization:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=miALDtr1kgx71cvgfFd9yqldm7fLmOIGf3ytGOYSs+8=; b=Lj+9Ny74D72i8Ks3HKLbTXMkeecXGcBaz7qO7YQhNr8QrZKl7S8JCvYlWO/ZotZZZX K5RaFkhxvfI8X4VUs26fX9SZIDPmR5mjR61MyOKoN3iM99kXtrbM9GDHsk4pSKuIEEkR WXDszaj/LrXba+pUnSmX+t9W7x+Om+1/bTYl3eVVOgmFQVzcnvHeo5IznaqMVfjnpcyM WXOinzMarQ1tZ5RPt2aoIxnAxC3MSKoeiRgz+QKdcLMn4Ybc37tjY883GIx9DLPaHym5 JZIOxkO9N8MQnWe0druNxOMS1xPbGE+BQ8DJDL4CjeHMfr0W9Jm4SQN7Q9E4AsVAucVJ LpxA== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l9si7239712oti.229.2020.02.25.04.51.51; Tue, 25 Feb 2020 04:52:03 -0800 (PST) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729609AbgBYMvq (ORCPT + 99 others); Tue, 25 Feb 2020 07:51:46 -0500 Received: from mga02.intel.com ([134.134.136.20]:47567 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728913AbgBYMvq (ORCPT ); Tue, 25 Feb 2020 07:51:46 -0500 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Feb 2020 04:51:45 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,484,1574150400"; d="scan'208";a="350143106" Received: from lahna.fi.intel.com (HELO lahna) ([10.237.72.163]) by fmsmga001.fm.intel.com with SMTP; 25 Feb 2020 04:51:42 -0800 Received: by lahna (sSMTP sendmail emulation); Tue, 25 Feb 2020 14:51:41 +0200 Date: Tue, 25 Feb 2020 14:51:41 +0200 From: Mika Westerberg To: Nicholas Johnson Cc: "linux-kernel@vger.kernel.org" , Srinivas Kandagatla Subject: Re: [PATCH v1 1/3] nvmem: Add support for write-only instances Message-ID: <20200225125141.GA2667@lahna.fi.intel.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Feb 24, 2020 at 05:42:33PM +0000, Nicholas Johnson wrote: > Mika Westerberg requires write-only nvmem for the Thunderbolt driver. > Refer to 03cd45d2e219 ("thunderbolt: Prevent crash if non-active NVMem > file is read"). Hence, there is at least one real-world use for > write-only nvmem instances. Well, I don't require anything ;-) It is the thunderbolt driver that has one nvmem device that is write-only and it may take advantage of this. > Add support for write-only nvmem instances by changing the nvmem attrs > to 0222 if the .reg_read callback is not populated. > > Add a WARN_ON in case a driver populates neither .reg_read nor > .reg_write because this behaviour should clearly never occur. > > Signed-off-by: Nicholas Johnson > --- > drivers/nvmem/nvmem-sysfs.c | 77 +++++++++++++++++++++++++++++++++---- > 1 file changed, 70 insertions(+), 7 deletions(-) > > diff --git a/drivers/nvmem/nvmem-sysfs.c b/drivers/nvmem/nvmem-sysfs.c > index 9e0c429cd..be3b94f0b 100644 > --- a/drivers/nvmem/nvmem-sysfs.c > +++ b/drivers/nvmem/nvmem-sysfs.c > @@ -147,6 +147,30 @@ static const struct attribute_group *nvmem_ro_dev_groups[] = { > NULL, > }; > > +/* write only permission */ > +static struct bin_attribute bin_attr_wo_nvmem = { > + .attr = { > + .name = "nvmem", > + .mode = 0222, I would say no sysfs attribute should ever be writable by the world. Actually I think maybe we make this one only writeable by root, in other words it would always require ->root_only to be set. > + }, > + .write = bin_attr_nvmem_write, > +}; > + > +static struct bin_attribute *nvmem_bin_wo_attributes[] = { > + &bin_attr_wo_nvmem, > + NULL, > +}; > + > +static const struct attribute_group nvmem_bin_wo_group = { > + .bin_attrs = nvmem_bin_wo_attributes, > + .attrs = nvmem_attrs, > +}; > + > +static const struct attribute_group *nvmem_wo_dev_groups[] = { > + &nvmem_bin_wo_group, > + NULL, > +}; > + > /* default read/write permissions, root only */ > static struct bin_attribute bin_attr_rw_root_nvmem = { > .attr = { > @@ -196,16 +220,50 @@ 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; > - > - return nvmem->read_only ? nvmem_ro_dev_groups : nvmem_rw_dev_groups; > + /* > + * If neither reg_read nor reg_write are provided, we cannot use this > + * nvmem entry, as any operation will cause kernel mode NULL reference. > + */ > + WARN_ON(!nvmem->reg_read && !nvmem->reg_write); This should also be documented in kernel-doc of struct nvmem_config. > + > + if (nvmem->reg_read && nvmem->reg_write) > + return config->root_only ? > + nvmem_rw_root_dev_groups : nvmem_rw_dev_groups; > + > + if (nvmem->reg_read && !nvmem->reg_write) > + return config->root_only ? > + nvmem_ro_root_dev_groups : nvmem_ro_dev_groups; > + > + return config->root_only ? > + nvmem_wo_root_dev_groups : nvmem_wo_dev_groups; > } > > /* > @@ -224,11 +282,16 @@ 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) { > if (config->root_only) > nvmem->eeprom = bin_attr_ro_root_nvmem; > else > nvmem->eeprom = bin_attr_ro_nvmem; > + } else if (!nvmem->reg_read && nvmem->reg_write) { > + if (config->root_only) > + nvmem->eeprom = bin_attr_wo_root_nvmem; > + else > + nvmem->eeprom = bin_attr_wo_nvmem; > } else { > if (config->root_only) > nvmem->eeprom = bin_attr_rw_root_nvmem; > -- > 2.25.1