Skip to contentSkip to conversation

ras: scrub: introduce subsystem + CXL/ACPI-RAS2 drivers

RFC PATCH In review [email protected] 6 participants 56 messages Apr 19, 2024 – Jun 26, 2024

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v8 cover ras: scrub: introduce subsystem + CXL/ACPI-RAS2 drivers

From: Shiju Jose <[email protected]>

Memory Scrub
============

Increasing DRAM size and cost has made memory subsystem reliability an important concern. These modules are used where potentially corrupted data could cause expensive or fatal issues. Memory errors are one of the top hardware failures that cause server and workload crashes.

Memory scrub is a feature where an ECC engine reads data from each memory media location, corrects with an ECC if necessary and writes the corrected data back to the same memory media location.

The memory DIMMs could be scrubbed at a configurable rate to detect uncorrected memory errors and attempts to recover from detected memory errors providing the following benefits.
- Proactively scrubbing memory DIMMs reduces the chance of a correctable error becoming uncorrectable.
- Once detected, uncorrected errors caught in unallocated memory pages are isolated and prevented from being allocated to an application or the OS.
- The probability of software/hardware products encountering memory errors is reduced.
Some details of background can be found in Reference [5].

There are 2 types of memory scrubbing,
1. Background (patrol) scrubbing of the RAM whilest the RAM is otherwise idle.
2. On-demand scrubbing for a specific address range/region of memory.

There are several types of interfaces to HW memory scrubbers identified such as ACPI NVDIMM ARS(Address Range Scrub), CXL memory device patrol scrub, CXL DDR5 ECS, ACPI RAS2 memory scrubbing.

The scrub control varies between different memory scrubbers. To allow for standard userspace tooling there is a need to present these controls with a standard ABI.

Introduce generic memory scrub subsystem which allows user to control underlying scrubbers in the system via generic sysfs scrub control interface.

Use case of scrub control feature
=================================
1. Scrub controls in user space allow the user space tool to disable and enable the feature in case disabling of the scrubbing and changing the scrub rate are needed for other purposes such as performance-aware operations which requires the background operations to be turned off or reduced.
2. Also allows to perform on-demand scrubbing for specific address range if supported by the scrubber.

Comparison of scrubbing features
================================
................................................................

 .              .   ACPI    . CXL patrol.  CXL ECS  .  ARS      .
 .  Name        .   RAS2    . scrub     .           .           .
 ................................................................
 .              .           .           .           .           .
 . On-demand    . Supported . No        . No        . Supported .
 . Scrubbing    .           .           .           .           .
 .              .           .           .           .           .  
 ................................................................
 .              .           .           .           .           .
 . Background   . Supported . Supported . Supported . No        .
 . scrubbing    .           .           .           .           .
 .              .           .           .           .           .
 ................................................................
 .              .           .           .           .           .
 . Mode of      . Scrub ctrl. per device. per memory.  Unknown  .
 . scrubbing    . per NUMA  .           . media     .           .
 .              . domain.   .           .           .           .
 ................................................................
 .              .           .           .           .           . 
 . Query scrub  . Supported . Supported . Supported . Supported .       
 . capabilities .           .           .           .           .
 .              .           .           .           .           .
 ................................................................
 .              .           .           .           .           . 
 . Setting      . Supported . No        . No        . Supported .       
 . address range.           .           .           .           .
 .              .           .           .           .           .
 ................................................................
 .              .           .           .           .           . 
 . Setting      . Supported . Supported . No        . No        .       
 . scrub rate   .           .           .           .           .
 .              .           .           .           .           .
 ................................................................
 .              .           .           .           .           . 
 . Unit for     . Not       . in hours  . No        . No        .       
 . scrub rate   . Defined   .           .           .           .
 .              .           .           .           .           .
 ................................................................
 .              . Supported .           .           .           .
 . Scrub        . on-demand . No        . No        . Supported .
 . status/      . scrubbing .           .           .           .
 . Completion   . only      .           .           .           .
 ................................................................
 . UC error     .           .CXL general.CXL general. ACPI UCE  .
 . reporting    . Exception .media/DRAM .media/DRAM . notify and.
 .              .           .event/media.event/media. query     .
 .              .           .scan?      .scan?      . ARS status.
 ................................................................
 .              .           .           .           .           .      
 . Clear UC     .  No       . No        .  No       . Supported .
 . error        .           .           .           .           .
 .              .           .           .           .           .  
 ................................................................
 .              .           .           .           .           .
 . Translate    . No        . No        . No        . Supported .
 . *(1)SPA to   .           .           .           .           .
 . *(2)DPA      .           .           .           .           .  
 ................................................................
 .              .           .           .           .           .
 . Error inject . No        . Can inject. No        . Supported .
 .              .           . poison for.           .           .
 .              .           . CXL       .           .           .  

................................................................
*(1) - SPA - System Physical Address. See section 9.19.7.8
Function Index 5 - Translate SPA of ACPI spec r6.5.
*(2) - DPA - Device Physical Address. See section 9.19.7.8
Function Index 5 - Translate SPA of ACPI spec r6.5.

CXL Scrubbing features
======================
Add support for control CXL patrol scrubber and ACPI RAS2 HW based memory patrol scrubber and register with the scrub subsystem to expose the scrub controls to the userspace tool.

CXL spec r3.1 section 8.2.9.9.11.1 describes the memory device patrol scrub control feature. The device patrol scrub proactively locates and makes corrections to errors in regular cycle. The patrol scrub control allows the request to configure patrol scrubber's input configurations.

The patrol scrub control allows the requester to specify the number of hours in which the patrol scrub cycles must be completed, provided that the requested number is not less than the minimum number of hours for the patrol scrub cycle that the device is capable of. In addition, the patrol scrub controls allow the host to disable and enable the feature in case disabling of the feature is needed for other purposes such as performance-aware operations which require the background operations to be turned off.

ACPI RAS2 Hardware-based Memory Scrubbing
=========================================
ACPI spec 6.5 section 5.2.21 ACPI RAS2 describes ACPI RAS2 table provides interfaces for platform RAS features and supports independent RAS controls and capabilities for a given RAS feature for multiple instances of the same component in a given system.
Memory RAS features apply to RAS capabilities, controls and operations that are specific to memory. RAS2 PCC sub-spaces for memory-specific RAS features have a Feature Type of 0x00 (Memory).

The platform can use the hardware-based memory scrubbing feature to expose controls and capabilities associated with hardware-based memory scrub engines. The RAS2 memory scrubbing feature supports following as per spec,
- Independent memory scrubbing controls for each NUMA domain, identified using its proximity domain.
Note: However AmpereComputing has single entry repeated as they have
centralized controls.
- Provision for background (patrol) scrubbing of the entire memory system, as well as on-demand scrubbing for a specific region of memory.

ACPI Address Range Scrubbing(ARS)
================================
ARS allows the platform to communicate memory errors to system software. This capability allows system software to prevent accesses to addresses with uncorrectable errors in memory. ARS functions manage all NVDIMMs present in the system. Only one scrub can be in progress system wide at any given time.
Following functions are supported as per the specification.
1. Query ARS Capabilities for a given address range, indicates platform supports the ACPI NVDIMM Root Device Unconsumed Error Notification.
2. Start ARS triggers an Address Range Scrub for the given memory range. Address scrubbing can be done for volatile memory, persistent memory, or both.
3. Query ARS Status command allows software to get the status of ARS, including the progress of ARS and ARS error record.
4. Clear Uncorrectable Error.
5. Translate SPA
6. ARS Error Inject etc.
iNote: Support for ARS is not added in this series because to reduce the line of code for review and could be added after initial code is merged.

Series adds,
1. Scrub subsystem driver supports configuring memory scrubbers in the system.
2. Support for CXL feature mailbox commands, which is used by CXL device scrubbing features.
3. CXL device scrub driver supporting patrol scrub control and register with scrub subsystem.
5. ACPI RAS2 driver adds OS interface for RAS2 communication through PCC mailbox and extracts ACPI RAS2 feature table (RAS2) and create platform device for the RAS memory features, which binds to the memory ACPI RAS2 driver.
7. Memory ACPI RAS2 driver gets the PCC subspace for communicating with the ACPI compliant platform supports ACPI RAS2. Add callback functions and registers with scrub subsystem to support user to control the HW patrol scrubbers exposed to the kernel via the ACPI RAS2 table.

Note: The RAS2 feature has dependency on the patch
"ACPICA: ACPI 6.5: Add support for RAS2 table" which Rafael has queued.
https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git/commit/?h=bleeding-edge&id=9726d821f88e284ecd998b76ae5f2174721cd9dc

The QEMU series to support the CXL specific scrub features is available here,
https://lore.kernel.org/qemu-devel/[email protected]/

Open Questions based on feedbacks from the community:
1. Leo: Standardize unit for scrub rate, for example ACPI RAS2 does not define unit for the scrub rate. RAS2 clarification needed.
2. Jonathan:
May the scrub class to be rename as RASCTL or something like that to allow for wider control options etc that open compute RAS API supports?
3. Jonathan: Any need for discoverability of capability to scan different regions, such as global PA space to the userspace. Left as future extension.
4. Jiaqi:
- STOP_PATROL_SCRUBBER from RAS2 must be blocked and, must not be exposed to OS/userspace. Stopping patrol scrubber is unacceptable for platform where OEM has enabled patrol scrubber, because the patrol scrubber is a key part of logging and is repurposed for other RAS actions.
If the OEM does not want to expose this control, they should lock it down so the interface is not exposed to the OS. These features are optional afterall.
- "Requested Address Range"/"Actual Address Range" (region to scrub) is a similarly bad thing to expose in RAS2.
If the OEM does not want to expose this, they should lock it down so the interface is not exposed to the OS. These features are optional afterall.
5. Shiju: How to determine initial status(background scrub / stopped etc).

References:
1. ACPI spec r6.5 section 5.2.21 ACPI RAS2.
2. ACPI spec r6.5 section 9.19.7.2 ARS.
3. CXL spec r3.1 8.2.9.9.11.1 Device patrol scrub control feature
4. CXL spec r3.1 8.2.9.9.11.2 DDR5 ECS feature
5. Background information about kernel support for memory scan, memory error detection and ACPI RASF.
https://lore.kernel.org/all/[email protected]/
6. Discussions on RASF:
https://lore.kernel.org/lkml/[email protected]/#r

Changes
=======
v7 -> v8:
1. Add more detailed cover letter and add info for basic analysis of ACPI ARS for comment from Dan Williams.
2. Changed file name etc from ras2 to acpi_ras2 in memory ACPI RAS2 driver for comment from Boris.
3. Add documents for usage for comment from Jonathan.
4. Changed logic in memory/acpi_ras2.c for enable background scrubbing to allow setting the scrub rate.
5. Merged memory/acpi_ras2_common.c with memory/acpi_ras2.c and obselete code, suggested by Jonathan.
6. Initial optimizations and cleanup especially in the memory/acpi_ras2.
7. Removed CXL ECS support for time being.
8. Removed support for region based scrub control from the scrub subsytem, which was needed for the CXL ECS, can be added later if required.
9. Fixed the format of few comments and a definition in CXL feature code for the feedbacks from Fan.
11. Jonathan done several optimizations, interface changes and cleanups all over the code.
12. Fixes for feedbacks from Daniel Ferguson(Amperecomputing) for RAS2.
13. Workaround for a RAS2 case of only one actual controller as reported by Daniel Ferguson(AmpereComputing) in their hardware.
14. Feedback from Yazen, move the common scrub and ras2 changes under /drivers/ras/.
15. Drop patch ACPICA: ACPI 6.5: Add support for RAS2 table because Rafael queued the patch.
https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git/commit/?h=bleeding-edge&id=9726d821f88e284ecd998b76ae5f2174721cd9dc

v6 -> v7:
1. Main changes for comments from Jonathan, Thanks.
1.1. CXL
- Changes for deal with small mail box and supporting multipart feature data transfers.
- Provide more specific parameters to mbox supported/get/set features interface functions.
- kvmalloc -> kmalloc in CXL scrub mem allocation for feature commands.
- Changed the way using __free(kfree)
- Removed readback and verify for setting CXL scrub patrol and ECS parameters. Could be added later if needed.
- In is_visible() callback functions for scrub control sysfs attrs changed to writeback the default attribute mode value instead of setting per attrs.
- Add documentation for sysfs interfaces for CXL ECS scrub control.
1.2. RAS2
- In rasf common code, rename rasf to ras2 because RASF seems obselete.
- Replace pr_* with dev_* log function calls from ACPI RAS2 and memory RAS2 drivers.
- In rasf common code, rename rasf to ras2.
- Removed including unnecessary .h file from memory RAS2 driver.
- In is_visible() callback functions for scrub control sysfs attrs changed to writeback the default attribute mode value instead of setting per attribute.

2. Changes for comments from Fan, Thanks.
- Add debug message if cxl patrol scrub and ecs init function calls fail.
3. Updated cover letter for feedback from Dan Williams.

v5 -> v6:
1. Changes for comments from Davidlohr, Thanks.
- Update CXL feature code based on spec 3.1.
- attrb -> attr
- Use enums with default counting.
2. Rebased to the latest kernel.

v4 -> v5:
1. Following are the main changes made based on the feedback from Dan Williams on v4. 1.1. In the scrub subsystem the common scrub control attributes are statically defined instead of dynamically created.
1.2. Add scrub subsystem support externally defined attribute group.
Add CXL ECS driver define ECS specific attribute group and pass to
the scrub subsystem.
1.3. Move cxl_mem_ecs_init() to cxl/core/region.c so that the CXL region_id
is used in the registration with the scrub subsystem.
1.4. Add previously posted RASF common and RAS2 patches to this scrub series.

2. Add support for the 'enable_background_scrub' attribute
for RAS2, on request from Bill Schwartz([email protected]).

v3 -> v4:
1. Fixes for the warnings/errors reported by kernel test robot.
2. Add support for reading the 'enable' attribute of CXL patrol scrub.

Changes
v2 -> v3:
1. Changes for comments from Davidlohr, Thanks.
- Updated cxl scrub kconfig
- removed usage of the flag is_support_feature from the function cxl_mem_get_supported_feature_entry().
- corrected spelling error.
- removed unnecessary debug message.
- removed export feature commands to the userspace.
2. Possible fix for the warnings/errors reported by kernel test robot.
3. Add documentation for the common scrub configure atrributes.

v1 -> v2:
1. Changes for comments from Dave Jiang, Thanks.
- Split patches.
- reversed xmas tree declarations.
- declared flags as enums.
- removed few unnecessary variable initializations.
- replaced PTR_ERR_OR_ZERO() with IS_ERR() and PTR_ERR().
- add auto clean declarations.
- replaced while loop with for loop.
- Removed allocation from cxl_get_supported_features() and cxl_get_feature() and make change to take allocated memory pointer from the caller.
- replaced if/else with switch case.
- replaced sprintf() with sysfs_emit() in 2 places.
- replaced goto label with return in few functions.
2. removed unused code for supported attributes from ecs.
3. Included following common patch for scrub configure driver to this series.
"memory: scrub: Add scrub driver supports configuring memory scrubbers
in the system"

Jonathan Cameron (2):
ACPICA: Add __free() based cleanup function for acpi_put_table
platform: Add __free() based cleanup function for platform_device_put

Shiju Jose (8):
ras: scrub: Add scrub subsystem
cxl/mbox: Add GET_SUPPORTED_FEATURES mailbox command cxl/mbox: Add GET_FEATURE mailbox command cxl/mbox: Add SET_FEATURE mailbox command cxl/memscrub: Add CXL device patrol scrub control feature ACPI:RAS2: Add ACPI RAS2 driver
ras: scrub: Add scrub control attributes for ACPI RAS2
ras: scrub: ACPI RAS2: Add memory ACPI RAS2 driver

 .../ABI/testing/sysfs-class-scrub-configure   |  71 ++++
 Documentation/scrub/scrub-configure.rst       |  85 ++++
 drivers/acpi/Kconfig                          |  10 +
 drivers/acpi/Makefile                         |   1 +
 drivers/acpi/ras2.c                           | 366 ++++++++++++++++
 drivers/cxl/Kconfig                           |  19 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/mbox.c                       | 153 +++++++
 drivers/cxl/core/memscrub.c                   | 314 ++++++++++++++
 drivers/cxl/cxlmem.h                          | 130 ++++++
 drivers/cxl/mem.c                             |   6 +
 drivers/ras/Kconfig                           |  17 +
 drivers/ras/Makefile                          |   2 +
 drivers/ras/acpi_ras2.c                       | 358 ++++++++++++++++
 drivers/ras/memory_scrub.c                    | 402 ++++++++++++++++++
 include/acpi/acpixf.h                         |   2 +
 include/acpi/ras2_acpi.h                      |  59 +++
 include/linux/memory_scrub.h                  |  45 ++
 include/linux/platform_device.h               |   1 +
 19 files changed, 2042 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-scrub-configure
 create mode 100644 Documentation/scrub/scrub-configure.rst
 create mode 100755 drivers/acpi/ras2.c
 create mode 100644 drivers/cxl/core/memscrub.c
 create mode 100644 drivers/ras/acpi_ras2.c
 create mode 100755 drivers/ras/memory_scrub.c
 create mode 100644 include/acpi/ras2_acpi.h
 create mode 100755 include/linux/memory_scrub.h
Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH v8 1/10 ras: scrub: Add scrub subsystem

From: Shiju Jose <[email protected]>

Add scrub subsystem supports configuring the memory scrubbers in the system. The scrub subsystem provides the interface for registering the scrub devices. The scrub control attributes are provided to the user in /sys/class/ras/rasX/scrub

---
 .../ABI/testing/sysfs-class-scrub-configure   |  47 +++
 drivers/ras/Kconfig                           |   7 +
 drivers/ras/Makefile                          |   1 +
 drivers/ras/memory_scrub.c                    | 271 ++++++++++++++++++
 include/linux/memory_scrub.h                  |  37 +++
 5 files changed, 363 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-scrub-configure
 create mode 100755 drivers/ras/memory_scrub.c
 create mode 100755 include/linux/memory_scrub.h
Documentation/ABI/testing/sysfs-class-scrub-configure new file +47 -0
new file mode 100644
index 000000000000..3ed77dbb00ad
@@ -0,0 +1,47 @@
+What:		/sys/class/ras/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The ras/ class subdirectory belongs to the
+		common ras features such as scrub subsystem.
+
+What:		/sys/class/ras/rasX/scrub/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The /sys/class/ras/ras{0,1,2,3,...}/scrub directories
+		correspond to each scrub device registered with the
+		scrub subsystem.
+
+What:		/sys/class/ras/rasX/scrub/name
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) name of the memory scrubber
+
+What:		/sys/class/ras/rasX/scrub/enable_background
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) Enable/Disable background(patrol) scrubbing if supported.
+
+What:		/sys/class/ras/rasX/scrub/rate_available
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) Supported range for the scrub rate by the scrubber.
+		The scrub rate represents in hours.
+
+What:		/sys/class/ras/rasX/scrub/rate
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) The scrub rate specified and it must be with in the
+		supported range by the scrubber.
+		The scrub rate represents in hours.
drivers/ras/Kconfig +7 -0
index fc4f4bb94a4c..181701479564 100644
@@ -46,4 +46,11 @@ config RAS_FMPM
 	  Memory will be retired during boot time and run time depending on
 	  platform-specific policies.
 
+config SCRUB
+	tristate "Memory scrub driver"
+	help
+	  This option selects the memory scrub subsystem, supports
+	  configuring the parameters of underlying scrubbers in the
+	  system for the DRAM memories.
+
 endif
drivers/ras/Makefile +1 -0
index 11f95d59d397..89bcf0d84355 100644
@@ -2,6 +2,7 @@
 obj-$(CONFIG_RAS)	+= ras.o
 obj-$(CONFIG_DEBUG_FS)	+= debugfs.o
 obj-$(CONFIG_RAS_CEC)	+= cec.o
+obj-$(CONFIG_SCRUB)	+= memory_scrub.o
 
 obj-$(CONFIG_RAS_FMPM)	+= amd/fmpm.o
 obj-y			+= amd/atl/
drivers/ras/memory_scrub.c new file +271 -0

271 lines not on this page · raw message

include/linux/memory_scrub.h new file +37 -0
new file mode 100755
index 000000000000..f0e1657a5072
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Memory scrub subsystem driver supports controlling
+ * the memory scrubbers in the system.
+ *
+ * Copyright (c) 2024 HiSilicon Limited.
+ */
+
+#ifndef __MEMORY_SCRUB_H
+#define __MEMORY_SCRUB_H
+
+#include <linux/types.h>
+
+struct device;
+
+/**
+ * struct scrub_ops - scrub device operations (all elements optional)
+ * @get_enabled_bg: check if currently performing background scrub.
+ * @set_enabled_bg: start or stop a bg-scrub.
+ * @get_name: get the memory scrubber name.
+ * @rate_avail_range: retrieve limits on supported rates.
+ * @rate_read: read the scrub rate
+ * @rate_write: set the scrub rate
+ */
+struct scrub_ops {
+	int (*get_enabled_bg)(struct device *dev, bool *enable);
+	int (*set_enabled_bg)(struct device *dev, bool enable);
+	int (*get_name)(struct device *dev, char *buf);
+	int (*rate_avail_range)(struct device *dev, u64 *min, u64 *max);
+	int (*rate_read)(struct device *dev, u64 *rate);
+	int (*rate_write)(struct device *dev, u64 rate);
+};
+
+struct device *
+devm_scrub_device_register(struct device *dev, void *drvdata,
+			   const struct scrub_ops *ops);
+#endif /* __MEMORY_SCRUB_H */
Signature · 1 line
2.34.1
#2 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 313 lines · On Sat, Apr 20, 2024 at 12:47:10AM +0800, [email protected] wrote:

From: Shiju Jose <[email protected]>

Add scrub subsystem supports configuring the memory scrubbers in the system. The scrub subsystem provides the interface for registering the scrub devices. The scrub control attributes are provided to the user in /sys/class/ras/rasX/scrub

---
 .../ABI/testing/sysfs-class-scrub-configure   |  47 +++
 drivers/ras/Kconfig                           |   7 +
 drivers/ras/Makefile                          |   1 +
 drivers/ras/memory_scrub.c                    | 271 ++++++++++++++++++
 include/linux/memory_scrub.h                  |  37 +++
 5 files changed, 363 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-scrub-configure
 create mode 100755 drivers/ras/memory_scrub.c
 create mode 100755 include/linux/memory_scrub.h
Documentation/ABI/testing/sysfs-class-scrub-configure new file +47 -0
new file mode 100644
index 000000000000..3ed77dbb00ad
@@ -0,0 +1,47 @@
+What:		/sys/class/ras/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The ras/ class subdirectory belongs to the
+		common ras features such as scrub subsystem.
+
+What:		/sys/class/ras/rasX/scrub/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The /sys/class/ras/ras{0,1,2,3,...}/scrub directories
+		correspond to each scrub device registered with the
+		scrub subsystem.
+
+What:		/sys/class/ras/rasX/scrub/name
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) name of the memory scrubber
+
+What:		/sys/class/ras/rasX/scrub/enable_background
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) Enable/Disable background(patrol) scrubbing if supported.
+
+What:		/sys/class/ras/rasX/scrub/rate_available
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) Supported range for the scrub rate by the scrubber.
+		The scrub rate represents in hours.
+
+What:		/sys/class/ras/rasX/scrub/rate
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) The scrub rate specified and it must be with in the
+		supported range by the scrubber.
+		The scrub rate represents in hours.
drivers/ras/Kconfig +7 -0
index fc4f4bb94a4c..181701479564 100644
@@ -46,4 +46,11 @@ config RAS_FMPM
 	  Memory will be retired during boot time and run time depending on
 	  platform-specific policies.
 
+config SCRUB
+	tristate "Memory scrub driver"
+	help
+	  This option selects the memory scrub subsystem, supports
+	  configuring the parameters of underlying scrubbers in the
+	  system for the DRAM memories.
+
 endif
drivers/ras/Makefile +1 -0
index 11f95d59d397..89bcf0d84355 100644
@@ -2,6 +2,7 @@
 obj-$(CONFIG_RAS)	+= ras.o
 obj-$(CONFIG_DEBUG_FS)	+= debugfs.o
 obj-$(CONFIG_RAS_CEC)	+= cec.o
+obj-$(CONFIG_SCRUB)	+= memory_scrub.o
 
 obj-$(CONFIG_RAS_FMPM)	+= amd/fmpm.o
 obj-y			+= amd/atl/
drivers/ras/memory_scrub.c new file +205 -0
new file mode 100755
index 000000000000..7e995380ec3a
@@ -0,0 +1,271 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Memory scrub subsystem supports configuring the registered
+ * memory scrubbers.
+ *
+ * Copyright (c) 2024 HiSilicon Limited.
+ */
+
+#define pr_fmt(fmt)     "MEM SCRUB: " fmt
+
+#include <linux/acpi.h>
+#include <linux/bitops.h>
+#include <linux/delay.h>
+#include <linux/kfifo.h>
+#include <linux/memory_scrub.h>
+#include <linux/platform_device.h>
+#include <linux/spinlock.h>
+
+/* memory scrubber config definitions */
+#define SCRUB_ID_PREFIX "ras"
+#define SCRUB_ID_FORMAT SCRUB_ID_PREFIX "%d"
+
+static DEFINE_IDA(scrub_ida);
+
+struct scrub_device {
+	int id;
+	struct device dev;
+	const struct scrub_ops *ops;
+};
+
+#define to_scrub_device(d) container_of(d, struct scrub_device, dev)
+static ssize_t enable_background_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf, size_t len)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	bool enable;
+	int ret;
+
+	ret = kstrtobool(buf, &enable);
+	if (ret < 0)
+		return ret;
+
+	ret = scrub_dev->ops->set_enabled_bg(dev, enable);
+	if (ret)
+		return ret;
+
+	return len;
+}
+
+static ssize_t enable_background_show(struct device *dev,
+				      struct device_attribute *attr, char *buf)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	bool enable;
+	int ret;
+
+	ret = scrub_dev->ops->get_enabled_bg(dev, &enable);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "%d\n", enable);
+}
+
+static ssize_t name_show(struct device *dev,
+			 struct device_attribute *attr, char *buf)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	int ret;
+
+	ret = scrub_dev->ops->get_name(dev, buf);
+	if (ret)
+		return ret;
+
+	return strlen(buf);
+}
+
+static ssize_t rate_show(struct device *dev, struct device_attribute *attr,
+			 char *buf)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	u64 val;
+	int ret;
+
+	ret = scrub_dev->ops->rate_read(dev, &val);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "0x%llx\n", val);
+}
+
+static ssize_t rate_store(struct device *dev, struct device_attribute *attr,
+			  const char *buf, size_t len)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	long val;
+	int ret;
+
+	ret = kstrtol(buf, 10, &val);
+	if (ret < 0)
+		return ret;
+
+	ret = scrub_dev->ops->rate_write(dev, val);
+	if (ret)
+		return ret;
+
+	return len;
+}
+
+static ssize_t rate_available_show(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	u64 min_sr, max_sr;
+	int ret;
+
+	ret = scrub_dev->ops->rate_avail_range(dev, &min_sr, &max_sr);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "0x%llx-0x%llx\n", min_sr, max_sr);
+}
+
+DEVICE_ATTR_RW(enable_background);
+DEVICE_ATTR_RO(name);
+DEVICE_ATTR_RW(rate);
+DEVICE_ATTR_RO(rate_available);
+
+static struct attribute *scrub_attrs[] = {
+	&dev_attr_enable_background.attr,
+	&dev_attr_name.attr,
+	&dev_attr_rate.attr,
+	&dev_attr_rate_available.attr,
+	NULL
+};
+
+static umode_t scrub_attr_visible(struct kobject *kobj,
+				  struct attribute *a, int attr_id)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	const struct scrub_ops *ops = scrub_dev->ops;
+
+	if (a == &dev_attr_enable_background.attr) {
+		if (ops->set_enabled_bg && ops->get_enabled_bg)
+			return a->mode;
+		if (ops->get_enabled_bg)
+			return 0444;
+		return 0;
+	}
+	if (a == &dev_attr_name.attr)
+		return ops->get_name ? a->mode : 0;
+	if (a == &dev_attr_rate_available.attr)
+		return ops->rate_avail_range ? a->mode : 0;
+	if (a == &dev_attr_rate.attr) { /* Write only makes little sense */
+		if (ops->rate_read && ops->rate_write)
+			return a->mode;
+		if (ops->rate_read)
+			return 0444;
+		return 0;
+	}
+
+	return 0;
+}
+
+static const struct attribute_group scrub_attr_group = {
+	.name		= "scrub",
+	.attrs		= scrub_attrs,
+	.is_visible	= scrub_attr_visible,
+};
+
+static const struct attribute_group *scrub_attr_groups[] = {
+	&scrub_attr_group,
+	NULL
+};
+
+static void scrub_dev_release(struct device *dev)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+
+	ida_free(&scrub_ida, scrub_dev->id);
+	kfree(scrub_dev);
+}
+
+static struct class scrub_class = {
+	.name = "ras",
+	.dev_groups = scrub_attr_groups,
+	.dev_release = scrub_dev_release,
+};
+
+static struct device *
+scrub_device_register(struct device *parent, void *drvdata,
+		      const struct scrub_ops *ops)
+{
+	struct scrub_device *scrub_dev;
+	struct device *hdev;
+	int err;
+
+	scrub_dev = kzalloc(sizeof(*scrub_dev), GFP_KERNEL);
+	if (!scrub_dev)
+		return ERR_PTR(-ENOMEM);
+	hdev = &scrub_dev->dev;
+
+	scrub_dev->id = ida_alloc(&scrub_ida, GFP_KERNEL);

drivers/ras/memory_scrub.c line 215

Quoted 10 lines
drivers/ras/memory_scrub.c new file +10 -0
+	if (scrub_dev->id < 0) {
+		kfree(scrub_dev);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	scrub_dev->ops = ops;
+	hdev->class = &scrub_class;
+	hdev->parent = parent;
+	dev_set_drvdata(hdev, drvdata);
+	dev_set_name(hdev, SCRUB_ID_FORMAT, scrub_dev->id);

Need to check the return value of dev_set_name?

fan

Quoted 102 lines
drivers/ras/memory_scrub.c new file +56 -0
+	err = device_register(hdev);
+	if (err) {
+		put_device(hdev);
+		return ERR_PTR(err);
+	}
+
+	return hdev;
+}
+
+static void devm_scrub_release(void *dev)
+{
+	device_unregister(dev);
+}
+
+/**
+ * devm_scrub_device_register - register scrubber device
+ * @dev: the parent device
+ * @drvdata: driver data to attach to the scrub device
+ * @ops: pointer to scrub_ops structure (optional)
+ *
+ * Returns the pointer to the new device on success, ERR_PTR() otherwise.
+ * The new device would be automatically unregistered with the parent device.
+ */
+struct device *
+devm_scrub_device_register(struct device *dev, void *drvdata,
+			   const struct scrub_ops *ops)
+{
+	struct device *hdev;
+	int ret;
+
+	if (!dev)
+		return ERR_PTR(-EINVAL);
+
+	hdev = scrub_device_register(dev, drvdata, ops);
+	if (IS_ERR(hdev))
+		return hdev;
+
+	ret = devm_add_action_or_reset(dev, devm_scrub_release, hdev);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return hdev;
+}
+EXPORT_SYMBOL_GPL(devm_scrub_device_register);
+
+static int __init memory_scrub_control_init(void)
+{
+	return class_register(&scrub_class);
+}
+subsys_initcall(memory_scrub_control_init);
+
+static void memory_scrub_control_exit(void)
+{
+	class_unregister(&scrub_class);
+}
+module_exit(memory_scrub_control_exit);
include/linux/memory_scrub.h new file +37 -0
new file mode 100755
index 000000000000..f0e1657a5072
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Memory scrub subsystem driver supports controlling
+ * the memory scrubbers in the system.
+ *
+ * Copyright (c) 2024 HiSilicon Limited.
+ */
+
+#ifndef __MEMORY_SCRUB_H
+#define __MEMORY_SCRUB_H
+
+#include <linux/types.h>
+
+struct device;
+
+/**
+ * struct scrub_ops - scrub device operations (all elements optional)
+ * @get_enabled_bg: check if currently performing background scrub.
+ * @set_enabled_bg: start or stop a bg-scrub.
+ * @get_name: get the memory scrubber name.
+ * @rate_avail_range: retrieve limits on supported rates.
+ * @rate_read: read the scrub rate
+ * @rate_write: set the scrub rate
+ */
+struct scrub_ops {
+	int (*get_enabled_bg)(struct device *dev, bool *enable);
+	int (*set_enabled_bg)(struct device *dev, bool enable);
+	int (*get_name)(struct device *dev, char *buf);
+	int (*rate_avail_range)(struct device *dev, u64 *min, u64 *max);
+	int (*rate_read)(struct device *dev, u64 *rate);
+	int (*rate_write)(struct device *dev, u64 rate);
+};
+
+struct device *
+devm_scrub_device_register(struct device *dev, void *drvdata,
+			   const struct scrub_ops *ops);
+#endif /* __MEMORY_SCRUB_H */
Signature · 1 line
2.34.1
author
#3 lore raw

RE: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 334 lines

-----Original Message-----
From: fan <[email protected]>
Sent: 24 April 2024 21:26
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang <[email protected]>; Linuxarm <[email protected]>
Subject: Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 310 lines · On Sat, Apr 20, 2024 at 12:47:10AM +0800, [email protected] wrote:

From: Shiju Jose <[email protected]>

Add scrub subsystem supports configuring the memory scrubbers in the system. The scrub subsystem provides the interface for registering the scrub devices. The scrub control attributes are provided to the user in /sys/class/ras/rasX/scrub

---
 .../ABI/testing/sysfs-class-scrub-configure   |  47 +++
 drivers/ras/Kconfig                           |   7 +
 drivers/ras/Makefile                          |   1 +
 drivers/ras/memory_scrub.c                    | 271 ++++++++++++++++++
 include/linux/memory_scrub.h                  |  37 +++
 5 files changed, 363 insertions(+)
 create mode 100644
Documentation/ABI/testing/sysfs-class-scrub-configure
 create mode 100755 drivers/ras/memory_scrub.c  create mode 100755

include/linux/memory_scrub.h

Documentation/ABI/testing/sysfs-class-scrub-configure new file +47 -0
new file mode 100644
index 000000000000..3ed77dbb00ad
@@ -0,0 +1,47 @@
+What:		/sys/class/ras/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The ras/ class subdirectory belongs to the
+		common ras features such as scrub subsystem.
+
+What:		/sys/class/ras/rasX/scrub/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The /sys/class/ras/ras{0,1,2,3,...}/scrub directories
+		correspond to each scrub device registered with the
+		scrub subsystem.
+
+What:		/sys/class/ras/rasX/scrub/name
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) name of the memory scrubber
+
+What:		/sys/class/ras/rasX/scrub/enable_background
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) Enable/Disable background(patrol) scrubbing if supported.
+
+What:		/sys/class/ras/rasX/scrub/rate_available
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) Supported range for the scrub rate by the scrubber.
+		The scrub rate represents in hours.
+
+What:		/sys/class/ras/rasX/scrub/rate
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) The scrub rate specified and it must be with in the
+		supported range by the scrubber.
+		The scrub rate represents in hours.
drivers/ras/Kconfig index renamed from drivers/ras/Kconfig +0 -0

fc4f4bb94a4c..181701479564 100644

drivers/ras/Kconfig +7 -0
@@ -46,4 +46,11 @@ config RAS_FMPM
 	  Memory will be retired during boot time and run time depending on
 	  platform-specific policies.
 
+config SCRUB
+	tristate "Memory scrub driver"
+	help
+	  This option selects the memory scrub subsystem, supports
+	  configuring the parameters of underlying scrubbers in the
+	  system for the DRAM memories.
+
 endif
drivers/ras/Makefile index renamed from drivers/ras/Makefile +0 -0

11f95d59d397..89bcf0d84355 100644

drivers/ras/Makefile +1 -0
@@ -2,6 +2,7 @@
 obj-$(CONFIG_RAS)	+= ras.o
 obj-$(CONFIG_DEBUG_FS)	+= debugfs.o
 obj-$(CONFIG_RAS_CEC)	+= cec.o
+obj-$(CONFIG_SCRUB)	+= memory_scrub.o
 
 obj-$(CONFIG_RAS_FMPM)	+= amd/fmpm.o
 obj-y			+= amd/atl/
