Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp857527lqp; Sun, 14 Apr 2024 02:48:54 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXAB7SYrYV5l7tYHQ9QZIKzKUgjV/zykQSziu6HuvXioWelCPuePEgf8eRUPVEjQinMnGU+/2BZFE2BGO+kxntikhOFE4rYXVGQ1akbxg== X-Google-Smtp-Source: AGHT+IEfi0kWaOjfrDQ+w6GUsXPH9oC+ZMXz9nLa1SxRGaHDNLCaaY+Vr4eFO8QDiY5tjOAbzDr0 X-Received: by 2002:a05:6808:1311:b0:3c6:e81:4272 with SMTP id y17-20020a056808131100b003c60e814272mr9424359oiv.10.1713088133872; Sun, 14 Apr 2024 02:48:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713088133; cv=pass; d=google.com; s=arc-20160816; b=ZJwY+4Xl0OadCoZHQ5p7J8uXXPByJ5i6p3dsw8k7grm1HG8dI9wHmLUZCg6bYELdUQ V6KUqw3i4pLx2x+MHCNDA4xh5lk7pfZUa4y7mQgjkvcDA5JpMWhoTDEe0kQ+wAvCsrML 2OXRM3STZ5bGSopO4ub4sUNWErd2wEEJ517+fx6dcn9nadfjOK76sSZ3Kgbr2Wr63rmR 3qJszUVRv+1fDKyMQs8qdjtyiIGYF30PfWkB4xC/Kd4FvyC0QVgSvw/RiOxeojZoMryd 4HjDLXJH56XBIgJOBOQj665Uu4NBFX4uv5TWDcDzCfraebKPLkCqRN1o75I1yeH1BKkK 8x1w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date; bh=4l2fqRCuIweQN7gHZUqIR3v4wOZvlFLidFCesZjA6yc=; fh=mq4JiBSFbKTeJRrvB7yjUMUuF9BSz1EgwSAhclfBJ8k=; b=TnU+SBhTuH5PXXTIGWEcX3BJ1n7NDCG88q7EoXoldp3h31u0DERAmzlT+IPkHallqG /lSrN8IWNl272floYMRwNAzB96y0CJ8Gb9LUdqrGuDjkFME2iguq2LmWxvzpnzLNN5TI 72rkbv8dj/ew0HgHNsf2UI8m/KIYjPSLRbTtml/42k2Tr7vIfnP2ftZS9per1320TTLE T1Czl+09TEI5Bj0J9IuWTU9VkeNzkGESYt9XJ1i+hmOi+OPdDkSLH5b/+rlvZ3fRYtPU wOelpI3ZNL0Uu1gH/IS0h5fLR7PEofEdWBj1qYUduWSWaNorLRzNJNb5hi/vEXeHkX/J jDaA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=pengutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-144082-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-144082-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id y8-20020a17090264c800b001e504940a11si6067982pli.231.2024.04.14.02.48.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Apr 2024 02:48:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-144082-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=pengutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-144082-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-144082-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 07143B20E7E for ; Sun, 14 Apr 2024 09:48:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9E24823774; Sun, 14 Apr 2024 09:48:43 +0000 (UTC) Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C348F1C2AF for ; Sun, 14 Apr 2024 09:48:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713088123; cv=none; b=CzNwugJIZfSBK9me60W4MTDY8Y+mKqK8aHUunEhyahGNpaTIZkS5rqaQz80NRGDGiR9a3b8k8qwA+rSscz2eVrOhDOUz3XhJuOUivPA6nEPOSS7JyV0jHF1ipQxsQfBHsXOZHyZJBJdTeWkPAZC69IFy6mQcL64Y5xmybI995Cw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713088123; c=relaxed/simple; bh=7Bp7WR/OmDqOUD3FxH4EWpUeRzpXGskbstL5e7ftf+E=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=LDv5GKOGs7aIxg9JhOdEjxZNEyf5nIJ6EUCSFH7ZGyDKgG5zI9uCkNXzRxskmDSD7yzVLIJdQ/nvbaSQXb2ANkuslnIJxwhO0iMzqVSSoJBTw6m7EvFJGJkERePAerZEN3HY+FVhjG36EnupGAhT9wZF6BwFWMfLoNWJ8UeyaJE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rvwTP-0000rN-2t; Sun, 14 Apr 2024 11:48:31 +0200 Received: from [2a0a:edc0:2:b01:1d::c5] (helo=pty.whiteo.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rvwTO-00CDev-Am; Sun, 14 Apr 2024 11:48:30 +0200 Received: from mfe by pty.whiteo.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1rvwTO-00CWeq-0i; Sun, 14 Apr 2024 11:48:30 +0200 Date: Sun, 14 Apr 2024 11:48:30 +0200 From: Marco Felsch To: Srinivas Kandagatla Cc: miquel.raynal@bootlin.com, michael@walle.cc, linux-kernel@vger.kernel.org, kernel@pengutronix.de, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, conor+dt@kernel.org, devicetree@vger.kernel.org Subject: Re: [PATCH] nvmem: core: add sysfs cell write support Message-ID: <20240414094830.jxpqn33ew2us37t7@pengutronix.de> References: <20240223154129.1902905-1-m.felsch@pengutronix.de> <406eb283-6ac0-4917-9dbf-e45d033bf3de@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <406eb283-6ac0-4917-9dbf-e45d033bf3de@linaro.org> X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: mfe@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Hi Srinivas, +Cc Rob, Krzysztof, Conor for below OF question. On 24-04-13, Srinivas Kandagatla wrote: > Thanks Marco for the work, > > On 23/02/2024 15:41, Marco Felsch wrote: > > Add the sysfs cell write support to make it possible to write to exposed > > cells from sysfs as well e.g. for device provisioning. The write support > > Which device are you testing this on? An EEPROM device. > AFAIU, Normally all the device provisioning happens early stages at > production line, not after OS is up and running. I might be wrong. > > Can you provide more details on what type of device provisioning that you > are referring to. We do have production data and you're right about this. But we have also cells which cover sw-feature switches and with the write support they can be accessed easily from user-space. > Write support should not be enabled by default, this has to be an explicit > Kconfig with a big WARNING that it could potentially corrupt the nvmem by > rouge writes. I'm okay with a Kconfig but I'm not okay with the warning. If an user do enable this feature on purpose we shouldn't print a warning. We do limit the write support to EEPROM devices only and to cells which do not have a special post processing. IMHO this is the simplest use-case and corruption shouldn't occure. Of course there can be supply interrruptions but in this case other storage devices can be corrupted as well. > I would also like this to be an optional feature from providers side too, as > not all nvmem providers want to have device provisioning support from Linux > side. You say instead of checking for NVMEM_TYPE_EEPROM, the nvmem-config should have an option which to tell the core that write-support should be exposed? I can do this but still it would expose the write support for all at24 users. We could have an optional of-property but OF purpose is to abstract hw and this clearly is not a hw-feature. What I can imagine is an nvmem_core module param and the default is set via the Kconfig option. Of course this way still all EEPROMs are either exposed as ro/rw but it's the user/distro choice. So in the end an OF abstraction would give us a more fine grained possibility to influence the behavior. @Rob, Krzysztof, Conor Would it be okay to abstract this via an OF property? > > is limited to EEPROM based nvmem devices and to nvmem-cells which don't > > require post-processing. > > > > > Signed-off-by: Marco Felsch > > --- > > drivers/nvmem/core.c | 43 ++++++++++++++++++++++++++++++++++++++++++- > > 1 file changed, 42 insertions(+), 1 deletion(-) > > > > diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c > > index 980123fb4dde..b1f86cb431ef 100644 > > --- a/drivers/nvmem/core.c > > +++ b/drivers/nvmem/core.c > > @@ -336,6 +336,40 @@ static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj, > > return read_len; > > } > > +static ssize_t nvmem_cell_attr_write(struct file *filp, struct kobject *kobj, > > + struct bin_attribute *attr, char *buf, > > + loff_t pos, size_t count) > > +{ > > + struct nvmem_cell_entry *entry; > > + struct nvmem_cell *cell; > > + int ret; > > + > > + entry = attr->private; > > + > > + if (!entry->nvmem->reg_write) > > nvmem->read_only ? In addition or as replacement? > > + return -EPERM; > > + > > + if (pos >= entry->bytes) > > + return -EFBIG; > > + > > + if (pos + count > entry->bytes) > > + count = entry->bytes - pos; > > + > > + cell = nvmem_create_cell(entry, entry->name, 0); > > + if (IS_ERR(cell)) > > + return PTR_ERR(cell); > > + > > + if (!cell) > > + return -EINVAL; > > + > > + ret = nvmem_cell_write(cell, buf, count); > > + > > + kfree_const(cell->id); > > + kfree(cell); > > + > > + return ret; > > +} > > + > > /* default read/write permissions */ > > static struct bin_attribute bin_attr_rw_nvmem = { > > .attr = { > > @@ -458,13 +492,20 @@ static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem) > > /* Initialize each attribute to take the name and size of the cell */ > > list_for_each_entry(entry, &nvmem->cells, node) { > > + umode_t mode = nvmem_bin_attr_get_umode(nvmem); > > + > > + /* Limit cell-write support to EEPROMs at the moment */ > > + if (entry->read_post_process || nvmem->type != NVMEM_TYPE_EEPROM) > > + mode &= ~0222; > > + > > sysfs_bin_attr_init(&attrs[i]); > > attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL, > > "%s@%x", entry->name, > > entry->offset); > > - attrs[i].attr.mode = 0444; > > + attrs[i].attr.mode = mode; > > attrs[i].size = entry->bytes; > > attrs[i].read = &nvmem_cell_attr_read; > can we not make this conditional based on read_only flag? We do use nvmem_bin_attr_get_umode() to query the mode which covers the read_only, but of course I can add it conditional. Regards, Marco > > + attrs[i].write = &nvmem_cell_attr_write; > > attrs[i].private = entry; > > if (!attrs[i].attr.name) { > > ret = -ENOMEM; > > thanks, > Srini >