linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Vesa Jääskeläinen" <vesa.jaaskelainen@vaisala.com>
To: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>,
	Nandor Han <nandor.han@vaisala.com>,
	robh+dt@kernel.org, linux-kernel@vger.kernel.org,
	devicetree@vger.kernel.org
Cc: Tomas Melin <tomas.melin@vaisala.com>
Subject: Re: [PATCH v4 2/4] nvmem: bootcount: add bootcount driver
Date: Wed, 23 Jun 2021 13:55:35 +0300	[thread overview]
Message-ID: <c965fa10-9296-e611-9515-675daa602538@vaisala.com> (raw)
In-Reply-To: <78af96b3-b07b-342b-edf2-c898c31e956e@linaro.org>

On 2021-06-03 11:03, Srinivas Kandagatla wrote:
> On 01/06/2021 08:58, Nandor Han wrote:
>> On 5/28/21 11:23 AM, Srinivas Kandagatla wrote:
>>> On 05/05/2021 11:42, Nandor Han wrote:
>>>> In order to have a robust system we want to be able to identify and 
>>>> take
>>>> actions if a boot loop occurs. This is possible by using the bootcount
>>>> feature, which can be used to identify the number of times device has
>>>> booted since bootcount was last time reset. Bootcount feature (1)
>>>> requires a collaboration between bootloader and user-space, where
>>>> the bootloader will increase a counter and user-space reset it.
>>>> If the counter is not reset and a pre-established threshold is reached,
>>>> bootloader can react and take action.
>>>>
>>>> This is the kernel side implementation, which can be used to
>>>> identify the number of times device has booted since bootcount was
>>>> last time reset.
>>>>
>>>
>>> If I understand this correctly, this driver is basically exposing a 
>>> nvmem cell via sysfs.
>>>
>>> Firstly, This sounds like totally a generic functionality that needs 
>>> to go into nvmem core rather than individual drivers.
>>>
>>> Do you see any reason for this not be in core?
>>
>> I agree that exposing a NVMEM cell via sysfs does look as a generic 
>> functionality. However, the bootcount feature contains also a magic
>> value that needs to be taken in consideration when extracting the
>> bootcount value. The size of the field storing the magic and value combo
>> is configurable as well. The driver will handle this values 
>> transparentlry for the user and expose only the validated
>> bootcount value. In case we will only use a generic implementation for
>> exposing a NVMEM cell via sysfs the aformention functionality will have
>> to be handled by userspace and this will force the userspace to have
>> knolwdge about bootcount value format and magic since they will have
>> to implement it's own functionality about this. In the current solution
>> the user only have to reset the value to 0 and that's it, the driver
>> will take care of the rest.
> 
> Should this not live in userspace HAL, kernel would provide an abstract 
> interface. User space in this case which is programming the bootcount is 
> already aware of this, so am hoping that it would be able to encapsulate 
> the magic as well with in.
> 
> Instead of accessing sysfs directly, its always recommended to access it 
> via a some abstraction HAL programs, so as to not break the userspace 
> across kernel releases, more info at 
> ./Documentation/admin-guide/sysfs-rules.rst
> 
> Other problem with having this in kernel is that we would endup with 
> endless number of drivers for each nvmem cell which is totally not 
> necessary.
> 
> Personally I do not want to endup in such a situation where people start 
> writing drivers for each cell.

If we look from U-Boot source code (for which the boot count support has 
been there for long time):

https://source.denx.de/u-boot/u-boot/-/tree/master/drivers/bootcount

In there we do have solutions for:

- Atmel AT91 -- one specific CPU register

- Davinci/Omap/beaglebone -- Uses internal RTC's scratcpad #2 register
   - In here Scratcpad #0/#1 were at least one point of time used for 
deep sleep recovery addresses -- and probably should not be exposed at 
all to user space

- As U-Boot environment variable -- what ever storage would be

- File system interface -- store as a file

- I2C (version 1) -- store in (volatile) 16 bit RTC register

- RAM -- Use multiple addreses for storing magics and actual boot count 
value

- I2C EEPROM -- Store in persistent cells in EEPROM

- RTC -- Store in RTC if the chip has support for it

- SPI flash -- Store in special location in serial flash

So we are already in situation that there exists multiple technical 
solutions :| And best bit here is that some of them can be customized by 
Kconfig options.

Another observation of that list is that those all are not NVMEM cells 
-- so more generic solution abstracting it away would be better. 
Actually the best solution for boot count is probably volatile register 
that persist over reset of the device and is not subject of flash endurance.

So perhaps there should be "core boot count internal API" for which 
driver (in this case nvmem specific) can register itself and then that 
"boot count core" would then expose it to user space. That would most 
likely be quite slim implementation. And most likely there would only be 
one solution per device but in theory it could also support case if 
device supports more than one place to store it then it would handle 
this transparently from the driver.

I believe the sysfs would be perfect for this especially when the path 
for the entry would stay the same independent of the solution behind the 
boot count. This would make it easy for user space to read the boot 
count in shell scripts or in applications and then resetting would be as 
easy as echoing "0" to sysfs entry.

Thanks,
Vesa Jääskeläinen

  reply	other threads:[~2021-06-23 10:55 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-05 10:42 [PATCH v4 0/4] Bootcount driver using NVMEM cell as backend Nandor Han
2021-05-05 10:42 ` [PATCH v4 1/4] dt-bindings: nvmem: Add bootcount-nvmem Nandor Han
2021-05-05 10:42 ` [PATCH v4 2/4] nvmem: bootcount: add bootcount driver Nandor Han
2021-05-28  8:23   ` Srinivas Kandagatla
2021-06-01  7:58     ` Nandor Han
2021-06-03  8:03       ` Srinivas Kandagatla
2021-06-23 10:55         ` Vesa Jääskeläinen [this message]
2021-05-05 10:42 ` [PATCH v4 3/4] nvmem: snvs_lpgpr: use cell stride for regmap size calculation Nandor Han
2021-05-05 10:42 ` [PATCH v4 4/4] nvmem: snvs_lpgpr: support two bytes NVMEM cell size Nandor Han
2021-05-27 10:44 ` [PATCH v4 0/4] Bootcount driver using NVMEM cell as backend Nandor Han

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c965fa10-9296-e611-9515-675daa602538@vaisala.com \
    --to=vesa.jaaskelainen@vaisala.com \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nandor.han@vaisala.com \
    --cc=robh+dt@kernel.org \
    --cc=srinivas.kandagatla@linaro.org \
    --cc=tomas.melin@vaisala.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).