drivers/ras/memory_scrub.c new file +201 -0
new file mode 100755 index 000000000000..7e995380ec3a
@@ -0,0 +1,271 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Memory scrub subsystem supports configuring the registered
+ * memory scrubbers.
+ *
+ * Copyright (c) 2024 HiSilicon Limited.
+ */
+
+#define pr_fmt(fmt)     "MEM SCRUB: " fmt
+
+#include <linux/acpi.h>
+#include <linux/bitops.h>
+#include <linux/delay.h>
+#include <linux/kfifo.h>
+#include <linux/memory_scrub.h>
+#include <linux/platform_device.h>
+#include <linux/spinlock.h>
+
+/* memory scrubber config definitions */ #define SCRUB_ID_PREFIX
+"ras"
+#define SCRUB_ID_FORMAT SCRUB_ID_PREFIX "%d"
+
+static DEFINE_IDA(scrub_ida);
+
+struct scrub_device {
+	int id;
+	struct device dev;
+	const struct scrub_ops *ops;
+};
+
+#define to_scrub_device(d) container_of(d, struct scrub_device, dev)
+static ssize_t enable_background_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf, size_t len) {
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	bool enable;
+	int ret;
+
+	ret = kstrtobool(buf, &enable);
+	if (ret < 0)
+		return ret;
+
+	ret = scrub_dev->ops->set_enabled_bg(dev, enable);
+	if (ret)
+		return ret;
+
+	return len;
+}
+
+static ssize_t enable_background_show(struct device *dev,
+				      struct device_attribute *attr, char *buf) {
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	bool enable;
+	int ret;
+
+	ret = scrub_dev->ops->get_enabled_bg(dev, &enable);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "%d\n", enable); }
+
+static ssize_t name_show(struct device *dev,
+			 struct device_attribute *attr, char *buf) {
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	int ret;
+
+	ret = scrub_dev->ops->get_name(dev, buf);
+	if (ret)
+		return ret;
+
+	return strlen(buf);
+}
+
+static ssize_t rate_show(struct device *dev, struct device_attribute *attr,
+			 char *buf)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	u64 val;
+	int ret;
+
+	ret = scrub_dev->ops->rate_read(dev, &val);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "0x%llx\n", val); }
+
+static ssize_t rate_store(struct device *dev, struct device_attribute *attr,
+			  const char *buf, size_t len)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	long val;
+	int ret;
+
+	ret = kstrtol(buf, 10, &val);
+	if (ret < 0)
+		return ret;
+
+	ret = scrub_dev->ops->rate_write(dev, val);
+	if (ret)
+		return ret;
+
+	return len;
+}
+
+static ssize_t rate_available_show(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	u64 min_sr, max_sr;
+	int ret;
+
+	ret = scrub_dev->ops->rate_avail_range(dev, &min_sr, &max_sr);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "0x%llx-0x%llx\n", min_sr, max_sr); }
+
+DEVICE_ATTR_RW(enable_background);
+DEVICE_ATTR_RO(name);
+DEVICE_ATTR_RW(rate);
+DEVICE_ATTR_RO(rate_available);
+
+static struct attribute *scrub_attrs[] = {
+	&dev_attr_enable_background.attr,
+	&dev_attr_name.attr,
+	&dev_attr_rate.attr,
+	&dev_attr_rate_available.attr,
+	NULL
+};
+
+static umode_t scrub_attr_visible(struct kobject *kobj,
+				  struct attribute *a, int attr_id) {
+	struct device *dev = kobj_to_dev(kobj);
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	const struct scrub_ops *ops = scrub_dev->ops;
+
+	if (a == &dev_attr_enable_background.attr) {
+		if (ops->set_enabled_bg && ops->get_enabled_bg)
+			return a->mode;
+		if (ops->get_enabled_bg)
+			return 0444;
+		return 0;
+	}
+	if (a == &dev_attr_name.attr)
+		return ops->get_name ? a->mode : 0;
+	if (a == &dev_attr_rate_available.attr)
+		return ops->rate_avail_range ? a->mode : 0;
+	if (a == &dev_attr_rate.attr) { /* Write only makes little sense */
+		if (ops->rate_read && ops->rate_write)
+			return a->mode;
+		if (ops->rate_read)
+			return 0444;
+		return 0;
+	}
+
+	return 0;
+}
+
+static const struct attribute_group scrub_attr_group = {
+	.name		= "scrub",
+	.attrs		= scrub_attrs,
+	.is_visible	= scrub_attr_visible,
+};
+
+static const struct attribute_group *scrub_attr_groups[] = {
+	&scrub_attr_group,
+	NULL
+};
+
+static void scrub_dev_release(struct device *dev) {
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+
+	ida_free(&scrub_ida, scrub_dev->id);
+	kfree(scrub_dev);
+}
+
+static struct class scrub_class = {
+	.name = "ras",
+	.dev_groups = scrub_attr_groups,
+	.dev_release = scrub_dev_release,
+};
+
+static struct device *
+scrub_device_register(struct device *parent, void *drvdata,
+		      const struct scrub_ops *ops)
+{
+	struct scrub_device *scrub_dev;
+	struct device *hdev;
+	int err;
+
+	scrub_dev = kzalloc(sizeof(*scrub_dev), GFP_KERNEL);
+	if (!scrub_dev)
+		return ERR_PTR(-ENOMEM);
+	hdev = &scrub_dev->dev;
+
+	scrub_dev->id = ida_alloc(&scrub_ida, GFP_KERNEL);
+	if (scrub_dev->id < 0) {
+		kfree(scrub_dev);
+		return ERR_PTR(-ENOMEM);
+	}
Quoted 8 lines

drivers/ras/memory_scrub.c line 215

Quoted 6 lines
drivers/ras/memory_scrub.c new file +6 -0
+
+	scrub_dev->ops = ops;
+	hdev->class = &scrub_class;
+	hdev->parent = parent;
+	dev_set_drvdata(hdev, drvdata);
+	dev_set_name(hdev, SCRUB_ID_FORMAT, scrub_dev->id);

Need to check the return value of dev_set_name?

Will do, though checking return value of dev_set_name() is not common in the kernel.

Quoted 5 lines

fan

drivers/ras/memory_scrub.c line 217

Quoted 2 lines
drivers/ras/memory_scrub.c new file +2 -0
+	err = device_register(hdev);
+	if (err) {

Thanks,
Shiju

maintainer
#4 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

In reply to [email protected]
Quoted 10 lines · On Sat, Apr 20, 2024 at 12:47:10AM +0800, [email protected] wrote:

From: Shiju Jose <[email protected]>

Add scrub subsystem supports configuring the memory scrubbers in the system. The scrub subsystem provides the interface for registering the scrub devices. The scrub control attributes are provided to the user in /sys/class/ras/rasX/scrub

Quoted 10 lines
---
 .../ABI/testing/sysfs-class-scrub-configure   |  47 +++
 drivers/ras/Kconfig                           |   7 +
 drivers/ras/Makefile                          |   1 +
 drivers/ras/memory_scrub.c                    | 271 ++++++++++++++++++
 include/linux/memory_scrub.h                  |  37 +++
 5 files changed, 363 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-scrub-configure
 create mode 100755 drivers/ras/memory_scrub.c
 create mode 100755 include/linux/memory_scrub.h

ERROR: modpost: missing MODULE_LICENSE() in drivers/ras/memory_scrub.o make[2]: *** [scripts/Makefile.modpost:145: Module.symvers] Error 1 make[1]: *** [/mnt/kernel/kernel/2nd/linux/Makefile:1871: modpost] Error 2
make: *** [Makefile:240: __sub-make] Error 2

Each patch of yours needs to build.

Quoted 10 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +4 -0
new file mode 100644
index 000000000000..3ed77dbb00ad
@@ -0,0 +1,47 @@
+What:		/sys/class/ras/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]

Documentation/ABI/testing/sysfs-class-scrub-configure line 14

Quoted 10 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +10 -0
+Description:
+		The ras/ class subdirectory belongs to the
+		common ras features such as scrub subsystem.
+
+What:		/sys/class/ras/rasX/scrub/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The /sys/class/ras/ras{0,1,2,3,...}/scrub directories

You have different scrubbers.

I'd prefer if you put their names in here instead and do this structure:

/sys/class/ras/scrub/cxl-patrol

		    /ars
		    /cxl-ecs
		    /acpi-ras2

and so on.

Unless the idea is for those devices to have multiple RAS-specific functionality than just scrubbing. Then you want to do

/sys/class/ras/cxl/scrub
/other_function

/sys/class/ras/ars/scrub

		  /...

You get the idea.

Quoted 8 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +8 -0
+		correspond to each scrub device registered with the
+		scrub subsystem.
+
+What:		/sys/class/ras/rasX/scrub/name
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:

Documentation/ABI/testing/sysfs-class-scrub-configure line 32

Quoted 10 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +10 -0
+		(RO) name of the memory scrubber
+
+What:		/sys/class/ras/rasX/scrub/enable_background
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) Enable/Disable background(patrol) scrubbing if supported.
+
+What:		/sys/class/ras/rasX/scrub/rate_available

That's dumping a range so I guess it should be called probably "possible_rates" or so, so that it is clear what it means.

If some scrubbers support only a discrete set of rate values, then "possible_rates" fits too if you dump them as a list of values.

Quoted 20 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +15 -0
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) Supported range for the scrub rate by the scrubber.
+		The scrub rate represents in hours.
+
+What:		/sys/class/ras/rasX/scrub/rate
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) The scrub rate specified and it must be with in the
+		supported range by the scrubber.
+		The scrub rate represents in hours.
drivers/ras/Kconfig +0 -0
index fc4f4bb94a4c..181701479564 100644

drivers/ras/Kconfig config RAS_FMPM · line 52

Quoted 7 lines
drivers/ras/Kconfig +4 -0
@@ -46,4 +46,11 @@ config RAS_FMPM
 	  Memory will be retired during boot time and run time depending on
 	  platform-specific policies.
 
+config SCRUB
+	tristate "Memory scrub driver"
+	help
+	  This option selects the memory scrub subsystem, supports

s/This option selects/Enable/

Quoted 31 lines
drivers/ras/Kconfig +3 -0
+	  configuring the parameters of underlying scrubbers in the
+	  system for the DRAM memories.
+
 endif
drivers/ras/Makefile +1 -0
index 11f95d59d397..89bcf0d84355 100644
@@ -2,6 +2,7 @@
 obj-$(CONFIG_RAS)	+= ras.o
 obj-$(CONFIG_DEBUG_FS)	+= debugfs.o
 obj-$(CONFIG_RAS_CEC)	+= cec.o
+obj-$(CONFIG_SCRUB)	+= memory_scrub.o
 
 obj-$(CONFIG_RAS_FMPM)	+= amd/fmpm.o
 obj-y			+= amd/atl/
drivers/ras/memory_scrub.c new file +9 -0
new file mode 100755
index 000000000000..7e995380ec3a
@@ -0,0 +1,271 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Memory scrub subsystem supports configuring the registered
+ * memory scrubbers.
+ *
+ * Copyright (c) 2024 HiSilicon Limited.
+ */
+
+#define pr_fmt(fmt)     "MEM SCRUB: " fmt

drivers/ras/memory_scrub.c line 19

Quoted 10 lines
drivers/ras/memory_scrub.c new file +10 -0
+
+#include <linux/acpi.h>
+#include <linux/bitops.h>
+#include <linux/delay.h>
+#include <linux/kfifo.h>
+#include <linux/memory_scrub.h>
+#include <linux/platform_device.h>
+#include <linux/spinlock.h>
+
+/* memory scrubber config definitions */

No need for that comment.

Quoted 4 lines
drivers/ras/memory_scrub.c new file +4 -0
+static ssize_t rate_available_show(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)
+{

drivers/ras/memory_scrub.c line 123

Quoted 10 lines
drivers/ras/memory_scrub.c new file +10 -0
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	u64 min_sr, max_sr;
+	int ret;
+
+	ret = scrub_dev->ops->rate_avail_range(dev, &min_sr, &max_sr);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "0x%llx-0x%llx\n", min_sr, max_sr);
+}

This glue driver will need to store the min and max scrub rates on init and rate_store() will have to verify the newly supplied rate is within that range before writing it.

Not the user, nor the underlying hw driver.

drivers/ras/memory_scrub.c line 128

Quoted 5 lines
drivers/ras/memory_scrub.c new file +5 -0
+
+DEVICE_ATTR_RW(enable_background);
+DEVICE_ATTR_RO(name);
+DEVICE_ATTR_RW(rate);
+DEVICE_ATTR_RO(rate_available);

static

Quoted 24 lines
drivers/ras/memory_scrub.c new file +24 -0
+
+static struct attribute *scrub_attrs[] = {
+	&dev_attr_enable_background.attr,
+	&dev_attr_name.attr,
+	&dev_attr_rate.attr,
+	&dev_attr_rate_available.attr,
+	NULL
+};
+
+static umode_t scrub_attr_visible(struct kobject *kobj,
+				  struct attribute *a, int attr_id)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	const struct scrub_ops *ops = scrub_dev->ops;
+
+	if (a == &dev_attr_enable_background.attr) {
+		if (ops->set_enabled_bg && ops->get_enabled_bg)
+			return a->mode;
+		if (ops->get_enabled_bg)
+			return 0444;
+		return 0;
+	}
+	if (a == &dev_attr_name.attr)

drivers/ras/memory_scrub.c line 162

Quoted 10 lines
drivers/ras/memory_scrub.c new file +10 -0
+		return ops->get_name ? a->mode : 0;
+	if (a == &dev_attr_rate_available.attr)
+		return ops->rate_avail_range ? a->mode : 0;
+	if (a == &dev_attr_rate.attr) { /* Write only makes little sense */
+		if (ops->rate_read && ops->rate_write)
+			return a->mode;
+		if (ops->rate_read)
+			return 0444;
+		return 0;
+	}

All of that stuff's permissions should be root-only.

Quoted 33 lines
drivers/ras/memory_scrub.c new file +33 -0
+
+	return 0;
+}
+
+static const struct attribute_group scrub_attr_group = {
+	.name		= "scrub",
+	.attrs		= scrub_attrs,
+	.is_visible	= scrub_attr_visible,
+};
+
+static const struct attribute_group *scrub_attr_groups[] = {
+	&scrub_attr_group,
+	NULL
+};
+
+static void scrub_dev_release(struct device *dev)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+
+	ida_free(&scrub_ida, scrub_dev->id);
+	kfree(scrub_dev);
+}
+
+static struct class scrub_class = {
+	.name = "ras",
+	.dev_groups = scrub_attr_groups,
+	.dev_release = scrub_dev_release,
+};
+
+static struct device *
+scrub_device_register(struct device *parent, void *drvdata,
+		      const struct scrub_ops *ops)
+{

drivers/ras/memory_scrub.c line 205

Quoted 10 lines
drivers/ras/memory_scrub.c new file +10 -0
+	struct scrub_device *scrub_dev;
+	struct device *hdev;
+	int err;
+
+	scrub_dev = kzalloc(sizeof(*scrub_dev), GFP_KERNEL);
+	if (!scrub_dev)
+		return ERR_PTR(-ENOMEM);
+	hdev = &scrub_dev->dev;
+
+	scrub_dev->id = ida_alloc(&scrub_ida, GFP_KERNEL);

What's that silly thing for?

Quoted 50 lines
drivers/ras/memory_scrub.c new file +50 -0
+	if (scrub_dev->id < 0) {
+		kfree(scrub_dev);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	scrub_dev->ops = ops;
+	hdev->class = &scrub_class;
+	hdev->parent = parent;
+	dev_set_drvdata(hdev, drvdata);
+	dev_set_name(hdev, SCRUB_ID_FORMAT, scrub_dev->id);
+	err = device_register(hdev);
+	if (err) {
+		put_device(hdev);
+		return ERR_PTR(err);
+	}
+
+	return hdev;
+}
+
+static void devm_scrub_release(void *dev)
+{
+	device_unregister(dev);
+}
+
+/**
+ * devm_scrub_device_register - register scrubber device
+ * @dev: the parent device
+ * @drvdata: driver data to attach to the scrub device
+ * @ops: pointer to scrub_ops structure (optional)
+ *
+ * Returns the pointer to the new device on success, ERR_PTR() otherwise.
+ * The new device would be automatically unregistered with the parent device.
+ */
+struct device *
+devm_scrub_device_register(struct device *dev, void *drvdata,
+			   const struct scrub_ops *ops)
+{
+	struct device *hdev;
+	int ret;
+
+	if (!dev)
+		return ERR_PTR(-EINVAL);
+
+	hdev = scrub_device_register(dev, drvdata, ops);
+	if (IS_ERR(hdev))
+		return hdev;
+
+	ret = devm_add_action_or_reset(dev, devm_scrub_release, hdev);
+	if (ret)
+		return ERR_PTR(ret);

drivers/ras/memory_scrub.c line 265

Quoted 10 lines
drivers/ras/memory_scrub.c new file +10 -0
+
+	return hdev;
+}
+EXPORT_SYMBOL_GPL(devm_scrub_device_register);
+
+static int __init memory_scrub_control_init(void)
+{
+	return class_register(&scrub_class);
+}
+subsys_initcall(memory_scrub_control_init);

You can't just blindly register this thing without checking whether there are even any hw scrubber devices on the system.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author
#5 lore raw

RE: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Hi Boris,

Thanks for the feedbacks.

Please find reply inline,

Thanks,
Shiju

Quoted 46 lines

-----Original Message-----
From: Borislav Petkov <[email protected]>
Sent: 25 April 2024 11:16
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang <[email protected]>; Linuxarm <[email protected]>
Subject: Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 20 lines · On Sat, Apr 20, 2024 at 12:47:10AM +0800, [email protected] wrote:

From: Shiju Jose <[email protected]>

Add scrub subsystem supports configuring the memory scrubbers in the system. The scrub subsystem provides the interface for registering the scrub devices. The scrub control attributes are provided to the user in /sys/class/ras/rasX/scrub

---
 .../ABI/testing/sysfs-class-scrub-configure   |  47 +++
 drivers/ras/Kconfig                           |   7 +
 drivers/ras/Makefile                          |   1 +
 drivers/ras/memory_scrub.c                    | 271 ++++++++++++++++++
 include/linux/memory_scrub.h                  |  37 +++
 5 files changed, 363 insertions(+)
 create mode 100644
Documentation/ABI/testing/sysfs-class-scrub-configure
 create mode 100755 drivers/ras/memory_scrub.c  create mode 100755
Quoted 1 line

include/linux/memory_scrub.h

Quoted 6 lines

ERROR: modpost: missing MODULE_LICENSE() in drivers/ras/memory_scrub.o make[2]: *** [scripts/Makefile.modpost:145: Module.symvers] Error 1 make[1]: *** [/mnt/kernel/kernel/2nd/linux/Makefile:1871: modpost] Error 2
make: *** [Makefile:240: __sub-make] Error 2

Each patch of yours needs to build.

Fixed.

Quoted 37 lines
Quoted 12 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +4 -0
new file mode 100644
index 000000000000..3ed77dbb00ad
@@ -0,0 +1,47 @@
+What:		/sys/class/ras/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]

Documentation/ABI/testing/sysfs-class-scrub-configure line 14

Quoted 10 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +10 -0
+Description:
+		The ras/ class subdirectory belongs to the
+		common ras features such as scrub subsystem.
+
+What:		/sys/class/ras/rasX/scrub/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The /sys/class/ras/ras{0,1,2,3,...}/scrub directories

You have different scrubbers.

I'd prefer if you put their names in here instead and do this structure:

/sys/class/ras/scrub/cxl-patrol

		    /ars
		    /cxl-ecs
		    /acpi-ras2

and so on.

Unless the idea is for those devices to have multiple RAS-specific functionality than just scrubbing. Then you want to do

Quoted 7 lines

/sys/class/ras/cxl/scrub
/other_function

/sys/class/ras/ars/scrub

		  /...

You get the idea.

It is expected to have multiple RAS-specific functionalities other than scrubbing in long run. Most of the classes in the kernel found as /sys/class/<class-name>/<class-name>X/

If not, however /sys/class/ras/<module -name>X/<feature> is more suitable because there are multiple device instances such as cxl devices with scrub control feature. For example, /sys/class/ras/cxlX/scrub

Quoted 23 lines
Quoted 9 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +8 -0
+		correspond to each scrub device registered with the
+		scrub subsystem.
+
+What:		/sys/class/ras/rasX/scrub/name
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:

Documentation/ABI/testing/sysfs-class-scrub-configure line 32

Quoted 10 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +10 -0
+		(RO) name of the memory scrubber
+
+What:		/sys/class/ras/rasX/scrub/enable_background
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) Enable/Disable background(patrol) scrubbing if supported.
+
+What:		/sys/class/ras/rasX/scrub/rate_available

That's dumping a range so I guess it should be called probably "possible_rates" or so, so that it is clear what it means.

Quoted 2 lines

If some scrubbers support only a discrete set of rate values, then "possible_rates" fits too if you dump them as a list of values.

Sure. Will check.

Quoted 24 lines
Quoted 24 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +15 -0
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) Supported range for the scrub rate by the scrubber.
+		The scrub rate represents in hours.
+
+What:		/sys/class/ras/rasX/scrub/rate
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) The scrub rate specified and it must be with in the
+		supported range by the scrubber.
+		The scrub rate represents in hours.
drivers/ras/Kconfig index renamed from drivers/ras/Kconfig +0 -0

fc4f4bb94a4c..181701479564 100644

drivers/ras/Kconfig +0 -0
@@ -46,4 +46,11 @@ config RAS_FMPM
 	  Memory will be retired during boot time and run time depending on
 	  platform-specific policies.
 
Quoted 6 lines

drivers/ras/Kconfig config RAS_FMPM · line 52

Quoted 4 lines
drivers/ras/Kconfig +4 -0
+config SCRUB
+	tristate "Memory scrub driver"
+	help
+	  This option selects the memory scrub subsystem, supports

s/This option selects/Enable/

Sure.

Quoted 35 lines
Quoted 35 lines
drivers/ras/Kconfig +3 -0
+	  configuring the parameters of underlying scrubbers in the
+	  system for the DRAM memories.
+
 endif
drivers/ras/Makefile index renamed from drivers/ras/Makefile +0 -0

11f95d59d397..89bcf0d84355 100644

drivers/ras/Makefile +1 -0
@@ -2,6 +2,7 @@
 obj-$(CONFIG_RAS)	+= ras.o
 obj-$(CONFIG_DEBUG_FS)	+= debugfs.o
 obj-$(CONFIG_RAS_CEC)	+= cec.o
+obj-$(CONFIG_SCRUB)	+= memory_scrub.o
 
 obj-$(CONFIG_RAS_FMPM)	+= amd/fmpm.o
 obj-y			+= amd/atl/
drivers/ras/memory_scrub.c new file +13 -0
new file mode 100755 index 000000000000..7e995380ec3a
@@ -0,0 +1,271 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Memory scrub subsystem supports configuring the registered
+ * memory scrubbers.
+ *
+ * Copyright (c) 2024 HiSilicon Limited.
+ */
+
+#define pr_fmt(fmt)     "MEM SCRUB: " fmt
+
+#include <linux/acpi.h>
+#include <linux/bitops.h>
+#include <linux/delay.h>
Quoted 8 lines

drivers/ras/memory_scrub.c line 19

Quoted 6 lines
drivers/ras/memory_scrub.c new file +6 -0
+#include <linux/kfifo.h>
+#include <linux/memory_scrub.h>
+#include <linux/platform_device.h>
+#include <linux/spinlock.h>
+
+/* memory scrubber config definitions */

No need for that comment.

Will remove.

Quoted 19 lines
Quoted 4 lines
drivers/ras/memory_scrub.c new file +3 -0
+static ssize_t rate_available_show(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)

drivers/ras/memory_scrub.c

Quoted 10 lines
drivers/ras/memory_scrub.c new file +10 -0
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	u64 min_sr, max_sr;
+	int ret;
+
+	ret = scrub_dev->ops->rate_avail_range(dev, &min_sr, &max_sr);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "0x%llx-0x%llx\n", min_sr, max_sr); }

This glue driver will need to store the min and max scrub rates on init and rate_store() will have to verify the newly supplied rate is within that range before writing it.

Quoted 1 line

Not the user, nor the underlying hw driver.

Presently underlying hw driver does the check. I think this will become more complex if does in the common rate_store() if we have to check against either a list of possible rates or min and max rates.

Quoted 36 lines

drivers/ras/memory_scrub.c line 128

Quoted 6 lines
drivers/ras/memory_scrub.c new file +5 -0
+
+DEVICE_ATTR_RW(enable_background);
+DEVICE_ATTR_RO(name);
+DEVICE_ATTR_RW(rate);
+DEVICE_ATTR_RO(rate_available);

static

Quoted 27 lines
drivers/ras/memory_scrub.c new file +27 -0
+
+static struct attribute *scrub_attrs[] = {
+	&dev_attr_enable_background.attr,
+	&dev_attr_name.attr,
+	&dev_attr_rate.attr,
+	&dev_attr_rate_available.attr,
+	NULL
+};
+
+static umode_t scrub_attr_visible(struct kobject *kobj,
+				  struct attribute *a, int attr_id) {
+	struct device *dev = kobj_to_dev(kobj);
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+	const struct scrub_ops *ops = scrub_dev->ops;
+
+	if (a == &dev_attr_enable_background.attr) {
+		if (ops->set_enabled_bg && ops->get_enabled_bg)
+			return a->mode;
+		if (ops->get_enabled_bg)
+			return 0444;
+		return 0;
+	}
+	if (a == &dev_attr_name.attr)
+		return ops->get_name ? a->mode : 0;
+	if (a == &dev_attr_rate_available.attr)
+		return ops->rate_avail_range ? a->mode : 0;
+	if (a == &dev_attr_rate.attr) { /* Write only makes little sense */
Quoted 8 lines

drivers/ras/memory_scrub.c line 162

Quoted 6 lines
drivers/ras/memory_scrub.c new file +6 -0
+		if (ops->rate_read && ops->rate_write)
+			return a->mode;
+		if (ops->rate_read)
+			return 0444;
+		return 0;
+	}

All of that stuff's permissions should be root-only.

Sure.

Quoted 37 lines
Quoted 37 lines
drivers/ras/memory_scrub.c new file +36 -0
+
+	return 0;
+}
+
+static const struct attribute_group scrub_attr_group = {
+	.name		= "scrub",
+	.attrs		= scrub_attrs,
+	.is_visible	= scrub_attr_visible,
+};
+
+static const struct attribute_group *scrub_attr_groups[] = {
+	&scrub_attr_group,
+	NULL
+};
+
+static void scrub_dev_release(struct device *dev) {
+	struct scrub_device *scrub_dev = to_scrub_device(dev);
+
+	ida_free(&scrub_ida, scrub_dev->id);
+	kfree(scrub_dev);
+}
+
+static struct class scrub_class = {
+	.name = "ras",
+	.dev_groups = scrub_attr_groups,
+	.dev_release = scrub_dev_release,
+};
+
+static struct device *
+scrub_device_register(struct device *parent, void *drvdata,
+		      const struct scrub_ops *ops)
+{
+	struct scrub_device *scrub_dev;
+	struct device *hdev;
+	int err;
+
Quoted 8 lines

drivers/ras/memory_scrub.c line 205

Quoted 6 lines
drivers/ras/memory_scrub.c new file +6 -0
+	scrub_dev = kzalloc(sizeof(*scrub_dev), GFP_KERNEL);
+	if (!scrub_dev)
+		return ERR_PTR(-ENOMEM);
+	hdev = &scrub_dev->dev;
+
+	scrub_dev->id = ida_alloc(&scrub_ida, GFP_KERNEL);

What's that silly thing for?

This is the ras instance id (X) used for scrub control feature, /sys/class/ras/rasX/scrub/

Quoted 54 lines
Quoted 22 lines
drivers/ras/memory_scrub.c new file +21 -0
+	if (scrub_dev->id < 0) {
+		kfree(scrub_dev);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	scrub_dev->ops = ops;
+	hdev->class = &scrub_class;
+	hdev->parent = parent;
+	dev_set_drvdata(hdev, drvdata);
+	dev_set_name(hdev, SCRUB_ID_FORMAT, scrub_dev->id);
+	err = device_register(hdev);
+	if (err) {
+		put_device(hdev);
+		return ERR_PTR(err);
+	}
+
+	return hdev;
+}
+
+static void devm_scrub_release(void *dev) {
+	device_unregister(dev);

drivers/ras/memory_scrub.c

Quoted 10 lines
drivers/ras/memory_scrub.c new file +10 -0
+}
+
+/**
+ * devm_scrub_device_register - register scrubber device
+ * @dev: the parent device
+ * @drvdata: driver data to attach to the scrub device
+ * @ops: pointer to scrub_ops structure (optional)
+ *
+ * Returns the pointer to the new device on success, ERR_PTR() otherwise.
+ * The new device would be automatically unregistered with the parent

device.

Quoted 21 lines
drivers/ras/memory_scrub.c new file +21 -0
+ */
+struct device *
+devm_scrub_device_register(struct device *dev, void *drvdata,
+			   const struct scrub_ops *ops)
+{
+	struct device *hdev;
+	int ret;
+
+	if (!dev)
+		return ERR_PTR(-EINVAL);
+
+	hdev = scrub_device_register(dev, drvdata, ops);
+	if (IS_ERR(hdev))
+		return hdev;
+
+	ret = devm_add_action_or_reset(dev, devm_scrub_release, hdev);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return hdev;
+}
Quoted 8 lines

drivers/ras/memory_scrub.c line 265

Quoted 5 lines
drivers/ras/memory_scrub.c new file +5 -0
+EXPORT_SYMBOL_GPL(devm_scrub_device_register);
+
+static int __init memory_scrub_control_init(void) {
+	return class_register(&scrub_class); }
+subsys_initcall(memory_scrub_control_init);

You can't just blindly register this thing without checking whether there are even any hw scrubber devices on the system.

I think it happens only when a dependent module as autoloaded based on a scrub device existing with exception of memory scrub control built in and who would build this in?

Quoted 5 lines

--
Regards/Gruss,
Boris.

Thanks,
Shiju

maintainer
#6 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 8 lines · Shiju Jose

It is expected to have multiple RAS-specific functionalities other than scrubbing in long run. Most of the classes in the kernel found as /sys/class/<class-name>/<class-name>X/

If not, however /sys/class/ras/<module -name>X/<feature> is more suitable because there are multiple device instances such as cxl devices with scrub control feature. For example, /sys/class/ras/cxlX/scrub

Make it as user-friendly as possible. cxlX is not as user-friendly as

/sys/class/ras/cxl/<mem_accelerator>

		  /<fancy_bla_thing>

and so on.

Yes, you can introduce a special category .../ras/cxl/ if there are multiple cxl devices which have RAS functionality on them.

Quoted 3 lines

Presently underlying hw driver does the check. I think this will become more complex if does in the common rate_store() if we have to check against either a list of possible rates or min and max rates.

Ok.

Quoted 6 lines
Quoted 6 lines

drivers/ras/memory_scrub.c line 128

Quoted 4 lines
drivers/ras/memory_scrub.c new file +4 -0
+DEVICE_ATTR_RW(enable_background);
+DEVICE_ATTR_RO(name);
+DEVICE_ATTR_RW(rate);
+DEVICE_ATTR_RO(rate_available);

static

Forgot one.

Quoted 1 line

This is the ras instance id (X) used for scrub control feature, /sys/class/ras/rasX/scrub/

Yeah, as discussed above.

Quoted 10 lines
Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+static int __init memory_scrub_control_init(void) {
+	return class_register(&scrub_class); }
+subsys_initcall(memory_scrub_control_init);

You can't just blindly register this thing without checking whether there are even any hw scrubber devices on the system.

I think it happens only when a dependent module as autoloaded based on a scrub device existing with exception of memory scrub control built in and who would build this in?

You think or you know?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author
#7 lore raw

RE: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 35 lines

-----Original Message-----
From: Borislav Petkov <[email protected]>
Sent: 06 May 2024 11:30
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang <[email protected]>; Linuxarm <[email protected]>
Subject: Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 8 lines · Shiju Jose

It is expected to have multiple RAS-specific functionalities other than scrubbing in long run. Most of the classes in the kernel found as /sys/class/<class-name>/<class-name>X/

If not, however /sys/class/ras/<module -name>X/<feature> is more suitable because there are multiple device instances such as cxl devices with scrub control feature. For example, /sys/class/ras/cxlX/scrub

Make it as user-friendly as possible. cxlX is not as user-friendly as

Quoted 7 lines

/sys/class/ras/cxl/<mem_accelerator>

		  /<fancy_bla_thing>

and so on.

Yes, you can introduce a special category .../ras/cxl/ if there are multiple cxl devices which have RAS functionality on them.

Sure.

Quoted 7 lines
Quoted 4 lines

Presently underlying hw driver does the check. I think this will become more complex if does in the common rate_store() if we have to check against either a list of possible rates or min and max rates.

Ok.

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

drivers/ras/memory_scrub.c line 128

Quoted 4 lines
drivers/ras/memory_scrub.c new file +4 -0
+DEVICE_ATTR_RW(enable_background);
+DEVICE_ATTR_RO(name);
+DEVICE_ATTR_RW(rate);
+DEVICE_ATTR_RO(rate_available);

static

Forgot one.

Will correct.

Quoted 5 lines
Quoted 3 lines

This is the ras instance id (X) used for scrub control feature, /sys/class/ras/rasX/scrub/

Yeah, as discussed above.

OK.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Review

Quoted 4 lines
(quoted diff) +3 -0
+static int __init memory_scrub_control_init(void) {
+	return class_register(&scrub_class); }
+subsys_initcall(memory_scrub_control_init);

You can't just blindly register this thing without checking whether there are even any hw scrubber devices on the system.

Quoted 5 lines
Quoted 3 lines

I think it happens only when a dependent module as autoloaded based on a scrub device existing with exception of memory scrub control built in and who would build this in?

You think or you know?

We know as I had tested.

Quoted 6 lines

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

Thanks,
Shiju

maintainer
#8 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 6 lines · Shiju Jose
Quoted 5 lines
Quoted 3 lines

I think it happens only when a dependent module as autoloaded based on a scrub device existing with exception of memory scrub control built in and who would build this in?

You think or you know?

We know as I had tested.

Does this thing register successfully on a machine which doesn't have those devices?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author
#9 lore raw

RE: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 28 lines

-----Original Message-----
From: Borislav Petkov <[email protected]>
Sent: 08 May 2024 18:20
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang <[email protected]>; Linuxarm <[email protected]>
Subject: Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 4 lines · Shiju Jose
Quoted 4 lines
Quoted 3 lines

I think it happens only when a dependent module as autoloaded based on a scrub device existing with exception of memory scrub control built in and who would build this in?

Quoted 6 lines
Quoted 2 lines
Quoted 1 line

You think or you know?

We know as I had tested.

Does this thing register successfully on a machine which doesn't have those devices?

I mean scrub subsystem module is not loaded and initialzed until a dependent device module is loaded and a device does not get registered with the scrub subsystem on a machine which doesn't have the corresponding scrub features.

Quoted 5 lines

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

Thanks,
Shiju

maintainer
#10 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 4 lines · Shiju Jose

I mean scrub subsystem module is not loaded and initialzed until a dependent device module is loaded and a device does not get registered with the scrub subsystem on a machine which doesn't have the corresponding scrub features.

Stop this rambling blabla please. This should *not* happen:

# insmod ./memory_scrub.ko
# echo $?
0
# lsmod
Module                  Size  Used by
memory_scrub           12288  0

This is on a silly guest which has none of those dependent devices crap.

Your scrub module should load only on a machine which has the hardware
- not just for fun and on anything.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#11 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 15 lines · On Wed, 8 May 2024 21:25:46 +0200 Borislav Petkov <[email protected]> wrote:
Quoted 4 lines · Shiju Jose

I mean scrub subsystem module is not loaded and initialzed until a dependent device module is loaded and a device does not get registered with the scrub subsystem on a machine which doesn't have the corresponding scrub features.

Stop this rambling blabla please. This should *not* happen:

# insmod ./memory_scrub.ko
# echo $?
0
# lsmod
Module                  Size  Used by
memory_scrub           12288  0
Quoted 4 lines

This is on a silly guest which has none of those dependent devices crap.

Your scrub module should load only on a machine which has the hardware
- not just for fun and on anything.

Fundamental question seems to be: Why should it not load? Shiju and I think it should, you think it shouldn't.
Note this is only if someone deliberately ignores all the infrastructure intended to make sure only relevant modules probe and modprobe / insmod by hand.

+CC some driver core folk and a few other subsystem maintainers who
have subsystems doing the same as this one.

Summary I think is:
Borislav is asking for this new scrub subsystem core module to not successfully probe and call class_register() if it is manually inserted and there is no hardware on the particular system. It's a standard class type situation with core driver providing consistent ABI and /sys/class/ras/ with drivers hanging off various buses (currently ACPI and CXL) registering with that class.

Many subsystem core drivers will probe and create subsystem specific sysfs directories on on systems that don't have any hardware needing drivers from that subsystem (if someone manually inserts them rather than relying on automatic module dependency handling.)
I don't see why this class driver should be different and have to jump through hoops to satisfy this requirement.

A quick look for callers of class_register() in their init functions found plenty of precedence. Many of the cases that don't do this are single use - i.e. class that only ever has one driver. There are even more if we take sysfs buses into account. (edac and IIO for example)

A few examples of same handling of class registration.
- input - that registers a lot more on class init, but sysfs class registration is in there.
- hwmon - other than some quirk setup same as the scrub driver.

Other than embedded systems with a custom build and kernel developers, who actually probes modules manually? Mostly people rely on modalias of the client drivers and them pulling in their dependencies. Modules are pretty pointless if you probe all the ones you've built whether or not the hardware is present.

It would of course be easy to do the class_register() on first driver use but I'm not seeing a lot of precedence + the scrub class module would still insmod successfully. I think preventing load would be messy and complex at best.

Jonathan

maintainer
#12 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 6 lines · Jonathan Cameron

Many subsystem core drivers will probe and create subsystem specific sysfs directories on on systems that don't have any hardware needing drivers from that subsystem (if someone manually inserts them rather than relying on automatic module dependency handling.)
I don't see why this class driver should be different and have to jump through hoops to satisfy this requirement.

You mean it should load because "Look ma, the others do it this way". Does it make any sense? Of course not.

Are you arguing for the nonsensical "it should load" case because it is simply easier this way? How hard is that "jump through hoops" thing anyway?

You mean it should load so that when booting an allmodconfig kernel there are not enough modules which are loading so lemme load one more. And then I need to go and rmmod them all before I need to do localmodconfig and build a tailored kernel for the machine.

Or is there some other reason to load silly modules, use up resources for no good reason whatsoever and bloat the machine?

You mean, f*ck it, right? Who cares...

Geez.

Signature · 1 line
Sent from a small device: formatting sucks and brevity is inevitable.
maintainer
#13 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 3 lines · Borislav Petkov

Are you arguing for the nonsensical "it should load" case because it is simply easier this way? How hard is that "jump through hoops" thing anyway?

Let's see: the following patches add something called GET_SUPPORTED_FEATURES which is used to detect whether the system has patrol scrub functionality etc.

Then there's ras2_acpi_init() which checks for a RAS2 ACPI table.

Are you saying that checking for those two things in the init function is jumping through hoops?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#14 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 5 lines · Borislav Petkov
Quoted 3 lines · Borislav Petkov

Are you arguing for the nonsensical "it should load" case because it is simply easier this way? How hard is that "jump through hoops" thing anyway?

Quoted 8 lines

Let's see: the following patches add something called GET_SUPPORTED_FEATURES which is used to detect whether the system has patrol scrub functionality etc.

Then there's ras2_acpi_init() which checks for a RAS2 ACPI table.

Are you saying that checking for those two things in the init function is jumping through hoops?

Wait, this discussion has gone off the rails.

Recall that there are 461 usages of module_pci_driver() in the kernel. Every one of those arranges for just registering a PCI driver when the module is loaded regardless of whether any devices that driver cares about are present.

Consider the case of the PCI subsystem that allows dynamically updating the device ids that a driver attaches. I.e.

echo $id > /sys/bus/pci/drivers/$driver/new_id

...the fundamentally does not work if the module unloads itself and the driver(s) it registers when the PCI bus core finds no devices to attach at runtime.

The mitigation for keeping unneeded modules unloaded is that udev does not autoload modules unless a PCI bus udev event matches a module's published PCI device table.

Don't want to waste module memory? Don't load the module.

maintainer
#15 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 4 lines · Dan Williams

Recall that there are 461 usages of module_pci_driver() in the kernel. Every one of those arranges for just registering a PCI driver when the module is loaded regardless of whether any devices that driver cares about are present.

Sorry, I read your text a bunch of times but I still have no clue what you're trying to tell me.

All *I* am saying is since this is a new subsystem and the methods for detecting the scrub functionality are two - either an ACPI table or a GET_SUPPORTED_FEATURES command, then the init function of the subsystem:

+static int __init memory_scrub_control_init(void) +{
+ return class_register(&scrub_class);
+} +subsys_initcall(memory_scrub_control_init);

can check for those two things before initializing.

If there is no scrubbing functionality, then it can return an error and not load.

The same as when we don't load x86 drivers on the wrong vendor and so on.

If the check is easy, why not do it?

Make more sense?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#16 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 8 lines · Borislav Petkov
Quoted 4 lines · Dan Williams

Recall that there are 461 usages of module_pci_driver() in the kernel. Every one of those arranges for just registering a PCI driver when the module is loaded regardless of whether any devices that driver cares about are present.

Sorry, I read your text a bunch of times but I still have no clue what you're trying to tell me.

Because taking this proposal to its logical end of "if a simple check is possible, why not do it in module_init()" has wide implications like the module_pci_driver() example.

Quoted 4 lines

All *I* am saying is since this is a new subsystem and the methods for detecting the scrub functionality are two - either an ACPI table or a GET_SUPPORTED_FEATURES command, then the init function of the subsystem:

No, at a minimum that's a layering violation. This is a generic library facility that should not care if it is being called for a CXL device or an ACPI device. It also causes functional issues, see below:

Quoted 6 lines
(quoted diff) +5 -0
+static int __init memory_scrub_control_init(void)
+{
+       return class_register(&scrub_class);
+}
+subsys_initcall(memory_scrub_control_init);
Quoted 7 lines

can check for those two things before initializing.

If there is no scrubbing functionality, then it can return an error and not load.

The same as when we don't load x86 drivers on the wrong vendor and so on.

I think it works for x86 drivers because the functionality in those modules is wholly contained within that one module. This scrub module is a service library for other modules.

Quoted 1 line

If the check is easy, why not do it?

It is functionally the wrong place to do the check. When module_init() fails it causes not only the current module to be unloaded but any dependent modules will also fail to load.

Let's take an example of the CXL driver wanting to register with this scrub interface to support the capability that *might* be available on some CXL devices. The cxl_pci.ko module, that houses cxl_pci_driver, grows a call to scrub_device_register(). That scrub_device_register() call is statically present in cxl_pci.ko so that when cxl_pci.ko loads symbol resolution requires scrub.ko to load.

Neither of those modules (cxl_pci.ko or scrub.ko) load automatically. Either udev loads cxl_pci.ko because it sees a device that matches cxl_mem_pci_tbl, or the user manually insmods those modules because they think they know better. No memory wasted unless the user explicitly asks for memory to be wasted.

If no CXL devices in the system have scrub capabilities, great, then scrub_device_register() will never be called.

Now, if memory_scrub_control_init() did its own awkward and redundant CXL scan, and fails with "no CXL scrub capable devices found" it would not only block scrub.ko from loading, but also cxl_pci.ko since cxl_pci.ko needs to resolve that symbol to load.

All of that said, you are right that there is still a scenario where memory is wasted. I.e. the case where a subsystem like CXL or ACPI wants the runtime *option* of calling scrub_device_register(), but never does. That will inflict the cost of registering a vestigial scrub_class. That can be mitigated with another layer of module indirection where cxl_pci_driver registers a cxl_scrub_device and then a cxl_scrub_driver in its own module calls scrub_device_register() with the scrub core.

I would entertain that extra indirection long before I would entertain memory_scrub_control_init() growing scrub device enumeration that belongs to the *caller* of scrub_device_register().

Quoted 1 line

Make more sense?

It is a reasonable question, but all module libraries incur init costs just by being linked by another module. You can walk /sys/class to see how many other subsystems are registering class devices but never using them.

I would not say "no" to a generic facility that patches out module dependencies until the first call, just not sure the return on investment would be worth it.

Lastly I think drivers based on ACPI tables are awkward. They really need to have an ACPI device to attach so that typical automatic Linux module loading machinery can be used. The fact this function is a subsys_initcall() is a red-flag since nothing should be depending on the load order of a little driver to control scrub parameters.

maintainer
#17 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 3 lines · Dan Williams

No, at a minimum that's a layering violation. This is a generic library facility that should not care if it is being called for a CXL device or an ACPI device.

Really?

Because this looks like creating a subsystem for those two newfangled scrubbing functionalities which will be present in CXL devices and by that ACPI RAS2 thing.

Because we have a *lot* of hw scrubbing functionality already. Just do:

git grep "scrub"

Some of it controls hw scrubbing. If this is a generic facility, does this mean that all those older scrubbers should be converted to it?

Or is this thing going to support only new stuff? I.e., we want to disable our scrubbers when doing performance-sensitive workloads and reenable them after that but we don't care about old systems?

And all that other bla about controlling scrubbers from userspace.

So which is it?

Quoted 3 lines

I think it works for x86 drivers because the functionality in those modules is wholly contained within that one module. This scrub module is a service library for other modules.

Well, you have that thing in EDAC. edac_core.ko is that service module and the chipset-specific drivers - at least on x86 - use a match_id to load only on the systems they should load on.

If I had a BIOS table which had "EDAC" in it, I won't load edac_core.ko either but there isn't one.

Quoted 3 lines

It is functionally the wrong place to do the check. When module_init() fails it causes not only the current module to be unloaded but any dependent modules will also fail to load.

See above. I'm under the assumption that this is using two methods to detect scrubbing functionality.

Quoted 7 lines

Let's take an example of the CXL driver wanting to register with this scrub interface to support the capability that *might* be available on some CXL devices. The cxl_pci.ko module, that houses cxl_pci_driver, grows a call to scrub_device_register(). That scrub_device_register() call is statically present in cxl_pci.ko so that when cxl_pci.ko loads symbol resolution requires scrub.ko to load.

Quoted 5 lines

Neither of those modules (cxl_pci.ko or scrub.ko) load automatically. Either udev loads cxl_pci.ko because it sees a device that matches cxl_mem_pci_tbl, or the user manually insmods those modules because they think they know better. No memory wasted unless the user explicitly asks for memory to be wasted.

The scrub.ko goes and asks the system: "Do you have a CXL device with scrubbing functionality?" "Yes: load." "No: ok, won't."

Quoted 7 lines

If no CXL devices in the system have scrub capabilities, great, then scrub_device_register() will never be called.

Now, if memory_scrub_control_init() did its own awkward and redundant CXL scan, and fails with "no CXL scrub capable devices found" it would not only block scrub.ko from loading, but also cxl_pci.ko since cxl_pci.ko needs to resolve that symbol to load.

Why would it fail the scan?

Isn't this fancy GET_SUPPORTED_FEATURES command giving you all info you need?

Quoted 3 lines

I would not say "no" to a generic facility that patches out module dependencies until the first call, just not sure the return on investment would be worth it.

From the discussion so far yeah, I think you're right, it is not worth the effort.

Quoted 5 lines

Lastly I think drivers based on ACPI tables are awkward. They really need to have an ACPI device to attach so that typical automatic Linux module loading machinery can be used. The fact this function is a subsys_initcall() is a red-flag since nothing should be depending on the load order of a little driver to control scrub parameters.

Yeah, it is becoming a mess before it has even started.

So I don't mind if such drivers get loaded as long as doing this is the best we can do given the situation. What gets me up the palms, as they say in .de, is "just because" and "look, the others do it too."

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#18 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 6 lines · Borislav Petkov
Quoted 3 lines · Dan Williams

No, at a minimum that's a layering violation. This is a generic library facility that should not care if it is being called for a CXL device or an ACPI device.

Really?

Yes.

Quoted 11 lines

Because this looks like creating a subsystem for those two newfangled scrubbing functionalities which will be present in CXL devices and by that ACPI RAS2 thing.

Because we have a *lot* of hw scrubbing functionality already. Just do:

git grep "scrub"

Some of it controls hw scrubbing. If this is a generic facility, does this mean that all those older scrubbers should be converted to it?

Quoted 7 lines

Or is this thing going to support only new stuff? I.e., we want to disable our scrubbers when doing performance-sensitive workloads and reenable them after that but we don't care about old systems?

And all that other bla about controlling scrubbers from userspace.

So which is it?

In fact this question matches my reaction to the last posting [1], and led to a much improved cover letter and the "Comparison of scrubbing features". To your point there are scrub capabilities already in the kernel and we would need to make a decision about what to do about them. I called out NVDIMM ARS as one example and am open to exploring converting that to the common scrub ABI, but not block this proposal in the meantime.

For me the proposal can be boiled down to, "here we (kernel community) are again with 2 new scrub interfaces to add to the kernel. Lets step back, define a common ABI for ACPI RAS 2 and CXL to stop the proliferation of scrub ABIs, and then make a decision about when/whether to integrate legacy scrub facilities into this new interface".

[1]: http://lore.kernel.org/r/[email protected]

Quoted 7 lines
Quoted 3 lines

I think it works for x86 drivers because the functionality in those modules is wholly contained within that one module. This scrub module is a service library for other modules.

Well, you have that thing in EDAC. edac_core.ko is that service module and the chipset-specific drivers - at least on x86 - use a match_id to load only on the systems they should load on.

Which is exactly the same mechanism being defined here. scrub_core.ko is a service module that would only be requested by an ACPI module or a CXL module after one of those loads based on their match_id.

Quoted 9 lines

If I had a BIOS table which had "EDAC" in it, I won't load edac_core.ko either but there isn't one.

Quoted 3 lines

It is functionally the wrong place to do the check. When module_init() fails it causes not only the current module to be unloaded but any dependent modules will also fail to load.

See above. I'm under the assumption that this is using two methods to detect scrubbing functionality.

The scrub_core, like edac_core, has no method to detect scrubbing facility, it is simply a passive library waiting for the first scrub_device_register() call.

Quoted 7 lines
Quoted 7 lines

Let's take an example of the CXL driver wanting to register with this scrub interface to support the capability that *might* be available on some CXL devices. The cxl_pci.ko module, that houses cxl_pci_driver, grows a call to scrub_device_register(). That scrub_device_register() call is statically present in cxl_pci.ko so that when cxl_pci.ko loads symbol resolution requires scrub.ko to load.

Quoted 8 lines
Quoted 5 lines

Neither of those modules (cxl_pci.ko or scrub.ko) load automatically. Either udev loads cxl_pci.ko because it sees a device that matches cxl_mem_pci_tbl, or the user manually insmods those modules because they think they know better. No memory wasted unless the user explicitly asks for memory to be wasted.

The scrub.ko goes and asks the system: "Do you have a CXL device with scrubbing functionality?" "Yes: load." "No: ok, won't."

Yeah, that's backwards. CXL enumeration belongs in the CXL driver and the CXL driver is fully responsible for deciding when to incur the costs of loading scrub_core.

Quoted 9 lines
Quoted 7 lines

If no CXL devices in the system have scrub capabilities, great, then scrub_device_register() will never be called.

Now, if memory_scrub_control_init() did its own awkward and redundant CXL scan, and fails with "no CXL scrub capable devices found" it would not only block scrub.ko from loading, but also cxl_pci.ko since cxl_pci.ko needs to resolve that symbol to load.

Why would it fail the scan?

cxl_pci.ko loads based on match_id and cxl_pci_probe() enumerates device capabilities. My interpretation of your feedback is that memory_scrub_control_init() should duplicate that cxl_pci_probe() enumeration?

Assume that it does and memory_scrub_control_init() finds no scrub facilities in any CXL devices and fails memory_scrub_control_init(). Any module that links to scrub_device_register() will also fail to load because module symbol resolution depends on all modules completing init.

Quoted 2 lines

Isn't this fancy GET_SUPPORTED_FEATURES command giving you all info you need?

Sure, but that's a driver-probe-time facility, not a module_init-time facility.

Quoted 7 lines
Quoted 5 lines

Lastly I think drivers based on ACPI tables are awkward. They really need to have an ACPI device to attach so that typical automatic Linux module loading machinery can be used. The fact this function is a subsys_initcall() is a red-flag since nothing should be depending on the load order of a little driver to control scrub parameters.

Yeah, it is becoming a mess before it has even started.

I assume you do not consider edac_core a mess?

Now, the question of how many legacy scrub interfaces should be considered in this design out of the gate is a worthwhile discussion. I am encouraged that this ABI is at least trying to handle more than 1 backend, which makes me feel better that adding a 3rd and 4th might not be prohibitive.

Quoted 3 lines

So I don't mind if such drivers get loaded as long as doing this is the best we can do given the situation. What gets me up the palms, as they say in .de, is "just because" and "look, the others do it too."

Which matches what I reacted to on the last posting:

"Maybe it is self evident to others, but for me there is little in these
changelogs besides 'mechanism exists, enable it'"

...and to me that feedback was taken to heart with much improved changelogs in this new posting.

This init time feature probing discussion feels like it was born from a micommunication / misunderstanding.

maintainer
#19 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 4 lines · Dan Williams

In fact this question matches my reaction to the last posting [1], and led to a much improved cover letter and the "Comparison of scrubbing features". To your point there are scrub capabilities already in the kernel and we would need to make a decision about what to do about them.

The answer to that question is whether this new userspace usage is going to want to control those too.

So

"Use case of scrub control feature"

from the cover letter is giving two short sentences about what one would do but I'm still meh. A whole subsystem needing a bunch of effort would need a lot more justification.

So can anyone please elaborate more on the use cases and why this new thing is needed?

Quoted 9 lines

I called out NVDIMM ARS as one example and am open to exploring converting that to the common scrub ABI, but not block this proposal in the meantime.

For me the proposal can be boiled down to, "here we (kernel community) are again with 2 new scrub interfaces to add to the kernel. Lets step back, define a common ABI for ACPI RAS 2 and CXL to stop the proliferation of scrub ABIs, and then make a decision about when/whether to integrate legacy scrub facilities into this new interface".

Fully agreed as long as there's valid users for it and we don't end up designing and supporting an interface which people are not sure if anyone uses. ras_userspace_consumers() from the other thread case-in-point.

Quoted 1 line

[1]: http://lore.kernel.org/r/[email protected]

^^^^^

Ha, you're speaking what I'm thinking here. :-)

Quoted 3 lines

The scrub_core, like edac_core, has no method to detect scrubbing facility, it is simply a passive library waiting for the first scrub_device_register() call.

Well, those scrub things still have methods which are better than nothing. EDAC is ancient. But ok, let's just say they're the same for the sake of simplicity.

Quoted 3 lines

Yeah, that's backwards. CXL enumeration belongs in the CXL driver and the CXL driver is fully responsible for deciding when to incur the costs of loading scrub_core.

Ok, fair enough.

Quoted 4 lines

Assume that it does and memory_scrub_control_init() finds no scrub facilities in any CXL devices and fails memory_scrub_control_init(). Any module that links to scrub_device_register() will also fail to load because module symbol resolution depends on all modules completing init.

My angle was: scan the system for *all* possible scrub functionalities and if none present, then fail. And since they're only two...

Quoted 2 lines

Sure, but that's a driver-probe-time facility, not a module_init-time facility.

Oh well.

Quoted 1 line

I assume you do not consider edac_core a mess?

The whole EDAC is a mess but that's a whole another story. :-)

Quoted 5 lines

Now, the question of how many legacy scrub interfaces should be considered in this design out of the gate is a worthwhile discussion. I am encouraged that this ABI is at least trying to handle more than 1 backend, which makes me feel better that adding a 3rd and 4th might not be prohibitive.

See above.

I'm perfectly fine with: "hey, we have a new scrub API interfacing to RAS scrub capability and it is *the* thing to use and all other hw scrub functionality should be shoehorned into it.

So this thing's design should at least try to anticipate supporting other scrub hw.

Because there's EDAC too. Why isn't this scrub thing part of EDAC? Why isn't this scrub API part of edac_core? I mean, this is all RAS so why design a whole new thing when the required glue is already there?

We can just as well have a

/sys/devices/system/edac/scrub/

node hierarchy and have everything there.

Why does it have to be yet another thing?

And if it needs to be separate, who's going to maintain it?

Quoted 7 lines

Which matches what I reacted to on the last posting:

"Maybe it is self evident to others, but for me there is little in these
changelogs besides 'mechanism exists, enable it'"

...and to me that feedback was taken to heart with much improved changelogs in this new posting.

Ok.

Quoted 2 lines

This init time feature probing discussion feels like it was born from a micommunication / misunderstanding.

Yes, it seems so, thanks for clarifying things.

I still am unclear on the usecases and how this is supposed to be used and also, as mentioned above, we have a *lot* of RAS functionality spread around the kernel. Perhaps we should start unifying it instead of adding more...

So the big picture and where we're headed to, needs to be clarified first.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#20 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Focusing on just one bit.

Quoted 19 lines
Quoted 5 lines

Now, the question of how many legacy scrub interfaces should be considered in this design out of the gate is a worthwhile discussion. I am encouraged that this ABI is at least trying to handle more than 1 backend, which makes me feel better that adding a 3rd and 4th might not be prohibitive.

See above.

I'm perfectly fine with: "hey, we have a new scrub API interfacing to RAS scrub capability and it is *the* thing to use and all other hw scrub functionality should be shoehorned into it.

So this thing's design should at least try to anticipate supporting other scrub hw.

Because there's EDAC too. Why isn't this scrub thing part of EDAC? Why isn't this scrub API part of edac_core? I mean, this is all RAS so why design a whole new thing when the required glue is already there?

Quoted 5 lines

We can just as well have a

/sys/devices/system/edac/scrub/

node hierarchy and have everything there.

A few questions about this. It seems an unusual use fake devices and a bus so I'm trying to understand how we might do something that looks more standard but perhaps also fit within the existing scheme. I appreciate this stuff has evolved over a long time, so lots of backwards compatibility concerns.

If I follow this right the current situation is:

/sys/devices/system/edac is the 'virtual' device registered on the edac bus.

Quoted 29 lines

Why does it have to be yet another thing?

And if it needs to be separate, who's going to maintain it?

Quoted 7 lines

Which matches what I reacted to on the last posting:

"Maybe it is self evident to others, but for me there is little in these
changelogs besides 'mechanism exists, enable it'"

...and to me that feedback was taken to heart with much improved changelogs in this new posting.

Ok.

Quoted 2 lines

This init time feature probing discussion feels like it was born from a micommunication / misunderstanding.

Yes, it seems so, thanks for clarifying things.

I still am unclear on the usecases and how this is supposed to be used and also, as mentioned above, we have a *lot* of RAS functionality spread around the kernel. Perhaps we should start unifying it instead of adding more...

So the big picture and where we're headed to, needs to be clarified first.

Thx.

#21 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 26 lines · On Fri, 17 May 2024 12:15:54 +0100 Jonathan Cameron…

Focusing on just one bit.

Quoted 24 lines
Quoted 5 lines

Now, the question of how many legacy scrub interfaces should be considered in this design out of the gate is a worthwhile discussion. I am encouraged that this ABI is at least trying to handle more than 1 backend, which makes me feel better that adding a 3rd and 4th might not be prohibitive.

See above.

I'm perfectly fine with: "hey, we have a new scrub API interfacing to RAS scrub capability and it is *the* thing to use and all other hw scrub functionality should be shoehorned into it.

So this thing's design should at least try to anticipate supporting other scrub hw.

Because there's EDAC too. Why isn't this scrub thing part of EDAC? Why isn't this scrub API part of edac_core? I mean, this is all RAS so why design a whole new thing when the required glue is already there?

We can just as well have a

/sys/devices/system/edac/scrub/

node hierarchy and have everything there.

Sorry - finger fumble, wasn't meant to send yet :(

Quoted 9 lines

A few questions about this. It seems an unusual use of fake devices and a bus so I'm trying to understand how we might do something that looks more standard but perhaps also fit within the existing scheme. I appreciate this stuff has evolved over a long time, so lots of backwards compatibility concerns.

If I follow this right the current situation is:

/sys/devices/system/edac is the 'virtual' device registered on the edac bus.

Actually that's wrong it's not on the edac bus as that is the bus registered via subsys_system_register() (which does create a fake device as per the docs telling us not to use it any more - fair enough, legacy).

The mc below it is a bare device - I think just to provide a directory? The comment on the release function seems to say that. This gives.

/sys/devices/system/edac/mc
/sys/bus/edac/devices/mc

Under that we have individual mc0/mc1 etc for the instances of that accessible via
/sys/devices/system/edac/mc/mc0
/sys/bus/edac/device/mc/mc0
Those are registered a children of mc. I'd have expected them to be children of the device that registered them - so for our case, a CXL mc0 node would be child of the CXL device rather than here but again I'm guessing legacy that had to be maintained.

In general this nesting seems unusual, as I'd have expected the registration directly on the edac bus with /sys/bus/edac/device/mc0
/sys/bus/edac/device/pci0

Given we are talking about something new, maybe this is an opportunity to not perpetuate this?

If we add scrub in here I'd prefer to just use the normal bus registration handling rather than creating a nest of additional nodes. So perhaps we could consider
/sys/bus/edac/device/scrub0 (or whatever name makes sense, as per the earlier discussion of cxl_scrub0 or similar).
Could consider moving the bus location of mc0 etc in future to there with symlinks to /sys/bus/edac/device/mc/* for backwards compatibility either via setting their parents or more explicit link creation.

These scrub0 would have their dev->parent set to who ever actually registered them providing that reference cleanly and letting all the normal device model stuff work more simply.

If we did that with the scrub nodes, the only substantial change from a separate subsystem as seen in this patch set would be to register them on the edac bus rather than a separate class.

As you pointed out, there is a simple scrub interface in the existing edac memory controller code. How would you suggest handling that? Have them all register an additional device on the bus (as a child of the mcX devices) perhaps? Seems an easy step forwards and should be no backwards compatibility concerns.

Quoted 3 lines
Quoted 3 lines

Why does it have to be yet another thing?

It absolutely doesn't as long as we can do it fairly cleanly within existing code. I wasn't sure that was possible, but you know edac a lot better than me and so I'll defer to you on that!

Quoted 2 lines
Quoted 2 lines

And if it needs to be separate, who's going to maintain it?

Several options for that, but fair question - bringing (at least some of) the RAS mess together will focus reviewer bandwidth etc better.

Quoted 14 lines
Quoted 14 lines
Quoted 8 lines

Which matches what I reacted to on the last posting:

"Maybe it is self evident to others, but for me there is little in these
changelogs besides 'mechanism exists, enable it'"

...and to me that feedback was taken to heart with much improved changelogs in this new posting.

Ok.

Quoted 2 lines

This init time feature probing discussion feels like it was born from a micommunication / misunderstanding.

Quoted 6 lines
Quoted 6 lines

Yes, it seems so, thanks for clarifying things.

I still am unclear on the usecases and how this is supposed to be used and also, as mentioned above, we have a *lot* of RAS functionality spread around the kernel. Perhaps we should start unifying it instead of adding more...

I'm definitely keen on unifying things as I agree, this mixture of different RAS functionality is a ever worsening mess.

Jonathan

Quoted 6 lines
Quoted 5 lines

So the big picture and where we're headed to, needs to be clarified first.

Thx.

maintainer
#22 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 8 lines · Jonathan Cameron

Given we are talking about something new, maybe this is an opportunity to not perpetuate this?

If we add scrub in here I'd prefer to just use the normal bus registration handling rather than creating a nest of additional nodes. So perhaps we could consider
/sys/bus/edac/device/scrub0 (or whatever name makes sense, as per the earlier discussion of cxl_scrub0 or similar).

Yes, my main worry is how this RAS functionality is going to be all organized in the tree. Yes, EDAC legacy methods can die but the user-visible part can't so we might as well use it to concentrate stuff there.

Quoted 3 lines

Could consider moving the bus location of mc0 etc in future to there with symlinks to /sys/bus/edac/device/mc/* for backwards compatibility either via setting their parents or more explicit link creation.

You can ignore the mc - that's the memory controller representation EDAC does and that's also kind of semi-legacy considering how heterogeneous devices are becoming. Nowadays, scrubbing functionality can be on anything that has memory and that's not only a memory controller.

So it would actually be the better thing to abstract that differently and use .../edac/device/ for the different RAS functionalities. I.e., have the "device" organize it all.

Quoted 3 lines

These scrub0 would have their dev->parent set to who ever actually registered them providing that reference cleanly and letting all the normal device model stuff work more simply.

Ack.

Quoted 9 lines

If we did that with the scrub nodes, the only substantial change from a separate subsystem as seen in this patch set would be to register them on the edac bus rather than a separate class.

As you pointed out, there is a simple scrub interface in the existing edac memory controller code. How would you suggest handling that? Have them all register an additional device on the bus (as a child of the mcX devices) perhaps? Seems an easy step forwards and should be no backwards compatibility concerns.

Well, you guys want to control that scrubbing from userspace and those old things probably do not fit that model? We could just not convert them for now and add them later if really needed. I.e., leave sleeping dogs lie.

Quoted 3 lines

It absolutely doesn't as long as we can do it fairly cleanly within existing code. I wasn't sure that was possible, but you know edac a lot better than me and so I'll defer to you on that!

Meh, I'm simply maintaining it because no one else wants to. :)

Quoted 2 lines

Several options for that, but fair question - bringing (at least some of) the RAS mess together will focus reviewer bandwidth etc better.

Review is more than appreciated, as always.

Quoted 2 lines

I'm definitely keen on unifying things as I agree, this mixture of different RAS functionality is a ever worsening mess.

Yap, it needs to be unified and reigned into something more user-friendly and manageable.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#23 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 10 lines · On Tue, 21 May 2024 10:06:21 +0200 Borislav Petkov <[email protected]> wrote:
Quoted 8 lines · Jonathan Cameron

Given we are talking about something new, maybe this is an opportunity to not perpetuate this?

If we add scrub in here I'd prefer to just use the normal bus registration handling rather than creating a nest of additional nodes. So perhaps we could consider
/sys/bus/edac/device/scrub0 (or whatever name makes sense, as per the earlier discussion of cxl_scrub0 or similar).

Quoted 4 lines

Yes, my main worry is how this RAS functionality is going to be all organized in the tree. Yes, EDAC legacy methods can die but the user-visible part can't so we might as well use it to concentrate stuff there.

Understood.

Quoted 5 lines
Quoted 4 lines

Could consider moving the bus location of mc0 etc in future to there with symlinks to /sys/bus/edac/device/mc/* for backwards compatibility either via setting their parents or more explicit link creation.

Quoted 8 lines

You can ignore the mc - that's the memory controller representation EDAC does and that's also kind of semi-legacy considering how heterogeneous devices are becoming. Nowadays, scrubbing functionality can be on anything that has memory and that's not only a memory controller.

So it would actually be the better thing to abstract that differently and use .../edac/device/ for the different RAS functionalities. I.e., have the "device" organize it all.

I'm not sure I follow this. Definitely worth ensuring we are thinking the same thing wrt to layout before we go further,

Do you mean keep it similar to the existing device/mc device/pci structure so /sys/bus/edac/devices/scrub/cxl_mem0_scrub etc? This would rely on symlinks to paper over the dev->parent not being the normal parent. Hence would be similar to /sys/bus/edac/devices/pci in edac_pci_create_sysfs() or equivalent in edac_device_create_sysfs().

Or is the ../edac/device bit about putting an extra device under edac/devices/? e.g.
/sys/bus/edac/devices/cxl_memX/scrub /sys/bus/edac/devices/cxl_memX/other_ras_thing
which would be fairly standard driver model stuff.

This would sit alongside 'legacy'
/sys/bus/edac/devices/mc/mcX
/sys/bus/edac/devices/pci/pciX etc

I'd prefer this second model as it's very standard and but grouping is per providing parent device, rather than functionality. However, it is rather different from the existing edac structure.

Where I've used the symlink approach in the past, it has always been about keeping a legacy interface in place, not where I'd start
with something new. Hence I think this is a question of how far
we 'breakaway' from existing edac structure.

Quoted 6 lines
Quoted 4 lines

These scrub0 would have their dev->parent set to who ever actually registered them providing that reference cleanly and letting all the normal device model stuff work more simply.

Ack.

This suggests the second option above, but I wanted to confirm as Shiju and I read this differently.

Quoted 11 lines
Quoted 10 lines

If we did that with the scrub nodes, the only substantial change from a separate subsystem as seen in this patch set would be to register them on the edac bus rather than a separate class.

As you pointed out, there is a simple scrub interface in the existing edac memory controller code. How would you suggest handling that? Have them all register an additional device on the bus (as a child of the mcX devices) perhaps? Seems an easy step forwards and should be no backwards compatibility concerns.

Quoted 4 lines

Well, you guys want to control that scrubbing from userspace and those old things probably do not fit that model? We could just not convert them for now and add them later if really needed. I.e., leave sleeping dogs lie.

Ok. There is an existing is the minimal sysfs existing interface but I'm fine with ignoring it for now.

Quoted 6 lines
Quoted 4 lines

It absolutely doesn't as long as we can do it fairly cleanly within existing code. I wasn't sure that was possible, but you know edac a lot better than me and so I'll defer to you on that!

Meh, I'm simply maintaining it because no one else wants to. :)

*much sympathy!* As we ramp up more on this stuff, we'll try and help out where we can.

Quoted 11 lines
Quoted 3 lines

Several options for that, but fair question - bringing (at least some of) the RAS mess together will focus reviewer bandwidth etc better.

Review is more than appreciated, as always.

Quoted 2 lines

I'm definitely keen on unifying things as I agree, this mixture of different RAS functionality is a ever worsening mess.

Yap, it needs to be unified and reigned into something more user-friendly and manageable.

Hopefully we all agree on a unified solution being the target.

Feels like we are converging. Now we are down to the details :)

Thanks,

Jonathan

Quoted 3 lines

Thx.

maintainer
#24 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 4 lines · Jonathan Cameron

Where I've used the symlink approach in the past, it has always been about keeping a legacy interface in place, not where I'd start
with something new. Hence I think this is a question of how far
we 'breakaway' from existing edac structure.

Yes, since we're designing this anew, we can do whatever we prefer. So let's do the standard driver model stuff.

However, there's also /sys/devices/system/edac/ so I don't know what the right thing to do there is. Are we supposed to put everything under /sys/bus/edac now and /sys/devices/system/edac is wrong now and should go away? Maybe we should talk to Greg first ... :)

Quoted 2 lines

This suggests the second option above, but I wanted to confirm as Shiju and I read this differently.

As said, we wanna do the new correct way of how a sysfs interface should look and leave the old one as it is.

Quoted 2 lines

Ok. There is an existing is the minimal sysfs existing interface but I'm fine with ignoring it for now.

Yes, we don't know who's even using it and why. That's why I'm very interested in how this new thing is going to be used so that we know what we're committing to supporting forever.

Quoted 2 lines

*much sympathy!* As we ramp up more on this stuff, we'll try and help out where we can.

Always appreciated! :-)

Quoted 3 lines

Hopefully we all agree on a unified solution being the target.

Feels like we are converging. Now we are down to the details :)

Yap.

Thanks!

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author
#25 lore raw

RE: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

In reply to Borislav Petkov
Quoted 36 lines

-----Original Message-----
From: Borislav Petkov <[email protected]>
Sent: 11 May 2024 11:17
To: Dan Williams <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Shiju Jose <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang <[email protected]>; Linuxarm <[email protected]>; Greg Kroah-Hartman <[email protected]>; Jean Delvare <[email protected]>; Guenter Roeck <[email protected]>; Dmitry Torokhov <[email protected]>
Subject: Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 4 lines · Dan Williams

In fact this question matches my reaction to the last posting [1], and led to a much improved cover letter and the "Comparison of scrubbing features". To your point there are scrub capabilities already in the kernel and we would need to make a decision about what to do about them.

The answer to that question is whether this new userspace usage is going to want to control those too.

So

Quoted 8 lines

"Use case of scrub control feature"

from the cover letter is giving two short sentences about what one would do but I'm still meh. A whole subsystem needing a bunch of effort would need a lot more justification.

So can anyone please elaborate more on the use cases and why this new thing is needed?

Following are some of the use cases of generic scrub control subsystem as given in the cover letter. Request please add any other use cases, which I missed.

1. There are several types of interfaces to HW memory scrubbers identified such as ACPI NVDIMM ARS(Address Range Scrub), CXL memory device patrol scrub, CXL DDR5 ECS, ACPI RAS2 memory scrubbing features and software based memory scrubber(discussed in the community Reference [5] in the cover letter). Also some scrubbers support controlling (background) patrol scrubbing(ACPI RAS2, CXL) and/or on-demand scrubbing(ACPI RAS2, ACPI ARS). However the scrub controls varies between memory scrubbers. Thus there is a need for a standard generic ABI and sysfs scrub controls for the userspace tools, which control HW and SW scrubbers in the system, for the easiness of use.
2. Scrub controls in user space allow the user space tool to disable and enable the feature in case disabling of the background patrol scrubbing and changing the scrub rate are needed for other purposes such as performance-aware operations which requires the background operations to be turned off or reduced.
3. Allows to perform on-demand scrubbing for specific address range if supported by the scrubber.
4. User space tools controls scrub the memory DIMMs regularly at a configurable scrub rate using the sysfs scrub controls discussed help,
- to detect uncorrectable memory errors early before user accessing memory, which helps to recover the detected memory errors.
- reduces the chance of a correctable error becoming uncorrectable.

Regards,
Shiju

#26 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 48 lines · On Mon, 20 May 2024 11:54:50 +0100 Shiju Jose <[email protected]> wrote:
Quoted 36 lines

-----Original Message-----
From: Borislav Petkov <[email protected]>
Sent: 11 May 2024 11:17
To: Dan Williams <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Shiju Jose <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang <[email protected]>; Linuxarm <[email protected]>; Greg Kroah-Hartman <[email protected]>; Jean Delvare <[email protected]>; Guenter Roeck <[email protected]>; Dmitry Torokhov <[email protected]>
Subject: Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 4 lines · Dan Williams

In fact this question matches my reaction to the last posting [1], and led to a much improved cover letter and the "Comparison of scrubbing features". To your point there are scrub capabilities already in the kernel and we would need to make a decision about what to do about them.

The answer to that question is whether this new userspace usage is going to want to control those too.

So

Quoted 8 lines

"Use case of scrub control feature"

from the cover letter is giving two short sentences about what one would do but I'm still meh. A whole subsystem needing a bunch of effort would need a lot more justification.

So can anyone please elaborate more on the use cases and why this new thing is needed?

Following are some of the use cases of generic scrub control subsystem as given in the cover letter. Request please add any other use cases, which I missed.

Quoted 6 lines

1. There are several types of interfaces to HW memory scrubbers identified such as ACPI NVDIMM ARS(Address Range Scrub), CXL memory device patrol scrub, CXL DDR5 ECS, ACPI RAS2 memory scrubbing features and software based memory scrubber(discussed in the community Reference [5] in the cover letter). Also some scrubbers support controlling (background) patrol scrubbing(ACPI RAS2, CXL) and/or on-demand scrubbing(ACPI RAS2, ACPI ARS). However the scrub controls varies between memory scrubbers. Thus there is a need for a standard generic ABI and sysfs scrub controls for the userspace tools, which control HW and SW scrubbers in the system, for the easiness of use.
2. Scrub controls in user space allow the user space tool to disable and enable the feature in case disabling of the background patrol scrubbing and changing the scrub rate are needed for other purposes such as performance-aware operations which requires the background operations to be turned off or reduced.
3. Allows to perform on-demand scrubbing for specific address range if supported by the scrubber.
4. User space tools controls scrub the memory DIMMs regularly at a configurable scrub rate using the sysfs scrub controls discussed help,
- to detect uncorrectable memory errors early before user accessing memory, which helps to recover the detected memory errors.
- reduces the chance of a correctable error becoming uncorrectable.

Just to add one more reason a user space interface is needed.
5. Policy control for hotplugged memory. There is not necessarily a system wide bios or similar in the loop to control the scrub settings on a CXL device that wasn't there at boot. What that setting should be is a policy decision as we are trading of reliability vs performance - hence it should be in control of userspace. As such, 'an' interface is needed. Seems more sensible to try and unify it with other similar interfaces than spin yet another one.

Quoted 4 lines

Regards,
Shiju

maintainer
#27 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 4 lines · Jonathan Cameron
Quoted 4 lines

Following are some of the use cases of generic scrub control subsystem as given in the cover letter. Request please add any other use cases, which I missed.

Quoted 11 lines
Quoted 11 lines

1. There are several types of interfaces to HW memory scrubbers identified such as ACPI NVDIMM ARS(Address Range Scrub), CXL memory device patrol scrub, CXL DDR5 ECS, ACPI RAS2 memory scrubbing features and software based memory scrubber(discussed in the community Reference [5] in the cover letter). Also some scrubbers support controlling (background) patrol scrubbing(ACPI RAS2, CXL) and/or on-demand scrubbing(ACPI RAS2, ACPI ARS). However the scrub controls varies between memory scrubbers. Thus there is a need for a standard generic ABI and sysfs scrub controls for the userspace tools, which control HW and SW scrubbers in the system, for the easiness of use.

This is all talking about what hw functionality there is. I'm more interested in the "there is a need" thing. What need? How?

In order to support something like this upstream, I'd like to know how it is going to be used and whether the major use cases are covered. So that everyone can benefit from it - not only your employer.

Quoted 5 lines
Quoted 5 lines

2. Scrub controls in user space allow the user space tool to disable and enable the feature in case disabling of the background patrol scrubbing and changing the scrub rate are needed for other purposes such as performance-aware operations which requires the background operations to be turned off or reduced.

Who's going to use those scrub controls? Tools? Admins? Scripts?

Quoted 8 lines
Quoted 8 lines

3. Allows to perform on-demand scrubbing for specific address range if supported by the scrubber.
4. User space tools controls scrub the memory DIMMs regularly at a configurable scrub rate using the sysfs scrub controls discussed help, - to detect uncorrectable memory errors early before user accessing memory, which helps to recover the detected memory errors. - reduces the chance of a correctable error becoming uncorrectable.

Yah, that's not my question: my question is, how is this new thing, which is exposed to userspace and which then means, this will be supported forever, how is this thing going to be used?

And the next question is: is that interface sufficient for those use cases?

Are we covering the majority of the usage scenarios?

Quoted 9 lines

Just to add one more reason a user space interface is needed.
5. Policy control for hotplugged memory. There is not necessarily a system wide bios or similar in the loop to control the scrub settings on a CXL device that wasn't there at boot. What that setting should be is a policy decision as we are trading of reliability vs performance - hence it should be in control of userspace.
As such, 'an' interface is needed. Seems more sensible to try and unify it with other similar interfaces than spin yet another one.

Yes, I get that: question is, let's say you have that interface. Now what do you do?

Do you go and start a scrub cycle by hand?

Do you have a script which does that based on some system reports?

Do you automate it? I wanna say yes because that's miles better than having to explain yet another set of knobs to users.

And so on and so on...

I'm trying to get you to imagine the *full* solution and then ask yourselves whether that new interface is adequate.

Makes more sense?

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#28 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 17 lines · On Mon, 27 May 2024 11:21:31 +0200 Borislav Petkov <[email protected]> wrote:
Quoted 4 lines · Jonathan Cameron
Quoted 4 lines

Following are some of the use cases of generic scrub control subsystem as given in the cover letter. Request please add any other use cases, which I missed.

Quoted 11 lines
Quoted 11 lines

1. There are several types of interfaces to HW memory scrubbers identified such as ACPI NVDIMM ARS(Address Range Scrub), CXL memory device patrol scrub, CXL DDR5 ECS, ACPI RAS2 memory scrubbing features and software based memory scrubber(discussed in the community Reference [5] in the cover letter). Also some scrubbers support controlling (background) patrol scrubbing(ACPI RAS2, CXL) and/or on-demand scrubbing(ACPI RAS2, ACPI ARS). However the scrub controls varies between memory scrubbers. Thus there is a need for a standard generic ABI and sysfs scrub controls for the userspace tools, which control HW and SW scrubbers in the system, for the easiness of use.

Quoted 6 lines

This is all talking about what hw functionality there is. I'm more interested in the "there is a need" thing. What need? How?

In order to support something like this upstream, I'd like to know how it is going to be used and whether the major use cases are covered. So that everyone can benefit from it - not only your employer.

Fair questions.

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

2. Scrub controls in user space allow the user space tool to disable and enable the feature in case disabling of the background patrol scrubbing and changing the scrub rate are needed for other purposes such as performance-aware operations which requires the background operations to be turned off or reduced.

Who's going to use those scrub controls? Tools? Admins? Scripts?

If dealing with disabling, I'd be surprised if it was a normal policy but if it were udev script or boot script. If unusual event (i.e. someone is trying to reduce jitter in a benchmark targetting something else) then interface is simple enough that an admin can poke it directly.

Quoted 10 lines
Quoted 9 lines
Quoted 9 lines

3. Allows to perform on-demand scrubbing for specific address range if supported by the scrubber.
4. User space tools controls scrub the memory DIMMs regularly at a configurable scrub rate using the sysfs scrub controls discussed help, - to detect uncorrectable memory errors early before user accessing memory, which helps to recover the detected memory errors. - reduces the chance of a correctable error becoming uncorrectable.

Quoted 8 lines

Yah, that's not my question: my question is, how is this new thing, which is exposed to userspace and which then means, this will be supported forever, how is this thing going to be used?

And the next question is: is that interface sufficient for those use cases?

Are we covering the majority of the usage scenarios?

To a certain extent this is bounded by what the hardware lets us do but agreed we should make sure it 'works' for the usecases we know about. Starting point is some more documentation in the patch set giving common flows (and maybe some example scripts).

Quoted 11 lines
Quoted 10 lines

Just to add one more reason a user space interface is needed.
5. Policy control for hotplugged memory. There is not necessarily a system wide bios or similar in the loop to control the scrub settings on a CXL device that wasn't there at boot. What that setting should be is a policy decision as we are trading of reliability vs performance - hence it should be in control of userspace.
As such, 'an' interface is needed. Seems more sensible to try and unify it with other similar interfaces than spin yet another one.

Quoted 4 lines

Yes, I get that: question is, let's say you have that interface. Now what do you do?

Do you go and start a scrub cycle by hand?

Typically no, but the option would be there to support an admin who is suspicious or who is trying to gather statistics or similar.

Quoted 3 lines

Do you have a script which does that based on some system reports?

That definitely makes sense for NVDIMM scrub as the model there is to only ever do it on a demand as a single scrub pass.
For a cyclic scrub we can spin a policy in rasdaemon or similar to possibly crank up the frequency if we are getting lots of 'non scrub' faults (i.e. correct error reported on demand accesses).

Shiju is our expert on this sort of userspace stats monitoring and handling so I'll leave him to come back with a proposal / PoC for doing that.

I can see two motivations though:
a) Gather better stats on suspect device by ensuring more correctable error detections.
b) Increase scrubbing on a device which is on it's way out but not replacable yet for some reason.

I would suggest this will be PoC level only for now as it will need a lot of testing on large fleets to do anything sophisticated.

Quoted 2 lines

Do you automate it? I wanna say yes because that's miles better than having to explain yet another set of knobs to users.

First instance, I'd expect an UDEV policy so when a new CXL memory turns up we set a default value. A cautious admin would have tweaked that script to set the default to scrub more often, an admin who knows they don't care might turn it off. We can include an example of that in next version I think.

Quoted 8 lines

And so on and so on...

I'm trying to get you to imagine the *full* solution and then ask yourselves whether that new interface is adequate.

Makes more sense?

Absolutely. One area that needs to improve (Dan raised it) is association with HPA ranges so we at can correlate easily error reports with which scrub engine. That can be done with existing version but it's fiddlier than it needs to be. This 'might' be a userspace script example, or maybe making associations tighter in kernel.

Jonathan

Quoted 2 lines

Thx.

maintainer
#29 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 2 lines · Jonathan Cameron

If dealing with disabling, I'd be surprised if it was a normal policy but if it were udev script or boot script. If unusual event (i.e. someone is

Yeah, I wouldn't disable it during boot but around my workload only. You want for automatic scrubs to still happen on the system.

Quoted 2 lines

trying to reduce jitter in a benchmark targetting something else) then interface is simple enough that an admin can poke it directly.

Right, for benchmarks direct poking is fine.

When it is supposed to be something more involved like, dunno, HPC doing a heavy workload and it wants to squeeze all performance so I guess turning off the scrubbers would be part of the setup script. So yeah, if this is properly documented, scripting around it is easy.

Quoted 4 lines

To a certain extent this is bounded by what the hardware lets us do but agreed we should make sure it 'works' for the usecases we know about. Starting point is some more documentation in the patch set giving common flows (and maybe some example scripts).

Yap, sounds good. As in: "These are the envisioned usages at the time of writing... " or so.

Quoted 4 lines
Quoted 1 line

Do you go and start a scrub cycle by hand?

Typically no, but the option would be there to support an admin who is suspicious or who is trying to gather statistics or similar.

Ok.

Quoted 5 lines

That definitely makes sense for NVDIMM scrub as the model there is to only ever do it on a demand as a single scrub pass.
For a cyclic scrub we can spin a policy in rasdaemon or similar to possibly crank up the frequency if we are getting lots of 'non scrub' faults (i.e. correct error reported on demand accesses).

I was going to suggest that: automating stuff with rasdaemon. It would definitely simplify talking to that API.

Quoted 11 lines

Shiju is our expert on this sort of userspace stats monitoring and handling so I'll leave him to come back with a proposal / PoC for doing that.

I can see two motivations though:
a) Gather better stats on suspect device by ensuring more correctable error detections.
b) Increase scrubbing on a device which is on it's way out but not replacable yet for some reason.

I would suggest this will be PoC level only for now as it will need a lot of testing on large fleets to do anything sophisticated.

Yeah, sounds like a good start.

Quoted 8 lines
Quoted 2 lines

Do you automate it? I wanna say yes because that's miles better than having to explain yet another set of knobs to users.

First instance, I'd expect an UDEV policy so when a new CXL memory turns up we set a default value. A cautious admin would have tweaked that script to set the default to scrub more often, an admin who knows they don't care might turn it off. We can include an example of that in next version I think.

Yes, and then hook into rasdaemon the moment it logs an error in some component to go and increase scrubbing of that component. But yeah, you said that above already.

Quoted 5 lines

Absolutely. One area that needs to improve (Dan raised it) is association with HPA ranges so we at can correlate easily error reports with which scrub engine. That can be done with existing version but it's fiddlier than it needs to be. This 'might' be a userspace script example, or maybe making associations tighter in kernel.

Right.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#30 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

In reply to Borislav Petkov
Quoted 2 lines

How hard is that "jump through hoops" thing anyway?

I'd conservatively estimate 500 lines of duplicated code from the CXL subsystem just to handle the setup and discovery of the mailbox, plus all the checks needed to establish the device is in a state to reply. Also locking or module load ordering constraints because we need to ensure mutual exclusion on that mailbox between this module and the CXL core. So it would approximately triple the size of this driver to check for CXL scrub support. Not to mention hotplug - which could possibly be solved with appropriate udev rules to try loading this again whenever a CXL memory device gets plugged in.

Alternative would be to make this ras class driver dependent on the CXL driver stack running first. Thus if you wanted RAS2 ACPI table support, you'd need to load a whole bunch of CXL stuff.

Add another similar driver in future and we get another few 100 lines of code or another dependency. To me those numbers make it unsustainable.

Quoted 4 lines

You mean it should load so that when booting an allmodconfig kernel there are not enough modules which are loading so lemme load one more. And then I need to go and rmmod them all before I need to do localmodconfig and build a tailored kernel for the machine.

Or is there some other reason to load silly modules, use up resources for no good reason whatsoever and bloat the machine?

As Dan, Shiju and I observed (and Shiju tested to be sure we weren't getting it wrong), normal setups including your allmodconfig build would not even load the driver. What are we missing?

Jonathan

#31 lore raw

RE: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

In reply to [email protected]
Quoted 27 lines · shiju.jose@ wrote:

From: Shiju Jose <[email protected]>

Add scrub subsystem supports configuring the memory scrubbers in the system. The scrub subsystem provides the interface for registering the scrub devices. The scrub control attributes are provided to the user in /sys/class/ras/rasX/scrub

---
 .../ABI/testing/sysfs-class-scrub-configure   |  47 +++
 drivers/ras/Kconfig                           |   7 +
 drivers/ras/Makefile                          |   1 +
 drivers/ras/memory_scrub.c                    | 271 ++++++++++++++++++
 include/linux/memory_scrub.h                  |  37 +++
 5 files changed, 363 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-scrub-configure
 create mode 100755 drivers/ras/memory_scrub.c
 create mode 100755 include/linux/memory_scrub.h
Documentation/ABI/testing/sysfs-class-scrub-configure new file +0 -0
new file mode 100644
index 000000000000..3ed77dbb00ad

Documentation/ABI/testing/sysfs-class-scrub-configure line 7

Quoted 7 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +7 -0
@@ -0,0 +1,47 @@
+What:		/sys/class/ras/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The ras/ class subdirectory belongs to the
+		common ras features such as scrub subsystem.

Why create "ras" class versus just a "srcub" class? I am otherwise not aware of a precedent for class device hierarchy. For example, on my system there is:

/sys/class/

├── scsi_device
├── scsi_disk
├── scsi_generic
└── scsi_host

...not:

/sys/class/scsi/

├── device
├── disk
├── generic
└── host

Documentation/ABI/testing/sysfs-class-scrub-configure line 16

Quoted 9 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +9 -0
+
+What:		/sys/class/ras/rasX/scrub/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The /sys/class/ras/ras{0,1,2,3,...}/scrub directories
+		correspond to each scrub device registered with the
+		scrub subsystem.

I notice there are some visibility rules in the code, but those expectations are not documented here.

This documentation would also help developers writing new users of scrub_device_register().

Quoted 101 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +31 -0
+
+What:		/sys/class/ras/rasX/scrub/name
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) name of the memory scrubber
+
+What:		/sys/class/ras/rasX/scrub/enable_background
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) Enable/Disable background(patrol) scrubbing if supported.
+
+What:		/sys/class/ras/rasX/scrub/rate_available
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) Supported range for the scrub rate by the scrubber.
+		The scrub rate represents in hours.
+
+What:		/sys/class/ras/rasX/scrub/rate
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) The scrub rate specified and it must be with in the
+		supported range by the scrubber.
+		The scrub rate represents in hours.
drivers/ras/Kconfig +7 -0
index fc4f4bb94a4c..181701479564 100644
@@ -46,4 +46,11 @@ config RAS_FMPM
 	  Memory will be retired during boot time and run time depending on
 	  platform-specific policies.
 
+config SCRUB
+	tristate "Memory scrub driver"
+	help
+	  This option selects the memory scrub subsystem, supports
+	  configuring the parameters of underlying scrubbers in the
+	  system for the DRAM memories.
+
 endif
drivers/ras/Makefile +1 -0
index 11f95d59d397..89bcf0d84355 100644
@@ -2,6 +2,7 @@
 obj-$(CONFIG_RAS)	+= ras.o
 obj-$(CONFIG_DEBUG_FS)	+= debugfs.o
 obj-$(CONFIG_RAS_CEC)	+= cec.o
+obj-$(CONFIG_SCRUB)	+= memory_scrub.o
 
 obj-$(CONFIG_RAS_FMPM)	+= amd/fmpm.o
 obj-y			+= amd/atl/
drivers/ras/memory_scrub.c new file +36 -0
new file mode 100755
index 000000000000..7e995380ec3a
@@ -0,0 +1,271 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Memory scrub subsystem supports configuring the registered
+ * memory scrubbers.
+ *
+ * Copyright (c) 2024 HiSilicon Limited.
+ */
+
+#define pr_fmt(fmt)     "MEM SCRUB: " fmt
+
+#include <linux/acpi.h>
+#include <linux/bitops.h>
+#include <linux/delay.h>
+#include <linux/kfifo.h>
+#include <linux/memory_scrub.h>
+#include <linux/platform_device.h>
+#include <linux/spinlock.h>
+
+/* memory scrubber config definitions */
+#define SCRUB_ID_PREFIX "ras"
+#define SCRUB_ID_FORMAT SCRUB_ID_PREFIX "%d"
+
+static DEFINE_IDA(scrub_ida);
+
+struct scrub_device {
+	int id;
+	struct device dev;
+	const struct scrub_ops *ops;
+};
+
+#define to_scrub_device(d) container_of(d, struct scrub_device, dev)
+static ssize_t enable_background_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf, size_t len)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);

drivers/ras/memory_scrub.c line 46

Quoted 10 lines
drivers/ras/memory_scrub.c new file +10 -0
+	bool enable;
+	int ret;
+
+	ret = kstrtobool(buf, &enable);
+	if (ret < 0)
+		return ret;
+
+	ret = scrub_dev->ops->set_enabled_bg(dev, enable);
+	if (ret)
+		return ret;

It strikes me as somewhat pointless to have such a thin sysfs implementation whose only job is to call down into a callback to do the work. Unless there are other consumers of 'struct scrub_ops' outside of these sysfs files why not just have the low-level drivers register their corresponding attributes themselves?

Unless the functionality is truly generic just let the low-level driver be responsible for conforming to the sysfs ABI expectations, and, for example, each register their own "enable_background" attribute if they support that semantic.

So scrub_device_register() would grow a 'const struct attribute_group **groups' argument, or something along those lines.

#32 lore raw

Re: [RFC PATCH v8 01/10] ras: scrub: Add scrub subsystem

Quoted 22 lines · On Thu, 9 May 2024 14:47:56 -0700 Dan Williams <[email protected]> wrote:
Quoted 21 lines · shiju.jose@ wrote:

From: Shiju Jose <[email protected]>

Add scrub subsystem supports configuring the memory scrubbers in the system. The scrub subsystem provides the interface for registering the scrub devices. The scrub control attributes are provided to the user in /sys/class/ras/rasX/scrub

---
 .../ABI/testing/sysfs-class-scrub-configure   |  47 +++
 drivers/ras/Kconfig                           |   7 +
 drivers/ras/Makefile                          |   1 +
 drivers/ras/memory_scrub.c                    | 271 ++++++++++++++++++
 include/linux/memory_scrub.h                  |  37 +++
 5 files changed, 363 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-scrub-configure
 create mode 100755 drivers/ras/memory_scrub.c
 create mode 100755 include/linux/memory_scrub.h
Quoted 13 lines

Documentation/ABI/testing/sysfs-class-scrub-configure line 7

Quoted 13 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +7 -0
new file mode 100644
index 000000000000..3ed77dbb00ad
@@ -0,0 +1,47 @@
+What:		/sys/class/ras/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The ras/ class subdirectory belongs to the
+		common ras features such as scrub subsystem. 

Hi Dan,

Quoted 4 lines

Why create "ras" class versus just a "srcub" class? I am otherwise not aware of a precedent for class device hierarchy. For example, on my system there is:

I think that's miss described - aim is on subsystem, the first feature supported is scrub. Intent here is to group RAS features of a given device / interface etc into one place. This was a request in an review of an earlier version on basis these interfaces tend to get grouped together in a device.
So options are

/sys/class/ras/cxl_mem0/scrub/rate etc. /sys/class/ras/cxl_mem0/ecs/rate etc
(maybe separate for ECS because it annoyingly looks nothing like scrub despite name
and there are multiple impelmentations)

vs
/sys/class/ras/cxl_mem0_scrub
/sys/class/ras/cxl_mem0_ecs
etc
Note that generic naming not including what the source was got negative reviews in favor of making that the device instance name here. So that rulled out simply
/sys/class/ras/scrubX/
/sys/class/ras/ecsX/

I don't mind which way we go; both are extensible.

Quoted 7 lines

/sys/class/

├── scsi_device
├── scsi_disk
├── scsi_generic
└── scsi_host
Quoted 7 lines

...not:

/sys/class/scsi/

├── device
├── disk
├── generic
└── host

That's a docs problem - this was never the intent.

Quoted 15 lines

Documentation/ABI/testing/sysfs-class-scrub-configure line 16

Quoted 11 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +9 -0
+
+What:		/sys/class/ras/rasX/scrub/
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		The /sys/class/ras/ras{0,1,2,3,...}/scrub directories
+		correspond to each scrub device registered with the
+		scrub subsystem.  

I notice there are some visibility rules in the code, but those expectations are not documented here.

Quoted 2 lines

This documentation would also help developers writing new users of scrub_device_register().

Agreed. One to improve.

Quoted 119 lines
Quoted 102 lines
Documentation/ABI/testing/sysfs-class-scrub-configure new file +31 -0
+
+What:		/sys/class/ras/rasX/scrub/name
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) name of the memory scrubber
+
+What:		/sys/class/ras/rasX/scrub/enable_background
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) Enable/Disable background(patrol) scrubbing if supported.
+
+What:		/sys/class/ras/rasX/scrub/rate_available
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) Supported range for the scrub rate by the scrubber.
+		The scrub rate represents in hours.
+
+What:		/sys/class/ras/rasX/scrub/rate
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) The scrub rate specified and it must be with in the
+		supported range by the scrubber.
+		The scrub rate represents in hours.
drivers/ras/Kconfig +7 -0
index fc4f4bb94a4c..181701479564 100644
@@ -46,4 +46,11 @@ config RAS_FMPM
 	  Memory will be retired during boot time and run time depending on
 	  platform-specific policies.
 
+config SCRUB
+	tristate "Memory scrub driver"
+	help
+	  This option selects the memory scrub subsystem, supports
+	  configuring the parameters of underlying scrubbers in the
+	  system for the DRAM memories.
+
 endif
drivers/ras/Makefile +1 -0
index 11f95d59d397..89bcf0d84355 100644
@@ -2,6 +2,7 @@
 obj-$(CONFIG_RAS)	+= ras.o
 obj-$(CONFIG_DEBUG_FS)	+= debugfs.o
 obj-$(CONFIG_RAS_CEC)	+= cec.o
+obj-$(CONFIG_SCRUB)	+= memory_scrub.o
 
 obj-$(CONFIG_RAS_FMPM)	+= amd/fmpm.o
 obj-y			+= amd/atl/
drivers/ras/memory_scrub.c new file +36 -0
new file mode 100755
index 000000000000..7e995380ec3a
@@ -0,0 +1,271 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Memory scrub subsystem supports configuring the registered
+ * memory scrubbers.
+ *
+ * Copyright (c) 2024 HiSilicon Limited.
+ */
+
+#define pr_fmt(fmt)     "MEM SCRUB: " fmt
+
+#include <linux/acpi.h>
+#include <linux/bitops.h>
+#include <linux/delay.h>
+#include <linux/kfifo.h>
+#include <linux/memory_scrub.h>
+#include <linux/platform_device.h>
+#include <linux/spinlock.h>
+
+/* memory scrubber config definitions */
+#define SCRUB_ID_PREFIX "ras"
+#define SCRUB_ID_FORMAT SCRUB_ID_PREFIX "%d"
+
+static DEFINE_IDA(scrub_ida);
+
+struct scrub_device {
+	int id;
+	struct device dev;
+	const struct scrub_ops *ops;
+};
+
+#define to_scrub_device(d) container_of(d, struct scrub_device, dev)
+static ssize_t enable_background_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf, size_t len)
+{
+	struct scrub_device *scrub_dev = to_scrub_device(dev);

drivers/ras/memory_scrub.c line 46

Quoted 10 lines
drivers/ras/memory_scrub.c new file +10 -0
+	bool enable;
+	int ret;
+
+	ret = kstrtobool(buf, &enable);
+	if (ret < 0)
+		return ret;
+
+	ret = scrub_dev->ops->set_enabled_bg(dev, enable);
+	if (ret)
+		return ret;  

It strikes me as somewhat pointless to have such a thin sysfs implementation whose only job is to call down into a callback to do the work. Unless there are other consumers of 'struct scrub_ops' outside of these sysfs files why not just have the low-level drivers register their corresponding attributes themselves?

Quoted 4 lines

Unless the functionality is truly generic just let the low-level driver be responsible for conforming to the sysfs ABI expectations, and, for example, each register their own "enable_background" attribute if they support that semantic.

This was me pushing for this based on that approach having been a pain in subystems I've been involved with in the past. so I'll answer.

Maybe if we think the number of scrub drivers remains very low we can rely on ABI review. However, it's painful. Everyone wants to add their own custom ABI, so every review consists of 'no that is isn't consistent' reviews. The callback schemes reduce that considerably. As someone with their name next to one of the largest sysfs ABIs in the kernel, maybe I'm projecting my pain points on this one.

Note that this approach has failed for multiple similar simple subsystems in the past and they have migrated to a (mostly) tighter description for ABI simply because those constraints are useful. A fairly recent one maybe 8 years ago? Was hwmon. There are other advantages that may not yet apply here (in kernel interfaces are much easier, even if they are only occasionally used for a given subsystem), but my motivation in pushing Shiju this way was to lock down the userspace interface.

Quoted 3 lines

So scrub_device_register() would grow a 'const struct attribute_group **groups' argument, or something along those lines.

Sure. Shiju had that in an earlier version. Personally I think it's an approach that may bite in the long run, but meh, maybe this will only ever have half a dozen drivers so it might remain manageable. If not, I love say 'I told you so' :)

Jonathan

#33 lore raw

RFC PATCH v8 2/10 cxl/mbox: Add GET_SUPPORTED_FEATURES mailbox command

In reply to [email protected]

From: Shiju Jose <[email protected]>

Add support for GET_SUPPORTED_FEATURES mailbox command.

CXL spec 3.1 section 8.2.9.6 describes optional device specific features. CXL devices supports features with changeable attributes.
Get Supported Features retrieves the list of supported device specific features. The settings of a feature can be retrieved using Get Feature and optionally modified using Set Feature.

---
 drivers/cxl/core/mbox.c | 27 ++++++++++++++++++
 drivers/cxl/cxlmem.h    | 61 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+)
drivers/cxl/core/mbox.c +27 -0
index f0f54aeccc87..82e279b821e2 100644
@@ -1291,6 +1291,33 @@ int cxl_set_timestamp(struct cxl_memdev_state *mds)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_set_timestamp, CXL);
 
+int cxl_get_supported_features(struct cxl_memdev_state *mds,
+			       u32 count, u16 start_index,
+			       struct cxl_mbox_get_supp_feats_out *feats_out)
+{
+	struct cxl_mbox_get_supp_feats_in pi;
+	struct cxl_mbox_cmd mbox_cmd;
+	int rc;
+
+	pi.count = cpu_to_le32(count);
+	pi.start_index = cpu_to_le16(start_index);
+
+	mbox_cmd = (struct cxl_mbox_cmd) {
+		.opcode = CXL_MBOX_OP_GET_SUPPORTED_FEATURES,
+		.size_in = sizeof(pi),
+		.payload_in = &pi,
+		.size_out = count,
+		.payload_out = feats_out,
+		.min_out = sizeof(*feats_out),
+	};
+	rc = cxl_internal_send_cmd(mds, &mbox_cmd);
+	if (rc < 0)
+		return rc;
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_get_supported_features, CXL);
+
 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 		       struct cxl_region *cxlr)
 {
drivers/cxl/cxlmem.h +61 -0

79 lines not on this page · raw message

Signature · 1 line
2.34.1
#34 lore raw

RFC PATCH v8 3/10 cxl/mbox: Add GET_FEATURE mailbox command

In reply to [email protected]

From: Shiju Jose <[email protected]>

Add support for GET_FEATURE mailbox command.

CXL spec 3.1 section 8.2.9.6 describes optional device specific features. The settings of a feature can be retrieved using Get Feature command.

---
 drivers/cxl/core/mbox.c | 53 +++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxlmem.h    | 28 ++++++++++++++++++++++
 2 files changed, 81 insertions(+)
drivers/cxl/core/mbox.c +53 -0
index 82e279b821e2..999965871048 100644
@@ -1318,6 +1318,59 @@ int cxl_get_supported_features(struct cxl_memdev_state *mds,
 }
 EXPORT_SYMBOL_NS_GPL(cxl_get_supported_features, CXL);
 
+size_t cxl_get_feature(struct cxl_memdev_state *mds,
+		       const uuid_t feat_uuid, void *feat_out,
+		       size_t feat_out_size,
+		       size_t feat_out_min_size,
+		       enum cxl_get_feat_selection selection)
+{
+	struct cxl_dev_state *cxlds = &mds->cxlds;
+	struct cxl_mbox_get_feat_in pi;
+	struct cxl_mbox_cmd mbox_cmd;
+	size_t data_rcvd_size = 0;
+	size_t data_to_rd_size, size_out;
+	int rc;
+
+	if (feat_out_size < feat_out_min_size) {
+		dev_err(cxlds->dev,
+			"%s: feature out buffer size(%lu) is not big enough\n",
+			__func__, feat_out_size);
+		return 0;
+	}
+
+	if (feat_out_size <= mds->payload_size)
+		size_out = feat_out_size;
+	else
+		size_out = mds->payload_size;
+	pi.uuid = feat_uuid;
+	pi.selection = selection;
+	do {
+		if ((feat_out_min_size - data_rcvd_size) <= mds->payload_size)
+			data_to_rd_size = feat_out_min_size - data_rcvd_size;
+		else
+			data_to_rd_size = mds->payload_size;
+
+		pi.offset = cpu_to_le16(data_rcvd_size);
+		pi.count = cpu_to_le16(data_to_rd_size);
+
+		mbox_cmd = (struct cxl_mbox_cmd) {
+			.opcode = CXL_MBOX_OP_GET_FEATURE,
+			.size_in = sizeof(pi),
+			.payload_in = &pi,
+			.size_out = size_out,
+			.payload_out = feat_out + data_rcvd_size,
+			.min_out = data_to_rd_size,
+		};
+		rc = cxl_internal_send_cmd(mds, &mbox_cmd);
+		if (rc < 0 || mbox_cmd.size_out == 0)
+			return 0;
+		data_rcvd_size += mbox_cmd.size_out;
+	} while (data_rcvd_size < feat_out_min_size);
+
+	return data_rcvd_size;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_get_feature, CXL);
+
 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 		       struct cxl_region *cxlr)
 {
drivers/cxl/cxlmem.h +28 -0
index 06231e63373e..c822eb30e6d1 100644
@@ -528,6 +528,7 @@ enum cxl_opcode {
 	CXL_MBOX_OP_GET_SUPPORTED_LOGS	= 0x0400,
 	CXL_MBOX_OP_GET_LOG		= 0x0401,
 	CXL_MBOX_OP_GET_SUPPORTED_FEATURES	= 0x0500,
+	CXL_MBOX_OP_GET_FEATURE		= 0x0501,
 	CXL_MBOX_OP_IDENTIFY		= 0x4000,
 	CXL_MBOX_OP_GET_PARTITION_INFO	= 0x4100,
 	CXL_MBOX_OP_SET_PARTITION_INFO	= 0x4101,
@@ -754,6 +755,28 @@ struct cxl_mbox_get_supp_feats_out {
 	struct cxl_mbox_supp_feat_entry feat_entries[];
 } __packed;
 
+/*
+ * Get Feature CXL 3.1 Spec 8.2.9.6.2
+ */
+
+/*
+ * Get Feature input payload
+ * CXL rev 3.1 section 8.2.9.6.2 Table 8-99
+ */
+enum cxl_get_feat_selection {
+	CXL_GET_FEAT_SEL_CURRENT_VALUE,
+	CXL_GET_FEAT_SEL_DEFAULT_VALUE,
+	CXL_GET_FEAT_SEL_SAVED_VALUE,
+	CXL_GET_FEAT_SEL_MAX
+};
+
+struct cxl_mbox_get_feat_in {
+	uuid_t uuid;
+	__le16 offset;
+	__le16 count;
+	u8 selection;
+}  __packed;
+
 /* Get Poison List  CXL 3.0 Spec 8.2.9.8.4.1 */
 struct cxl_mbox_poison_in {
 	__le64 offset;
@@ -888,6 +911,11 @@ int cxl_set_timestamp(struct cxl_memdev_state *mds);
 int cxl_get_supported_features(struct cxl_memdev_state *mds,
 			       u32 count, u16 start_index,
 			       struct cxl_mbox_get_supp_feats_out *feats_out);
+size_t cxl_get_feature(struct cxl_memdev_state *mds,
+		       const uuid_t feat_uuid, void *feat_out,
+		       size_t feat_out_size,
+		       size_t feat_out_min_size,
+		       enum cxl_get_feat_selection selection);
 int cxl_poison_state_init(struct cxl_memdev_state *mds);
 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 		       struct cxl_region *cxlr);
Signature · 1 line
2.34.1
#35 lore raw

Re: [RFC PATCH v8 03/10] cxl/mbox: Add GET_FEATURE mailbox command

Quoted 35 lines · On Sat, Apr 20, 2024 at 12:47:12AM +0800, [email protected] wrote:

From: Shiju Jose <[email protected]>

Add support for GET_FEATURE mailbox command.

CXL spec 3.1 section 8.2.9.6 describes optional device specific features. The settings of a feature can be retrieved using Get Feature command.

---
 drivers/cxl/core/mbox.c | 53 +++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxlmem.h    | 28 ++++++++++++++++++++++
 2 files changed, 81 insertions(+)
drivers/cxl/core/mbox.c +14 -0
index 82e279b821e2..999965871048 100644
@@ -1318,6 +1318,59 @@ int cxl_get_supported_features(struct cxl_memdev_state *mds,
 }
 EXPORT_SYMBOL_NS_GPL(cxl_get_supported_features, CXL);
 
+size_t cxl_get_feature(struct cxl_memdev_state *mds,
+		       const uuid_t feat_uuid, void *feat_out,
+		       size_t feat_out_size,
+		       size_t feat_out_min_size,
+		       enum cxl_get_feat_selection selection)
+{
+	struct cxl_dev_state *cxlds = &mds->cxlds;
+	struct cxl_mbox_get_feat_in pi;
+	struct cxl_mbox_cmd mbox_cmd;
+	size_t data_rcvd_size = 0;
+	size_t data_to_rd_size, size_out;
+	int rc;
+
+	if (feat_out_size < feat_out_min_size) {

drivers/cxl/core/mbox.c int cxl_get_supported_features(struct cxl_memdev_state *mds, · line 1344

Quoted 10 lines
drivers/cxl/core/mbox.c +10 -0
+		dev_err(cxlds->dev,
+			"%s: feature out buffer size(%lu) is not big enough\n",
+			__func__, feat_out_size);
+		return 0;
+	}
+
+	if (feat_out_size <= mds->payload_size)
+		size_out = feat_out_size;
+	else
+		size_out = mds->payload_size;

Using min() instead?

    size_out = min(feat_out_size, mds->payload_size)

drivers/cxl/core/mbox.c int cxl_get_supported_features(struct cxl_memdev_state *mds, · line 1351

Quoted 7 lines
drivers/cxl/core/mbox.c +7 -0
+	pi.uuid = feat_uuid;
+	pi.selection = selection;
+	do {
+		if ((feat_out_min_size - data_rcvd_size) <= mds->payload_size)
+			data_to_rd_size = feat_out_min_size - data_rcvd_size;
+		else
+			data_to_rd_size = mds->payload_size;

data_to_rd_size = min(feat_out_min_size - data_rcvd_size, mds->payload_size);

It seems feat_out_min_size is always the same as feat_out_size in this series, what is it for? For the loop here, my understanding is we need to fill up the out buffer multiple times if the feature cannot be held in a call, so it seems feat_out_min_size should be feat_out_size here.

Fan

Quoted 81 lines
drivers/cxl/core/mbox.c +22 -0
+
+		pi.offset = cpu_to_le16(data_rcvd_size);
+		pi.count = cpu_to_le16(data_to_rd_size);
+
+		mbox_cmd = (struct cxl_mbox_cmd) {
+			.opcode = CXL_MBOX_OP_GET_FEATURE,
+			.size_in = sizeof(pi),
+			.payload_in = &pi,
+			.size_out = size_out,
+			.payload_out = feat_out + data_rcvd_size,
+			.min_out = data_to_rd_size,
+		};
+		rc = cxl_internal_send_cmd(mds, &mbox_cmd);
+		if (rc < 0 || mbox_cmd.size_out == 0)
+			return 0;
+		data_rcvd_size += mbox_cmd.size_out;
+	} while (data_rcvd_size < feat_out_min_size);
+
+	return data_rcvd_size;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_get_feature, CXL);
+
 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 		       struct cxl_region *cxlr)
 {
drivers/cxl/cxlmem.h +28 -0
index 06231e63373e..c822eb30e6d1 100644
@@ -528,6 +528,7 @@ enum cxl_opcode {
 	CXL_MBOX_OP_GET_SUPPORTED_LOGS	= 0x0400,
 	CXL_MBOX_OP_GET_LOG		= 0x0401,
 	CXL_MBOX_OP_GET_SUPPORTED_FEATURES	= 0x0500,
+	CXL_MBOX_OP_GET_FEATURE		= 0x0501,
 	CXL_MBOX_OP_IDENTIFY		= 0x4000,
 	CXL_MBOX_OP_GET_PARTITION_INFO	= 0x4100,
 	CXL_MBOX_OP_SET_PARTITION_INFO	= 0x4101,
@@ -754,6 +755,28 @@ struct cxl_mbox_get_supp_feats_out {
 	struct cxl_mbox_supp_feat_entry feat_entries[];
 } __packed;
 
+/*
+ * Get Feature CXL 3.1 Spec 8.2.9.6.2
+ */
+
+/*
+ * Get Feature input payload
+ * CXL rev 3.1 section 8.2.9.6.2 Table 8-99
+ */
+enum cxl_get_feat_selection {
+	CXL_GET_FEAT_SEL_CURRENT_VALUE,
+	CXL_GET_FEAT_SEL_DEFAULT_VALUE,
+	CXL_GET_FEAT_SEL_SAVED_VALUE,
+	CXL_GET_FEAT_SEL_MAX
+};
+
+struct cxl_mbox_get_feat_in {
+	uuid_t uuid;
+	__le16 offset;
+	__le16 count;
+	u8 selection;
+}  __packed;
+
 /* Get Poison List  CXL 3.0 Spec 8.2.9.8.4.1 */
 struct cxl_mbox_poison_in {
 	__le64 offset;
@@ -888,6 +911,11 @@ int cxl_set_timestamp(struct cxl_memdev_state *mds);
 int cxl_get_supported_features(struct cxl_memdev_state *mds,
 			       u32 count, u16 start_index,
 			       struct cxl_mbox_get_supp_feats_out *feats_out);
+size_t cxl_get_feature(struct cxl_memdev_state *mds,
+		       const uuid_t feat_uuid, void *feat_out,
+		       size_t feat_out_size,
+		       size_t feat_out_min_size,
+		       enum cxl_get_feat_selection selection);
 int cxl_poison_state_init(struct cxl_memdev_state *mds);
 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 		       struct cxl_region *cxlr);
Signature · 1 line
2.34.1
author
#36 lore raw

RE: [RFC PATCH v8 03/10] cxl/mbox: Add GET_FEATURE mailbox command

Quoted 65 lines

-----Original Message-----
From: fan <[email protected]>
Sent: 25 April 2024 00:19
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang <[email protected]>; Linuxarm <[email protected]>
Subject: Re: [RFC PATCH v8 03/10] cxl/mbox: Add GET_FEATURE mailbox command

Quoted 10 lines · On Sat, Apr 20, 2024 at 12:47:12AM +0800, [email protected] wrote:

From: Shiju Jose <[email protected]>

Add support for GET_FEATURE mailbox command.

CXL spec 3.1 section 8.2.9.6 describes optional device specific features. The settings of a feature can be retrieved using Get Feature command.

---
 drivers/cxl/core/mbox.c | 53

+++++++++++++++++++++++++++++++++++++++++

Quoted 29 lines
 drivers/cxl/cxlmem.h    | 28 ++++++++++++++++++++++
 2 files changed, 81 insertions(+)
drivers/cxl/core/mbox.c index renamed from drivers/cxl/core/mbox.c +0 -0

82e279b821e2..999965871048 100644

drivers/cxl/core/mbox.c +18 -0
@@ -1318,6 +1318,59 @@ int cxl_get_supported_features(struct
 cxl_memdev_state *mds,  }
 EXPORT_SYMBOL_NS_GPL(cxl_get_supported_features, CXL);
 
+size_t cxl_get_feature(struct cxl_memdev_state *mds,
+		       const uuid_t feat_uuid, void *feat_out,
+		       size_t feat_out_size,
+		       size_t feat_out_min_size,
+		       enum cxl_get_feat_selection selection) {
+	struct cxl_dev_state *cxlds = &mds->cxlds;
+	struct cxl_mbox_get_feat_in pi;
+	struct cxl_mbox_cmd mbox_cmd;
+	size_t data_rcvd_size = 0;
+	size_t data_to_rd_size, size_out;
+	int rc;
+
+	if (feat_out_size < feat_out_min_size) {
+		dev_err(cxlds->dev,
+			"%s: feature out buffer size(%lu) is not big enough\n",
+			__func__, feat_out_size);
+		return 0;
+	}
Quoted 8 lines

drivers/cxl/core/mbox.c int cxl_get_supported_features(struct cxl_memdev_state *mds, · line 1344

Quoted 5 lines
drivers/cxl/core/mbox.c +5 -0
+
+	if (feat_out_size <= mds->payload_size)
+		size_out = feat_out_size;
+	else
+		size_out = mds->payload_size;

Using min() instead?
size_out = min(feat_out_size, mds->payload_size)

Will do.

Quoted 10 lines

drivers/cxl/core/mbox.c int cxl_get_supported_features(struct cxl_memdev_state *mds, · line 1351

Quoted 8 lines
drivers/cxl/core/mbox.c +7 -0
+	pi.uuid = feat_uuid;
+	pi.selection = selection;
+	do {
+		if ((feat_out_min_size - data_rcvd_size) <= mds->payload_size)
+			data_to_rd_size = feat_out_min_size - data_rcvd_size;
+		else
+			data_to_rd_size = mds->payload_size;

data_to_rd_size = min(feat_out_min_size - data_rcvd_size, mds->payload_size);

Will do.

Quoted 5 lines

It seems feat_out_min_size is always the same as feat_out_size in this series, what is it for? For the loop here, my understanding is we need to fill up the out buffer multiple times if the feature cannot be held in a call, so it seems feat_out_min_size should be feat_out_size here.

feat_out_size and feat_out_min_size added separately because this function is a common interface
and it might be useful for the features like DDR5 ECS Control, where the Get feature output payload
size is relatively high and actually required data is small, contains DDR5 ECS control feature readable attributes for N number of memory media FRUs.

Quoted 73 lines

Fan

Quoted 70 lines
drivers/cxl/core/mbox.c +22 -0
+
+		pi.offset = cpu_to_le16(data_rcvd_size);
+		pi.count = cpu_to_le16(data_to_rd_size);
+
+		mbox_cmd = (struct cxl_mbox_cmd) {
+			.opcode = CXL_MBOX_OP_GET_FEATURE,
+			.size_in = sizeof(pi),
+			.payload_in = &pi,
+			.size_out = size_out,
+			.payload_out = feat_out + data_rcvd_size,
+			.min_out = data_to_rd_size,
+		};
+		rc = cxl_internal_send_cmd(mds, &mbox_cmd);
+		if (rc < 0 || mbox_cmd.size_out == 0)
+			return 0;
+		data_rcvd_size += mbox_cmd.size_out;
+	} while (data_rcvd_size < feat_out_min_size);
+
+	return data_rcvd_size;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_get_feature, CXL);
+
 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 		       struct cxl_region *cxlr)
 {
drivers/cxl/cxlmem.h index renamed from drivers/cxl/cxlmem.h +0 -0

06231e63373e..c822eb30e6d1 100644

drivers/cxl/cxlmem.h +24 -0
@@ -528,6 +528,7 @@ enum cxl_opcode {
 	CXL_MBOX_OP_GET_SUPPORTED_LOGS	= 0x0400,
 	CXL_MBOX_OP_GET_LOG		= 0x0401,
 	CXL_MBOX_OP_GET_SUPPORTED_FEATURES	= 0x0500,
+	CXL_MBOX_OP_GET_FEATURE		= 0x0501,
 	CXL_MBOX_OP_IDENTIFY		= 0x4000,
 	CXL_MBOX_OP_GET_PARTITION_INFO	= 0x4100,
 	CXL_MBOX_OP_SET_PARTITION_INFO	= 0x4101,
@@ -754,6 +755,28 @@ struct cxl_mbox_get_supp_feats_out {
 	struct cxl_mbox_supp_feat_entry feat_entries[];  } __packed;
 
+/*
+ * Get Feature CXL 3.1 Spec 8.2.9.6.2  */
+
+/*
+ * Get Feature input payload
+ * CXL rev 3.1 section 8.2.9.6.2 Table 8-99  */ enum
+cxl_get_feat_selection {
+	CXL_GET_FEAT_SEL_CURRENT_VALUE,
+	CXL_GET_FEAT_SEL_DEFAULT_VALUE,
+	CXL_GET_FEAT_SEL_SAVED_VALUE,
+	CXL_GET_FEAT_SEL_MAX
+};
+
+struct cxl_mbox_get_feat_in {
+	uuid_t uuid;
+	__le16 offset;
+	__le16 count;
+	u8 selection;
+}  __packed;
+
 /* Get Poison List  CXL 3.0 Spec 8.2.9.8.4.1 */  struct
 cxl_mbox_poison_in {
 	__le64 offset;
@@ -888,6 +911,11 @@ int cxl_set_timestamp(struct cxl_memdev_state
 *mds);  int cxl_get_supported_features(struct cxl_memdev_state *mds,
 			       u32 count, u16 start_index,
 			       struct cxl_mbox_get_supp_feats_out *feats_out);
+size_t cxl_get_feature(struct cxl_memdev_state *mds,
+		       const uuid_t feat_uuid, void *feat_out,
+		       size_t feat_out_size,
Quoted 8 lines
Quoted 8 lines
drivers/cxl/cxlmem.h +2 -1
+		       size_t feat_out_min_size,
+		       enum cxl_get_feat_selection selection);
 int cxl_poison_state_init(struct cxl_memdev_state *mds);  int
 cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 		       struct cxl_region *cxlr);
--

2.34.1

Thanks,
Shiju

#37 lore raw

RFC PATCH v8 4/10 cxl/mbox: Add SET_FEATURE mailbox command

In reply to [email protected]

From: Shiju Jose <[email protected]>

Add support for SET_FEATURE mailbox command.

CXL spec 3.1 section 8.2.9.6 describes optional device specific features. CXL devices supports features with changeable attributes.
The settings of a feature can be optionally modified using Set Feature command.

---
 drivers/cxl/core/mbox.c | 73 +++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxlmem.h    | 33 +++++++++++++++++++
 2 files changed, 106 insertions(+)
drivers/cxl/core/mbox.c +73 -0
index 999965871048..4ca1238e8fec 100644
@@ -1371,6 +1371,79 @@ size_t cxl_get_feature(struct cxl_memdev_state *mds,
 }
 EXPORT_SYMBOL_NS_GPL(cxl_get_feature, CXL);
 
+/*
+ * FEAT_DATA_MIN_PAYLOAD_SIZE - min extra number of bytes should be
+ * available in the mailbox for storing the actual feature data so that
+ * the feature data transfer would work as expected.
+ */
+#define FEAT_DATA_MIN_PAYLOAD_SIZE 10
+int cxl_set_feature(struct cxl_memdev_state *mds,
+		    const uuid_t feat_uuid, u8 feat_version,
+		    void *feat_data, size_t feat_data_size,
+		    u8 feat_flag)
+{
+	struct cxl_memdev_set_feat_pi {
+		struct cxl_mbox_set_feat_hdr hdr;
+		u8 feat_data[];
+	}  __packed;
+	size_t data_in_size, data_sent_size = 0;
+	struct cxl_mbox_cmd mbox_cmd;
+	size_t hdr_size;
+	int rc = 0;
+
+	struct cxl_memdev_set_feat_pi *pi __free(kfree) =
+					kmalloc(mds->payload_size, GFP_KERNEL);
+	pi->hdr.uuid = feat_uuid;
+	pi->hdr.version = feat_version;
+	feat_flag &= ~CXL_SET_FEAT_FLAG_DATA_TRANSFER_MASK;
+	hdr_size = sizeof(pi->hdr);
+	/*
+	 * Check minimum mbox payload size is available for
+	 * the feature data transfer.
+	 */
+	if (hdr_size + FEAT_DATA_MIN_PAYLOAD_SIZE > mds->payload_size)
+		return -ENOMEM;
+
+	if ((hdr_size + feat_data_size) <= mds->payload_size) {
+		pi->hdr.flags = cpu_to_le32(feat_flag |
+				       CXL_SET_FEAT_FLAG_FULL_DATA_TRANSFER);
+		data_in_size = feat_data_size;
+	} else {
+		pi->hdr.flags = cpu_to_le32(feat_flag |
+				       CXL_SET_FEAT_FLAG_INITIATE_DATA_TRANSFER);
+		data_in_size = mds->payload_size - hdr_size;
+	}
+
+	do {
+		pi->hdr.offset = cpu_to_le16(data_sent_size);
+		memcpy(pi->feat_data, feat_data + data_sent_size, data_in_size);
+		mbox_cmd = (struct cxl_mbox_cmd) {
+			.opcode = CXL_MBOX_OP_SET_FEATURE,
+			.size_in = hdr_size + data_in_size,
+			.payload_in = pi,
+		};
+		rc = cxl_internal_send_cmd(mds, &mbox_cmd);
+		if (rc < 0)
+			return rc;
+
+		data_sent_size += data_in_size;
+		if (data_sent_size >= feat_data_size)
+			return 0;
+
+		if ((feat_data_size - data_sent_size) <= (mds->payload_size - hdr_size)) {
+			data_in_size = feat_data_size - data_sent_size;
+			pi->hdr.flags = cpu_to_le32(feat_flag |
+					       CXL_SET_FEAT_FLAG_FINISH_DATA_TRANSFER);
+		} else {
+			pi->hdr.flags = cpu_to_le32(feat_flag |
+					       CXL_SET_FEAT_FLAG_CONTINUE_DATA_TRANSFER);
+		}
+	} while (true);
+
+	return rc;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_set_feature, CXL);
+
 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 		       struct cxl_region *cxlr)
 {
drivers/cxl/cxlmem.h +33 -0
index c822eb30e6d1..1c50a3e2eced 100644
@@ -529,6 +529,7 @@ enum cxl_opcode {
 	CXL_MBOX_OP_GET_LOG		= 0x0401,
 	CXL_MBOX_OP_GET_SUPPORTED_FEATURES	= 0x0500,
 	CXL_MBOX_OP_GET_FEATURE		= 0x0501,
+	CXL_MBOX_OP_SET_FEATURE		= 0x0502,
 	CXL_MBOX_OP_IDENTIFY		= 0x4000,
 	CXL_MBOX_OP_GET_PARTITION_INFO	= 0x4100,
 	CXL_MBOX_OP_SET_PARTITION_INFO	= 0x4101,
@@ -777,6 +778,34 @@ struct cxl_mbox_get_feat_in {
 	u8 selection;
 }  __packed;
 
+/*
+ * Set Feature CXL 3.1 Spec 8.2.9.6.3
+ */
+
+/*
+ * Set Feature input payload
+ * CXL rev 3.1 section 8.2.9.6.3 Table 8-101
+ */
+/* Set Feature : Payload in flags */
+#define CXL_SET_FEAT_FLAG_DATA_TRANSFER_MASK	GENMASK(2, 0)
+enum cxl_set_feat_flag_data_transfer {
+	CXL_SET_FEAT_FLAG_FULL_DATA_TRANSFER,
+	CXL_SET_FEAT_FLAG_INITIATE_DATA_TRANSFER,
+	CXL_SET_FEAT_FLAG_CONTINUE_DATA_TRANSFER,
+	CXL_SET_FEAT_FLAG_FINISH_DATA_TRANSFER,
+	CXL_SET_FEAT_FLAG_ABORT_DATA_TRANSFER,
+	CXL_SET_FEAT_FLAG_DATA_TRANSFER_MAX
+};
+#define CXL_SET_FEAT_FLAG_DATA_SAVED_ACROSS_RESET	BIT(3)
+
+struct cxl_mbox_set_feat_hdr {
+	uuid_t uuid;
+	__le32 flags;
+	__le16 offset;
+	u8 version;
+	u8 rsvd[9];
+}  __packed;
+
 /* Get Poison List  CXL 3.0 Spec 8.2.9.8.4.1 */
 struct cxl_mbox_poison_in {
 	__le64 offset;
@@ -916,6 +945,10 @@ size_t cxl_get_feature(struct cxl_memdev_state *mds,
 		       size_t feat_out_size,
 		       size_t feat_out_min_size,
 		       enum cxl_get_feat_selection selection);
+int cxl_set_feature(struct cxl_memdev_state *mds,
+		    const uuid_t feat_uuid, u8 feat_version,
+		    void *feat_data, size_t feat_data_size,
+		    u8 feat_flag);
 int cxl_poison_state_init(struct cxl_memdev_state *mds);
 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 		       struct cxl_region *cxlr);
Signature · 1 line
2.34.1
#38 lore raw

Re: [RFC PATCH v8 04/10] cxl/mbox: Add SET_FEATURE mailbox command

Quoted 83 lines · On Sat, Apr 20, 2024 at 12:47:13AM +0800, [email protected] wrote:

From: Shiju Jose <[email protected]>

Add support for SET_FEATURE mailbox command.

CXL spec 3.1 section 8.2.9.6 describes optional device specific features. CXL devices supports features with changeable attributes.
The settings of a feature can be optionally modified using Set Feature command.

---
 drivers/cxl/core/mbox.c | 73 +++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxlmem.h    | 33 +++++++++++++++++++
 2 files changed, 106 insertions(+)
drivers/cxl/core/mbox.c +60 -0
index 999965871048..4ca1238e8fec 100644
@@ -1371,6 +1371,79 @@ size_t cxl_get_feature(struct cxl_memdev_state *mds,
 }
 EXPORT_SYMBOL_NS_GPL(cxl_get_feature, CXL);
 
+/*
+ * FEAT_DATA_MIN_PAYLOAD_SIZE - min extra number of bytes should be
+ * available in the mailbox for storing the actual feature data so that
+ * the feature data transfer would work as expected.
+ */
+#define FEAT_DATA_MIN_PAYLOAD_SIZE 10
+int cxl_set_feature(struct cxl_memdev_state *mds,
+		    const uuid_t feat_uuid, u8 feat_version,
+		    void *feat_data, size_t feat_data_size,
+		    u8 feat_flag)
+{
+	struct cxl_memdev_set_feat_pi {
+		struct cxl_mbox_set_feat_hdr hdr;
+		u8 feat_data[];
+	}  __packed;
+	size_t data_in_size, data_sent_size = 0;
+	struct cxl_mbox_cmd mbox_cmd;
+	size_t hdr_size;
+	int rc = 0;
+
+	struct cxl_memdev_set_feat_pi *pi __free(kfree) =
+					kmalloc(mds->payload_size, GFP_KERNEL);
+	pi->hdr.uuid = feat_uuid;
+	pi->hdr.version = feat_version;
+	feat_flag &= ~CXL_SET_FEAT_FLAG_DATA_TRANSFER_MASK;
+	hdr_size = sizeof(pi->hdr);
+	/*
+	 * Check minimum mbox payload size is available for
+	 * the feature data transfer.
+	 */
+	if (hdr_size + FEAT_DATA_MIN_PAYLOAD_SIZE > mds->payload_size)
+		return -ENOMEM;
+
+	if ((hdr_size + feat_data_size) <= mds->payload_size) {
+		pi->hdr.flags = cpu_to_le32(feat_flag |
+				       CXL_SET_FEAT_FLAG_FULL_DATA_TRANSFER);
+		data_in_size = feat_data_size;
+	} else {
+		pi->hdr.flags = cpu_to_le32(feat_flag |
+				       CXL_SET_FEAT_FLAG_INITIATE_DATA_TRANSFER);
+		data_in_size = mds->payload_size - hdr_size;
+	}
+
+	do {
+		pi->hdr.offset = cpu_to_le16(data_sent_size);
+		memcpy(pi->feat_data, feat_data + data_sent_size, data_in_size);
+		mbox_cmd = (struct cxl_mbox_cmd) {
+			.opcode = CXL_MBOX_OP_SET_FEATURE,
+			.size_in = hdr_size + data_in_size,
+			.payload_in = pi,
+		};
+		rc = cxl_internal_send_cmd(mds, &mbox_cmd);
+		if (rc < 0)
+			return rc;
+
+		data_sent_size += data_in_size;
+		if (data_sent_size >= feat_data_size)
+			return 0;
+
+		if ((feat_data_size - data_sent_size) <= (mds->payload_size - hdr_size)) {

drivers/cxl/core/mbox.c size_t cxl_get_feature(struct cxl_memdev_state *mds, · line 1443

Quoted 10 lines
drivers/cxl/core/mbox.c +10 -0
+			data_in_size = feat_data_size - data_sent_size;
+			pi->hdr.flags = cpu_to_le32(feat_flag |
+					       CXL_SET_FEAT_FLAG_FINISH_DATA_TRANSFER);
+		} else {
+			pi->hdr.flags = cpu_to_le32(feat_flag |
+					       CXL_SET_FEAT_FLAG_CONTINUE_DATA_TRANSFER);
+		}
+	} while (true);
+
+	return rc;

Dead code.

Fan

Quoted 67 lines
drivers/cxl/core/mbox.c +3 -0
+}
+EXPORT_SYMBOL_NS_GPL(cxl_set_feature, CXL);
+
 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 		       struct cxl_region *cxlr)
 {
drivers/cxl/cxlmem.h +33 -0
index c822eb30e6d1..1c50a3e2eced 100644
@@ -529,6 +529,7 @@ enum cxl_opcode {
 	CXL_MBOX_OP_GET_LOG		= 0x0401,
 	CXL_MBOX_OP_GET_SUPPORTED_FEATURES	= 0x0500,
 	CXL_MBOX_OP_GET_FEATURE		= 0x0501,
+	CXL_MBOX_OP_SET_FEATURE		= 0x0502,
 	CXL_MBOX_OP_IDENTIFY		= 0x4000,
 	CXL_MBOX_OP_GET_PARTITION_INFO	= 0x4100,
 	CXL_MBOX_OP_SET_PARTITION_INFO	= 0x4101,
@@ -777,6 +778,34 @@ struct cxl_mbox_get_feat_in {
 	u8 selection;
 }  __packed;
 
+/*
+ * Set Feature CXL 3.1 Spec 8.2.9.6.3
+ */
+
+/*
+ * Set Feature input payload
+ * CXL rev 3.1 section 8.2.9.6.3 Table 8-101
+ */
+/* Set Feature : Payload in flags */
+#define CXL_SET_FEAT_FLAG_DATA_TRANSFER_MASK	GENMASK(2, 0)
+enum cxl_set_feat_flag_data_transfer {
+	CXL_SET_FEAT_FLAG_FULL_DATA_TRANSFER,
+	CXL_SET_FEAT_FLAG_INITIATE_DATA_TRANSFER,
+	CXL_SET_FEAT_FLAG_CONTINUE_DATA_TRANSFER,
+	CXL_SET_FEAT_FLAG_FINISH_DATA_TRANSFER,
+	CXL_SET_FEAT_FLAG_ABORT_DATA_TRANSFER,
+	CXL_SET_FEAT_FLAG_DATA_TRANSFER_MAX
+};
+#define CXL_SET_FEAT_FLAG_DATA_SAVED_ACROSS_RESET	BIT(3)
+
+struct cxl_mbox_set_feat_hdr {
+	uuid_t uuid;
+	__le32 flags;
+	__le16 offset;
+	u8 version;
+	u8 rsvd[9];
+}  __packed;
+
 /* Get Poison List  CXL 3.0 Spec 8.2.9.8.4.1 */
 struct cxl_mbox_poison_in {
 	__le64 offset;
@@ -916,6 +945,10 @@ size_t cxl_get_feature(struct cxl_memdev_state *mds,
 		       size_t feat_out_size,
 		       size_t feat_out_min_size,
 		       enum cxl_get_feat_selection selection);
+int cxl_set_feature(struct cxl_memdev_state *mds,
+		    const uuid_t feat_uuid, u8 feat_version,
+		    void *feat_data, size_t feat_data_size,
+		    u8 feat_flag);
 int cxl_poison_state_init(struct cxl_memdev_state *mds);
 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 		       struct cxl_region *cxlr);
Signature · 1 line
2.34.1
#39 lore raw

RFC PATCH v8 5/10 cxl/memscrub: Add CXL device patrol scrub control feature

In reply to [email protected]

From: Shiju Jose <[email protected]>

CXL spec 3.1 section 8.2.9.9.11.1 describes the device patrol scrub control feature. The device patrol scrub proactively locates and makes corrections to errors in regular cycle.

Allow specifying the number of hours within which the patrol scrub must be completed, subject to minimum and maximum limits reported by the device. Also allow disabling scrub allowing trade-off error rates against performance.

Register with scrub subsystem to provide scrub control attributes to the user.

---
 Documentation/scrub/scrub-configure.rst |  52 ++++
 drivers/cxl/Kconfig                     |  19 ++
 drivers/cxl/core/Makefile               |   1 +
 drivers/cxl/core/memscrub.c             | 314 ++++++++++++++++++++++++
 drivers/cxl/cxlmem.h                    |   8 +
 drivers/cxl/mem.c                       |   6 +
 6 files changed, 400 insertions(+)
 create mode 100644 Documentation/scrub/scrub-configure.rst
 create mode 100644 drivers/cxl/core/memscrub.c
Documentation/scrub/scrub-configure.rst new file +52 -0
new file mode 100644
index 000000000000..2275366b60d3
@@ -0,0 +1,52 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+================
+Scrub subsystem
+================
+
+Copyright (c) 2024 HiSilicon Limited.
+
+:Author:   Shiju Jose <[email protected]>
+:License:  The GNU Free Documentation License, Version 1.2
+          (dual licensed under the GPL v2)
+:Original Reviewers:
+
+- Written for: 6.9
+- Updated for:
+
+Introduction
+------------
+The scrub subsystem provides interface for controlling attributes
+of memory scrubbers in the system. The scrub device drivers
+in the system register with the scrub subsystem.The scrub subsystem
+driver exposes the scrub controls to the user in the sysfs.
+
+The File System
+---------------
+
+The control attributes of the registered scrubbers could be
+accessed in the /sys/class/ras/rasX/scrub/
+
+sysfs
+-----
+
+Sysfs files are documented in
+`Documentation/ABI/testing/sysfs-class-scrub-configure`.
+
+Example
+-------
+
+The usage takes the form shown in this example::
+
+1. CXL patrol scrubber
+    # cat /sys/class/ras/ras0/scrub/rate_available
+    # 0x1-0xff
+    # echo 30 > /sys/class/ras/ras0/scrub/rate
+    # cat /sys/class/ras/ras0/scrub/rate
+    # 0x1e
+    # echo 1 > /sys/class/ras/ras0/scrub/enable_background
+    # cat /sys/class/ras/ras0/scrub/enable_background
+    # 1
+    # echo 0 > /sys/class/ras/ras0/scrub/enable_background
+    # cat /sys/class/ras/ras0/scrub/enable_background
+    # 0
drivers/cxl/Kconfig +19 -0
index 5f3c9c5529b9..3621b9f27e80 100644
@@ -144,4 +144,23 @@ config CXL_REGION_INVALIDATION_TEST
 	  If unsure, or if this kernel is meant for production environments,
 	  say N.
 
+config CXL_SCRUB
+	bool "CXL: Memory scrub feature"
+	depends on CXL_PCI
+	depends on CXL_MEM
+	depends on SCRUB
+	help
+	  The CXL memory scrub control is an optional feature allows host to
+	  control the scrub configurations of CXL Type 3 devices, which
+	  supports patrol scrubbing.
+
+	  Registers with the scrub subsystem to provide control attributes
+	  of CXL memory device scrubber to the user.
+	  Provides interface functions to support configuring the CXL memory
+	  device patrol scrubber.
+
+	  Say 'y/n' to enable/disable control of memory scrub parameters for
+	  CXL.mem devices. See section 8.2.9.9.11.1 of CXL 3.1 specification
+	  for detailed description of CXL memory patrol scrub control feature.
+
 endif
drivers/cxl/core/Makefile +1 -0
index 9259bcc6773c..e0fc814c3983 100644
@@ -16,3 +16,4 @@ cxl_core-y += pmu.o
 cxl_core-y += cdat.o
 cxl_core-$(CONFIG_TRACING) += trace.o
 cxl_core-$(CONFIG_CXL_REGION) += region.o
+cxl_core-$(CONFIG_CXL_SCRUB) += memscrub.o
drivers/cxl/core/memscrub.c new file +314 -0

314 lines not on this page · raw message

drivers/cxl/cxlmem.h +8 -0
index 1c50a3e2eced..f95e39febd73 100644
@@ -956,6 +956,14 @@ int cxl_trigger_poison_list(struct cxl_memdev *cxlmd);
 int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa);
 int cxl_clear_poison(struct cxl_memdev *cxlmd, u64 dpa);
 
+/* cxl memory scrub functions */
+#ifdef CONFIG_CXL_SCRUB
+int cxl_mem_patrol_scrub_init(struct cxl_memdev *cxlmd);
+#else
+static inline int cxl_mem_patrol_scrub_init(struct cxl_memdev *cxlmd)
+{ return 0; }
+#endif
+
 #ifdef CONFIG_CXL_SUSPEND
 void cxl_mem_active_inc(void);
 void cxl_mem_active_dec(void);
drivers/cxl/mem.c +6 -0
index 0c79d9ce877c..399e43463626 100644
@@ -117,6 +117,12 @@ static int cxl_mem_probe(struct device *dev)
 	if (!cxlds->media_ready)
 		return -EBUSY;
 
+	rc = cxl_mem_patrol_scrub_init(cxlmd);
+	if (rc) {
+		dev_dbg(&cxlmd->dev, "CXL patrol scrub init failed\n");
+		return rc;
+	}
+
 	/*
 	 * Someone is trying to reattach this device after it lost its port
 	 * connection (an endpoint port previously registered by this memdev was
Signature · 1 line
2.34.1
#40 lore raw

Re: [RFC PATCH v8 05/10] cxl/memscrub: Add CXL device patrol scrub control feature

Quoted 233 lines · On Sat, Apr 20, 2024 at 12:47:14AM +0800, [email protected] wrote:

From: Shiju Jose <[email protected]>

CXL spec 3.1 section 8.2.9.9.11.1 describes the device patrol scrub control feature. The device patrol scrub proactively locates and makes corrections to errors in regular cycle.

Allow specifying the number of hours within which the patrol scrub must be completed, subject to minimum and maximum limits reported by the device. Also allow disabling scrub allowing trade-off error rates against performance.

Register with scrub subsystem to provide scrub control attributes to the user.

---
 Documentation/scrub/scrub-configure.rst |  52 ++++
 drivers/cxl/Kconfig                     |  19 ++
 drivers/cxl/core/Makefile               |   1 +
 drivers/cxl/core/memscrub.c             | 314 ++++++++++++++++++++++++
 drivers/cxl/cxlmem.h                    |   8 +
 drivers/cxl/mem.c                       |   6 +
 6 files changed, 400 insertions(+)
 create mode 100644 Documentation/scrub/scrub-configure.rst
 create mode 100644 drivers/cxl/core/memscrub.c
Documentation/scrub/scrub-configure.rst new file +52 -0
new file mode 100644
index 000000000000..2275366b60d3
@@ -0,0 +1,52 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+================
+Scrub subsystem
+================
+
+Copyright (c) 2024 HiSilicon Limited.
+
+:Author:   Shiju Jose <[email protected]>
+:License:  The GNU Free Documentation License, Version 1.2
+          (dual licensed under the GPL v2)
+:Original Reviewers:
+
+- Written for: 6.9
+- Updated for:
+
+Introduction
+------------
+The scrub subsystem provides interface for controlling attributes
+of memory scrubbers in the system. The scrub device drivers
+in the system register with the scrub subsystem.The scrub subsystem
+driver exposes the scrub controls to the user in the sysfs.
+
+The File System
+---------------
+
+The control attributes of the registered scrubbers could be
+accessed in the /sys/class/ras/rasX/scrub/
+
+sysfs
+-----
+
+Sysfs files are documented in
+`Documentation/ABI/testing/sysfs-class-scrub-configure`.
+
+Example
+-------
+
+The usage takes the form shown in this example::
+
+1. CXL patrol scrubber
+    # cat /sys/class/ras/ras0/scrub/rate_available
+    # 0x1-0xff
+    # echo 30 > /sys/class/ras/ras0/scrub/rate
+    # cat /sys/class/ras/ras0/scrub/rate
+    # 0x1e
+    # echo 1 > /sys/class/ras/ras0/scrub/enable_background
+    # cat /sys/class/ras/ras0/scrub/enable_background
+    # 1
+    # echo 0 > /sys/class/ras/ras0/scrub/enable_background
+    # cat /sys/class/ras/ras0/scrub/enable_background
+    # 0
drivers/cxl/Kconfig +19 -0
index 5f3c9c5529b9..3621b9f27e80 100644
@@ -144,4 +144,23 @@ config CXL_REGION_INVALIDATION_TEST
 	  If unsure, or if this kernel is meant for production environments,
 	  say N.
 
+config CXL_SCRUB
+	bool "CXL: Memory scrub feature"
+	depends on CXL_PCI
+	depends on CXL_MEM
+	depends on SCRUB
+	help
+	  The CXL memory scrub control is an optional feature allows host to
+	  control the scrub configurations of CXL Type 3 devices, which
+	  supports patrol scrubbing.
+
+	  Registers with the scrub subsystem to provide control attributes
+	  of CXL memory device scrubber to the user.
+	  Provides interface functions to support configuring the CXL memory
+	  device patrol scrubber.
+
+	  Say 'y/n' to enable/disable control of memory scrub parameters for
+	  CXL.mem devices. See section 8.2.9.9.11.1 of CXL 3.1 specification
+	  for detailed description of CXL memory patrol scrub control feature.
+
 endif
drivers/cxl/core/Makefile +1 -0
index 9259bcc6773c..e0fc814c3983 100644
@@ -16,3 +16,4 @@ cxl_core-y += pmu.o
 cxl_core-y += cdat.o
 cxl_core-$(CONFIG_TRACING) += trace.o
 cxl_core-$(CONFIG_CXL_REGION) += region.o
+cxl_core-$(CONFIG_CXL_SCRUB) += memscrub.o
drivers/cxl/core/memscrub.c new file +104 -0
new file mode 100644
index 000000000000..a50f6e384394
@@ -0,0 +1,314 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * CXL memory scrub driver.
+ *
+ * Copyright (c) 2024 HiSilicon Limited.
+ *
+ *  - Provides functions to configure patrol scrub feature of the
+ *    CXL memory devices.
+ *  - Registers with the scrub subsystem driver to expose the sysfs attributes
+ *    to the user for configuring the CXL memory patrol scrub feature.
+ */
+
+#define pr_fmt(fmt)	"CXL_MEM_SCRUB: " fmt
+
+#include <cxlmem.h>
+#include <linux/cleanup.h>
+#include <linux/limits.h>
+#include <linux/memory_scrub.h>
+
+static int cxl_mem_get_supported_feature_entry(struct cxl_memdev *cxlmd, const uuid_t *feat_uuid,
+					       struct cxl_mbox_supp_feat_entry *feat_entry_out)
+{
+	struct cxl_mbox_supp_feat_entry *feat_entry;
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
+	int feat_index, feats_out_size;
+	int nentries, count;
+	int ret;
+
+	feat_index = 0;
+	feats_out_size = sizeof(struct cxl_mbox_get_supp_feats_out) +
+			  sizeof(struct cxl_mbox_supp_feat_entry);
+	struct cxl_mbox_get_supp_feats_out *feats_out __free(kfree) =
+					kmalloc(feats_out_size, GFP_KERNEL);
+	if (!feats_out)
+		return -ENOMEM;
+
+	while (true) {
+		memset(feats_out, 0, feats_out_size);
+		ret = cxl_get_supported_features(mds, feats_out_size,
+						 feat_index, feats_out);
+		if (ret)
+			return ret;
+
+		nentries = feats_out->nr_entries;
+		if (!nentries)
+			return -EOPNOTSUPP;
+
+		/* Check CXL memdev supports the feature */
+		feat_entry = feats_out->feat_entries;
+		for (count = 0; count < nentries; count++, feat_entry++) {
+			if (uuid_equal(&feat_entry->uuid, feat_uuid)) {
+				memcpy(feat_entry_out, feat_entry,
+				       sizeof(*feat_entry_out));
+				return 0;
+			}
+		}
+		feat_index += nentries;
+	}
+}
+
+/* CXL memory patrol scrub control definitions */
+#define CXL_MEMDEV_PS_GET_FEAT_VERSION	0x01
+#define CXL_MEMDEV_PS_SET_FEAT_VERSION	0x01
+
+static const uuid_t cxl_patrol_scrub_uuid =
+	UUID_INIT(0x96dad7d6, 0xfde8, 0x482b, 0xa7, 0x33, 0x75, 0x77, 0x4e,     \
+		  0x06, 0xdb, 0x8a);
+
+/* CXL memory patrol scrub control functions */
+struct cxl_patrol_scrub_context {
+	struct device *dev;
+	u16 get_feat_size;
+	u16 set_feat_size;
+	bool scrub_cycle_changeable;
+};
+
+/**
+ * struct cxl_memdev_ps_params - CXL memory patrol scrub parameter data structure.
+ * @enable:     [IN & OUT] enable(1)/disable(0) patrol scrub.
+ * @scrub_cycle_changeable: [OUT] scrub cycle attribute of patrol scrub is changeable.
+ * @rate:       [IN] Requested patrol scrub cycle in hours.
+ *              [OUT] Current patrol scrub cycle in hours.
+ * @min_rate:[OUT] minimum patrol scrub cycle, in hours, supported.
+ */
+struct cxl_memdev_ps_params {
+	bool enable;
+	bool scrub_cycle_changeable;
+	u16 rate;
+	u16 min_rate;
+};
+
+enum cxl_scrub_param {
+	cxl_ps_param_enable,
+	cxl_ps_param_rate,
+};
+
+#define	CXL_MEMDEV_PS_SCRUB_CYCLE_CHANGE_CAP_MASK	BIT(0)
+#define	CXL_MEMDEV_PS_SCRUB_CYCLE_REALTIME_REPORT_CAP_MASK	BIT(1)
+#define	CXL_MEMDEV_PS_CUR_SCRUB_CYCLE_MASK	GENMASK(7, 0)
+#define	CXL_MEMDEV_PS_MIN_SCRUB_CYCLE_MASK	GENMASK(15, 8)
+#define	CXL_MEMDEV_PS_FLAG_ENABLED_MASK	BIT(0)
+
+struct cxl_memdev_ps_rd_attrs {

drivers/cxl/core/memscrub.c line 114

Quoted 10 lines
drivers/cxl/core/memscrub.c new file +10 -0
+	u8 scrub_cycle_cap;
+	__le16 scrub_cycle;
+	u8 scrub_flags;
+}  __packed;
+
+struct cxl_memdev_ps_wr_attrs {
+	u8 scrub_cycle_hr;
+	u8 scrub_flags;
+}  __packed;
+

In this patch, generally "rate" is used for cycle in hour, here we use scrub_cycle_hr. I am not sure whether "rate" is the proper term for the purpose, "interval" or "cycle" seems more straightforward for me. But someone else may have a different thought about it.

Quoted 224 lines
drivers/cxl/core/memscrub.c new file +200 -0
+static int cxl_mem_ps_get_attrs(struct device *dev,
+				struct cxl_memdev_ps_params *params)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
+	size_t rd_data_size = sizeof(struct cxl_memdev_ps_rd_attrs);
+	size_t data_size;
+
+	if (!mds)
+		return -EFAULT;
+
+	struct cxl_memdev_ps_rd_attrs *rd_attrs __free(kfree) =
+						kmalloc(rd_data_size, GFP_KERNEL);
+	if (!rd_attrs)
+		return -ENOMEM;
+
+	data_size = cxl_get_feature(mds, cxl_patrol_scrub_uuid, rd_attrs,
+				    rd_data_size, rd_data_size,
+				    CXL_GET_FEAT_SEL_CURRENT_VALUE);
+	if (!data_size)
+		return -EIO;
+
+	params->scrub_cycle_changeable = FIELD_GET(CXL_MEMDEV_PS_SCRUB_CYCLE_CHANGE_CAP_MASK,
+						   rd_attrs->scrub_cycle_cap);
+	params->enable = FIELD_GET(CXL_MEMDEV_PS_FLAG_ENABLED_MASK,
+				   rd_attrs->scrub_flags);
+	params->rate = FIELD_GET(CXL_MEMDEV_PS_CUR_SCRUB_CYCLE_MASK,
+				 rd_attrs->scrub_cycle);
+	params->min_rate = FIELD_GET(CXL_MEMDEV_PS_MIN_SCRUB_CYCLE_MASK,
+				      rd_attrs->scrub_cycle);
+
+	return 0;
+}
+
+static int cxl_mem_ps_set_attrs(struct device *dev, struct cxl_memdev_ps_params *params,
+				enum cxl_scrub_param param_type)
+{
+	struct cxl_memdev_ps_wr_attrs wr_attrs;
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
+	struct cxl_memdev_ps_params rd_params;
+	int ret;
+
+	ret = cxl_mem_ps_get_attrs(dev, &rd_params);
+	if (ret) {
+		dev_err(dev, "Get cxlmemdev patrol scrub params failed ret=%d\n",
+			ret);
+		return ret;
+	}
+
+	switch (param_type) {
+	case cxl_ps_param_enable:
+		wr_attrs.scrub_flags = FIELD_PREP(CXL_MEMDEV_PS_FLAG_ENABLED_MASK,
+						   params->enable);
+		wr_attrs.scrub_cycle_hr = FIELD_PREP(CXL_MEMDEV_PS_CUR_SCRUB_CYCLE_MASK,
+						      rd_params.rate);
+		break;
+	case cxl_ps_param_rate:
+		if (params->rate < rd_params.min_rate) {
+			dev_err(dev, "Invalid CXL patrol scrub cycle(%d) to set\n",
+				params->rate);
+			dev_err(dev, "Minimum supported CXL patrol scrub cycle in hour %d\n",
+			       params->min_rate);
+			return -EINVAL;
+		}
+		wr_attrs.scrub_cycle_hr = FIELD_PREP(CXL_MEMDEV_PS_CUR_SCRUB_CYCLE_MASK,
+						     params->rate);
+		wr_attrs.scrub_flags = FIELD_PREP(CXL_MEMDEV_PS_FLAG_ENABLED_MASK,
+						  rd_params.enable);
+		break;
+	}
+
+	ret = cxl_set_feature(mds, cxl_patrol_scrub_uuid, CXL_MEMDEV_PS_SET_FEAT_VERSION,
+			      &wr_attrs, sizeof(wr_attrs),
+			      CXL_SET_FEAT_FLAG_DATA_SAVED_ACROSS_RESET);
+	if (ret)
+		dev_err(dev, "CXL patrol scrub set feature failed ret=%d\n",
+			ret);
+
+	return ret;
+}
+
+static int cxl_patrol_scrub_get_enabled_bg(struct device *dev, bool *enabled)
+{
+	struct cxl_memdev_ps_params params;
+	int ret;
+
+	ret = cxl_mem_ps_get_attrs(dev->parent, &params);
+	if (ret)
+		return ret;
+
+	*enabled = params.enable;
+
+	return 0;
+}
+
+static int cxl_patrol_scrub_set_enabled_bg(struct device *dev, bool enable)
+{
+	struct cxl_memdev_ps_params params = {
+		.enable = enable,
+	};
+
+	return cxl_mem_ps_set_attrs(dev->parent, &params, cxl_ps_param_enable);
+}
+
+static int cxl_patrol_scrub_get_name(struct device *dev, char *name)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev->parent);
+
+	return sysfs_emit(name, "%s_%s\n", "cxl_patrol_scrub",
+			  dev_name(&cxlmd->dev));
+}
+
+static int cxl_patrol_scrub_write_rate(struct device *dev, u64 rate)
+{
+	struct cxl_memdev_ps_params params = {
+		.rate = rate,
+	};
+
+	return cxl_mem_ps_set_attrs(dev->parent, &params, cxl_ps_param_rate);
+}
+
+static int cxl_patrol_scrub_read_rate(struct device *dev, u64 *rate)
+{
+	struct cxl_memdev_ps_params params;
+	int ret;
+
+	ret = cxl_mem_ps_get_attrs(dev->parent, &params);
+	if (ret)
+		return ret;
+
+	*rate = params.rate;
+
+	return 0;
+}
+
+static int cxl_patrol_scrub_read_rate_avail(struct device *dev, u64 *min, u64 *max)
+{
+	struct cxl_memdev_ps_params params;
+	int ret;
+
+	ret = cxl_mem_ps_get_attrs(dev->parent, &params);
+	if (ret)
+		return ret;
+	*min = params.min_rate;
+	*max = U8_MAX; /* Max set by register size */
+
+	return 0;
+}
+
+static const struct scrub_ops cxl_ps_scrub_ops = {
+	.get_enabled_bg = cxl_patrol_scrub_get_enabled_bg,
+	.set_enabled_bg = cxl_patrol_scrub_set_enabled_bg,
+	.get_name = cxl_patrol_scrub_get_name,
+	.rate_read = cxl_patrol_scrub_read_rate,
+	.rate_write = cxl_patrol_scrub_write_rate,
+	.rate_avail_range = cxl_patrol_scrub_read_rate_avail,
+};
+
+int cxl_mem_patrol_scrub_init(struct cxl_memdev *cxlmd)
+{
+	struct cxl_patrol_scrub_context *cxl_ps_ctx;
+	struct cxl_mbox_supp_feat_entry feat_entry;
+	struct cxl_memdev_ps_params params;
+	struct device *cxl_scrub_dev;
+	int ret;
+
+	ret = cxl_mem_get_supported_feature_entry(cxlmd, &cxl_patrol_scrub_uuid,
+						  &feat_entry);
+	if (ret < 0)
+		return ret;
+
+	if (!(feat_entry.attr_flags & CXL_FEAT_ENTRY_FLAG_CHANGABLE))
+		return -EOPNOTSUPP;
+
+	ret = cxl_mem_ps_get_attrs(&cxlmd->dev, &params);
+	if (ret)
+		return dev_err_probe(&cxlmd->dev, ret,
+				     "Get CXL patrol scrub params failed\n");
+
+	cxl_ps_ctx = devm_kzalloc(&cxlmd->dev, sizeof(*cxl_ps_ctx), GFP_KERNEL);
+	if (!cxl_ps_ctx)
+		return -ENOMEM;
+
+	*cxl_ps_ctx = (struct cxl_patrol_scrub_context) {
+		.get_feat_size = feat_entry.get_size,
+		.set_feat_size = feat_entry.set_size,
+		.scrub_cycle_changeable =  params.scrub_cycle_changeable,
+	};
+
+	cxl_scrub_dev = devm_scrub_device_register(&cxlmd->dev, cxl_ps_ctx,
+						   &cxl_ps_scrub_ops);
+	if (IS_ERR(cxl_scrub_dev))
+		return PTR_ERR(cxl_scrub_dev);
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_mem_patrol_scrub_init, CXL);
drivers/cxl/cxlmem.h +8 -0
index 1c50a3e2eced..f95e39febd73 100644
@@ -956,6 +956,14 @@ int cxl_trigger_poison_list(struct cxl_memdev *cxlmd);
 int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa);
 int cxl_clear_poison(struct cxl_memdev *cxlmd, u64 dpa);
 
+/* cxl memory scrub functions */
+#ifdef CONFIG_CXL_SCRUB
+int cxl_mem_patrol_scrub_init(struct cxl_memdev *cxlmd);
+#else
+static inline int cxl_mem_patrol_scrub_init(struct cxl_memdev *cxlmd)
+{ return 0; }
+#endif
+
 #ifdef CONFIG_CXL_SUSPEND
 void cxl_mem_active_inc(void);
 void cxl_mem_active_dec(void);
drivers/cxl/mem.c +0 -0
index 0c79d9ce877c..399e43463626 100644

drivers/cxl/mem.c static int cxl_mem_probe(struct device *dev) · line 124

Quoted 8 lines
drivers/cxl/mem.c +5 -0
@@ -117,6 +117,12 @@ static int cxl_mem_probe(struct device *dev)
 	if (!cxlds->media_ready)
 		return -EBUSY;
 
+	rc = cxl_mem_patrol_scrub_init(cxlmd);
+	if (rc) {
+		dev_dbg(&cxlmd->dev, "CXL patrol scrub init failed\n");
+		return rc;
+	}

If the device does not support memory patrol scrub feature, the above function will return -EOPNOTSUPP. Since the feature is optional, should we just warn it and let it go through?

Fan

Quoted 7 lines
drivers/cxl/mem.c +1 -0
+
 	/*
 	 * Someone is trying to reattach this device after it lost its port
 	 * connection (an endpoint port previously registered by this memdev was
Signature · 1 line
2.34.1
author
#41 lore raw

RE: [RFC PATCH v8 05/10] cxl/memscrub: Add CXL device patrol scrub control feature

Quoted 272 lines

-----Original Message-----
From: fan <[email protected]>
Sent: 27 April 2024 00:57
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang <[email protected]>; Linuxarm <[email protected]>
Subject: Re: [RFC PATCH v8 05/10] cxl/memscrub: Add CXL device patrol scrub control feature

Quoted 8 lines · On Sat, Apr 20, 2024 at 12:47:14AM +0800, [email protected] wrote:

From: Shiju Jose <[email protected]>

CXL spec 3.1 section 8.2.9.9.11.1 describes the device patrol scrub control feature. The device patrol scrub proactively locates and makes corrections to errors in regular cycle.

Allow specifying the number of hours within which the patrol scrub must be completed, subject to minimum and maximum limits reported by the

device.

Quoted 130 lines

Also allow disabling scrub allowing trade-off error rates against performance.

Register with scrub subsystem to provide scrub control attributes to the user.

---
 Documentation/scrub/scrub-configure.rst |  52 ++++
 drivers/cxl/Kconfig                     |  19 ++
 drivers/cxl/core/Makefile               |   1 +
 drivers/cxl/core/memscrub.c             | 314 ++++++++++++++++++++++++
 drivers/cxl/cxlmem.h                    |   8 +
 drivers/cxl/mem.c                       |   6 +
 6 files changed, 400 insertions(+)
 create mode 100644 Documentation/scrub/scrub-configure.rst
 create mode 100644 drivers/cxl/core/memscrub.c
Documentation/scrub/scrub-configure.rst new file +51 -0
new file mode 100644
index 000000000000..2275366b60d3
@@ -0,0 +1,52 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+================
+Scrub subsystem
+================
+
+Copyright (c) 2024 HiSilicon Limited.
+
+:Author:   Shiju Jose <[email protected]>
+:License:  The GNU Free Documentation License, Version 1.2
+          (dual licensed under the GPL v2) :Original Reviewers:
+
+- Written for: 6.9
+- Updated for:
+
+Introduction
+------------
+The scrub subsystem provides interface for controlling attributes of
+memory scrubbers in the system. The scrub device drivers in the
+system register with the scrub subsystem.The scrub subsystem driver
+exposes the scrub controls to the user in the sysfs.
+
+The File System
+---------------
+
+The control attributes of the registered scrubbers could be accessed
+in the /sys/class/ras/rasX/scrub/
+
+sysfs
+-----
+
+Sysfs files are documented in
+`Documentation/ABI/testing/sysfs-class-scrub-configure`.
+
+Example
+-------
+
+The usage takes the form shown in this example::
+
+1. CXL patrol scrubber
+    # cat /sys/class/ras/ras0/scrub/rate_available
+    # 0x1-0xff
+    # echo 30 > /sys/class/ras/ras0/scrub/rate
+    # cat /sys/class/ras/ras0/scrub/rate
+    # 0x1e
+    # echo 1 > /sys/class/ras/ras0/scrub/enable_background
+    # cat /sys/class/ras/ras0/scrub/enable_background
+    # 1
+    # echo 0 > /sys/class/ras/ras0/scrub/enable_background
+    # cat /sys/class/ras/ras0/scrub/enable_background
+    # 0
drivers/cxl/Kconfig index renamed from drivers/cxl/Kconfig +0 -0

5f3c9c5529b9..3621b9f27e80 100644

drivers/cxl/Kconfig +19 -0
@@ -144,4 +144,23 @@ config CXL_REGION_INVALIDATION_TEST
 	  If unsure, or if this kernel is meant for production environments,
 	  say N.
 
+config CXL_SCRUB
+	bool "CXL: Memory scrub feature"
+	depends on CXL_PCI
+	depends on CXL_MEM
+	depends on SCRUB
+	help
+	  The CXL memory scrub control is an optional feature allows host to
+	  control the scrub configurations of CXL Type 3 devices, which
+	  supports patrol scrubbing.
+
+	  Registers with the scrub subsystem to provide control attributes
+	  of CXL memory device scrubber to the user.
+	  Provides interface functions to support configuring the CXL memory
+	  device patrol scrubber.
+
+	  Say 'y/n' to enable/disable control of memory scrub parameters for
+	  CXL.mem devices. See section 8.2.9.9.11.1 of CXL 3.1 specification
+	  for detailed description of CXL memory patrol scrub control feature.
+
 endif
drivers/cxl/core/Makefile +1 -0
index 9259bcc6773c..e0fc814c3983 100644
@@ -16,3 +16,4 @@ cxl_core-y += pmu.o
 cxl_core-y += cdat.o
 cxl_core-$(CONFIG_TRACING) += trace.o
 cxl_core-$(CONFIG_CXL_REGION) += region.o
+cxl_core-$(CONFIG_CXL_SCRUB) += memscrub.o
drivers/cxl/core/memscrub.c new file +10 -0
new file mode 100644 index 000000000000..a50f6e384394
@@ -0,0 +1,314 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * CXL memory scrub driver.
+ *
+ * Copyright (c) 2024 HiSilicon Limited.
+ *
+ *  - Provides functions to configure patrol scrub feature of the
+ *    CXL memory devices.
+ *  - Registers with the scrub subsystem driver to expose the sysfs attributes
+ *    to the user for configuring the CXL memory patrol scrub feature.

drivers/cxl/core/memscrub.c line 20

Quoted 10 lines
drivers/cxl/core/memscrub.c new file +10 -0
+ */
+
+#define pr_fmt(fmt)	"CXL_MEM_SCRUB: " fmt
+
+#include <cxlmem.h>
+#include <linux/cleanup.h>
+#include <linux/limits.h>
+#include <linux/memory_scrub.h>
+
+static int cxl_mem_get_supported_feature_entry(struct cxl_memdev *cxlmd,

const uuid_t *feat_uuid,

drivers/cxl/core/memscrub.c line 21

Quoted 1 line
drivers/cxl/core/memscrub.c new file +1 -0
+					       struct cxl_mbox_supp_feat_entry

*feat_entry_out) {

Quoted 35 lines
drivers/cxl/core/memscrub.c new file +35 -0
+	struct cxl_mbox_supp_feat_entry *feat_entry;
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
+	int feat_index, feats_out_size;
+	int nentries, count;
+	int ret;
+
+	feat_index = 0;
+	feats_out_size = sizeof(struct cxl_mbox_get_supp_feats_out) +
+			  sizeof(struct cxl_mbox_supp_feat_entry);
+	struct cxl_mbox_get_supp_feats_out *feats_out __free(kfree) =
+					kmalloc(feats_out_size, GFP_KERNEL);
+	if (!feats_out)
+		return -ENOMEM;
+
+	while (true) {
+		memset(feats_out, 0, feats_out_size);
+		ret = cxl_get_supported_features(mds, feats_out_size,
+						 feat_index, feats_out);
+		if (ret)
+			return ret;
+
+		nentries = feats_out->nr_entries;
+		if (!nentries)
+			return -EOPNOTSUPP;
+
+		/* Check CXL memdev supports the feature */
+		feat_entry = feats_out->feat_entries;
+		for (count = 0; count < nentries; count++, feat_entry++) {
+			if (uuid_equal(&feat_entry->uuid, feat_uuid)) {
+				memcpy(feat_entry_out, feat_entry,
+				       sizeof(*feat_entry_out));
+				return 0;
+			}
+		}

drivers/cxl/core/memscrub.c

Quoted 10 lines
drivers/cxl/core/memscrub.c new file +10 -0
+		feat_index += nentries;
+	}
+}
+
+/* CXL memory patrol scrub control definitions */
+#define CXL_MEMDEV_PS_GET_FEAT_VERSION	0x01
+#define CXL_MEMDEV_PS_SET_FEAT_VERSION	0x01
+
+static const uuid_t cxl_patrol_scrub_uuid =
+	UUID_INIT(0x96dad7d6, 0xfde8, 0x482b, 0xa7, 0x33, 0x75, 0x77, 0x4e,

\

drivers/cxl/core/memscrub.c

Quoted 12 lines
drivers/cxl/core/memscrub.c new file +12 -0
+		  0x06, 0xdb, 0x8a);
+
+/* CXL memory patrol scrub control functions */ struct
+cxl_patrol_scrub_context {
+	struct device *dev;
+	u16 get_feat_size;
+	u16 set_feat_size;
+	bool scrub_cycle_changeable;
+};
+
+/**
+ * struct cxl_memdev_ps_params - CXL memory patrol scrub parameter data

structure.

drivers/cxl/core/memscrub.c line 80

Quoted 2 lines
drivers/cxl/core/memscrub.c new file +2 -0
+ * @enable:     [IN & OUT] enable(1)/disable(0) patrol scrub.
+ * @scrub_cycle_changeable: [OUT] scrub cycle attribute of patrol scrub is

changeable.

Quoted 8 lines
drivers/cxl/core/memscrub.c new file +8 -0
+ * @rate:       [IN] Requested patrol scrub cycle in hours.
+ *              [OUT] Current patrol scrub cycle in hours.
+ * @min_rate:[OUT] minimum patrol scrub cycle, in hours, supported.
+ */
+struct cxl_memdev_ps_params {
+	bool enable;
+	bool scrub_cycle_changeable;
+	u16 rate;

drivers/cxl/core/memscrub.c

Quoted 10 lines
drivers/cxl/core/memscrub.c new file +10 -0
+	u16 min_rate;
+};
+
+enum cxl_scrub_param {
+	cxl_ps_param_enable,
+	cxl_ps_param_rate,
+};
+
+#define	CXL_MEMDEV_PS_SCRUB_CYCLE_CHANGE_CAP_MASK	BIT(0)
+#define
	CXL_MEMDEV_PS_SCRUB_CYCLE_REALTIME_REPORT_CAP_MASK
	BIT(1)

drivers/cxl/core/memscrub.c line 100

Quoted 2 lines
drivers/cxl/core/memscrub.c new file +2 -0
+#define	CXL_MEMDEV_PS_CUR_SCRUB_CYCLE_MASK	GENMASK(7, 0)
+#define	CXL_MEMDEV_PS_MIN_SCRUB_CYCLE_MASK	GENMASK(15,

8)

Quoted 10 lines
drivers/cxl/core/memscrub.c new file +10 -0
+#define	CXL_MEMDEV_PS_FLAG_ENABLED_MASK	BIT(0)
+
+struct cxl_memdev_ps_rd_attrs {
+	u8 scrub_cycle_cap;
+	__le16 scrub_cycle;
+	u8 scrub_flags;
+}  __packed;
+
+struct cxl_memdev_ps_wr_attrs {
+	u8 scrub_cycle_hr;
Quoted 8 lines

drivers/cxl/core/memscrub.c line 109

Quoted 3 lines
drivers/cxl/core/memscrub.c new file +3 -0
+	u8 scrub_flags;
+}  __packed;
+

In this patch, generally "rate" is used for cycle in hour, here we use scrub_cycle_hr. I am not sure whether "rate" is the proper term for the purpose, "interval" or "cycle" seems more straightforward for me.
But someone else may have a different thought about it.

"rate" is used in scrub control subsystem as common term based on RAS2 definition and thus used in the callbacks here. May be change to "cycle" in every related drivers?

Quoted 240 lines
Quoted 4 lines
drivers/cxl/core/memscrub.c new file +3 -0
+static int cxl_mem_ps_get_attrs(struct device *dev,
+				struct cxl_memdev_ps_params *params) {
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);

drivers/cxl/core/memscrub.c

Quoted 10 lines
drivers/cxl/core/memscrub.c new file +10 -0
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
+	size_t rd_data_size = sizeof(struct cxl_memdev_ps_rd_attrs);
+	size_t data_size;
+
+	if (!mds)
+		return -EFAULT;
+
+	struct cxl_memdev_ps_rd_attrs *rd_attrs __free(kfree) =
+						kmalloc(rd_data_size,

GFP_KERNEL);

drivers/cxl/core/memscrub.c

Quoted 10 lines
drivers/cxl/core/memscrub.c new file +10 -0
+	if (!rd_attrs)
+		return -ENOMEM;
+
+	data_size = cxl_get_feature(mds, cxl_patrol_scrub_uuid, rd_attrs,
+				    rd_data_size, rd_data_size,
+				    CXL_GET_FEAT_SEL_CURRENT_VALUE);
+	if (!data_size)
+		return -EIO;
+
+	params->scrub_cycle_changeable =

FIELD_GET(CXL_MEMDEV_PS_SCRUB_CYCLE_CHANGE_CAP_MASK,

drivers/cxl/core/memscrub.c line 138

Quoted 2 lines
drivers/cxl/core/memscrub.c new file +2 -0
+						   rd_attrs->scrub_cycle_cap);
+	params->enable =

FIELD_GET(CXL_MEMDEV_PS_FLAG_ENABLED_MASK,

drivers/cxl/core/memscrub.c line 140

Quoted 2 lines
drivers/cxl/core/memscrub.c new file +2 -0
+				   rd_attrs->scrub_flags);
+	params->rate =

FIELD_GET(CXL_MEMDEV_PS_CUR_SCRUB_CYCLE_MASK,

drivers/cxl/core/memscrub.c line 142

Quoted 2 lines
drivers/cxl/core/memscrub.c new file +2 -0
+				 rd_attrs->scrub_cycle);
+	params->min_rate =

FIELD_GET(CXL_MEMDEV_PS_MIN_SCRUB_CYCLE_MASK,

drivers/cxl/core/memscrub.c

Quoted 6 lines
drivers/cxl/core/memscrub.c new file +6 -0
+				      rd_attrs->scrub_cycle);
+
+	return 0;
+}
+
+static int cxl_mem_ps_set_attrs(struct device *dev, struct

cxl_memdev_ps_params *params,

drivers/cxl/core/memscrub.c

Quoted 12 lines
drivers/cxl/core/memscrub.c new file +12 -0
+				enum cxl_scrub_param param_type)
+{
+	struct cxl_memdev_ps_wr_attrs wr_attrs;
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
+	struct cxl_memdev_ps_params rd_params;
+	int ret;
+
+	ret = cxl_mem_ps_get_attrs(dev, &rd_params);
+	if (ret) {
+		dev_err(dev, "Get cxlmemdev patrol scrub params failed

ret=%d\n",

drivers/cxl/core/memscrub.c

Quoted 7 lines
drivers/cxl/core/memscrub.c new file +7 -0
+			ret);
+		return ret;
+	}
+
+	switch (param_type) {
+	case cxl_ps_param_enable:
+		wr_attrs.scrub_flags =

FIELD_PREP(CXL_MEMDEV_PS_FLAG_ENABLED_MASK,

drivers/cxl/core/memscrub.c line 169

Quoted 2 lines
drivers/cxl/core/memscrub.c new file +2 -0
+						   params->enable);
+		wr_attrs.scrub_cycle_hr =

FIELD_PREP(CXL_MEMDEV_PS_CUR_SCRUB_CYCLE_MASK,

drivers/cxl/core/memscrub.c

Quoted 5 lines
drivers/cxl/core/memscrub.c new file +5 -0
+						      rd_params.rate);
+		break;
+	case cxl_ps_param_rate:
+		if (params->rate < rd_params.min_rate) {
+			dev_err(dev, "Invalid CXL patrol scrub cycle(%d) to

set\n",

drivers/cxl/core/memscrub.c line 176

Quoted 2 lines
drivers/cxl/core/memscrub.c new file +2 -0
+				params->rate);
+			dev_err(dev, "Minimum supported CXL patrol scrub

cycle in hour %d\n",

drivers/cxl/core/memscrub.c

Quoted 4 lines
drivers/cxl/core/memscrub.c new file +4 -0
+			       params->min_rate);
+			return -EINVAL;
+		}
+		wr_attrs.scrub_cycle_hr =

FIELD_PREP(CXL_MEMDEV_PS_CUR_SCRUB_CYCLE_MASK,

drivers/cxl/core/memscrub.c line 182

Quoted 2 lines
drivers/cxl/core/memscrub.c new file +2 -0
+						     params->rate);
+		wr_attrs.scrub_flags =

FIELD_PREP(CXL_MEMDEV_PS_FLAG_ENABLED_MASK,

drivers/cxl/core/memscrub.c

Quoted 5 lines
drivers/cxl/core/memscrub.c new file +5 -0
+						  rd_params.enable);
+		break;
+	}
+
+	ret = cxl_set_feature(mds, cxl_patrol_scrub_uuid,

CXL_MEMDEV_PS_SET_FEAT_VERSION,

drivers/cxl/core/memscrub.c line 195

Quoted 2 lines
drivers/cxl/core/memscrub.c new file +2 -0
+			      &wr_attrs, sizeof(wr_attrs),
+

CXL_SET_FEAT_FLAG_DATA_SAVED_ACROSS_RESET);

Quoted 81 lines
drivers/cxl/core/memscrub.c new file +81 -0
+	if (ret)
+		dev_err(dev, "CXL patrol scrub set feature failed ret=%d\n",
+			ret);
+
+	return ret;
+}
+
+static int cxl_patrol_scrub_get_enabled_bg(struct device *dev, bool
+*enabled) {
+	struct cxl_memdev_ps_params params;
+	int ret;
+
+	ret = cxl_mem_ps_get_attrs(dev->parent, &params);
+	if (ret)
+		return ret;
+
+	*enabled = params.enable;
+
+	return 0;
+}
+
+static int cxl_patrol_scrub_set_enabled_bg(struct device *dev, bool
+enable) {
+	struct cxl_memdev_ps_params params = {
+		.enable = enable,
+	};
+
+	return cxl_mem_ps_set_attrs(dev->parent, &params,
+cxl_ps_param_enable); }
+
+static int cxl_patrol_scrub_get_name(struct device *dev, char *name)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev->parent);
+
+	return sysfs_emit(name, "%s_%s\n", "cxl_patrol_scrub",
+			  dev_name(&cxlmd->dev));
+}
+
+static int cxl_patrol_scrub_write_rate(struct device *dev, u64 rate)
+{
+	struct cxl_memdev_ps_params params = {
+		.rate = rate,
+	};
+
+	return cxl_mem_ps_set_attrs(dev->parent, &params,
+cxl_ps_param_rate); }
+
+static int cxl_patrol_scrub_read_rate(struct device *dev, u64 *rate)
+{
+	struct cxl_memdev_ps_params params;
+	int ret;
+
+	ret = cxl_mem_ps_get_attrs(dev->parent, &params);
+	if (ret)
+		return ret;
+
+	*rate = params.rate;
+
+	return 0;
+}
+
+static int cxl_patrol_scrub_read_rate_avail(struct device *dev, u64
+*min, u64 *max) {
+	struct cxl_memdev_ps_params params;
+	int ret;
+
+	ret = cxl_mem_ps_get_attrs(dev->parent, &params);
+	if (ret)
+		return ret;
+	*min = params.min_rate;
+	*max = U8_MAX; /* Max set by register size */
+
+	return 0;
+}
+
+static const struct scrub_ops cxl_ps_scrub_ops = {
+	.get_enabled_bg = cxl_patrol_scrub_get_enabled_bg,
+	.set_enabled_bg = cxl_patrol_scrub_set_enabled_bg,
+	.get_name = cxl_patrol_scrub_get_name,
+	.rate_read = cxl_patrol_scrub_read_rate,
+	.rate_write = cxl_patrol_scrub_write_rate,

drivers/cxl/core/memscrub.c

Quoted 10 lines
drivers/cxl/core/memscrub.c new file +10 -0
+	.rate_avail_range = cxl_patrol_scrub_read_rate_avail, };
+
+int cxl_mem_patrol_scrub_init(struct cxl_memdev *cxlmd) {
+	struct cxl_patrol_scrub_context *cxl_ps_ctx;
+	struct cxl_mbox_supp_feat_entry feat_entry;
+	struct cxl_memdev_ps_params params;
+	struct device *cxl_scrub_dev;
+	int ret;
+
+	ret = cxl_mem_get_supported_feature_entry(cxlmd,

&cxl_patrol_scrub_uuid,

drivers/cxl/core/memscrub.c

Quoted 13 lines
drivers/cxl/core/memscrub.c new file +13 -0
+						  &feat_entry);
+	if (ret < 0)
+		return ret;
+
+	if (!(feat_entry.attr_flags & CXL_FEAT_ENTRY_FLAG_CHANGABLE))
+		return -EOPNOTSUPP;
+
+	ret = cxl_mem_ps_get_attrs(&cxlmd->dev, &params);
+	if (ret)
+		return dev_err_probe(&cxlmd->dev, ret,
+				     "Get CXL patrol scrub params failed\n");
+
+	cxl_ps_ctx = devm_kzalloc(&cxlmd->dev, sizeof(*cxl_ps_ctx),

GFP_KERNEL);

Quoted 42 lines
drivers/cxl/core/memscrub.c new file +17 -0
+	if (!cxl_ps_ctx)
+		return -ENOMEM;
+
+	*cxl_ps_ctx = (struct cxl_patrol_scrub_context) {
+		.get_feat_size = feat_entry.get_size,
+		.set_feat_size = feat_entry.set_size,
+		.scrub_cycle_changeable =  params.scrub_cycle_changeable,
+	};
+
+	cxl_scrub_dev = devm_scrub_device_register(&cxlmd->dev, cxl_ps_ctx,
+						   &cxl_ps_scrub_ops);
+	if (IS_ERR(cxl_scrub_dev))
+		return PTR_ERR(cxl_scrub_dev);
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_mem_patrol_scrub_init, CXL);
drivers/cxl/cxlmem.h index renamed from drivers/cxl/cxlmem.h +0 -0

1c50a3e2eced..f95e39febd73 100644

drivers/cxl/cxlmem.h +6 -0
@@ -956,6 +956,14 @@ int cxl_trigger_poison_list(struct cxl_memdev
 *cxlmd);  int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa);
 int cxl_clear_poison(struct cxl_memdev *cxlmd, u64 dpa);
 
+/* cxl memory scrub functions */
+#ifdef CONFIG_CXL_SCRUB
+int cxl_mem_patrol_scrub_init(struct cxl_memdev *cxlmd); #else static
+inline int cxl_mem_patrol_scrub_init(struct cxl_memdev *cxlmd) {
+return 0; } #endif
+
 #ifdef CONFIG_CXL_SUSPEND
 void cxl_mem_active_inc(void);
 void cxl_mem_active_dec(void);
drivers/cxl/mem.c index renamed from drivers/cxl/mem.c +0 -0

0c79d9ce877c..399e43463626 100644

drivers/cxl/mem.c +0 -0
@@ -117,6 +117,12 @@ static int cxl_mem_probe(struct device *dev)
 	if (!cxlds->media_ready)
 		return -EBUSY;
 
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_probe(struct device *dev) · line 124

Quoted 5 lines
drivers/cxl/mem.c +5 -0
+	rc = cxl_mem_patrol_scrub_init(cxlmd);
+	if (rc) {
+		dev_dbg(&cxlmd->dev, "CXL patrol scrub init failed\n");
+		return rc;
+	}

If the device does not support memory patrol scrub feature, the above function will return -EOPNOTSUPP. Since the feature is optional, should we just warn it and let it go through?

Feedback from Jonathan was that, if this feature is built in, then should not proceed if the patrol scrub init failed, though it is an optional feature.

Quoted 10 lines

Fan

Quoted 8 lines
drivers/cxl/mem.c +1 -0
+
 	/*
 	 * Someone is trying to reattach this device after it lost its port
 	 * connection (an endpoint port previously registered by this memdev

was
--
2.34.1

Thanks,
Shiju

#42 lore raw

Re: [RFC PATCH v8 05/10] cxl/memscrub: Add CXL device patrol scrub control feature

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/cxl/mem.c index renamed from drivers/cxl/mem.c +0 -0

0c79d9ce877c..399e43463626 100644

drivers/cxl/mem.c +0 -0
@@ -117,6 +117,12 @@ static int cxl_mem_probe(struct device *dev)
 	if (!cxlds->media_ready)
 		return -EBUSY;
 
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_probe(struct device *dev) · line 124

Quoted 5 lines
drivers/cxl/mem.c +5 -0
+	rc = cxl_mem_patrol_scrub_init(cxlmd);
+	if (rc) {
+		dev_dbg(&cxlmd->dev, "CXL patrol scrub init failed\n");
+		return rc;
+	}  

If the device does not support memory patrol scrub feature, the above function will return -EOPNOTSUPP. Since the feature is optional, should we just warn it and let it go through?

Quoted 2 lines

Feedback from Jonathan was that, if this feature is built in, then should not proceed if the patrol scrub init failed, though it is an optional feature.

Oops. That wasn't my intent. If the feature is implemented by the hardware and init fails, then I think we should fail probe. Or maybe just print a very shouty message about it being broken. If the feature is simply not implemented we should definitely not fail.

Jonathan

Quoted 13 lines
Quoted 11 lines

Fan

Quoted 8 lines
drivers/cxl/mem.c +1 -0
+
 	/*
 	 * Someone is trying to reattach this device after it lost its port
 	 * connection (an endpoint port previously registered by this memdev

was
--
2.34.1

Thanks,
Shiju

#43 lore raw

Re: [RFC PATCH v8 05/10] cxl/memscrub: Add CXL device patrol scrub control feature

In reply to [email protected]
Quoted 17 lines · shiju.jose@ wrote:

From: Shiju Jose <[email protected]>

CXL spec 3.1 section 8.2.9.9.11.1 describes the device patrol scrub control feature. The device patrol scrub proactively locates and makes corrections to errors in regular cycle.

Allow specifying the number of hours within which the patrol scrub must be completed, subject to minimum and maximum limits reported by the device. Also allow disabling scrub allowing trade-off error rates against performance.

Register with scrub subsystem to provide scrub control attributes to the user.

[..]

drivers/cxl/mem.c static int cxl_mem_probe(struct device *dev) · line 124

Quoted 13 lines
drivers/cxl/mem.c +5 -0
index 0c79d9ce877c..399e43463626 100644
@@ -117,6 +117,12 @@ static int cxl_mem_probe(struct device *dev)
 	if (!cxlds->media_ready)
 		return -EBUSY;
 
+	rc = cxl_mem_patrol_scrub_init(cxlmd);
+	if (rc) {
+		dev_dbg(&cxlmd->dev, "CXL patrol scrub init failed\n");
+		return rc;
+	}

2 concerns:

* Why should cxl_mem_probe() fail just because this optional scrub interface did not register?

* Why is this not located in cxl_region_probe()? If the ras2 scrub is an HPA-based scrub I think CXL should do the work to interface with the scrub interface at the same level. This also provides another in-kernel user for all the DPA-to-HPA translation infrastructure that the CXL driver contains. Pretty much the only reason the CXL driver needs to exist at all is address translation, so at a minimum it seems a waste to inflict more need to understand DPAs on userspace.

#44 lore raw

Re: [RFC PATCH v8 05/10] cxl/memscrub: Add CXL device patrol scrub control feature

Quoted 35 lines · On Thu, 9 May 2024 17:26:46 -0700 Dan Williams <[email protected]> wrote:
Quoted 17 lines · shiju.jose@ wrote:

From: Shiju Jose <[email protected]>

CXL spec 3.1 section 8.2.9.9.11.1 describes the device patrol scrub control feature. The device patrol scrub proactively locates and makes corrections to errors in regular cycle.

Allow specifying the number of hours within which the patrol scrub must be completed, subject to minimum and maximum limits reported by the device. Also allow disabling scrub allowing trade-off error rates against performance.

Register with scrub subsystem to provide scrub control attributes to the user.

[..]

drivers/cxl/mem.c static int cxl_mem_probe(struct device *dev) · line 124

Quoted 13 lines
drivers/cxl/mem.c +5 -0
index 0c79d9ce877c..399e43463626 100644
@@ -117,6 +117,12 @@ static int cxl_mem_probe(struct device *dev)
 	if (!cxlds->media_ready)
 		return -EBUSY;
 
+	rc = cxl_mem_patrol_scrub_init(cxlmd);
+	if (rc) {
+		dev_dbg(&cxlmd->dev, "CXL patrol scrub init failed\n");
+		return rc;
+	}  

2 concerns:

Quoted 3 lines

* Why should cxl_mem_probe() fail just because this optional scrub interface did not register?

Flip the dev_dbg to dev_warn() and indeed carry on.

Quoted 7 lines

* Why is this not located in cxl_region_probe()? If the ras2 scrub is an HPA-based scrub I think CXL should do the work to interface with the scrub interface at the same level. This also provides another in-kernel user for all the DPA-to-HPA translation infrastructure that the CXL driver contains. Pretty much the only reason the CXL driver needs to exist at all is address translation, so at a minimum it seems a waste to inflict more need to understand DPAs on userspace.

As you might expect this will get messy - I'm not saying it's a bad thing to do, but complexities that come to mind include:

* Scrub is device wide (unlike RAS2 which in theory supports HPA range control) So if you map a given DPA range into multiple regions then the controls will interfere. Maybe scrub at max rate requested for any region is fine.
* Interleave - so we'd be controlling multiple hardware scrubbers.
* Comes and goes with regions. Do we stop scrubbing if no region? Not sure.

My guess is break down is:
1) Component registered for each CXL mem device to handle the control + combining of all regions specific requests.
2) Region specific component that exposes the controls on HPA basis, and requests from all it's CXL mem device drivers a minimum service level.
3) Device specific scrub instance (perhaps) reflecting that some scrub may make sense when not yet in a region (identify bad mem etc).

So I think we will end up with a lot more layering in here, but end result will indeed be better.

This has been going on a while, so not sure the DPA to HPA stuff was all in place and at the time I think was still an open question of whether that should be a userspace problem or not. Anyhow time to adapt :)

Jonathan

#45 lore raw

RFC PATCH v8 6/10 ACPICA: Add __free() based cleanup function for acpi_put_table

In reply to [email protected]

From: Jonathan Cameron <[email protected]>

Add __free() based cleanup function for acpi_put_table.

---
 include/acpi/acpixf.h | 2 ++
 1 file changed, 2 insertions(+)
include/acpi/acpixf.h +2 -0
index 3d90716f9522..fc64d903a703 100644
@@ -492,6 +492,8 @@ ACPI_EXTERNAL_RETURN_STATUS(acpi_status
 					    **out_table))
 ACPI_EXTERNAL_RETURN_VOID(void acpi_put_table(struct acpi_table_header *table))
 
+DEFINE_FREE(acpi_put_table, struct acpi_table_header *, if (!IS_ERR_OR_NULL(_T)) acpi_put_table(_T))
+
 ACPI_EXTERNAL_RETURN_STATUS(acpi_status
 			    acpi_get_table_by_index(u32 table_index,
 						    struct acpi_table_header
Signature · 1 line
2.34.1
#46 lore raw

Re: [RFC PATCH v8 06/10] ACPICA: Add __free() based cleanup function for acpi_put_table

Quoted 7 lines · On Sat, 20 Apr 2024 00:47:15 +0800 <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

Add __free() based cleanup function for acpi_put_table.

---

Reviewing (and rejecting) my own patch time ;(

I was thinking this would be useful more widely but hadn't looked as closely as I should have done. Sorry Shiju for sending you down a bad path.

include/acpi/acpixf.h ACPI_EXTERNAL_RETURN_STATUS(acpi_status · line 495

Quoted 12 lines
 include/acpi/acpixf.h | 2 ++
 1 file changed, 2 insertions(+)
include/acpi/acpixf.h +1 -0
index 3d90716f9522..fc64d903a703 100644
@@ -492,6 +492,8 @@ ACPI_EXTERNAL_RETURN_STATUS(acpi_status
 					    **out_table))
 ACPI_EXTERNAL_RETURN_VOID(void acpi_put_table(struct acpi_table_header *table))
 
+DEFINE_FREE(acpi_put_table, struct acpi_table_header *, if (!IS_ERR_OR_NULL(_T)) acpi_put_table(_T))

This is reliant on acpi_get_table2() in patch 8 / below being used as acpi_get_table() doesn't return the table.

Maybe we are better off treating acpi_get_table() / acpi_put_table() as if it were a conditional lock? Or change the 93 instances of acpi_get_table to deal with it returning a copy of the table handle pointer

That would bring it inline with many other get functions in the kernel + make our life easier using tooling like this.

+static struct acpi_table_header *acpi_get_table2(acpi_string signature,
+ u32 instance)
+{
+ struct acpi_table_header *header = NULL;
+ acpi_status status = acpi_get_table(signature, instance, &header);
+
+ if (ACPI_FAILURE(status))
+ return ERR_PTR(-EINVAL);
+
+ return header;
+}
So that we could do things like:
+ struct acpi_table_header *pAcpiTable __free(acpi_put_table) =
+ acpi_get_table2("RAS2", 0);

and avoid having to call acpi_put_table() in error paths etc.

The snag is that acpi_get_table() is from acpica (via this wrapper) so any modification would be a little messy. Also a number of cases use the status value via
const char *msg = acpi_format_exception(status);

Which we'd need to return via some path (a parameter probably). We 'could' do that but the advantages of this are getting eroded.

Upshot, this is messier than I thought, so we probably shouldn't do it.

The code in ras2 can be done reasonably neatly an outer wrapper function that gets the table and an inner one that deals with the actual processing of the entries.

Pity as there are some messy bits of code this would tidy up. In most of those a helper function also works.

Jonathan

p.s. Whilst looking at this I noticed that acpi_has_watchdog() if it succeeds doesn't put the wdat table which seems suspicious as a side effect.

Quoted 4 lines
include/acpi/acpixf.h +1 -0
+
 ACPI_EXTERNAL_RETURN_STATUS(acpi_status
 			    acpi_get_table_by_index(u32 table_index,
 						    struct acpi_table_header
#47 lore raw

RFC PATCH v8 7/10 platform: Add __free() based cleanup function for platform_device_put

In reply to [email protected]

From: Jonathan Cameron <[email protected]>

Add __free() based cleanup function for platform_device_put().

---
 include/linux/platform_device.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/platform_device.h +1 -0
index 7a41c72c1959..1ddc35623b4c 100644
@@ -232,6 +232,7 @@ extern int platform_device_add_data(struct platform_device *pdev,
 extern int platform_device_add(struct platform_device *pdev);
 extern void platform_device_del(struct platform_device *pdev);
 extern void platform_device_put(struct platform_device *pdev);
+DEFINE_FREE(platform_device_put, struct platform_device *, if (_T) platform_device_put(_T))
 
 struct platform_driver {
 	int (*probe)(struct platform_device *);
Signature · 1 line
2.34.1
#48 lore raw

RFC PATCH v8 8/10 ACPI:RAS2: Add ACPI RAS2 driver

In reply to [email protected]

From: Shiju Jose <[email protected]>

Add support for ACPI RAS2 feature table (RAS2) defined in the ACPI 6.5 Specification, section 5.2.21.
Driver contains RAS2 Init, which extracts the RAS2 table and driver adds platform device for each memory features which binds to the RAS2 memory driver.

Driver uses PCC mailbox to communicate with the ACPI HW and the driver adds OSPM interfaces to send RAS2 commands.

---
 drivers/acpi/Kconfig     |  10 ++
 drivers/acpi/Makefile    |   1 +
 drivers/acpi/ras2.c      | 366 +++++++++++++++++++++++++++++++++++++++
 include/acpi/ras2_acpi.h |  59 +++++++
 4 files changed, 436 insertions(+)
 create mode 100755 drivers/acpi/ras2.c
 create mode 100644 include/acpi/ras2_acpi.h
drivers/acpi/Kconfig +10 -0
index ff1689bb3124..638f1e38f961 100644
@@ -284,6 +284,16 @@ config ACPI_CPPC_LIB
 	  If your platform does not support CPPC in firmware,
 	  leave this option disabled.
 
+config ACPI_RAS2
+	bool "ACPI RAS2 driver"
+	select MAILBOX
+	select PCC
+	help
+	  The driver adds support for ACPI RAS2 feature table(extracts RAS2
+	  table from OS system table) and OSPM interfaces to send RAS2
+	  commands via PCC mailbox subspace. Driver adds platform device for
+	  the RAS2 memory features which binds to the RAS2 memory driver.
+
 config ACPI_PROCESSOR
 	tristate "Processor"
 	depends on X86 || ARM64 || LOONGARCH || RISCV
drivers/acpi/Makefile +1 -0
index 8cc8c0d9c873..1df9de524c62 100644
@@ -104,6 +104,7 @@ obj-$(CONFIG_ACPI_EC_DEBUGFS)	+= ec_sys.o
 obj-$(CONFIG_ACPI_BGRT)		+= bgrt.o
 obj-$(CONFIG_ACPI_CPPC_LIB)	+= cppc_acpi.o
 obj-$(CONFIG_ACPI_SPCR_TABLE)	+= spcr.o
+obj-$(CONFIG_ACPI_RAS2)		+= ras2.o
 obj-$(CONFIG_ACPI_DEBUGGER_USER) += acpi_dbg.o
 obj-$(CONFIG_ACPI_PPTT) 	+= pptt.o
 obj-$(CONFIG_ACPI_PFRUT)	+= pfr_update.o pfr_telemetry.o
drivers/acpi/ras2.c new file +366 -0

366 lines not on this page · raw message

include/acpi/ras2_acpi.h new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
2.34.1
#49 lore raw

Re: [RFC PATCH v8 08/10] ACPI:RAS2: Add ACPI RAS2 driver

Quoted 5 lines
drivers/acpi/ras2.c new file +5 -0
+static int ras2_check_pcc_chan(struct ras2_pcc_subspace *pcc_subspace)
+{
+	struct acpi_ras2_shared_memory __iomem *generic_comm_base =pcc_subspace->pcc_comm_addr;
+	ktime_t next_deadline = ktime_add(ktime_get(), pcc_subspace->deadline);
+	u16 status;

drivers/acpi/ras2.c line 50

Quoted 10 lines
drivers/acpi/ras2.c new file +10 -0
+
+	while (!ktime_after(ktime_get(), next_deadline)) {
+		/*
+		 * As per ACPI spec, the PCC space will be initialized by
+		 * platform and should have set the command completion bit when
+		 * PCC can be used by OSPM
+		 */
+		status = readw_relaxed(&generic_comm_base->status);
+		if (status & RAS2_PCC_CMD_ERROR)
+			return -EIO;

We need to clear the error bit before reporting an error. Maybe the error was transient, or specific to the last transaction. So clearing it here, lets us try again later. Like This:

		if (status & RAS2_PCC_CMD_ERROR) {
			status &= ~RAS2_PCC_CMD_ERROR;
			writew_relaxed(status, &generic_comm_base->status);
			return -EIO;
		}

Also, we are thinking that using the "Set RAS Capability Status" as a way to communicate RAS2 Scrub specific error conditions is appropriate. If we agree on that idea, then we can additionally check the set_capabilities_status and return an appropriate error.. for example, we can add a new error mapping function:
static int report_cap_error(u32 cap_status)

{
	switch (cap_status) {
		case 1:  /* Not Valid */
		case 2:  /* Not Supported */
			return -EPERM;
		case 3:  /* Busy */
			return -EBUSY;
		case 4:  /* FailedF */
		case 5:  /* Aborted */
		case 6:  /* Invalid Data */
			return -EINVAL;
		default: /* 0 or other, Success */
			return 0;
	}
}

and then instead, modify ras2_check_pcc_chan in this way:

		if (status & RAS2_PCC_CMD_ERROR) {
			cap_status = readw_relaxed(&generic_comm_base->set_capabilities_status);
			ret = report_cap_error(cap_status);

			status &= ~RAS2_PCC_CMD_ERROR;
			writew_relaxed(status, &generic_comm_base->status);
			return ret;
		}
Quoted 94 lines
drivers/acpi/ras2.c new file +94 -0
+		if (status & RAS2_PCC_CMD_COMPLETE)
+			return 0;
+		/*
+		 * Reducing the bus traffic in case this loop takes longer than
+		 * a few retries.
+		 */
+		msleep(10);
+	}
+
+	return -EIO;
+}
+
+/**
+ * ras2_send_pcc_cmd() - Send RAS2 command via PCC channel
+ * @ras2_ctx:	pointer to the ras2 context structure
+ * @cmd:	command to send
+ *
+ * Returns: 0 on success, an error otherwise
+ */
+int ras2_send_pcc_cmd(struct ras2_scrub_ctx *ras2_ctx, u16 cmd)
+{
+	struct ras2_pcc_subspace *pcc_subspace = ras2_ctx->pcc_subspace;
+	struct acpi_ras2_shared_memory *generic_comm_base = pcc_subspace->pcc_comm_addr;
+	static ktime_t last_cmd_cmpl_time, last_mpar_reset;
+	struct mbox_chan *pcc_channel;
+	unsigned int time_delta;
+	static int mpar_count;
+	int ret;
+
+	guard(mutex)(&ras2_pcc_subspace_lock);
+	ret = ras2_check_pcc_chan(pcc_subspace);
+	if (ret)
+		return ret;
+	pcc_channel = pcc_subspace->pcc_chan->mchan;
+
+	/*
+	 * Handle the Minimum Request Turnaround Time(MRTT)
+	 * "The minimum amount of time that OSPM must wait after the completion
+	 * of a command before issuing the next command, in microseconds"
+	 */
+	if (pcc_subspace->pcc_mrtt) {
+		time_delta = ktime_us_delta(ktime_get(), last_cmd_cmpl_time);
+		if (pcc_subspace->pcc_mrtt > time_delta)
+			udelay(pcc_subspace->pcc_mrtt - time_delta);
+	}
+
+	/*
+	 * Handle the non-zero Maximum Periodic Access Rate(MPAR)
+	 * "The maximum number of periodic requests that the subspace channel can
+	 * support, reported in commands per minute. 0 indicates no limitation."
+	 *
+	 * This parameter should be ideally zero or large enough so that it can
+	 * handle maximum number of requests that all the cores in the system can
+	 * collectively generate. If it is not, we will follow the spec and just
+	 * not send the request to the platform after hitting the MPAR limit in
+	 * any 60s window
+	 */
+	if (pcc_subspace->pcc_mpar) {
+		if (mpar_count == 0) {
+			time_delta = ktime_ms_delta(ktime_get(), last_mpar_reset);
+			if (time_delta < 60 * MSEC_PER_SEC) {
+				dev_dbg(ras2_ctx->dev,
+					"PCC cmd not sent due to MPAR limit");
+				return -EIO;
+			}
+			last_mpar_reset = ktime_get();
+			mpar_count = pcc_subspace->pcc_mpar;
+		}
+		mpar_count--;
+	}
+
+	/* Write to the shared comm region. */
+	writew_relaxed(cmd, &generic_comm_base->command);
+
+	/* Flip CMD COMPLETE bit */
+	writew_relaxed(0, &generic_comm_base->status);
+
+	/* Ring doorbell */
+	ret = mbox_send_message(pcc_channel, &cmd);
+	if (ret < 0) {
+		dev_err(ras2_ctx->dev,
+			"Err sending PCC mbox message. cmd:%d, ret:%d\n",
+			cmd, ret);
+		return ret;
+	}
+
+	/*
+	 * If Minimum Request Turnaround Time is non-zero, we need
+	 * to record the completion time of both READ and WRITE
+	 * command for proper handling of MRTT, so we need to check
+	 * for pcc_mrtt in addition to CMD_READ
+	 */
+	if (cmd == RAS2_PCC_CMD_EXEC || pcc_subspace->pcc_mrtt) {
+		ret = ras2_check_pcc_chan(pcc_subspace);

drivers/acpi/ras2.c line 154

Quoted 10 lines
drivers/acpi/ras2.c new file +10 -0
+		if (pcc_subspace->pcc_mrtt)
+			last_cmd_cmpl_time = ktime_get();
+	}
+
+	if (pcc_channel->mbox->txdone_irq)
+		mbox_chan_txdone(pcc_channel, ret);
+	else
+		mbox_client_txdone(pcc_channel, ret);
+
+	return 0;

As of now, this code doesn't consider errors occurring after the mbox_send_message.
Checking errors is, probably, necessary to take appropriate action - such as reporting an error to the user. Also, error or no, it is important to call the txdone bits.

One simple solution that works is to modify the previous "return 0" to look like:

	return ret >= 0 ? 0 : ret;
Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL_GPL(ras2_send_pcc_cmd);
#50 lore raw

RFC PATCH v8 9/10 ras: scrub: Add scrub control attributes for ACPI RAS2

In reply to [email protected]

From: Shiju Jose <[email protected]>

Add scrub control attributes for ACPI RAS2 patrol scrub feature.

---
 .../ABI/testing/sysfs-class-scrub-configure   |  28 +++-
 drivers/ras/memory_scrub.c                    | 131 ++++++++++++++++++
 include/linux/memory_scrub.h                  |   8 ++
 3 files changed, 165 insertions(+), 2 deletions(-)
Documentation/ABI/testing/sysfs-class-scrub-configure +26 -2
index 3ed77dbb00ad..7178776249f8 100644
@@ -15,12 +15,21 @@ Description:
 		correspond to each scrub device registered with the
 		scrub subsystem.
 
-What:		/sys/class/ras/rasX/scrub/name
+What:		/sys/class/ras/rasX/scrub/addr_range_base
 Date:		March 2024
 KernelVersion:	6.9
 Contact:	[email protected]
 Description:
-		(RO) name of the memory scrubber
+		(RW) The base of the address range of the memory region
+		to be scrubbed (on-demand scrubbing).
+
+What:		/sys/class/ras/rasX/scrub/addr_range_size
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) The size of the address range of the memory region
+		to be scrubbed (on-demand scrubbing).
 
 What:		/sys/class/ras/rasX/scrub/enable_background
 Date:		March 2024
@@ -29,6 +38,21 @@ Contact:	[email protected]
 Description:
 		(RW) Enable/Disable background(patrol) scrubbing if supported.
 
+What:		/sys/class/ras/rasX/scrub/enable_on_demand
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RW) Enable/Disable on-demand scrubbing the memory region
+		if supported.
+
+What:		/sys/class/ras/rasX/scrub/name
+Date:		March 2024
+KernelVersion:	6.9
+Contact:	[email protected]
+Description:
+		(RO) name of the memory scrubber
+
 What:		/sys/class/ras/rasX/scrub/rate_available
 Date:		March 2024
 KernelVersion:	6.9
drivers/ras/memory_scrub.c +131 -0

168 lines not on this page · raw message

include/linux/memory_scrub.h +8 -0
index f0e1657a5072..d8edb48677c9 100755
@@ -15,16 +15,24 @@ struct device;
 
 /**
  * struct scrub_ops - scrub device operations (all elements optional)
+ * @read_range: read base and offset of scrubbing range.
+ * @write_range: set the base and offset of the scrubbing range.
  * @get_enabled_bg: check if currently performing background scrub.
  * @set_enabled_bg: start or stop a bg-scrub.
+ * @get_enabled_od: check if currently performing on-demand scrub.
+ * @set_enabled_od: start or stop an on-demand scrub.
  * @get_name: get the memory scrubber name.
  * @rate_avail_range: retrieve limits on supported rates.
  * @rate_read: read the scrub rate
  * @rate_write: set the scrub rate
  */
 struct scrub_ops {
+	int (*read_range)(struct device *dev, u64 *base, u64 *size);
+	int (*write_range)(struct device *dev, u64 base, u64 size);
 	int (*get_enabled_bg)(struct device *dev, bool *enable);
 	int (*set_enabled_bg)(struct device *dev, bool enable);
+	int (*get_enabled_od)(struct device *dev, bool *enable);
+	int (*set_enabled_od)(struct device *dev, bool enable);
 	int (*get_name)(struct device *dev, char *buf);
 	int (*rate_avail_range)(struct device *dev, u64 *min, u64 *max);
 	int (*rate_read)(struct device *dev, u64 *rate);
Signature · 1 line
2.34.1
#51 lore raw

RFC PATCH v8 10/10 ras: scrub: ACPI RAS2: Add memory ACPI RAS2 driver

In reply to [email protected]

From: Shiju Jose <[email protected]>

Memory ACPI RAS2 driver binds to the platform device add by the ACPI RAS2 table parser.

Driver uses a PCC subspace for communicating with the ACPI compliant platform to provide control of memory scrub parameters via the scrub subsystem.

---
 Documentation/scrub/scrub-configure.rst |  33 +++
 drivers/ras/Kconfig                     |  10 +
 drivers/ras/Makefile                    |   1 +
 drivers/ras/acpi_ras2.c                 | 358 ++++++++++++++++++++++++
 4 files changed, 402 insertions(+)
 create mode 100644 drivers/ras/acpi_ras2.c
Documentation/scrub/scrub-configure.rst +33 -0
index 2275366b60d3..7a1bf87bc6d7 100644
@@ -50,3 +50,36 @@ The usage takes the form shown in this example::
     # echo 0 > /sys/class/ras/ras0/scrub/enable_background
     # cat /sys/class/ras/ras0/scrub/enable_background
     # 0
+
+2. RAS2
+2.1 On demand scrubbing for a specific memory region.
+    # echo 0x120000 > /sys/class/ras/ras1/scrub/addr_range_base
+    # echo 0x150000 > /sys/class/ras/ras1/scrub/addr_range_size
+    # cat /sys/class/ras/ras1/scrub/rate_available
+    # 0x1-0x18
+    # echo 20 > /sys/class/ras/ras1/scrub/rate
+    # echo 1 > /sys/class/ras/ras1/scrub/enable_on_demand
+    # cat /sys/class/ras/ras1/scrub/enable_on_demand
+    # 1
+    # cat /sys/class/ras/ras1/scrub/rate
+    # 0x14
+    # cat /sys/class/ras/ras1/scrub/addr_range_base
+    # 0x120000
+    # cat /sys/class/ras/ras1/scrub/addr_range_size
+    # 0x150000
+    # echo 0 > /sys/class/ras/ras1/scrub/enable_on_demand
+    # cat /sys/class/ras/ras1/scrub/enable_on_demand
+    # 0
+
+2.2 Background scrubbing the entire memory
+    # cat /sys/class/ras/ras1/scrub/rate_available
+    # 0x1-0x18
+    # echo 3 > /sys/class/ras/ras1/scrub/rate
+    # echo 1 > /sys/class/ras/ras1/scrub/enable_background
+    # cat /sys/class/ras/ras1/scrub/enable_background
+    # 1
+    # cat /sys/class/ras/ras1/scrub/rate
+    # 0x3
+    # echo 0 > /sys/class/ras/ras1/scrub/enable_background
+    # cat /sys/class/ras/ras1/scrub/enable_background
+    # 0
drivers/ras/Kconfig +10 -0
index 181701479564..57c346dfc01f 100644
@@ -53,4 +53,14 @@ config SCRUB
 	  configuring the parameters of underlying scrubbers in the
 	  system for the DRAM memories.
 
+config MEM_ACPI_RAS2
+	tristate "Memory ACPI RAS2 driver"
+	depends on ACPI_RAS2
+	depends on SCRUB
+	help
+	  The driver binds to the platform device added by the ACPI RAS2
+	  table parser. Use a PCC channel subspace for communicating with
+	  the ACPI compliant platform to provide control of memory scrub
+	  parameters via the scrub subsystem.
+
 endif
drivers/ras/Makefile +1 -0
index 89bcf0d84355..48339fee1cb3 100644
@@ -3,6 +3,7 @@ obj-$(CONFIG_RAS)	+= ras.o
 obj-$(CONFIG_DEBUG_FS)	+= debugfs.o
 obj-$(CONFIG_RAS_CEC)	+= cec.o
 obj-$(CONFIG_SCRUB)	+= memory_scrub.o
+obj-$(CONFIG_MEM_ACPI_RAS2)	+= acpi_ras2.o
 
 obj-$(CONFIG_RAS_FMPM)	+= amd/fmpm.o
 obj-y			+= amd/atl/
drivers/ras/acpi_ras2.c new file +363 -0

363 lines not on this page · raw message

Signature · 1 line
2.34.1
#52 lore raw

Re: [RFC PATCH v8 10/10] ras: scrub: ACPI RAS2: Add memory ACPI RAS2 driver

drivers/ras/acpi_ras2.c line 83

Quoted 13 lines
drivers/ras/acpi_ras2.c new file +13 -0
+/* Context - lock must be held */
+static int ras2_get_patrol_scrub_running(struct ras2_scrub_ctx *ras2_ctx,
+					 bool *running)
+{
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace->pcc_comm_addr;
+	int ret;
+
+	if (ras2_ctx->bg)
+		*running = true;
+
+	ps_sm->common.set_capabilities[0] = RAS2_SUPPORT_HW_PARTOL_SCRUB;
+	ps_sm->params.patrol_scrub_command = RAS2_GET_PATROL_PARAMETERS;

Need to reset the address range (base and size). A user may have previously called "Enable Background" where the code zeros out these parameters.

	ps_sm->params.requested_address_range[0] = ras2_ctx->base;
	ps_sm->params.requested_address_range[1] = ras2_ctx->size;
Quoted 15 lines
drivers/ras/acpi_ras2.c new file +15 -0
+
+	ret = ras2_send_pcc_cmd(ras2_ctx, RAS2_PCC_CMD_EXEC);
+	if (ret) {
+		dev_err(ras2_ctx->dev, "failed to read parameters\n");
+		return ret;
+	}
+
+	*running = ps_sm->params.flags & RAS2_PATROL_SCRUB_FLAG_SCRUBBER_RUNNING;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_write_rate(struct device *dev, u64 rate)
+{
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);

drivers/ras/acpi_ras2.c line 108

Quoted 10 lines
drivers/ras/acpi_ras2.c new file +10 -0
+	bool running;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ret = ras2_get_patrol_scrub_running(ras2_ctx, &running);
+	if (ret)
+		return ret;
+
+	if (running)
+		return -EBUSY;

I suggest we do not check if the patrol scrub is running when we are merely updating cached values. More importantly, if we had
previously wrote an invalid value (that is only invalidated by firmware after
executing a command), then when we try to write a correct value, this "ras2_get_patrol_scrub_running" check will always fail, therefore preventing us from correcting our error.

Quoted 41 lines
drivers/ras/acpi_ras2.c new file +41 -0
+
+	if (rate < ras2_ctx->rate_min || rate > ras2_ctx->rate_max)
+		return -EINVAL;
+
+	ras2_ctx->rate = rate;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_rate(struct device *dev, u64 *rate)
+{
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*rate = ras2_ctx->rate;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_rate_avail(struct device *dev, u64 *min, u64 *max)
+{
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*min = ras2_ctx->rate_min;
+	*max = ras2_ctx->rate_max;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_range(struct device *dev, u64 *base, u64 *size)
+{
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*base = ras2_ctx->base;
+	*size = ras2_ctx->size;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_write_range(struct device *dev, u64 base, u64 size)
+{
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);

drivers/ras/acpi_ras2.c line 159

Quoted 10 lines
drivers/ras/acpi_ras2.c new file +10 -0
+	bool running;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ret = ras2_get_patrol_scrub_running(ras2_ctx, &running);
+	if (ret)
+		return ret;
+
+	if (running)
+		return -EBUSY;

I suggest we do not check if the patrol scrub is running. See previous comment above.

Quoted 72 lines
drivers/ras/acpi_ras2.c new file +72 -0
+
+	ras2_ctx->base = base;
+	ras2_ctx->size = size;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_set_enabled_bg(struct device *dev, bool enable)
+{
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace->pcc_comm_addr;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ps_sm->common.set_capabilities[0] = RAS2_SUPPORT_HW_PARTOL_SCRUB;
+	if (enable) {
+		ps_sm->params.requested_address_range[0] = 0;
+		ps_sm->params.requested_address_range[1] = 0;
+		ps_sm->params.scrub_params_in &= ~RAS2_PATROL_SCRUB_RATE_IN_MASK;
+		ps_sm->params.scrub_params_in |= FIELD_PREP(RAS2_PATROL_SCRUB_RATE_IN_MASK,
+							    ras2_ctx->rate);
+		ps_sm->params.patrol_scrub_command = RAS2_START_PATROL_SCRUBBER;
+	} else {
+		ps_sm->params.patrol_scrub_command = RAS2_STOP_PATROL_SCRUBBER;
+	}
+	ps_sm->params.scrub_params_in &= ~RAS2_PATROL_SCRUB_EN_BACKGROUND;
+	ps_sm->params.scrub_params_in |= FIELD_PREP(RAS2_PATROL_SCRUB_EN_BACKGROUND,
+						    enable);
+
+	ret = ras2_send_pcc_cmd(ras2_ctx, RAS2_PCC_CMD_EXEC);
+	if (ret) {
+		dev_err(ras2_ctx->dev, "%s: failed to enable(%d) background scrubbing\n",
+			__func__, enable);
+		return ret;
+	}
+	ras2_ctx->bg = true;
+
+	/* Update the cache to account for rounding of supplied parameters and similar */
+	return ras2_update_patrol_scrub_params_cache(ras2_ctx);
+}
+
+static int ras2_hw_scrub_get_enabled_bg(struct device *dev, bool *enabled)
+{
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*enabled = ras2_ctx->bg;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_set_enabled_od(struct device *dev, bool enable)
+{
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace->pcc_comm_addr;
+	bool enabled;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ps_sm->common.set_capabilities[0] = RAS2_SUPPORT_HW_PARTOL_SCRUB;
+	if (enable) {
+		if (!ras2_ctx->size) {
+			dev_warn(ras2_ctx->dev,
+				 "%s: Invalid requested address range, requested_address_range[0]=0x%llx "
+				 "requested_address_range[1]=0x%llx\n", __func__,
+				 ps_sm->params.requested_address_range[0],
+				 ps_sm->params.requested_address_range[1]);
+			return -ERANGE;
+		}
+		ret = ras2_get_patrol_scrub_running(ras2_ctx, &enabled);
+		if (ret)

drivers/ras/acpi_ras2.c line 241

Quoted 10 lines
drivers/ras/acpi_ras2.c new file +10 -0
+			return ret;
+
+		if (enabled)
+			return 0;
+
+		ps_sm->params.scrub_params_in &= ~RAS2_PATROL_SCRUB_RATE_IN_MASK;
+		ps_sm->params.scrub_params_in |= FIELD_PREP(RAS2_PATROL_SCRUB_RATE_IN_MASK,
+							    ras2_ctx->rate);
+		ps_sm->params.requested_address_range[0] = ras2_ctx->base;
+		ps_sm->params.requested_address_range[1] = ras2_ctx->size;

We need to clear the RAS2_PATROL_SCRUB_EN_BACKGROUND bit in the input parameters.
This is in case "Enable Background" was previously called, and this bit was set.

		ps_sm->params.scrub_params_in &= ~RAS2_PATROL_SCRUB_EN_BACKGROUND;
Quoted 14 lines
drivers/ras/acpi_ras2.c new file +14 -0
+		ps_sm->params.patrol_scrub_command = RAS2_START_PATROL_SCRUBBER;
+	} else {
+		ps_sm->params.patrol_scrub_command = RAS2_STOP_PATROL_SCRUBBER;
+	}
+
+	ret = ras2_send_pcc_cmd(ras2_ctx, RAS2_PCC_CMD_EXEC);
+	if (ret) {
+		dev_err(ras2_ctx->dev, "failed to enable(%d) the demand scrubbing\n", enable);
+		return ret;
+	}
+	ras2_ctx->bg = false;
+
+	return ras2_update_patrol_scrub_params_cache(ras2_ctx);
+}
author
#53 lore raw

RE: [RFC PATCH v8 10/10] ras: scrub: ACPI RAS2: Add memory ACPI RAS2 driver

Hi Daniel,

Thanks for the feedback.

Quoted 38 lines

-----Original Message-----
From: Daniel Ferguson <[email protected]>
Sent: 05 June 2024 22:33
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang <[email protected]>; Linuxarm <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC PATCH v8 10/10] ras: scrub: ACPI RAS2: Add memory ACPI RAS2 driver

drivers/ras/acpi_ras2.c

Quoted 13 lines
drivers/ras/acpi_ras2.c new file +6 -0
+/* Context - lock must be held */
+static int ras2_get_patrol_scrub_running(struct ras2_scrub_ctx *ras2_ctx,
+					 bool *running)
+{
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace-

pcc_comm_addr;

drivers/ras/acpi_ras2.c new file +6 -0
+	int ret;
+
+	if (ras2_ctx->bg)
+		*running = true;
+
+	ps_sm->common.set_capabilities[0] =

RAS2_SUPPORT_HW_PARTOL_SCRUB;

Quoted 7 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	ps_sm->params.patrol_scrub_command =

RAS2_GET_PATROL_PARAMETERS;

Need to reset the address range (base and size). A user may have previously called "Enable Background" where the code zeros out these parameters.

	ps_sm->params.requested_address_range[0] = ras2_ctx->base;
	ps_sm->params.requested_address_range[1] = ras2_ctx->size;

The address range is being set to the above in the ras2_hw_scrub_set_enabled_od(), because they are valid for on-demand scrubbing only.

However the ras2_ctx->base and ras2_ctx->size are set to the
ras2_ctx->base = ps_sm->params.actual_address_range[0];
ras2_ctx->size = ps_sm->params.actual_address_range[1];
in the ras2_update_patrol_scrub_params_cache(), which is called after enabling bg scrub and on-demand scrub.
Thus ras2_ctx->base and ras2_ctx->size may have a 0 or garbage value for bg scrub because address range is not valid for bg scrubbing as perc ACPI specification. I will add checks to retain the cached address range if bg scrub is enabled.

Quoted 26 lines
Quoted 26 lines
drivers/ras/acpi_ras2.c new file +24 -0
+
+	ret = ras2_send_pcc_cmd(ras2_ctx, RAS2_PCC_CMD_EXEC);
+	if (ret) {
+		dev_err(ras2_ctx->dev, "failed to read parameters\n");
+		return ret;
+	}
+
+	*running = ps_sm->params.flags &
+RAS2_PATROL_SCRUB_FLAG_SCRUBBER_RUNNING;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_write_rate(struct device *dev, u64 rate) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	bool running;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ret = ras2_get_patrol_scrub_running(ras2_ctx, &running);
+	if (ret)
+		return ret;
+
+	if (running)
Quoted 8 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		return -EBUSY;

I suggest we do not check if the patrol scrub is running when we are merely updating cached values. More importantly, if we had previously wrote an invalid value (that is only invalidated by firmware after executing a command), then when we try to write a correct value, this "ras2_get_patrol_scrub_running" check will always fail, therefore preventing us from correcting our error.

In our opinion, write the rate and range etc, though updating the cached values, should be allowed only when the scrub is NOT running to avoid confusion thinking they are actually set in the running scrubber, when read them back in the userspace.

Quoted 46 lines
Quoted 46 lines
drivers/ras/acpi_ras2.c new file +45 -0
+
+	if (rate < ras2_ctx->rate_min || rate > ras2_ctx->rate_max)
+		return -EINVAL;
+
+	ras2_ctx->rate = rate;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_rate(struct device *dev, u64 *rate) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*rate = ras2_ctx->rate;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_rate_avail(struct device *dev, u64
+*min, u64 *max) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*min = ras2_ctx->rate_min;
+	*max = ras2_ctx->rate_max;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_range(struct device *dev, u64 *base,
+u64 *size) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*base = ras2_ctx->base;
+	*size = ras2_ctx->size;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_write_range(struct device *dev, u64 base,
+u64 size) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	bool running;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ret = ras2_get_patrol_scrub_running(ras2_ctx, &running);
Quoted 8 lines

drivers/ras/acpi_ras2.c line 108

Quoted 5 lines
drivers/ras/acpi_ras2.c new file +5 -0
+	if (ret)
+		return ret;
+
+	if (running)
+		return -EBUSY;

I suggest we do not check if the patrol scrub is running. See previous comment above.

Same as above.

Quoted 105 lines
Quoted 14 lines
drivers/ras/acpi_ras2.c new file +12 -0
+
+	ras2_ctx->base = base;
+	ras2_ctx->size = size;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_set_enabled_bg(struct device *dev, bool
+enable) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace-

pcc_comm_addr;

drivers/ras/acpi_ras2.c

Quoted 4 lines
drivers/ras/acpi_ras2.c new file +4 -0
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ps_sm->common.set_capabilities[0] =

RAS2_SUPPORT_HW_PARTOL_SCRUB;

drivers/ras/acpi_ras2.c

Quoted 4 lines
drivers/ras/acpi_ras2.c new file +4 -0
+	if (enable) {
+		ps_sm->params.requested_address_range[0] = 0;
+		ps_sm->params.requested_address_range[1] = 0;
+		ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_RATE_IN_MASK;

Review

Quoted 1 line
(quoted diff) +1 -0
+		ps_sm->params.scrub_params_in |=

FIELD_PREP(RAS2_PATROL_SCRUB_RATE_IN_MASK,

Review

Quoted 2 lines
(quoted diff) +2 -0
+							    ras2_ctx->rate);
+		ps_sm->params.patrol_scrub_command =

RAS2_START_PATROL_SCRUBBER;

Review

Quoted 2 lines
(quoted diff) +2 -0
+	} else {
+		ps_sm->params.patrol_scrub_command =

RAS2_STOP_PATROL_SCRUBBER;

Review

Quoted 2 lines
(quoted diff) +2 -0
+	}
+	ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_EN_BACKGROUND;

Review

Quoted 1 line
(quoted diff) +1 -0
+	ps_sm->params.scrub_params_in |=

FIELD_PREP(RAS2_PATROL_SCRUB_EN_BACKGROUND,

drivers/ras/acpi_ras2.c

Quoted 5 lines
drivers/ras/acpi_ras2.c new file +5 -0
+						    enable);
+
+	ret = ras2_send_pcc_cmd(ras2_ctx, RAS2_PCC_CMD_EXEC);
+	if (ret) {
+		dev_err(ras2_ctx->dev, "%s: failed to enable(%d) background

scrubbing\n",

drivers/ras/acpi_ras2.c

Quoted 6 lines
drivers/ras/acpi_ras2.c new file +6 -0
+			__func__, enable);
+		return ret;
+	}
+	ras2_ctx->bg = true;
+
+	/* Update the cache to account for rounding of supplied parameters and

similar */

Quoted 18 lines
drivers/ras/acpi_ras2.c new file +17 -0
+	return ras2_update_patrol_scrub_params_cache(ras2_ctx);
+}
+
+static int ras2_hw_scrub_get_enabled_bg(struct device *dev, bool
+*enabled) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*enabled = ras2_ctx->bg;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_set_enabled_od(struct device *dev, bool
+enable) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace-

pcc_comm_addr;

drivers/ras/acpi_ras2.c

Quoted 5 lines
drivers/ras/acpi_ras2.c new file +5 -0
+	bool enabled;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ps_sm->common.set_capabilities[0] =

RAS2_SUPPORT_HW_PARTOL_SCRUB;

drivers/ras/acpi_ras2.c

Quoted 4 lines
drivers/ras/acpi_ras2.c new file +4 -0
+	if (enable) {
+		if (!ras2_ctx->size) {
+			dev_warn(ras2_ctx->dev,
+				 "%s: Invalid requested address range,

requested_address_range[0]=0x%llx "

Review

Quoted 1 line
(quoted diff) +1 -0
+				 "requested_address_range[1]=0x%llx\n",

__func__,

drivers/ras/acpi_ras2.c

Quoted 12 lines
drivers/ras/acpi_ras2.c new file +12 -0
+				 ps_sm->params.requested_address_range[0],
+				 ps_sm->params.requested_address_range[1]);
+			return -ERANGE;
+		}
+		ret = ras2_get_patrol_scrub_running(ras2_ctx, &enabled);
+		if (ret)
+			return ret;
+
+		if (enabled)
+			return 0;
+
+		ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_RATE_IN_MASK;

Review

Quoted 1 line
(quoted diff) +1 -0
+		ps_sm->params.scrub_params_in |=

FIELD_PREP(RAS2_PATROL_SCRUB_RATE_IN_MASK,

drivers/ras/acpi_ras2.c line 241

Quoted 3 lines
drivers/ras/acpi_ras2.c new file +3 -0
+							    ras2_ctx->rate);
+		ps_sm->params.requested_address_range[0] = ras2_ctx->base;
+		ps_sm->params.requested_address_range[1] = ras2_ctx->size;

We need to clear the RAS2_PATROL_SCRUB_EN_BACKGROUND bit in the input parameters.
This is in case "Enable Background" was previously called, and this bit was set.

Quoted 2 lines
		ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_EN_BACKGROUND;

We need to stop background scrub if it is already running before start an on-demand scrubbing. The RAS2_PATROL_SCRUB_EN_BACKGROUND bit would be cleared with disable bg scrub with the following code
in ras2_hw_scrub_set_enabled_bg() when disable background scrub('enable' is 0 in this case). ps_sm->params.scrub_params_in &= ~RAS2_PATROL_SCRUB_EN_BACKGROUND; ps_sm->params.scrub_params_in |= FIELD_PREP(RAS2_PATROL_SCRUB_EN_BACKGROUND,

						    enable);

Hope it make sense?

Quoted 13 lines

Review

Quoted 3 lines
(quoted diff) +1 -0
+		ps_sm->params.patrol_scrub_command =

RAS2_START_PATROL_SCRUBBER;

Review

Quoted 2 lines
(quoted diff) +2 -0
+	} else {
+		ps_sm->params.patrol_scrub_command =

RAS2_STOP_PATROL_SCRUBBER;

drivers/ras/acpi_ras2.c

Quoted 5 lines
drivers/ras/acpi_ras2.c new file +5 -0
+	}
+
+	ret = ras2_send_pcc_cmd(ras2_ctx, RAS2_PCC_CMD_EXEC);
+	if (ret) {
+		dev_err(ras2_ctx->dev, "failed to enable(%d) the demand

scrubbing\n", enable);

Quoted 8 lines

drivers/ras/acpi_ras2.c line 255

Quoted 6 lines
drivers/ras/acpi_ras2.c new file +6 -0
+		return ret;
+	}
+	ras2_ctx->bg = false;
+
+	return ras2_update_patrol_scrub_params_cache(ras2_ctx);
+}

Thanks,
Shiju

#54 lore raw

Re: [RFC PATCH v8 10/10] ras: scrub: ACPI RAS2: Add memory ACPI RAS2 driver

Quoted 92 lines · Shiju Jose

Hi Daniel,

Thanks for the feedback.

Quoted 38 lines

-----Original Message-----
From: Daniel Ferguson <[email protected]>
Sent: 05 June 2024 22:33
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang <[email protected]>; Linuxarm <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC PATCH v8 10/10] ras: scrub: ACPI RAS2: Add memory ACPI RAS2 driver

drivers/ras/acpi_ras2.c

Quoted 13 lines
drivers/ras/acpi_ras2.c new file +6 -0
+/* Context - lock must be held */
+static int ras2_get_patrol_scrub_running(struct ras2_scrub_ctx *ras2_ctx,
+					 bool *running)
+{
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace-

pcc_comm_addr;

drivers/ras/acpi_ras2.c new file +6 -0
+	int ret;
+
+	if (ras2_ctx->bg)
+		*running = true;
+
+	ps_sm->common.set_capabilities[0] =

RAS2_SUPPORT_HW_PARTOL_SCRUB;

Quoted 7 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	ps_sm->params.patrol_scrub_command =

RAS2_GET_PATROL_PARAMETERS;

Need to reset the address range (base and size). A user may have previously called "Enable Background" where the code zeros out these parameters.

	ps_sm->params.requested_address_range[0] = ras2_ctx->base;
	ps_sm->params.requested_address_range[1] = ras2_ctx->size;

The address range is being set to the above in the ras2_hw_scrub_set_enabled_od(), because they are valid for on-demand scrubbing only.

However the ras2_ctx->base and ras2_ctx->size are set to the
ras2_ctx->base = ps_sm->params.actual_address_range[0];
ras2_ctx->size = ps_sm->params.actual_address_range[1];
in the ras2_update_patrol_scrub_params_cache(), which is called after enabling bg scrub and on-demand scrub.
Thus ras2_ctx->base and ras2_ctx->size may have a 0 or garbage value for bg scrub because address range is not valid for bg scrubbing as perc ACPI specification. I will add checks to retain the cached address range if bg scrub is enabled.

Quoted 26 lines
Quoted 26 lines
drivers/ras/acpi_ras2.c new file +24 -0
+
+	ret = ras2_send_pcc_cmd(ras2_ctx, RAS2_PCC_CMD_EXEC);
+	if (ret) {
+		dev_err(ras2_ctx->dev, "failed to read parameters\n");
+		return ret;
+	}
+
+	*running = ps_sm->params.flags &
+RAS2_PATROL_SCRUB_FLAG_SCRUBBER_RUNNING;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_write_rate(struct device *dev, u64 rate) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	bool running;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ret = ras2_get_patrol_scrub_running(ras2_ctx, &running);
+	if (ret)
+		return ret;
+
+	if (running)
Quoted 8 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		return -EBUSY;

I suggest we do not check if the patrol scrub is running when we are merely updating cached values. More importantly, if we had previously wrote an invalid value (that is only invalidated by firmware after executing a command), then when we try to write a correct value, this "ras2_get_patrol_scrub_running" check will always fail, therefore preventing us from correcting our error.

Quoted 1 line

In our opinion, write the rate and range etc, though updating the cached values, should be allowed only when the scrub is NOT running to avoid confusion thinking they are actually set in the running scrubber, when read them back in the userspace.

It may be that I didn't explain myself properly last time. Let me try again.

1) This driver code does not currently check to see if an 'addr_range_base' is valid or not. Validation occurs in the platform firmware, when either GET_PATROL_PARAMETERS or START_PATROL_SCRUBBER is executed. If our platform firmware detects an invalid address, it raises an error.

2) Therefore, a user can specify an invalid address, and the user will not know that the address is invalid until after the cached parameters (used to check if the patrol scrubber is running) are written to.

3) Now, every time the user attempts to write a value to either base, size, or rate; the preceding call to ras2_get_patrol_scrub_running will result in an error, and the attempt to write a different value fails.

To Conclude:
If a user specifies an invalid address, the only way to correct the invalid address is to reboot or module reload. To me, that seems like a show-stopper.

Quoted 163 lines
Quoted 46 lines
Quoted 46 lines
drivers/ras/acpi_ras2.c new file +45 -0
+
+	if (rate < ras2_ctx->rate_min || rate > ras2_ctx->rate_max)
+		return -EINVAL;
+
+	ras2_ctx->rate = rate;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_rate(struct device *dev, u64 *rate) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*rate = ras2_ctx->rate;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_rate_avail(struct device *dev, u64
+*min, u64 *max) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*min = ras2_ctx->rate_min;
+	*max = ras2_ctx->rate_max;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_range(struct device *dev, u64 *base,
+u64 *size) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*base = ras2_ctx->base;
+	*size = ras2_ctx->size;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_write_range(struct device *dev, u64 base,
+u64 size) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	bool running;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ret = ras2_get_patrol_scrub_running(ras2_ctx, &running);
Quoted 8 lines

drivers/ras/acpi_ras2.c line 108

Quoted 5 lines
drivers/ras/acpi_ras2.c new file +5 -0
+	if (ret)
+		return ret;
+
+	if (running)
+		return -EBUSY;

I suggest we do not check if the patrol scrub is running. See previous comment above.

Same as above.

Quoted 105 lines
Quoted 14 lines
drivers/ras/acpi_ras2.c new file +12 -0
+
+	ras2_ctx->base = base;
+	ras2_ctx->size = size;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_set_enabled_bg(struct device *dev, bool
+enable) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace-

pcc_comm_addr;

drivers/ras/acpi_ras2.c

Quoted 4 lines
drivers/ras/acpi_ras2.c new file +4 -0
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ps_sm->common.set_capabilities[0] =

RAS2_SUPPORT_HW_PARTOL_SCRUB;

drivers/ras/acpi_ras2.c

Quoted 4 lines
drivers/ras/acpi_ras2.c new file +4 -0
+	if (enable) {
+		ps_sm->params.requested_address_range[0] = 0;
+		ps_sm->params.requested_address_range[1] = 0;
+		ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_RATE_IN_MASK;

Review

Quoted 1 line
(quoted diff) +1 -0
+		ps_sm->params.scrub_params_in |=

FIELD_PREP(RAS2_PATROL_SCRUB_RATE_IN_MASK,

Review

Quoted 2 lines
(quoted diff) +2 -0
+							    ras2_ctx->rate);
+		ps_sm->params.patrol_scrub_command =

RAS2_START_PATROL_SCRUBBER;

Review

Quoted 2 lines
(quoted diff) +2 -0
+	} else {
+		ps_sm->params.patrol_scrub_command =

RAS2_STOP_PATROL_SCRUBBER;

Review

Quoted 2 lines
(quoted diff) +2 -0
+	}
+	ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_EN_BACKGROUND;

Review

Quoted 1 line
(quoted diff) +1 -0
+	ps_sm->params.scrub_params_in |=

FIELD_PREP(RAS2_PATROL_SCRUB_EN_BACKGROUND,

drivers/ras/acpi_ras2.c

Quoted 5 lines
drivers/ras/acpi_ras2.c new file +5 -0
+						    enable);
+
+	ret = ras2_send_pcc_cmd(ras2_ctx, RAS2_PCC_CMD_EXEC);
+	if (ret) {
+		dev_err(ras2_ctx->dev, "%s: failed to enable(%d) background

scrubbing\n",

drivers/ras/acpi_ras2.c

Quoted 6 lines
drivers/ras/acpi_ras2.c new file +6 -0
+			__func__, enable);
+		return ret;
+	}
+	ras2_ctx->bg = true;
+
+	/* Update the cache to account for rounding of supplied parameters and

similar */

Quoted 18 lines
drivers/ras/acpi_ras2.c new file +17 -0
+	return ras2_update_patrol_scrub_params_cache(ras2_ctx);
+}
+
+static int ras2_hw_scrub_get_enabled_bg(struct device *dev, bool
+*enabled) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*enabled = ras2_ctx->bg;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_set_enabled_od(struct device *dev, bool
+enable) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace-

pcc_comm_addr;

drivers/ras/acpi_ras2.c

Quoted 5 lines
drivers/ras/acpi_ras2.c new file +5 -0
+	bool enabled;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ps_sm->common.set_capabilities[0] =

RAS2_SUPPORT_HW_PARTOL_SCRUB;

drivers/ras/acpi_ras2.c

Quoted 4 lines
drivers/ras/acpi_ras2.c new file +4 -0
+	if (enable) {
+		if (!ras2_ctx->size) {
+			dev_warn(ras2_ctx->dev,
+				 "%s: Invalid requested address range,

requested_address_range[0]=0x%llx "

Review

Quoted 1 line
(quoted diff) +1 -0
+				 "requested_address_range[1]=0x%llx\n",

__func__,

drivers/ras/acpi_ras2.c

Quoted 12 lines
drivers/ras/acpi_ras2.c new file +12 -0
+				 ps_sm->params.requested_address_range[0],
+				 ps_sm->params.requested_address_range[1]);
+			return -ERANGE;
+		}
+		ret = ras2_get_patrol_scrub_running(ras2_ctx, &enabled);
+		if (ret)
+			return ret;
+
+		if (enabled)
+			return 0;
+
+		ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_RATE_IN_MASK;

Review

Quoted 1 line
(quoted diff) +1 -0
+		ps_sm->params.scrub_params_in |=

FIELD_PREP(RAS2_PATROL_SCRUB_RATE_IN_MASK,

drivers/ras/acpi_ras2.c line 241

Quoted 3 lines
drivers/ras/acpi_ras2.c new file +3 -0
+							    ras2_ctx->rate);
+		ps_sm->params.requested_address_range[0] = ras2_ctx->base;
+		ps_sm->params.requested_address_range[1] = ras2_ctx->size;

We need to clear the RAS2_PATROL_SCRUB_EN_BACKGROUND bit in the input parameters.
This is in case "Enable Background" was previously called, and this bit was set.

Quoted 2 lines
		ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_EN_BACKGROUND;

Quoted 8 lines

We need to stop background scrub if it is already running before start an on-demand scrubbing. The RAS2_PATROL_SCRUB_EN_BACKGROUND bit would be cleared with disable bg scrub with the following code
in ras2_hw_scrub_set_enabled_bg() when disable background scrub('enable' is 0 in this case). ps_sm->params.scrub_params_in &= ~RAS2_PATROL_SCRUB_EN_BACKGROUND; ps_sm->params.scrub_params_in |= FIELD_PREP(RAS2_PATROL_SCRUB_EN_BACKGROUND,

						    enable);

Hope it make sense?

Yes, this makes sense. But, on our platform, we automatically enable background when on-demand finishes(or is stopped). Similarly, if we enable on-demand then we automatically disable background. So, some sort of patrol is always on-going. The user is unable to turn them both off at the same time.

Due to our implementation choices, this causes some weirdness with how the driver represents enable_background and enable_on_demand independently, since our scrubbers are not independent.

I'm going to leave this conversation here for now, because on different platforms, maybe having independent control for background and on-demand is desired.

Quoted 23 lines
Quoted 13 lines

Review

Quoted 3 lines
(quoted diff) +1 -0
+		ps_sm->params.patrol_scrub_command =

RAS2_START_PATROL_SCRUBBER;

Review

Quoted 2 lines
(quoted diff) +2 -0
+	} else {
+		ps_sm->params.patrol_scrub_command =

RAS2_STOP_PATROL_SCRUBBER;

drivers/ras/acpi_ras2.c

Quoted 5 lines
drivers/ras/acpi_ras2.c new file +5 -0
+	}
+
+	ret = ras2_send_pcc_cmd(ras2_ctx, RAS2_PCC_CMD_EXEC);
+	if (ret) {
+		dev_err(ras2_ctx->dev, "failed to enable(%d) the demand

scrubbing\n", enable);

Quoted 8 lines

drivers/ras/acpi_ras2.c line 255

Quoted 6 lines
drivers/ras/acpi_ras2.c new file +6 -0
+		return ret;
+	}
+	ras2_ctx->bg = false;
+
+	return ras2_update_patrol_scrub_params_cache(ras2_ctx);
+}

Thanks,
Shiju

author
#55 lore raw

RE: [RFC PATCH v8 10/10] ras: scrub: ACPI RAS2: Add memory ACPI RAS2 driver

Quoted 136 lines

-----Original Message-----
From: Daniel Ferguson <[email protected]>
Sent: 21 June 2024 19:07
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang <[email protected]>; Linuxarm <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC PATCH v8 10/10] ras: scrub: ACPI RAS2: Add memory ACPI RAS2 driver

Quoted 14 lines · Shiju Jose

Hi Daniel,

Thanks for the feedback.

Quoted 10 lines

-----Original Message-----
From: Daniel Ferguson <[email protected]>
Sent: 05 June 2024 22:33
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected];

[email protected];

Quoted 6 lines
Quoted 6 lines

[email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>; [email protected]; wanghuiqiang

<[email protected]>;

Quoted 3 lines
Quoted 3 lines

Linuxarm <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Jonathan Cameron

<[email protected]>;

Quoted 20 lines
Quoted 20 lines

[email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC PATCH v8 10/10] ras: scrub: ACPI RAS2: Add memory ACPI
RAS2 driver

drivers/ras/acpi_ras2.c

Quoted 13 lines
drivers/ras/acpi_ras2.c new file +6 -0
+/* Context - lock must be held */
+static int ras2_get_patrol_scrub_running(struct ras2_scrub_ctx *ras2_ctx,
+					 bool *running)
+{
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace-

pcc_comm_addr;

drivers/ras/acpi_ras2.c new file +6 -0
+	int ret;
+
+	if (ras2_ctx->bg)
+		*running = true;
+
+	ps_sm->common.set_capabilities[0] =

RAS2_SUPPORT_HW_PARTOL_SCRUB;

Quoted 5 lines
Quoted 5 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	ps_sm->params.patrol_scrub_command =

RAS2_GET_PATROL_PARAMETERS;

Need to reset the address range (base and size). A user may have previously called "Enable Background" where the code zeros out these

parameters.

Quoted 4 lines
Quoted 2 lines
	ps_sm->params.requested_address_range[0] = ras2_ctx->base;
	ps_sm->params.requested_address_range[1] = ras2_ctx->size;

The address range is being set to the above in the ras2_hw_scrub_set_enabled_od(), because they are valid for on-demand

scrubbing only.

Quoted 5 lines

However the ras2_ctx->base and ras2_ctx->size are set to the ras2_ctx->base = ps_sm->params.actual_address_range[0]; ras2_ctx->size = ps_sm->params.actual_address_range[1];
in the ras2_update_patrol_scrub_params_cache(), which is called after

enabling bg scrub and on-demand scrub.

Quoted 1 line

Thus ras2_ctx->base and ras2_ctx->size may have a 0 or garbage value for bg

scrub because address range is not valid for bg scrubbing as perc ACPI specification. I will add checks to retain the cached address range if bg scrub is enabled.

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
drivers/ras/acpi_ras2.c new file +23 -0
+
+	ret = ras2_send_pcc_cmd(ras2_ctx, RAS2_PCC_CMD_EXEC);
+	if (ret) {
+		dev_err(ras2_ctx->dev, "failed to read parameters\n");
+		return ret;
+	}
+
+	*running = ps_sm->params.flags &
+RAS2_PATROL_SCRUB_FLAG_SCRUBBER_RUNNING;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_write_rate(struct device *dev, u64 rate) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	bool running;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ret = ras2_get_patrol_scrub_running(ras2_ctx, &running);
+	if (ret)
+		return ret;
+
Quoted 8 lines
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (running)
+		return -EBUSY;

I suggest we do not check if the patrol scrub is running when we are merely updating cached values. More importantly, if we had previously wrote an invalid value (that is only invalidated by firmware after executing a command), then when we try to write a correct value, this

"ras2_get_patrol_scrub_running"

Quoted 3 lines
Quoted 1 line

check will always fail, therefore preventing us from correcting our error.

In our opinion, write the rate and range etc, though updating the cached

values, should be allowed only when the scrub is NOT running to avoid confusion thinking they are actually set in the running scrubber, when read them back in the userspace.

Quoted 6 lines

It may be that I didn't explain myself properly last time. Let me try again.

1) This driver code does not currently check to see if an 'addr_range_base' is valid or not. Validation occurs in the platform firmware, when either GET_PATROL_PARAMETERS or START_PATROL_SCRUBBER is executed. If our platform firmware detects an invalid address, it raises an error.

Thanks for giving more details.

Quoted 8 lines

2) Therefore, a user can specify an invalid address, and the user will not know that the address is invalid until after the cached parameters (used to check if the patrol scrubber is running) are written to.

3) Now, every time the user attempts to write a value to either base, size, or rate; the preceding call to ras2_get_patrol_scrub_running will result in an error, and the attempt to write a different value fails.

Thanks for giving more details.
1. In write_range function, I added basic checks to make sure the address range base or size to set are non-zero and return error to the user if so. But unable to check those address parameters to set are in the supported address range of a scrub device because ACPI RAS2 Table 5.87: Parameter Block Structure for PATROL_SCRUB does have fields to publish supported address range for a patrol scrubber. The field "Actual Address Range (OUTPUT)" seems not suitable for this purpose because " The platform calculates the nearest patrol scrub boundary address from where it can start. This range should be a superset of the Requested Address Range." as described in the ACPI spec?

In ras2_hw_scrub_write_rate() , we already have check for scrub rate to set is within supported min and max scrub rates, set by the firmware in the above table in response to GET_PATROL_PARAMETERS.

2. You mentioned that "If our platform firmware detects an invalid address, it raises an error." In this case my understanding is that the scrubbing should not have started and thus the kernel will get the scrub NOT running status and the user will be able to correct the address?

Quoted 190 lines

To Conclude:
If a user specifies an invalid address, the only way to correct the invalid address is to reboot or module reload. To me, that seems like a show-stopper.

Quoted 54 lines
Quoted 46 lines
Quoted 46 lines
drivers/ras/acpi_ras2.c new file +45 -0
+
+	if (rate < ras2_ctx->rate_min || rate > ras2_ctx->rate_max)
+		return -EINVAL;
+
+	ras2_ctx->rate = rate;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_rate(struct device *dev, u64 *rate) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*rate = ras2_ctx->rate;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_rate_avail(struct device *dev, u64
+*min, u64 *max) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*min = ras2_ctx->rate_min;
+	*max = ras2_ctx->rate_max;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_read_range(struct device *dev, u64 *base,
+u64 *size) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*base = ras2_ctx->base;
+	*size = ras2_ctx->size;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_write_range(struct device *dev, u64 base,
+u64 size) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	bool running;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ret = ras2_get_patrol_scrub_running(ras2_ctx, &running);
Quoted 8 lines

drivers/ras/acpi_ras2.c line 108

Quoted 5 lines
drivers/ras/acpi_ras2.c new file +5 -0
+	if (ret)
+		return ret;
+
+	if (running)
+		return -EBUSY;

I suggest we do not check if the patrol scrub is running. See previous comment above.

Quoted 105 lines

Same as above.

Quoted 103 lines
Quoted 14 lines
drivers/ras/acpi_ras2.c new file +12 -0
+
+	ras2_ctx->base = base;
+	ras2_ctx->size = size;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_set_enabled_bg(struct device *dev, bool
+enable) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace-

pcc_comm_addr;

drivers/ras/acpi_ras2.c

Quoted 4 lines
drivers/ras/acpi_ras2.c new file +4 -0
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ps_sm->common.set_capabilities[0] =

RAS2_SUPPORT_HW_PARTOL_SCRUB;

drivers/ras/acpi_ras2.c

Quoted 4 lines
drivers/ras/acpi_ras2.c new file +4 -0
+	if (enable) {
+		ps_sm->params.requested_address_range[0] = 0;
+		ps_sm->params.requested_address_range[1] = 0;
+		ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_RATE_IN_MASK;

Review

Quoted 1 line
(quoted diff) +1 -0
+		ps_sm->params.scrub_params_in |=

FIELD_PREP(RAS2_PATROL_SCRUB_RATE_IN_MASK,

Review

Quoted 2 lines
(quoted diff) +2 -0
+							    ras2_ctx->rate);
+		ps_sm->params.patrol_scrub_command =

RAS2_START_PATROL_SCRUBBER;

Review

Quoted 2 lines
(quoted diff) +2 -0
+	} else {
+		ps_sm->params.patrol_scrub_command =

RAS2_STOP_PATROL_SCRUBBER;

Review

Quoted 2 lines
(quoted diff) +2 -0
+	}
+	ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_EN_BACKGROUND;

Review

Quoted 1 line
(quoted diff) +1 -0
+	ps_sm->params.scrub_params_in |=

FIELD_PREP(RAS2_PATROL_SCRUB_EN_BACKGROUND,

drivers/ras/acpi_ras2.c

Quoted 5 lines
drivers/ras/acpi_ras2.c new file +5 -0
+						    enable);
+
+	ret = ras2_send_pcc_cmd(ras2_ctx, RAS2_PCC_CMD_EXEC);
+	if (ret) {
+		dev_err(ras2_ctx->dev, "%s: failed to enable(%d) background

scrubbing\n",

drivers/ras/acpi_ras2.c

Quoted 7 lines
drivers/ras/acpi_ras2.c new file +7 -0
+			__func__, enable);
+		return ret;
+	}
+	ras2_ctx->bg = true;
+
+	/* Update the cache to account for rounding of supplied parameters
+and

similar */

Quoted 18 lines
drivers/ras/acpi_ras2.c new file +17 -0
+	return ras2_update_patrol_scrub_params_cache(ras2_ctx);
+}
+
+static int ras2_hw_scrub_get_enabled_bg(struct device *dev, bool
+*enabled) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+
+	*enabled = ras2_ctx->bg;
+
+	return 0;
+}
+
+static int ras2_hw_scrub_set_enabled_od(struct device *dev, bool
+enable) {
+	struct ras2_scrub_ctx *ras2_ctx = dev_get_drvdata(dev);
+	struct acpi_ras2_ps_shared_mem __iomem *ps_sm = (void *)
+					ras2_ctx->pcc_subspace-

pcc_comm_addr;

drivers/ras/acpi_ras2.c

Quoted 5 lines
drivers/ras/acpi_ras2.c new file +5 -0
+	bool enabled;
+	int ret;
+
+	guard(mutex)(&ras2_ctx->lock);
+	ps_sm->common.set_capabilities[0] =

RAS2_SUPPORT_HW_PARTOL_SCRUB;

drivers/ras/acpi_ras2.c

Quoted 4 lines
drivers/ras/acpi_ras2.c new file +4 -0
+	if (enable) {
+		if (!ras2_ctx->size) {
+			dev_warn(ras2_ctx->dev,
+				 "%s: Invalid requested address range,

requested_address_range[0]=0x%llx "

Review

Quoted 1 line
(quoted diff) +1 -0
+				 "requested_address_range[1]=0x%llx\n",

__func__,

drivers/ras/acpi_ras2.c

Quoted 12 lines
drivers/ras/acpi_ras2.c new file +12 -0
+				 ps_sm->params.requested_address_range[0],
+				 ps_sm->params.requested_address_range[1]);
+			return -ERANGE;
+		}
+		ret = ras2_get_patrol_scrub_running(ras2_ctx, &enabled);
+		if (ret)
+			return ret;
+
+		if (enabled)
+			return 0;
+
+		ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_RATE_IN_MASK;

Review

Quoted 1 line
(quoted diff) +1 -0
+		ps_sm->params.scrub_params_in |=

FIELD_PREP(RAS2_PATROL_SCRUB_RATE_IN_MASK,

drivers/ras/acpi_ras2.c line 241

Quoted 3 lines
drivers/ras/acpi_ras2.c new file +3 -0
+							    ras2_ctx->rate);
+		ps_sm->params.requested_address_range[0] = ras2_ctx->base;
+		ps_sm->params.requested_address_range[1] = ras2_ctx->size;

We need to clear the RAS2_PATROL_SCRUB_EN_BACKGROUND bit in the

input

Quoted 2 lines
Quoted 2 lines

parameters.
This is in case "Enable Background" was previously called, and this bit was

set.

Quoted 4 lines
Quoted 3 lines
		ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_EN_BACKGROUND;

We need to stop background scrub if it is already running before start an on-

demand scrubbing.

Quoted 1 line

The RAS2_PATROL_SCRUB_EN_BACKGROUND bit would be cleared with

disable

Quoted 3 lines

bg scrub with the following code in ras2_hw_scrub_set_enabled_bg() when disable background scrub('enable' is 0 in this case). ps_sm->params.scrub_params_in &=

~RAS2_PATROL_SCRUB_EN_BACKGROUND;

Quoted 1 line

ps_sm->params.scrub_params_in |=

FIELD_PREP(RAS2_PATROL_SCRUB_EN_BACKGROUND,

Quoted 2 lines
						    enable);

Hope it make sense?

Yes, this makes sense. But, on our platform, we automatically enable background when on-demand finishes(or is stopped). Similarly, if we enable on- demand then we automatically disable background. So, some sort of patrol is always on-going. The user is unable to turn them both off at the same time.

Quoted 7 lines

Due to our implementation choices, this causes some weirdness with how the driver represents enable_background and enable_on_demand independently, since our scrubbers are not independent.

I'm going to leave this conversation here for now, because on different platforms, maybe having independent control for background and on-demand is desired.

Ok. I added clearing the RAS2_PATROL_SCRUB_EN_BACKGROUND bit in the input parameters in ras2_hw_scrub_set_enabled_od () as you requested so that it will work in your platforms.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Review

Quoted 4 lines
(quoted diff) +1 -0
+		ps_sm->params.patrol_scrub_command =

RAS2_START_PATROL_SCRUBBER;

Thanks,
Shiju