Skip to contentSkip to conversation

CXL 2.0 Support

RFC PATCH Superseded Ben Widawsky 4 participants 35 messages Dec 9, 2020 – Jan 7, 2021

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover CXL 2.0 Support

Changes since v1 [1]

A few additions have been made:
- IOCTL (UAPI) interface has been added with commands
- Kernel docs have been created
- A new debug macro is introduced and sprinkled throughout.

A deletion was made:
- Removal of the non-standard _OSC UUID.

The detailed list of fixes is:
- fix cxl_register() no previous prototype warning (0day robot)
- s/REGLOG/REGLOC/ (Ben)
- Wait for doorbell on cxl_mem_mbox_get() and add comment on why (Ben)
- make "type-3" a proper adjective, add spec references, also did the same for the Kconfig (Bjorn)
- align some defines (Bjorn)
- s/bar/BAR (Bjorn)
- rename cxl_bus_prepared() to cxl_bus_acquire() (Bjorn)
- move definition of struct cxl_mem to "cxl/mem: Map memory device registers" (Bjorn)
- use consistent hex/decimal (Bjorn)
- use consistent upper/lower hex values (Bjorn)
- get rid of READ_ONCE (Bjorn)
- add offsets to debug messages (Bjorn)
- cleanup SPDX comment style (Bjorn, Christoph)
- change errors returned by case (Bjorn, Dan)
- 80 character violation cleanups (Christoph)
- cleanup CXL_BUS_PROVIDER dependencies (Christoph, Randy)
- remove "raw" from mmio functions (Dan)
- rename PCI_DVSEC_VENDOR_CXL to add _ID (Jonathan)
- combine introduction of mbox infrastruct and cxl_mem_identify() (Jonathan)
- add ABI documentation for sysfs attributes (Jonathan)
- document scope of cxl_memdev_lock (Jonathan)
- rework cxl_register() to have devm semantics (reaction to comments about cxl_mem_remove() and cxl_mem_add_memdev() semantics) (Jonathan)
- fix cxl_mem_exit() ordering (Jonathan)
- use GENMASK/GET_FIELD (Jonathan)
- fix and add comments for cap ids (Jonathan)
- use _OFFSET postfix in definitions (Jonathan)
- save pci_set_drvdata for later (Jonathan)

[1]: https://lore.kernel.org/linux-cxl/[email protected]/

---

Introduce support for “type-3” memory devices defined in the recently released Compute Express Link (CXL) 2.0 specification[2]. Specifically, these are the memory devices defined by section 8.2.8.5 of the CXL 2.0 spec. A reference implementation emulating these devices has been submitted to the QEMU mailing list and is available on gitlab [3]. “Type-3” is a CXL device that acts as a memory expander for RAM or PMEM. It might be interleaved with other CXL devices in a given physical address range.

These changes allow for foundational enumeration of CXL 2.0 memory devices as well as basic userspace interaction. The functionality present is:
- Initial driver bring-up
- Device enumeration and an initial sysfs representation
- Submit a basic firmware command via ‘mailbox’ to an emulated memory device with non-volatile capacity.
- Provide an interface to send "raw" commands to the hardware.

Some of the functionality that is still missing includes:
- Memory interleaving at the host bridge, root port, or switch level
- CXL 1.1 Root Complex Integrated Endpoint Support
- CXL 2.0 Hot plug support
- A bevy of supported device commands

In addition to the core functionality of discovering the spec defined registers and resources, introduce a CXL device model that will be the foundation for translating CXL capabilities into existing Linux infrastructure for Persistent Memory and other memory devices. For now, this only includes support for the management command mailbox that type-3 devices surface. These control devices fill the role of “DIMMs” / nmemX memory-devices in LIBNVDIMM terms.

Now, while implementing the driver some feedback for the specification was generated to cover perceived gaps and address conflicts. The feedback is presented as a reference implementation in the driver and QEMU emulation. Specifically the following concepts are original to the Linux implementation and feedback / collaboration is requested to develop these into specification proposals:
1. Top level ACPI object (ACPI0017)
2. HW imposed address space and interleave constraints

ACPI0017
--------
Introduce a new ACPI namespace device with an _HID of ACPI0017. The purpose of this object is twofold, support a legacy OS with a set of out-of-tree CXL modules, and establish an attach point for a driver that knows about interleaving. Both of these boil down to the same point, to centralize Operating System support for resources described by the CXL Early Discovery Table (CEDT).

The legacy OS problem stems from the spec's description of a host bridge, ACPI0016 is denoted as the _HID for host bridges, with a _CID of PNP0A08. In a CXL unaware version of Linux, the core ACPI subsystem will bind a driver to PNP0A08 and preclude a CXL-aware driver from binding to ACPI0016. An ACPI0017 device allows a standalone CXL-aware driver to register for handling / coordinating CEDT and CXL-specific _OSC control.

Similarly when managing interleaving there needs to be some management layer above the ACPI0016 device that is capable of assembling leaf nodes into interleave sets. As is the case with ACPI0012 that does this central coordination for NFIT defined resources, ACPI0017 does the same for CEDT described resources.

Memory Windows
-------
For CXL.mem capable platforms, there is a need for a mechanism for platform firmware to make the Operating System aware of any restrictions that hardware might have in address space. For example, in a system with 4 host bridges all participating in an interleave set, the firmware needs to provide some description of this. That information is missing from the CXL 2.0 spec as of today and it also is not implemented in the driver. A variety of ACPI based mechanisms, for example _CRS fields on the ACPI0017 device, were considered.

Next steps after this basic foundation is expanded command support and LIBNVDIMM integration. This is the initial “release early / release often” version of the Linux CXL enabling.

[2]: https://www.computeexpresslink.org/
[3]: https://gitlab.com/bwidawsk/qemu/-/tree/cxl-2.0v2

Ben Widawsky (10):
docs: cxl: Add basic documentation
cxl/mem: Map memory device registers
cxl/mem: Find device capabilities
cxl/mem: Implement polled mode mailbox
cxl/mem: Add basic IOCTL interface
cxl/mem: Add send command
cxl/mem: Add a "RAW" send command
cxl: Add basic debugging
MAINTAINERS: Add maintainers of the CXL driver
WIP/cxl/mem: Add get firmware for testing

Dan Williams (2):
cxl/mem: Introduce a driver for CXL-2.0-Type-3 endpoints cxl/mem: Register CXL memX devices

Vishal Verma (2):
cxl/acpi: Add an acpi_cxl module for the CXL interconnect cxl/acpi: add OSC support

 Documentation/ABI/testing/sysfs-bus-cxl |   26 +
 Documentation/cxl/index.rst             |   12 +
 Documentation/cxl/memory-devices.rst    |   51 ++
 Documentation/index.rst                 |    1 +
 MAINTAINERS                             |   10 +
 drivers/Kconfig                         |    1 +
 drivers/Makefile                        |    1 +
 drivers/cxl/Kconfig                     |   58 ++
 drivers/cxl/Makefile                    |    9 +
 drivers/cxl/acpi.c                      |  352 ++++++++
 drivers/cxl/acpi.h                      |   35 +
 drivers/cxl/bus.c                       |   54 ++
 drivers/cxl/bus.h                       |    8 +
 drivers/cxl/cxl.h                       |  188 +++++
 drivers/cxl/mem.c                       | 1022 +++++++++++++++++++++++
 drivers/cxl/pci.h                       |   34 +
 include/acpi/actbl1.h                   |   51 ++
 include/uapi/linux/cxl_mem.h            |  148 ++++
 18 files changed, 2061 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-cxl
 create mode 100644 Documentation/cxl/index.rst
 create mode 100644 Documentation/cxl/memory-devices.rst
 create mode 100644 drivers/cxl/Kconfig
 create mode 100644 drivers/cxl/Makefile
 create mode 100644 drivers/cxl/acpi.c
 create mode 100644 drivers/cxl/acpi.h
 create mode 100644 drivers/cxl/bus.c
 create mode 100644 drivers/cxl/bus.h
 create mode 100644 drivers/cxl/cxl.h
 create mode 100644 drivers/cxl/mem.c
 create mode 100644 drivers/cxl/pci.h
 create mode 100644 include/uapi/linux/cxl_mem.h
Signature · 1 line
2.29.2
#1 lore raw

RFC PATCH v2 1/14 docs: cxl: Add basic documentation

This starts a new cxl directory because CXL is a new bus and it is expected that the documentation will grow large over time. Currently only memory devices are documented as they are all that's supported.

---
 Documentation/cxl/index.rst          | 12 ++++++++++++
 Documentation/cxl/memory-devices.rst | 15 +++++++++++++++
 Documentation/index.rst              |  1 +
 3 files changed, 28 insertions(+)
 create mode 100644 Documentation/cxl/index.rst
 create mode 100644 Documentation/cxl/memory-devices.rst
Documentation/cxl/index.rst new file +12 -0
new file mode 100644
index 000000000000..036e49553542
@@ -0,0 +1,12 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+====================
+Compute Express Link
+====================
+
+.. toctree::
+   :maxdepth: 1
+
+   memory-devices
+
+.. only::  subproject and html
Documentation/cxl/memory-devices.rst new file +15 -0
new file mode 100644
index 000000000000..aa4262280c67
@@ -0,0 +1,15 @@
+.. SPDX-License-Identifier: GPL-2.0
+.. include:: <isonum.txt>
+
+===================================
+Compute Express Link Memory Devices
+===================================
+
+A Compute Express Link Memory Device is a CXL component that implements the
+CXL.mem protocol. It contains some amount of volatile memory, persistent memory,
+or both.
+
+Driver Infrastructure
+=====================
+
+This sections covers the driver infrastructure for a CXL memory device.
Documentation/index.rst +1 -0
index 57719744774c..38678f6c5676 100644
@@ -137,6 +137,7 @@ needed).
    misc-devices/index
    scheduler/index
    mhi/index
+   cxl/index
 
 Architecture-agnostic documentation
 -----------------------------------
Signature · 1 line
2.29.2
#2 lore raw

RFC PATCH v2 2/14 cxl/acpi: Add an acpi_cxl module for the CXL interconnect

In reply to Ben Widawsky

From: Vishal Verma <[email protected]>

Add an acpi_cxl module to coordinate the ACPI portions of the CXL (Compute eXpress Link) interconnect. This driver binds to ACPI0017 objects in the ACPI tree, and coordinates access to the resources provided by the ACPI CEDT (CXL Early Discovery Table).

It also coordinates operations of the root port _OSC object to notify platform firmware that the OS has native support for the CXL capabilities of endpoints.

Note: the actbl1.h changes are speculative. The expectation is that they will arrive through the ACPICA tree in due time.

---
 drivers/Kconfig       |  1 +
 drivers/Makefile      |  1 +
 drivers/cxl/Kconfig   | 36 ++++++++++++++++
 drivers/cxl/Makefile  |  5 +++
 drivers/cxl/acpi.c    | 97 +++++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/acpi.h    | 15 +++++++
 include/acpi/actbl1.h | 51 +++++++++++++++++++++++
 7 files changed, 206 insertions(+)
 create mode 100644 drivers/cxl/Kconfig
 create mode 100644 drivers/cxl/Makefile
 create mode 100644 drivers/cxl/acpi.c
 create mode 100644 drivers/cxl/acpi.h
drivers/Kconfig +1 -0
index dcecc9f6e33f..62c753a73651 100644
@@ -6,6 +6,7 @@ menu "Device Drivers"
 source "drivers/amba/Kconfig"
 source "drivers/eisa/Kconfig"
 source "drivers/pci/Kconfig"
+source "drivers/cxl/Kconfig"
 source "drivers/pcmcia/Kconfig"
 source "drivers/rapidio/Kconfig"
 
drivers/Makefile +1 -0
index c0cd1b9075e3..5dad349de73b 100644
@@ -73,6 +73,7 @@ obj-$(CONFIG_NVM)		+= lightnvm/
 obj-y				+= base/ block/ misc/ mfd/ nfc/
 obj-$(CONFIG_LIBNVDIMM)		+= nvdimm/
 obj-$(CONFIG_DAX)		+= dax/
+obj-$(CONFIG_CXL_BUS)		+= cxl/
 obj-$(CONFIG_DMA_SHARED_BUFFER) += dma-buf/
 obj-$(CONFIG_NUBUS)		+= nubus/
 obj-y				+= macintosh/
drivers/cxl/Kconfig new file +36 -0
new file mode 100644
index 000000000000..68da926ba5b1
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: GPL-2.0-only
+menuconfig CXL_BUS
+	tristate "CXL (Compute Express Link) Devices Support"
+	help
+	  CXL is a bus that is electrically compatible with PCI-E, but layers
+	  three protocols on that signalling (CXL.io, CXL.cache, and CXL.mem).
+	  The CXL.cache protocol allows devices to hold cachelines locally, the
+	  CXL.mem protocol allows devices to be fully coherent memory targets,
+	  the CXL.io protocol is equivalent to PCI-E. Say 'y' to enable support
+	  for the configuration and management of devices supporting these
+	  protocols.
+
+if CXL_BUS
+
+config CXL_BUS_PROVIDER
+	tristate
+
+config CXL_ACPI
+	tristate "CXL ACPI: Platform Support"
+	depends on ACPI
+	default CXL_BUS
+	select CXL_BUS_PROVIDER
+	help
+	  Say 'y/m' to enable a driver (named "cxl_acpi.ko" when built
+	  as a module) that will enable support for CXL.mem endpoint
+	  devices. In general, CXL Platform Support is a prerequisite
+	  for any CXL device driver that wants to claim ownership of a
+	  component register space. By default platform firmware assumes
+	  Linux is unaware of CXL capabilities and requires explicit
+	  opt-in. This platform component also mediates resources
+	  described by the CEDT (CXL Early Discovery Table).  See
+	  Chapter 9.14.1 CXL Early Discovery Table (CEDT) in the CXL 2.0
+	  specification.
+
+	  If unsure say 'm'
+endif
drivers/cxl/Makefile new file +5 -0
new file mode 100644
index 000000000000..d38cd34a2582
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
+
+ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=CXL
+cxl_acpi-y := acpi.o
drivers/cxl/acpi.c new file +97 -0

97 lines not on this page · raw message

drivers/cxl/acpi.h new file +15 -0
new file mode 100644
index 000000000000..d638f8886ab7
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright(c) 2020 Intel Corporation. All rights reserved. */
+
+#ifndef __CXL_ACPI_H__
+#define __CXL_ACPI_H__
+#include <linux/acpi.h>
+
+struct cxl_acpi_desc {
+	struct acpi_table_header acpi_header;
+	struct device *dev;
+};
+
+int cxl_bus_acquire(struct pci_dev *pci_dev);
+
+#endif	/* __CXL_ACPI_H__ */
include/acpi/actbl1.h +51 -0

63 lines not on this page · raw message

Signature · 1 line
2.29.2
#3 lore raw

RFC PATCH v2 3/14 cxl/acpi: add OSC support

In reply to Ben Widawsky

From: Vishal Verma <[email protected]>

Add support to advertise OS capabilities, and request OS control for CXL features using the ACPI _OSC mechanism. Advertise support for all possible CXL features, and attempt to request control for all possible features.

Based on a patch by Sean Kelley.

---
 Documentation/cxl/memory-devices.rst |  15 ++
 drivers/cxl/acpi.c                   | 261 ++++++++++++++++++++++++++-
 drivers/cxl/acpi.h                   |  20 ++
 3 files changed, 293 insertions(+), 3 deletions(-)
Documentation/cxl/memory-devices.rst +15 -0
index aa4262280c67..6ce88f9d5f4f 100644
@@ -13,3 +13,18 @@ Driver Infrastructure
 =====================
 
 This sections covers the driver infrastructure for a CXL memory device.
+
+ACPI CXL
+--------
+
+.. kernel-doc:: drivers/cxl/acpi.c
+   :doc: cxl acpi
+
+.. kernel-doc:: drivers/cxl/acpi.c
+   :internal:
+
+External Interfaces
+===================
+
+.. kernel-doc:: drivers/cxl/acpi.c
+   :export:
drivers/cxl/acpi.c +258 -3

280 lines not on this page · raw message

drivers/cxl/acpi.h +20 -0
index d638f8886ab7..6ef154021745 100644
@@ -12,4 +12,24 @@ struct cxl_acpi_desc {
 
 int cxl_bus_acquire(struct pci_dev *pci_dev);
 
+/* Indexes into _OSC Capabilities Buffer */
+#define PCI_OSC_SUPPORT_DWORD			1	/* DWORD 2 */
+#define PCI_OSC_CONTROL_DWORD			2	/* DWORD 3 */
+#define CXL_OSC_SUPPORT_DWORD			3	/* DWORD 4 */
+#define CXL_OSC_CONTROL_DWORD			4	/* DWORD 5 */
+
+/* CXL Host Bridge _OSC: Capabilities DWORD 4: Support Field */
+#define CXL_OSC_PORT_REG_ACCESS_SUPPORT		0x00000001
+#define CXL_OSC_PORT_DEV_REG_ACCESS_SUPPORT	0x00000002
+#define CXL_OSC_PER_SUPPORT			0x00000004
+#define CXL_OSC_NATIVE_HP_SUPPORT		0x00000008
+#define CXL_OSC_SUPPORT_VALID_MASK		(CXL_OSC_PORT_REG_ACCESS_SUPPORT |	\
+						 CXL_OSC_PORT_DEV_REG_ACCESS_SUPPORT |	\
+						 CXL_OSC_PER_SUPPORT |			\
+						 CXL_OSC_NATIVE_HP_SUPPORT)
+
+/* CXL Host Bridge _OSC: Capabilities DWORD 5: Control Field */
+#define CXL_OSC_MEM_ERROR_CONTROL		0x00000001
+#define CXL_OSC_CONTROL_VALID_MASK		(CXL_OSC_MEM_ERROR_CONTROL)
+
 #endif	/* __CXL_ACPI_H__ */
Signature · 1 line
2.29.2
#4 lore raw

RFC PATCH v2 4/14 cxl/mem: Introduce a driver for CXL-2.0-Type-3 endpoints

In reply to Ben Widawsky

From: Dan Williams <[email protected]>

The CXL.mem protocol allows a device to act as a provider of "System RAM" and/or "Persistent Memory" that is fully coherent as if the memory was attached to the typical CPU memory controller.

With the CXL-2.0 specification a PCI endpoint can implement a "Type-3" device interface and give the operating system control over "Host Managed Device Memory". See section 2.3 Type 3 CXL Device.

The memory range exported by the device may optionally be described by the platform firmware memory map, or by infrastructure like LIBNVDIMM to provision persistent memory capacity from one, or more, CXL.mem devices.

A pre-requisite for Linux-managed memory-capacity provisioning is this cxl_mem driver that can speak the mailbox protocol defined in section 8.2.8.4 Mailbox Registers.

For now just land the driver boiler-plate and fill it in with functionality in subsequent commits.

---
 Documentation/cxl/memory-devices.rst |  9 ++++
 drivers/cxl/Kconfig                  | 22 +++++++++
 drivers/cxl/Makefile                 |  2 +
 drivers/cxl/mem.c                    | 69 ++++++++++++++++++++++++++++
 drivers/cxl/pci.h                    | 20 ++++++++
 5 files changed, 122 insertions(+)
 create mode 100644 drivers/cxl/mem.c
 create mode 100644 drivers/cxl/pci.h
Documentation/cxl/memory-devices.rst +9 -0
index 6ce88f9d5f4f..134c9b6b4ff4 100644
@@ -23,6 +23,15 @@ ACPI CXL
 .. kernel-doc:: drivers/cxl/acpi.c
    :internal:
 
+CXL Memory Device
+-----------------
+
+.. kernel-doc:: drivers/cxl/mem.c
+   :doc: cxl mem
+
+.. kernel-doc:: drivers/cxl/mem.c
+   :internal:
+
 External Interfaces
 ===================
 
drivers/cxl/Kconfig +22 -0
index 68da926ba5b1..0ac5080cd6e0 100644
@@ -33,4 +33,26 @@ config CXL_ACPI
 	  specification.
 
 	  If unsure say 'm'
+
+config CXL_MEM
+        tristate "CXL.mem: Endpoint Support"
+        depends on PCI && CXL_BUS_PROVIDER
+        default CXL_BUS_PROVIDER
+        help
+          The CXL.mem protocol allows a device to act as a provider of
+          "System RAM" and/or "Persistent Memory" that is fully coherent
+          as if the memory was attached to the typical CPU memory
+          controller.
+
+	  Say 'y/m' to enable a driver (named "cxl_mem.ko" when built as
+	  a module) that will attach to CXL.mem devices for
+	  configuration, provisioning, and health monitoring. This
+	  driver is required for dynamic provisioning of CXL.mem
+	  attached memory which is a pre-requisite for persistent memory
+	  support. Typically volatile memory is mapped by platform
+	  firmware and included in the platform memory map, but in some
+	  cases the OS is responsible for mapping that memory. See
+	  Chapter 2.3 Type 3 CXL Device in the CXL 2.0 specification.
+
+          If unsure say 'm'.
 endif
drivers/cxl/Makefile +2 -0
index d38cd34a2582..97fdffb00f2d 100644
@@ -1,5 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
+obj-$(CONFIG_CXL_MEM) += cxl_mem.o
 
 ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=CXL
 cxl_acpi-y := acpi.o
+cxl_mem-y := mem.o
drivers/cxl/mem.c new file +69 -0

69 lines not on this page · raw message

drivers/cxl/pci.h new file +20 -0
new file mode 100644
index 000000000000..a8a9935fa90b
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright(c) 2020 Intel Corporation. All rights reserved. */
+#ifndef __CXL_PCI_H__
+#define __CXL_PCI_H__
+
+#define PCI_CLASS_MEMORY_CXL	0x050210
+
+/*
+ * See section 8.1 Configuration Space Registers in the CXL 2.0
+ * Specification
+ */
+#define PCI_EXT_CAP_ID_DVSEC		0x23
+#define PCI_DVSEC_VENDOR_ID_CXL		0x1E98
+#define PCI_DVSEC_VENDOR_ID_OFFSET	0x4
+#define PCI_DVSEC_ID_CXL		0x0
+#define PCI_DVSEC_ID_OFFSET		0x8
+
+#define PCI_DVSEC_ID_CXL_REGLOC		0x8
+
+#endif /* __CXL_PCI_H__ */
Signature · 1 line
2.29.2
#5 lore raw

RFC PATCH v2 5/14 cxl/mem: Map memory device registers

In reply to Ben Widawsky

All the necessary bits are initialized in order to find and map the register space for CXL Memory Devices. This is accomplished by using the Register Locator DVSEC (CXL 2.0 - 8.1.9.1) to determine which PCI BAR to use, and how much of an offset from that BAR should be added.

If the memory device registers are found and mapped a new internal data structure tracking device state is allocated.

---
 drivers/cxl/cxl.h | 17 ++++++++++
 drivers/cxl/mem.c | 83 +++++++++++++++++++++++++++++++++++++++++++++--
 drivers/cxl/pci.h | 14 ++++++++
 3 files changed, 112 insertions(+), 2 deletions(-)
 create mode 100644 drivers/cxl/cxl.h
drivers/cxl/cxl.h new file +17 -0
new file mode 100644
index 000000000000..d81d0ba4617c
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright(c) 2020 Intel Corporation. */
+
+#ifndef __CXL_H__
+#define __CXL_H__
+
+/**
+ * struct cxl_mem - A CXL memory device
+ * @pdev: The PCI device associated with this CXL device.
+ * @regs: IO mappings to the device's MMIO
+ */
+struct cxl_mem {
+	struct pci_dev *pdev;
+	void __iomem *regs;
+};
+
+#endif
drivers/cxl/mem.c +81 -2

107 lines not on this page · raw message

drivers/cxl/pci.h +14 -0
index a8a9935fa90b..df222edb6ac3 100644
@@ -17,4 +17,18 @@
 
 #define PCI_DVSEC_ID_CXL_REGLOC		0x8
 
+/* BAR Indicator Register (BIR) */
+#define CXL_REGLOC_BIR_SHIFT 0
+#define CXL_REGLOC_BIR_MASK 0x7
+
+/* Register Block Identifier (RBI) */
+#define CXL_REGLOC_RBI_SHIFT 8
+#define CXL_REGLOC_RBI_MASK 0xff
+#define CXL_REGLOC_RBI_EMPTY 0
+#define CXL_REGLOC_RBI_COMPONENT 1
+#define CXL_REGLOC_RBI_VIRT 2
+#define CXL_REGLOC_RBI_MEMDEV 3
+
+#define CXL_REGLOC_ADDR_MASK 0xffff0000
+
 #endif /* __CXL_PCI_H__ */
Signature · 1 line
2.29.2
#6 lore raw

RFC PATCH v2 6/14 cxl/mem: Find device capabilities

In reply to Ben Widawsky

CXL devices contain an array of capabilities that describe the interactions software can interact with the device, or firmware running on the device. A CXL compliant device must implement the device status and the mailbox capability. A CXL compliant memory device must implement the memory device capability.

Each of the capabilities can [will] provide an offset within the MMIO region for interacting with the CXL device.

---
 drivers/cxl/cxl.h | 92 ++++++++++++++++++++++++++++++++++++++++++++++-
 drivers/cxl/mem.c | 74 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 164 insertions(+), 2 deletions(-)
drivers/cxl/cxl.h +91 -1
index d81d0ba4617c..91bb40f73ddc 100644
@@ -4,6 +4,38 @@
 #ifndef __CXL_H__
 #define __CXL_H__
 
+#include <linux/bitfield.h>
+#include <linux/bitops.h>
+#include <linux/io.h>
+
+#define CXL_SET_FIELD(value, field)                                            \
+	({                                                                     \
+		WARN_ON(!FIELD_FIT(field##_MASK, value));                      \
+		FIELD_PREP(field##_MASK, value);                               \
+	})
+
+#define CXL_GET_FIELD(word, field) FIELD_GET(field##_MASK, word)
+
+/* Device */
+#define CXLDEV_CAP_ARRAY_REG 0x0
+#define CXLDEV_CAP_ARRAY_CAP_ID 0
+#define CXLDEV_CAP_ARRAY_ID(x) ((x) & (0xffff))
+#define CXLDEV_CAP_ARRAY_COUNT(x) (((x) >> 32) & 0xffff)
+
+#define CXL_CAP_CAP_ID_DEVICE_STATUS 0x1
+#define CXL_CAP_CAP_ID_PRIMARY_MAILBOX 0x2
+#define CXL_CAP_CAP_ID_SECONDARY_MAILBOX 0x3
+#define CXL_CAP_CAP_ID_MEMDEV 0x4000
+
+/* Mailbox (CXL 2.0 - 8.2.8.4) */
+#define CXLDEV_MB_CAPS_OFFSET 0x00
+#define   CXLDEV_MB_CAP_PAYLOAD_SIZE_MASK GENMASK(5, 0)
+#define   CXLDEV_MB_CAP_PAYLOAD_SIZE_SHIFT 0
+#define CXLDEV_MB_CTRL_OFFSET 0x04
+#define CXLDEV_MB_CMD_OFFSET 0x08
+#define CXLDEV_MB_STATUS_OFFSET 0x10
+#define CXLDEV_MB_BG_CMD_STATUS_OFFSET 0x18
+
 /**
  * struct cxl_mem - A CXL memory device
  * @pdev: The PCI device associated with this CXL device.
@@ -12,6 +44,64 @@
 struct cxl_mem {
 	struct pci_dev *pdev;
 	void __iomem *regs;
+
+	/* Cap 0001h - CXL_CAP_CAP_ID_DEVICE_STATUS */
+	struct {
+		void __iomem *regs;
+	} status;
+
+	/* Cap 0002h - CXL_CAP_CAP_ID_PRIMARY_MAILBOX */
+	struct {
+		void __iomem *regs;
+		size_t payload_size;
+	} mbox;
+
+	/* Cap 4000h - CXL_CAP_CAP_ID_MEMDEV */
+	struct {
+		void __iomem *regs;
+	} mem;
 };
 
-#endif
+#define cxl_reg(type)                                                          \
+	static inline void cxl_write_##type##_reg32(struct cxl_mem *cxlm,      \
+						    u32 reg, u32 value)        \
+	{                                                                      \
+		void __iomem *reg_addr = cxlm->type.regs;                      \
+		writel(value, reg_addr + reg);                                 \
+	}                                                                      \
+	static inline void cxl_write_##type##_reg64(struct cxl_mem *cxlm,      \
+						    u32 reg, u64 value)        \
+	{                                                                      \
+		void __iomem *reg_addr = cxlm->type.regs;                      \
+		writeq(value, reg_addr + reg);                                 \
+	}                                                                      \
+	static inline u32 cxl_read_##type##_reg32(struct cxl_mem *cxlm,        \
+						  u32 reg)                     \
+	{                                                                      \
+		void __iomem *reg_addr = cxlm->type.regs;                      \
+		return readl(reg_addr + reg);                                  \
+	}                                                                      \
+	static inline u64 cxl_read_##type##_reg64(struct cxl_mem *cxlm,        \
+						  u32 reg)                     \
+	{                                                                      \
+		void __iomem *reg_addr = cxlm->type.regs;                      \
+		return readq(reg_addr + reg);                                  \
+	}
+
+cxl_reg(status);
+cxl_reg(mbox);
+
+static inline u32 __cxl_read_reg32(struct cxl_mem *cxlm, u32 reg)
+{
+	void __iomem *reg_addr = cxlm->regs;
+
+	return readl(reg_addr + reg);
+}
+
+static inline u64 __cxl_read_reg64(struct cxl_mem *cxlm, u32 reg)
+{
+	void __iomem *reg_addr = cxlm->regs;
+
+	return readq(reg_addr + reg);
+}
+#endif /* __CXL_H__ */
drivers/cxl/mem.c +73 -1

86 lines not on this page · raw message

Signature · 1 line
2.29.2
#7 lore raw

RFC PATCH v2 7/14 cxl/mem: Implement polled mode mailbox

In reply to Ben Widawsky

Provide enough functionality to utilize the mailbox of a memory device. The mailbox is used to interact with the firmware running on the memory device.

The CXL specification defines separate capabilities for the mailbox and the memory device. While we can confirm the mailbox is ready, in order to actually interact with the memory device, you must also confirm the device's firmware is ready.

Create a function to handle sending a command, optionally with a payload, to the memory device, polling on a result, and then optionally copying out the payload. The algorithm for doing this comes straight out of the CXL 2.0 specification.

Primary mailboxes are capable of generating an interrupt when submitting a command in the background. That implementation is saved for a later time.

Secondary mailboxes aren't implemented at this time.

The flow is proven with one implemented command, "identify".

---
 drivers/cxl/cxl.h |  75 +++++++++++++
 drivers/cxl/mem.c | 265 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 339 insertions(+), 1 deletion(-)
drivers/cxl/cxl.h +75 -0
index 91bb40f73ddc..6d641309013d 100644
@@ -32,9 +32,38 @@
 #define   CXLDEV_MB_CAP_PAYLOAD_SIZE_MASK GENMASK(5, 0)
 #define   CXLDEV_MB_CAP_PAYLOAD_SIZE_SHIFT 0
 #define CXLDEV_MB_CTRL_OFFSET 0x04
+#define   CXLDEV_MB_CTRL_DOORBELL BIT(0)
 #define CXLDEV_MB_CMD_OFFSET 0x08
+#define   CXLDEV_MB_CMD_PAYLOAD_LENGTH_SHIFT 16
+#define   CXLDEV_MB_CMD_PAYLOAD_LENGTH_MASK GENMASK(36, 16)
 #define CXLDEV_MB_STATUS_OFFSET 0x10
+#define   CXLDEV_MB_STATUS_RET_CODE_SHIFT 32
+#define   CXLDEV_MB_STATUS_RET_CODE_MASK GENMASK(47, 32)
 #define CXLDEV_MB_BG_CMD_STATUS_OFFSET 0x18
+#define CXLDEV_MB_PAYLOAD_OFFSET 0x20
+
+/* Memory Device (CXL 2.0 - 8.2.8.5.1.1) */
+#define CXLMDEV_STATUS_OFFSET 0x0
+#define   CXLMDEV_DEV_FATAL BIT(0)
+#define   CXLMDEV_FW_HALT BIT(1)
+#define   CXLMDEV_STATUS_MEDIA_STATUS_SHIFT 2
+#define   CXLMDEV_STATUS_MEDIA_STATUS_MASK GENMASK(3, 2)
+#define     CXLMDEV_MS_NOT_READY 0
+#define     CXLMDEV_MS_READY 1
+#define     CXLMDEV_MS_ERROR 2
+#define     CXLMDEV_MS_DISABLED 3
+#define   CXLMDEV_READY(status) \
+		(CXL_GET_FIELD(status, CXLMDEV_STATUS_MEDIA_STATUS) == CXLMDEV_MS_READY)
+#define   CXLMDEV_MBOX_IF_READY BIT(4)
+#define   CXLMDEV_RESET_NEEDED_SHIFT 5
+#define   CXLMDEV_RESET_NEEDED_MASK GENMASK(7, 5)
+#define     CXLMDEV_RESET_NEEDED_NOT 0
+#define     CXLMDEV_RESET_NEEDED_COLD 1
+#define     CXLMDEV_RESET_NEEDED_WARM 2
+#define     CXLMDEV_RESET_NEEDED_HOT 3
+#define     CXLMDEV_RESET_NEEDED_CXL 4
+#define   CXLMDEV_RESET_NEEDED(status) \
+		(CXL_GET_FIELD(status, CXLMDEV_RESET_NEEDED) != CXLMDEV_RESET_NEEDED_NOT)
 
 /**
  * struct cxl_mem - A CXL memory device
@@ -45,6 +74,18 @@ struct cxl_mem {
 	struct pci_dev *pdev;
 	void __iomem *regs;
 
+	spinlock_t mbox_lock; /* Protects device mailbox and firmware */
+
+	struct {
+		struct range range;
+	} pmem;
+
+	struct {
+		struct range range;
+	} ram;
+
+	char firmware_version[0x10];
+
 	/* Cap 0001h - CXL_CAP_CAP_ID_DEVICE_STATUS */
 	struct {
 		void __iomem *regs;
@@ -90,6 +131,7 @@ struct cxl_mem {
 
 cxl_reg(status);
 cxl_reg(mbox);
+cxl_reg(mem);
 
 static inline u32 __cxl_read_reg32(struct cxl_mem *cxlm, u32 reg)
 {
@@ -104,4 +146,37 @@ static inline u64 __cxl_read_reg64(struct cxl_mem *cxlm, u32 reg)
 
 	return readq(reg_addr + reg);
 }
+
+static inline void cxl_mbox_payload_fill(struct cxl_mem *cxlm, u8 *input,
+					 unsigned int length)
+{
+	memcpy_toio(cxlm->mbox.regs + CXLDEV_MB_PAYLOAD_OFFSET, input, length);
+}
+
+static inline void cxl_mbox_payload_drain(struct cxl_mem *cxlm, u8 *output,
+					  unsigned int length)
+{
+	memcpy_fromio(output, cxlm->mbox.regs + CXLDEV_MB_PAYLOAD_OFFSET,
+		      length);
+}
+
+#define CXL_MBOX_IDENTIFY 0x4000
+
+struct cxl_mbox_identify {
+	char fw_revision[0x10];
+	__le64 total_capacity;
+	__le64 volatile_capacity;
+	__le64 persistent_capacity;
+	__le64 partition_align;
+	__le16 info_event_log_size;
+	__le16 warning_event_log_size;
+	__le16 failure_event_log_size;
+	__le16 fatal_event_log_size;
+	__le32 lsa_size;
+	u8 poison_list_max_mer[3];
+	__le16 inject_poison_limit;
+	u8 poison_caps;
+	u8 qos_telemetry_caps;
+} __packed;
+
 #endif /* __CXL_H__ */
drivers/cxl/mem.c +264 -1

294 lines not on this page · raw message

Signature · 1 line
2.29.2
#8 lore raw

Re: [RFC PATCH v2 07/14] cxl/mem: Implement polled mode mailbox

On 20-12-08 16:24:11, Ben Widawsky wrote:

[snip]

Quoted 23 lines
drivers/cxl/mem.c +23 -0
+static int cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm,
+				 struct mbox_cmd *mbox_cmd)
+{
+	u64 cmd, status;
+	size_t out_len;
+	int rc;
+
+	lockdep_assert_held(&cxlm->mbox_lock);
+
+	/*
+	 * Here are the steps from 8.2.8.4 of the CXL 2.0 spec.
+	 *   1. Caller reads MB Control Register to verify doorbell is clear
+	 *   2. Caller writes Command Register
+	 *   3. Caller writes Command Payload Registers if input payload is non-empty
+	 *   4. Caller writes MB Control Register to set doorbell
+	 *   5. Caller either polls for doorbell to be clear or waits for interrupt if configured
+	 *   6. Caller reads MB Status Register to fetch Return code
+	 *   7. If command successful, Caller reads Command Register to get Payload Length
+	 *   8. If output payload is non-empty, host reads Command Payload Registers
+	 */
+
+	/* #1 */
+	WARN_ON(cxl_doorbell_busy(cxlm));

drivers/cxl/mem.c line 97

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+
+	/* #2 */
+	cxl_write_mbox_reg64(cxlm, CXLDEV_MB_CMD_OFFSET, mbox_cmd->opcode);
+
+	if (mbox_cmd->size_in) {
+		/* #3 */
+		CXL_SET_FIELD(mbox_cmd->size_in, CXLDEV_MB_CMD_PAYLOAD_LENGTH);
+		cxl_mbox_payload_fill(cxlm, mbox_cmd->payload,
+				      mbox_cmd->size_in);
+	}

There is a bug here where the payload length isn't written on input. It was working in v1. It will be fixed in v3.

[snip]

#9 lore raw

RFC PATCH v2 8/14 cxl/mem: Register CXL memX devices

In reply to Ben Widawsky

From: Dan Williams <[email protected]>

Create the /sys/bus/cxl hierarchy to enumerate memory devices (per-endpoint control devices), memory address space devices (platform address ranges with interleaving, performance, and persistence attributes), and memory regions (active provisioned memory from an address space device that is in use as System RAM or delegated to libnvdimm as Persistent Memory regions).

For now, only the per-endpoint control devices are registered on the 'cxl' bus.

---
 Documentation/ABI/testing/sysfs-bus-cxl |  26 +++
 Documentation/cxl/memory-devices.rst    |   3 +
 drivers/cxl/Makefile                    |   2 +
 drivers/cxl/bus.c                       |  54 +++++
 drivers/cxl/bus.h                       |   8 +
 drivers/cxl/cxl.h                       |   3 +
 drivers/cxl/mem.c                       | 249 +++++++++++++++++++++++-
 7 files changed, 343 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-cxl
 create mode 100644 drivers/cxl/bus.c
 create mode 100644 drivers/cxl/bus.h
Documentation/ABI/testing/sysfs-bus-cxl new file +26 -0
new file mode 100644
index 000000000000..fe7b87eba988
@@ -0,0 +1,26 @@
+What:		/sys/bus/cxl/devices/memX/firmware_version
+Date:		December, 2020
+KernelVersion:	v5.12
+Contact:	[email protected]
+Description:
+		(RO) "FW Revision" string as reported by the Identify
+		Memory Device Output Payload in the CXL-2.0
+		specification.
+
+What:		/sys/bus/cxl/devices/memX/ram/size
+Date:		December, 2020
+KernelVersion:	v5.12
+Contact:	[email protected]
+Description:
+		(RO) "Volatile Only Capacity" as reported by the
+		Identify Memory Device Output Payload in the CXL-2.0
+		specification.
+
+What:		/sys/bus/cxl/devices/memX/pmem/size
+Date:		December, 2020
+KernelVersion:	v5.12
+Contact:	[email protected]
+Description:
+		(RO) "Persistent Only Capacity" as reported by the
+		Identify Memory Device Output Payload in the CXL-2.0
+		specification.
Documentation/cxl/memory-devices.rst +3 -0
index 134c9b6b4ff4..5f723c25382b 100644
@@ -37,3 +37,6 @@ External Interfaces
 
 .. kernel-doc:: drivers/cxl/acpi.c
    :export:
+
+.. kernel-doc:: drivers/cxl/bus.c
+   :export:
drivers/cxl/Makefile +2 -0
index 97fdffb00f2d..f9baaeb94bda 100644
@@ -1,7 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_CXL_BUS_PROVIDER) += cxl_bus.o
 obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
 obj-$(CONFIG_CXL_MEM) += cxl_mem.o
 
 ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=CXL
+cxl_bus-y := bus.o
 cxl_acpi-y := acpi.o
 cxl_mem-y := mem.o
drivers/cxl/bus.c new file +54 -0

54 lines not on this page · raw message

drivers/cxl/bus.h new file +8 -0
new file mode 100644
index 000000000000..5fc75ebd3650
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright(c) 2020 Intel Corporation. All rights reserved. */
+#ifndef __CXL_BUS_H__
+#define __CXL_BUS_H__
+
+int cxl_register(struct device *dev);
+
+#endif /* __CXL_BUS_H__ */
drivers/cxl/cxl.h +3 -0
index 6d641309013d..77c2dee6843c 100644
@@ -3,6 +3,7 @@
 
 #ifndef __CXL_H__
 #define __CXL_H__
+#include <linux/range.h>
 
 #include <linux/bitfield.h>
 #include <linux/bitops.h>
@@ -65,6 +66,7 @@
 #define   CXLMDEV_RESET_NEEDED(status) \
 		(CXL_GET_FIELD(status, CXLMDEV_RESET_NEEDED) != CXLMDEV_RESET_NEEDED_NOT)
 
+struct cxl_memdev;
 /**
  * struct cxl_mem - A CXL memory device
  * @pdev: The PCI device associated with this CXL device.
@@ -73,6 +75,7 @@
 struct cxl_mem {
 	struct pci_dev *pdev;
 	void __iomem *regs;
+	struct cxl_memdev *cxlmd;
 
 	spinlock_t mbox_lock; /* Protects device mailbox and firmware */
 
drivers/cxl/mem.c +247 -2

289 lines not on this page · raw message

Signature · 1 line
2.29.2
#10 lore raw

RFC PATCH v2 9/14 cxl/mem: Add basic IOCTL interface

In reply to Ben Widawsky

Add a straightforward IOCTL that provides a mechanism for userspace to query the supported memory device commands.

Memory device commands are specified in 8.2.9 of the CXL 2.0 specification. They are submitted through a mailbox mechanism specified in 8.2.8.4.

---

I did attempt to use the same struct for querying commands as well as sending commands (upcoming patch). The number of unused fields between the two made for a bad fit IMO.

---
 Documentation/cxl/memory-devices.rst |   9 +++
 drivers/cxl/mem.c                    |  89 +++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h         | 102 +++++++++++++++++++++++++++
 3 files changed, 200 insertions(+)
 create mode 100644 include/uapi/linux/cxl_mem.h
Documentation/cxl/memory-devices.rst +9 -0
index 5f723c25382b..ec54674b3822 100644
@@ -32,6 +32,15 @@ CXL Memory Device
 .. kernel-doc:: drivers/cxl/mem.c
    :internal:
 
+CXL IOCTL Interface
+-------------------
+
+.. kernel-doc:: include/uapi/linux/cxl_mem.h
+   :doc: UAPI
+
+.. kernel-doc:: include/uapi/linux/cxl_mem.h
+   :internal:
+
 External Interfaces
 ===================
 
drivers/cxl/mem.c +89 -0

109 lines not on this page · raw message

include/uapi/linux/cxl_mem.h new file +102 -0

102 lines not on this page · raw message

Signature · 1 line
2.29.2
#11 lore raw

Re: [RFC PATCH 09/14] cxl/mem: Add basic IOCTL interface

Quoted 66 lines · Ben Widawsky

Add a straightforward IOCTL that provides a mechanism for userspace to query the supported memory device commands.

Memory device commands are specified in 8.2.9 of the CXL 2.0 specification. They are submitted through a mailbox mechanism specified in 8.2.8.4.

---

I did attempt to use the same struct for querying commands as well as sending commands (upcoming patch). The number of unused fields between the two made for a bad fit IMO.

---
 Documentation/cxl/memory-devices.rst |   9 +++
 drivers/cxl/mem.c                    |  89 +++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h         | 102 +++++++++++++++++++++++++++
 3 files changed, 200 insertions(+)
 create mode 100644 include/uapi/linux/cxl_mem.h
Documentation/cxl/memory-devices.rst +9 -0
index 5f723c25382b..ec54674b3822 100644
@@ -32,6 +32,15 @@ CXL Memory Device
 .. kernel-doc:: drivers/cxl/mem.c
    :internal:
 
+CXL IOCTL Interface
+-------------------
+
+.. kernel-doc:: include/uapi/linux/cxl_mem.h
+   :doc: UAPI
+
+.. kernel-doc:: include/uapi/linux/cxl_mem.h
+   :internal:
+
 External Interfaces
 ===================
 
drivers/cxl/mem.c +7 -0
index bb6ea58f6c7b..2c4aadcea0e4 100644
@@ -7,6 +7,7 @@
 #include <linux/idr.h>
 #include <linux/pci.h>
 #include <linux/io.h>
+#include <uapi/linux/cxl_mem.h>
 #include "acpi.h"
 #include "pci.h"
 #include "cxl.h"
@@ -73,6 +74,49 @@ static DEFINE_IDR(cxl_mem_idr);
 /* protect cxl_mem_idr allocations */
 static DEFINE_MUTEX(cxl_memdev_lock);
 
+/*
+ * This table defines the supported mailboxes commands for the driver. The id is
+ * ordinal and thus gaps in this table aren't allowed. This table is made up of
+ * a UAPI structure. Non-negative values in the table will be validated against
+ * the user's input. For example, if size_in is 0, and the user passed in 1, it
+ * is an error.

drivers/cxl/mem.c static DEFINE_IDR(cxl_mem_idr); · line 92

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+ */
+#define CXL_CMD(_id, _flags, sin, sout, _name, _enable, op)                    \
+       {                                                                      \
+               { .id = CXL_MEM_COMMAND_ID_##_id,                              \
+                 .flags = CXL_MEM_COMMAND_FLAG_##_flags,                      \
+                 .size_in = sin,                                              \
+                 .size_out = sout,                                            \
+                 .name = _name },                                             \
+                       .enable = _enable, .opcode = op                        \
+       }

Seems the ordinality requirement could be dropped if the definition was:

#define CXL_CMD(_id, _flags, sin, sout, _name, _enable, op)                    \
       [CXL_MEM_COMMAND_ID_##_id] = {
                             \
               { .id = CXL_MEM_COMMAND_ID_##_id,                              \

...

Then command 0 and 42 could be defined out of order in the table. Especially if we need to config-disable or deprecate commands, I think it would be useful if this table was tolerant to being sparse.

Quoted 66 lines
drivers/cxl/mem.c +57 -0
+
+/**
+ * struct cxl_mem_command - Driver representation of a memory device command
+ * @info: Command information as it exists for the UAPI
+ * @opcode: The actual bits used for the mailbox protocol
+ * @enable: Whether the command is enabled. The driver may support a large set
+ *         of commands that may not be enabled. The primary reason a command
+ *         would not be enabled is for commands that are specified as optional
+ *         and the hardware doesn't support the command.
+ *
+ * The cxl_mem_command is the driver's internal representation of commands that
+ * are supported by the driver. Some of these commands may not be supported by
+ * the hardware (!@enable). The driver will use @info to validate the fields
+ * passed in by the user then submit the @opcode to the hardware.
+ *
+ * See struct cxl_command_info.
+ */
+struct cxl_mem_command {
+       const struct cxl_command_info info;
+       const u16 opcode;
+       bool enable;
+};
+
+static struct cxl_mem_command mem_commands[] = {
+       CXL_CMD(INVALID, NONE, 0, 0, "Reserved", false, 0),
+};
+
 static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
 {
        const int timeout = msecs_to_jiffies(2000);
@@ -268,8 +312,53 @@ static int cxl_mem_open(struct inode *inode, struct file *file)
        return 0;
 }
 
+static int cxl_mem_count_commands(void)
+{
+       int i, n = 0;
+
+       for (i = 0; i < ARRAY_SIZE(mem_commands); i++) {
+               struct cxl_mem_command *c = &mem_commands[i];
+
+               if (c->enable)
+                       n++;
+       }
+
+       return n;
+}
+
 static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
+       if (cmd == CXL_MEM_QUERY_COMMANDS) {
+               struct cxl_mem_query_commands __user *q = (void __user *)arg;
+               u32 n_commands;
+               int i, j;
+
+               if (get_user(n_commands, (u32 __user *)arg))
+                       return -EFAULT;
+
+               if (n_commands == 0)
+                       return put_user(cxl_mem_count_commands(),
+                                       (u32 __user *)arg);
+
+               for (i = 0, j = 0;
+                    i < ARRAY_SIZE(mem_commands) && j < n_commands; i++) {
+                       struct cxl_mem_command *c = &mem_commands[i];
+                       const struct cxl_command_info *info = &c->info;

drivers/cxl/mem.c static int cxl_mem_open(struct inode *inode, struct file *file) · line 356

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+
+                       if (!c->enable)
+                               continue;
+
+                       if (copy_to_user(&q->commands[j], info, sizeof(*info)))
+                               return -EFAULT;
+
+                       if (copy_to_user(&q->commands[j].name, info->name,
+                                        strlen(info->name)))
+                               return -EFAULT;

Not sure why this is needed, see comment below about @name in cxl_mem_query_commands.

Quoted 63 lines
drivers/cxl/mem.c +5 -0
+
+                       j++;
+               }
+       }
+
        return -ENOTTY;
 }
 
include/uapi/linux/cxl_mem.h new file +49 -0
new file mode 100644
index 000000000000..1d1e143f98ec
@@ -0,0 +1,102 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * CXL IOCTLs for Memory Devices
+ */
+
+#ifndef _UAPI_CXL_MEM_H_
+#define _UAPI_CXL_MEM_H_
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/**
+ * DOC: UAPI
+ *
+ * CXL memory devices expose UAPI to have a standard user interface.
+ * Userspace can refer to these structure definitions and UAPI formats
+ * to communicate to driver
+ */
+
+#define CXL_MEM_QUERY_COMMANDS _IOR('C', 1, struct cxl_mem_query_commands)
+
+#define CXL_MEM_COMMAND_NAME_LENGTH 32
+
+/**
+ * struct cxl_command_info - Command information returned from a query.
+ * @id: ID number for the command.
+ * @flags: Flags that specify command behavior.
+ *
+ *          - CXL_MEM_COMMAND_FLAG_TAINT: Using this command will taint the kernel.
+ * @size_in: Expected input size, or -1 if variable length.
+ * @size_out: Expected output size, or -1 if variable length.
+ * @name: Name describing the command.
+ *
+ * Represents a single command that is supported by both the driver and the
+ * hardware. The is returned as part of an array from the query ioctl. The
+ * following would be a command named "foobar" that takes a variable length
+ * input and returns 0 bytes of output.
+ *
+ *  - @id = 10
+ *  - @name = foobar
+ *  - @flags = 0
+ *  - @size_in = -1
+ *  - @size_out = 0
+ *
+ * See struct cxl_mem_query_commands.
+ */
+struct cxl_command_info {
+       __u32 id;

include/uapi/linux/cxl_mem.h line 59

Quoted 10 lines
include/uapi/linux/cxl_mem.h new file +10 -0
+#define CXL_MEM_COMMAND_ID_INVALID 0
+
+       __u32 flags;
+#define CXL_MEM_COMMAND_FLAG_NONE 0
+#define CXL_MEM_COMMAND_FLAG_TAINT BIT(0)
+
+       __s32 size_in;
+       __s32 size_out;
+
+       char name[32];

Why does the name for a command need to be shuffled back and forth over the ioctl interface. Can't this be handled by a static lookup table defined in the header?

Quoted 46 lines
include/uapi/linux/cxl_mem.h new file +43 -1
+};
+
+/**
+ * struct cxl_mem_query_commands - Query supported commands.
+ * @n_commands: In/out parameter. When @n_commands is > 0, the driver will
+ *             return min(num_support_commands, n_commands). When @n_commands
+ *             is 0, driver will return the number of total supported commands.
+ * @rsvd: Reserved for future use.
+ * @commands: Output array of supported commands. This array must be allocated
+ *            by userspace to be at least min(num_support_commands, @n_commands)
+ *
+ * Allow userspace to query the available commands supported by both the driver,
+ * and the hardware. Commands that aren't supported by either the driver, or the
+ * hardware are not returned in the query.
+ *
+ * Examples:
+ *
+ *  - { .n_commands = 0 } // Get number of supported commands
+ *  - { .n_commands = 15, .commands = buf } // Return first 15 (or less)
+ *    supported commands
+ *
+ *  See struct cxl_command_info.
+ */
+struct cxl_mem_query_commands {
+       /*
+        * Input: Number of commands to return (space allocated by user)
+        * Output: Number of commands supported by the driver/hardware
+        *
+        * If n_commands is 0, kernel will only return number of commands and
+        * not try to populate commands[], thus allowing userspace to know how
+        * much space to allocate
+        */
+       __u32 n_commands;
+       __u32 rsvd;
+
+       struct cxl_command_info __user commands[]; /* out: supported commands */
+};
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
--

2.29.2

#12 lore raw

Re: [RFC PATCH 09/14] cxl/mem: Add basic IOCTL interface

Quoted 84 lines · Dan Williams
Quoted 66 lines · Ben Widawsky

Add a straightforward IOCTL that provides a mechanism for userspace to query the supported memory device commands.

Memory device commands are specified in 8.2.9 of the CXL 2.0 specification. They are submitted through a mailbox mechanism specified in 8.2.8.4.

---

I did attempt to use the same struct for querying commands as well as sending commands (upcoming patch). The number of unused fields between the two made for a bad fit IMO.

---
 Documentation/cxl/memory-devices.rst |   9 +++
 drivers/cxl/mem.c                    |  89 +++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h         | 102 +++++++++++++++++++++++++++
 3 files changed, 200 insertions(+)
 create mode 100644 include/uapi/linux/cxl_mem.h
Documentation/cxl/memory-devices.rst +9 -0
index 5f723c25382b..ec54674b3822 100644
@@ -32,6 +32,15 @@ CXL Memory Device
 .. kernel-doc:: drivers/cxl/mem.c
    :internal:
 
+CXL IOCTL Interface
+-------------------
+
+.. kernel-doc:: include/uapi/linux/cxl_mem.h
+   :doc: UAPI
+
+.. kernel-doc:: include/uapi/linux/cxl_mem.h
+   :internal:
+
 External Interfaces
 ===================
 
drivers/cxl/mem.c +7 -0
index bb6ea58f6c7b..2c4aadcea0e4 100644
@@ -7,6 +7,7 @@
 #include <linux/idr.h>
 #include <linux/pci.h>
 #include <linux/io.h>
+#include <uapi/linux/cxl_mem.h>
 #include "acpi.h"
 #include "pci.h"
 #include "cxl.h"
@@ -73,6 +74,49 @@ static DEFINE_IDR(cxl_mem_idr);
 /* protect cxl_mem_idr allocations */
 static DEFINE_MUTEX(cxl_memdev_lock);
 
+/*
+ * This table defines the supported mailboxes commands for the driver. The id is
+ * ordinal and thus gaps in this table aren't allowed. This table is made up of
+ * a UAPI structure. Non-negative values in the table will be validated against
+ * the user's input. For example, if size_in is 0, and the user passed in 1, it
+ * is an error.

drivers/cxl/mem.c static DEFINE_IDR(cxl_mem_idr); · line 92

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+ */
+#define CXL_CMD(_id, _flags, sin, sout, _name, _enable, op)                    \
+       {                                                                      \
+               { .id = CXL_MEM_COMMAND_ID_##_id,                              \
+                 .flags = CXL_MEM_COMMAND_FLAG_##_flags,                      \
+                 .size_in = sin,                                              \
+                 .size_out = sout,                                            \
+                 .name = _name },                                             \
+                       .enable = _enable, .opcode = op                        \
+       }

Seems the ordinality requirement could be dropped if the definition was:

#define CXL_CMD(_id, _flags, sin, sout, _name, _enable, op)                    \
       [CXL_MEM_COMMAND_ID_##_id] = {
                             \
               { .id = CXL_MEM_COMMAND_ID_##_id,                              \
Quoted 6 lines

...

Then command 0 and 42 could be defined out of order in the table. Especially if we need to config-disable or deprecate commands, I think it would be useful if this table was tolerant to being sparse.

How sparse are we talking? The current form does support sparseness, but obviously gets quite large if the ID numbering is similar to random distribution.

I think if we do see this being more like random distribution, it can be supported, but I think it adds a decent amount of complexity for what I see as not much reward - unless you know of a fairly simple way to create this data structure with full sparse ID support?

Quoted 149 lines
Quoted 66 lines
drivers/cxl/mem.c +57 -0
+
+/**
+ * struct cxl_mem_command - Driver representation of a memory device command
+ * @info: Command information as it exists for the UAPI
+ * @opcode: The actual bits used for the mailbox protocol
+ * @enable: Whether the command is enabled. The driver may support a large set
+ *         of commands that may not be enabled. The primary reason a command
+ *         would not be enabled is for commands that are specified as optional
+ *         and the hardware doesn't support the command.
+ *
+ * The cxl_mem_command is the driver's internal representation of commands that
+ * are supported by the driver. Some of these commands may not be supported by
+ * the hardware (!@enable). The driver will use @info to validate the fields
+ * passed in by the user then submit the @opcode to the hardware.
+ *
+ * See struct cxl_command_info.
+ */
+struct cxl_mem_command {
+       const struct cxl_command_info info;
+       const u16 opcode;
+       bool enable;
+};
+
+static struct cxl_mem_command mem_commands[] = {
+       CXL_CMD(INVALID, NONE, 0, 0, "Reserved", false, 0),
+};
+
 static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
 {
        const int timeout = msecs_to_jiffies(2000);
@@ -268,8 +312,53 @@ static int cxl_mem_open(struct inode *inode, struct file *file)
        return 0;
 }
 
+static int cxl_mem_count_commands(void)
+{
+       int i, n = 0;
+
+       for (i = 0; i < ARRAY_SIZE(mem_commands); i++) {
+               struct cxl_mem_command *c = &mem_commands[i];
+
+               if (c->enable)
+                       n++;
+       }
+
+       return n;
+}
+
 static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
+       if (cmd == CXL_MEM_QUERY_COMMANDS) {
+               struct cxl_mem_query_commands __user *q = (void __user *)arg;
+               u32 n_commands;
+               int i, j;
+
+               if (get_user(n_commands, (u32 __user *)arg))
+                       return -EFAULT;
+
+               if (n_commands == 0)
+                       return put_user(cxl_mem_count_commands(),
+                                       (u32 __user *)arg);
+
+               for (i = 0, j = 0;
+                    i < ARRAY_SIZE(mem_commands) && j < n_commands; i++) {
+                       struct cxl_mem_command *c = &mem_commands[i];
+                       const struct cxl_command_info *info = &c->info;

drivers/cxl/mem.c static int cxl_mem_open(struct inode *inode, struct file *file) · line 356

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+
+                       if (!c->enable)
+                               continue;
+
+                       if (copy_to_user(&q->commands[j], info, sizeof(*info)))
+                               return -EFAULT;
+
+                       if (copy_to_user(&q->commands[j].name, info->name,
+                                        strlen(info->name)))
+                               return -EFAULT;

Not sure why this is needed, see comment below about @name in cxl_mem_query_commands.

Quoted 69 lines
drivers/cxl/mem.c +5 -0
+
+                       j++;
+               }
+       }
+
        return -ENOTTY;
 }
 
include/uapi/linux/cxl_mem.h new file +55 -0
new file mode 100644
index 000000000000..1d1e143f98ec
@@ -0,0 +1,102 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * CXL IOCTLs for Memory Devices
+ */
+
+#ifndef _UAPI_CXL_MEM_H_
+#define _UAPI_CXL_MEM_H_
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/**
+ * DOC: UAPI
+ *
+ * CXL memory devices expose UAPI to have a standard user interface.
+ * Userspace can refer to these structure definitions and UAPI formats
+ * to communicate to driver
+ */
+
+#define CXL_MEM_QUERY_COMMANDS _IOR('C', 1, struct cxl_mem_query_commands)
+
+#define CXL_MEM_COMMAND_NAME_LENGTH 32
+
+/**
+ * struct cxl_command_info - Command information returned from a query.
+ * @id: ID number for the command.
+ * @flags: Flags that specify command behavior.
+ *
+ *          - CXL_MEM_COMMAND_FLAG_TAINT: Using this command will taint the kernel.
+ * @size_in: Expected input size, or -1 if variable length.
+ * @size_out: Expected output size, or -1 if variable length.
+ * @name: Name describing the command.
+ *
+ * Represents a single command that is supported by both the driver and the
+ * hardware. The is returned as part of an array from the query ioctl. The
+ * following would be a command named "foobar" that takes a variable length
+ * input and returns 0 bytes of output.
+ *
+ *  - @id = 10
+ *  - @name = foobar
+ *  - @flags = 0
+ *  - @size_in = -1
+ *  - @size_out = 0
+ *
+ * See struct cxl_mem_query_commands.
+ */
+struct cxl_command_info {
+       __u32 id;
+#define CXL_MEM_COMMAND_ID_INVALID 0
+
+       __u32 flags;
+#define CXL_MEM_COMMAND_FLAG_NONE 0
+#define CXL_MEM_COMMAND_FLAG_TAINT BIT(0)
+
Quoted 9 lines

include/uapi/linux/cxl_mem.h line 59

Quoted 4 lines
include/uapi/linux/cxl_mem.h new file +4 -0
+       __s32 size_in;
+       __s32 size_out;
+
+       char name[32];

Why does the name for a command need to be shuffled back and forth over the ioctl interface. Can't this be handled by a static lookup table defined in the header?

I was thinking of cases where the userspace application doesn't match the current kernel's UAPI and giving the driver flexibility to return whatever.

OTTOMH, I also can't think of a way to do this if you want to do define the table sparsely though. Do you have ideas for that?

Quoted 46 lines
Quoted 46 lines
include/uapi/linux/cxl_mem.h new file +43 -1
+};
+
+/**
+ * struct cxl_mem_query_commands - Query supported commands.
+ * @n_commands: In/out parameter. When @n_commands is > 0, the driver will
+ *             return min(num_support_commands, n_commands). When @n_commands
+ *             is 0, driver will return the number of total supported commands.
+ * @rsvd: Reserved for future use.
+ * @commands: Output array of supported commands. This array must be allocated
+ *            by userspace to be at least min(num_support_commands, @n_commands)
+ *
+ * Allow userspace to query the available commands supported by both the driver,
+ * and the hardware. Commands that aren't supported by either the driver, or the
+ * hardware are not returned in the query.
+ *
+ * Examples:
+ *
+ *  - { .n_commands = 0 } // Get number of supported commands
+ *  - { .n_commands = 15, .commands = buf } // Return first 15 (or less)
+ *    supported commands
+ *
+ *  See struct cxl_command_info.
+ */
+struct cxl_mem_query_commands {
+       /*
+        * Input: Number of commands to return (space allocated by user)
+        * Output: Number of commands supported by the driver/hardware
+        *
+        * If n_commands is 0, kernel will only return number of commands and
+        * not try to populate commands[], thus allowing userspace to know how
+        * much space to allocate
+        */
+       __u32 n_commands;
+       __u32 rsvd;
+
+       struct cxl_command_info __user commands[]; /* out: supported commands */
+};
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
--

2.29.2

#13 lore raw

Re: [RFC PATCH 09/14] cxl/mem: Add basic IOCTL interface

Quoted 88 lines · Ben Widawsky
Quoted 86 lines · Dan Williams
Quoted 66 lines · Ben Widawsky

Add a straightforward IOCTL that provides a mechanism for userspace to query the supported memory device commands.

Memory device commands are specified in 8.2.9 of the CXL 2.0 specification. They are submitted through a mailbox mechanism specified in 8.2.8.4.

---

I did attempt to use the same struct for querying commands as well as sending commands (upcoming patch). The number of unused fields between the two made for a bad fit IMO.

---
 Documentation/cxl/memory-devices.rst |   9 +++
 drivers/cxl/mem.c                    |  89 +++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h         | 102 +++++++++++++++++++++++++++
 3 files changed, 200 insertions(+)
 create mode 100644 include/uapi/linux/cxl_mem.h
Documentation/cxl/memory-devices.rst +9 -0
index 5f723c25382b..ec54674b3822 100644
@@ -32,6 +32,15 @@ CXL Memory Device
 .. kernel-doc:: drivers/cxl/mem.c
    :internal:
 
+CXL IOCTL Interface
+-------------------
+
+.. kernel-doc:: include/uapi/linux/cxl_mem.h
+   :doc: UAPI
+
+.. kernel-doc:: include/uapi/linux/cxl_mem.h
+   :internal:
+
 External Interfaces
 ===================
 
drivers/cxl/mem.c +7 -0
index bb6ea58f6c7b..2c4aadcea0e4 100644
@@ -7,6 +7,7 @@
 #include <linux/idr.h>
 #include <linux/pci.h>
 #include <linux/io.h>
+#include <uapi/linux/cxl_mem.h>
 #include "acpi.h"
 #include "pci.h"
 #include "cxl.h"
@@ -73,6 +74,49 @@ static DEFINE_IDR(cxl_mem_idr);
 /* protect cxl_mem_idr allocations */
 static DEFINE_MUTEX(cxl_memdev_lock);
 
+/*
+ * This table defines the supported mailboxes commands for the driver. The id is
+ * ordinal and thus gaps in this table aren't allowed. This table is made up of
+ * a UAPI structure. Non-negative values in the table will be validated against
+ * the user's input. For example, if size_in is 0, and the user passed in 1, it
+ * is an error.

drivers/cxl/mem.c static DEFINE_IDR(cxl_mem_idr); · line 92

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+ */
+#define CXL_CMD(_id, _flags, sin, sout, _name, _enable, op)                    \
+       {                                                                      \
+               { .id = CXL_MEM_COMMAND_ID_##_id,                              \
+                 .flags = CXL_MEM_COMMAND_FLAG_##_flags,                      \
+                 .size_in = sin,                                              \
+                 .size_out = sout,                                            \
+                 .name = _name },                                             \
+                       .enable = _enable, .opcode = op                        \
+       }

Seems the ordinality requirement could be dropped if the definition was:

#define CXL_CMD(_id, _flags, sin, sout, _name, _enable, op)                    \
       [CXL_MEM_COMMAND_ID_##_id] = {
                             \
               { .id = CXL_MEM_COMMAND_ID_##_id,                              \

...

Quoted 8 lines
Quoted 4 lines

Then command 0 and 42 could be defined out of order in the table. Especially if we need to config-disable or deprecate commands, I think it would be useful if this table was tolerant to being sparse.

How sparse are we talking? The current form does support sparseness, but obviously gets quite large if the ID numbering is similar to random distribution.

"Sparse" may have been the wrong word to use. I was implying sparse enough that if I add command N+1 I don't need to be careful where I put it in mem_commands, but still be able to rely on lookups into mem_commands being indexed by the command-id.

Quoted 4 lines

I think if we do see this being more like random distribution, it can be supported, but I think it adds a decent amount of complexity for what I see as not much reward - unless you know of a fairly simple way to create this data structure with full sparse ID support?

I'm expecting the command distribution to be mostly uniform, it's more of the lookup property that I think would be useful especially for the dynamic case of walking mem_commands to update it relative to what the hardware supports or other metadata. Speaking of which I think @enable should be turned into @flags of which 'enable' is one, in case we want to define more flags in the future.

Quoted 160 lines
Quoted 150 lines
Quoted 67 lines
drivers/cxl/mem.c +57 -0
+
+/**
+ * struct cxl_mem_command - Driver representation of a memory device command
+ * @info: Command information as it exists for the UAPI
+ * @opcode: The actual bits used for the mailbox protocol
+ * @enable: Whether the command is enabled. The driver may support a large set
+ *         of commands that may not be enabled. The primary reason a command
+ *         would not be enabled is for commands that are specified as optional
+ *         and the hardware doesn't support the command.
+ *
+ * The cxl_mem_command is the driver's internal representation of commands that
+ * are supported by the driver. Some of these commands may not be supported by
+ * the hardware (!@enable). The driver will use @info to validate the fields
+ * passed in by the user then submit the @opcode to the hardware.
+ *
+ * See struct cxl_command_info.
+ */
+struct cxl_mem_command {
+       const struct cxl_command_info info;
+       const u16 opcode;
+       bool enable;
+};
+
+static struct cxl_mem_command mem_commands[] = {
+       CXL_CMD(INVALID, NONE, 0, 0, "Reserved", false, 0),
+};
+
 static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
 {
        const int timeout = msecs_to_jiffies(2000);
@@ -268,8 +312,53 @@ static int cxl_mem_open(struct inode *inode, struct file *file)
        return 0;
 }
 
+static int cxl_mem_count_commands(void)
+{
+       int i, n = 0;
+
+       for (i = 0; i < ARRAY_SIZE(mem_commands); i++) {
+               struct cxl_mem_command *c = &mem_commands[i];
+
+               if (c->enable)
+                       n++;
+       }
+
+       return n;
+}
+
 static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
+       if (cmd == CXL_MEM_QUERY_COMMANDS) {
+               struct cxl_mem_query_commands __user *q = (void __user *)arg;
+               u32 n_commands;
+               int i, j;
+
+               if (get_user(n_commands, (u32 __user *)arg))
+                       return -EFAULT;
+
+               if (n_commands == 0)
+                       return put_user(cxl_mem_count_commands(),
+                                       (u32 __user *)arg);
+
+               for (i = 0, j = 0;
+                    i < ARRAY_SIZE(mem_commands) && j < n_commands; i++) {
+                       struct cxl_mem_command *c = &mem_commands[i];
+                       const struct cxl_command_info *info = &c->info;

drivers/cxl/mem.c static int cxl_mem_open(struct inode *inode, struct file *file) · line 356

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+
+                       if (!c->enable)
+                               continue;
+
+                       if (copy_to_user(&q->commands[j], info, sizeof(*info)))
+                               return -EFAULT;
+
+                       if (copy_to_user(&q->commands[j].name, info->name,
+                                        strlen(info->name)))
+                               return -EFAULT;

Not sure why this is needed, see comment below about @name in cxl_mem_query_commands.

Quoted 69 lines
drivers/cxl/mem.c +5 -0
+
+                       j++;
+               }
+       }
+
        return -ENOTTY;
 }
 
include/uapi/linux/cxl_mem.h new file +55 -0
new file mode 100644
index 000000000000..1d1e143f98ec
@@ -0,0 +1,102 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * CXL IOCTLs for Memory Devices
+ */
+
+#ifndef _UAPI_CXL_MEM_H_
+#define _UAPI_CXL_MEM_H_
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/**
+ * DOC: UAPI
+ *
+ * CXL memory devices expose UAPI to have a standard user interface.
+ * Userspace can refer to these structure definitions and UAPI formats
+ * to communicate to driver
+ */
+
+#define CXL_MEM_QUERY_COMMANDS _IOR('C', 1, struct cxl_mem_query_commands)
+
+#define CXL_MEM_COMMAND_NAME_LENGTH 32
+
+/**
+ * struct cxl_command_info - Command information returned from a query.
+ * @id: ID number for the command.
+ * @flags: Flags that specify command behavior.
+ *
+ *          - CXL_MEM_COMMAND_FLAG_TAINT: Using this command will taint the kernel.
+ * @size_in: Expected input size, or -1 if variable length.
+ * @size_out: Expected output size, or -1 if variable length.
+ * @name: Name describing the command.
+ *
+ * Represents a single command that is supported by both the driver and the
+ * hardware. The is returned as part of an array from the query ioctl. The
+ * following would be a command named "foobar" that takes a variable length
+ * input and returns 0 bytes of output.
+ *
+ *  - @id = 10
+ *  - @name = foobar
+ *  - @flags = 0
+ *  - @size_in = -1
+ *  - @size_out = 0
+ *
+ * See struct cxl_mem_query_commands.
+ */
+struct cxl_command_info {
+       __u32 id;
+#define CXL_MEM_COMMAND_ID_INVALID 0
+
+       __u32 flags;
+#define CXL_MEM_COMMAND_FLAG_NONE 0
+#define CXL_MEM_COMMAND_FLAG_TAINT BIT(0)
+
Quoted 9 lines

include/uapi/linux/cxl_mem.h line 59

Quoted 4 lines
include/uapi/linux/cxl_mem.h new file +4 -0
+       __s32 size_in;
+       __s32 size_out;
+
+       char name[32];

Why does the name for a command need to be shuffled back and forth over the ioctl interface. Can't this be handled by a static lookup table defined in the header?

Quoted 2 lines

I was thinking of cases where the userspace application doesn't match the current kernel's UAPI and giving the driver flexibility to return whatever.

How / why would the application by looking at @name for UAPI compatibility?

Quoted 2 lines

OTTOMH, I also can't think of a way to do this if you want to do define the table sparsely though. Do you have ideas for that?

I don't think the name lookup would be sparse. i.e. it would be ok for mem_commands to not have an entry for everything in the name lookup table. As for defining the table it could use C preprocessor trick popularized by Steven Rostedt:

#define CMDS                                                     \
        C(CMD1, "command one"),     \
        C(CMD2, "command two")     \

#undef C
#define C(a, b) a
enum commands_enum { CMDS };
#undef C
#define C(a, b) { b }

static struct {
        const char *name;
} commands[] = { CMDS };

#undef C

...then there's no way for the command ids to get out of sync with the names.

#14 lore raw

Re: [RFC PATCH 09/14] cxl/mem: Add basic IOCTL interface

Quoted 94 lines · Dan Williams
Quoted 93 lines · Ben Widawsky
Quoted 90 lines · Dan Williams
Quoted 66 lines · Ben Widawsky

Add a straightforward IOCTL that provides a mechanism for userspace to query the supported memory device commands.

Memory device commands are specified in 8.2.9 of the CXL 2.0 specification. They are submitted through a mailbox mechanism specified in 8.2.8.4.

---

I did attempt to use the same struct for querying commands as well as sending commands (upcoming patch). The number of unused fields between the two made for a bad fit IMO.

---
 Documentation/cxl/memory-devices.rst |   9 +++
 drivers/cxl/mem.c                    |  89 +++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h         | 102 +++++++++++++++++++++++++++
 3 files changed, 200 insertions(+)
 create mode 100644 include/uapi/linux/cxl_mem.h
Documentation/cxl/memory-devices.rst +9 -0
index 5f723c25382b..ec54674b3822 100644
@@ -32,6 +32,15 @@ CXL Memory Device
 .. kernel-doc:: drivers/cxl/mem.c
    :internal:
 
+CXL IOCTL Interface
+-------------------
+
+.. kernel-doc:: include/uapi/linux/cxl_mem.h
+   :doc: UAPI
+
+.. kernel-doc:: include/uapi/linux/cxl_mem.h
+   :internal:
+
 External Interfaces
 ===================
 
drivers/cxl/mem.c +7 -0
index bb6ea58f6c7b..2c4aadcea0e4 100644
@@ -7,6 +7,7 @@
 #include <linux/idr.h>
 #include <linux/pci.h>
 #include <linux/io.h>
+#include <uapi/linux/cxl_mem.h>
 #include "acpi.h"
 #include "pci.h"
 #include "cxl.h"
@@ -73,6 +74,49 @@ static DEFINE_IDR(cxl_mem_idr);
 /* protect cxl_mem_idr allocations */
 static DEFINE_MUTEX(cxl_memdev_lock);
 
+/*
+ * This table defines the supported mailboxes commands for the driver. The id is
+ * ordinal and thus gaps in this table aren't allowed. This table is made up of
+ * a UAPI structure. Non-negative values in the table will be validated against
+ * the user's input. For example, if size_in is 0, and the user passed in 1, it
+ * is an error.

drivers/cxl/mem.c static DEFINE_IDR(cxl_mem_idr); · line 92

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+ */
+#define CXL_CMD(_id, _flags, sin, sout, _name, _enable, op)                    \
+       {                                                                      \
+               { .id = CXL_MEM_COMMAND_ID_##_id,                              \
+                 .flags = CXL_MEM_COMMAND_FLAG_##_flags,                      \
+                 .size_in = sin,                                              \
+                 .size_out = sout,                                            \
+                 .name = _name },                                             \
+                       .enable = _enable, .opcode = op                        \
+       }

Seems the ordinality requirement could be dropped if the definition was:

#define CXL_CMD(_id, _flags, sin, sout, _name, _enable, op)                    \
       [CXL_MEM_COMMAND_ID_##_id] = {
                             \
               { .id = CXL_MEM_COMMAND_ID_##_id,                              \

...

Then command 0 and 42 could be defined out of order in the table. Especially if we need to config-disable or deprecate commands, I think it would be useful if this table was tolerant to being sparse.

Quoted 9 lines
Quoted 3 lines

How sparse are we talking? The current form does support sparseness, but obviously gets quite large if the ID numbering is similar to random distribution.

"Sparse" may have been the wrong word to use. I was implying sparse enough that if I add command N+1 I don't need to be careful where I put it in mem_commands, but still be able to rely on lookups into mem_commands being indexed by the command-id.

I'm not sure I understand the issue then. It's already demonstrated via the first command being reserved - ie. already sparse.

CXL_CMD(INVALID, NONE, 0, 0, "Reserved", false, 0)

As long as the command doesn't have @enable set, it's effectively ignored for all user interactions.

If you look at the last patch in the series, WIP, there is an example for enabling one.

Quoted 5 lines
Quoted 4 lines

I think if we do see this being more like random distribution, it can be supported, but I think it adds a decent amount of complexity for what I see as not much reward - unless you know of a fairly simple way to create this data structure with full sparse ID support?

Quoted 7 lines

I'm expecting the command distribution to be mostly uniform, it's more of the lookup property that I think would be useful especially for the dynamic case of walking mem_commands to update it relative to what the hardware supports or other metadata. Speaking of which I think @enable should be turned into @flags of which 'enable' is one, in case we want to define more flags in the future.

I like the idea of moving enable to flags. I still don't see a reason to change how it's defined today, can you give me an example where what is there won't work?

Quoted 181 lines
Quoted 160 lines
Quoted 150 lines
Quoted 67 lines
drivers/cxl/mem.c +57 -0
+
+/**
+ * struct cxl_mem_command - Driver representation of a memory device command
+ * @info: Command information as it exists for the UAPI
+ * @opcode: The actual bits used for the mailbox protocol
+ * @enable: Whether the command is enabled. The driver may support a large set
+ *         of commands that may not be enabled. The primary reason a command
+ *         would not be enabled is for commands that are specified as optional
+ *         and the hardware doesn't support the command.
+ *
+ * The cxl_mem_command is the driver's internal representation of commands that
+ * are supported by the driver. Some of these commands may not be supported by
+ * the hardware (!@enable). The driver will use @info to validate the fields
+ * passed in by the user then submit the @opcode to the hardware.
+ *
+ * See struct cxl_command_info.
+ */
+struct cxl_mem_command {
+       const struct cxl_command_info info;
+       const u16 opcode;
+       bool enable;
+};
+
+static struct cxl_mem_command mem_commands[] = {
+       CXL_CMD(INVALID, NONE, 0, 0, "Reserved", false, 0),
+};
+
 static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
 {
        const int timeout = msecs_to_jiffies(2000);
@@ -268,8 +312,53 @@ static int cxl_mem_open(struct inode *inode, struct file *file)
        return 0;
 }
 
+static int cxl_mem_count_commands(void)
+{
+       int i, n = 0;
+
+       for (i = 0; i < ARRAY_SIZE(mem_commands); i++) {
+               struct cxl_mem_command *c = &mem_commands[i];
+
+               if (c->enable)
+                       n++;
+       }
+
+       return n;
+}
+
 static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
+       if (cmd == CXL_MEM_QUERY_COMMANDS) {
+               struct cxl_mem_query_commands __user *q = (void __user *)arg;
+               u32 n_commands;
+               int i, j;
+
+               if (get_user(n_commands, (u32 __user *)arg))
+                       return -EFAULT;
+
+               if (n_commands == 0)
+                       return put_user(cxl_mem_count_commands(),
+                                       (u32 __user *)arg);
+
+               for (i = 0, j = 0;
+                    i < ARRAY_SIZE(mem_commands) && j < n_commands; i++) {
+                       struct cxl_mem_command *c = &mem_commands[i];
+                       const struct cxl_command_info *info = &c->info;

drivers/cxl/mem.c static int cxl_mem_open(struct inode *inode, struct file *file) · line 356

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+
+                       if (!c->enable)
+                               continue;
+
+                       if (copy_to_user(&q->commands[j], info, sizeof(*info)))
+                               return -EFAULT;
+
+                       if (copy_to_user(&q->commands[j].name, info->name,
+                                        strlen(info->name)))
+                               return -EFAULT;

Not sure why this is needed, see comment below about @name in cxl_mem_query_commands.

Quoted 69 lines
drivers/cxl/mem.c +5 -0
+
+                       j++;
+               }
+       }
+
        return -ENOTTY;
 }
 
include/uapi/linux/cxl_mem.h new file +55 -0
new file mode 100644
index 000000000000..1d1e143f98ec
@@ -0,0 +1,102 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * CXL IOCTLs for Memory Devices
+ */
+
+#ifndef _UAPI_CXL_MEM_H_
+#define _UAPI_CXL_MEM_H_
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/**
+ * DOC: UAPI
+ *
+ * CXL memory devices expose UAPI to have a standard user interface.
+ * Userspace can refer to these structure definitions and UAPI formats
+ * to communicate to driver
+ */
+
+#define CXL_MEM_QUERY_COMMANDS _IOR('C', 1, struct cxl_mem_query_commands)
+
+#define CXL_MEM_COMMAND_NAME_LENGTH 32
+
+/**
+ * struct cxl_command_info - Command information returned from a query.
+ * @id: ID number for the command.
+ * @flags: Flags that specify command behavior.
+ *
+ *          - CXL_MEM_COMMAND_FLAG_TAINT: Using this command will taint the kernel.
+ * @size_in: Expected input size, or -1 if variable length.
+ * @size_out: Expected output size, or -1 if variable length.
+ * @name: Name describing the command.
+ *
+ * Represents a single command that is supported by both the driver and the
+ * hardware. The is returned as part of an array from the query ioctl. The
+ * following would be a command named "foobar" that takes a variable length
+ * input and returns 0 bytes of output.
+ *
+ *  - @id = 10
+ *  - @name = foobar
+ *  - @flags = 0
+ *  - @size_in = -1
+ *  - @size_out = 0
+ *
+ * See struct cxl_mem_query_commands.
+ */
+struct cxl_command_info {
+       __u32 id;
+#define CXL_MEM_COMMAND_ID_INVALID 0
+
+       __u32 flags;
+#define CXL_MEM_COMMAND_FLAG_NONE 0
+#define CXL_MEM_COMMAND_FLAG_TAINT BIT(0)
+
Quoted 9 lines

include/uapi/linux/cxl_mem.h line 59

Quoted 4 lines
include/uapi/linux/cxl_mem.h new file +4 -0
+       __s32 size_in;
+       __s32 size_out;
+
+       char name[32];

Why does the name for a command need to be shuffled back and forth over the ioctl interface. Can't this be handled by a static lookup table defined in the header?

Quoted 2 lines

I was thinking of cases where the userspace application doesn't match the current kernel's UAPI and giving the driver flexibility to return whatever.

How / why would the application by looking at @name for UAPI compatibility?

Quoted 2 lines

OTTOMH, I also can't think of a way to do this if you want to do define the table sparsely though. Do you have ideas for that?

I don't think the name lookup would be sparse. i.e. it would be ok for mem_commands to not have an entry for everything in the name lookup table. As for defining the table it could use C preprocessor trick popularized by Steven Rostedt:

#define CMDS                                                     \
        C(CMD1, "command one"),     \
        C(CMD2, "command two")     \

#undef C
#define C(a, b) a
enum commands_enum { CMDS };
#undef C
#define C(a, b) { b }

Quoted 6 lines
static struct {
        const char *name;
} commands[] = { CMDS };

#undef C

...then there's no way for the command ids to get out of sync with the names.

I will move it to the header and drop name[32] from UAPI. My personal preference is to have the driver fill in the field, but I have no objective reason for that.

reviewer
#15 lore raw

Re: [RFC PATCH 09/14] cxl/mem: Add basic IOCTL interface

In reply to Ben Widawsky

Review

Quoted 2 lines · Ben Widawsky
(quoted diff) +2 -0
+
+#define CXL_MEM_QUERY_COMMANDS _IOR('C', 1, struct cxl_mem_query_commands)

Hi,
I could have missed it, but IOCTL major "numbers" (like 'C') should be listed in Documentation/userspace-api/ioctl/ioctl-number.rst.

thanks.

Signature · 1 line
~Randy
#16 lore raw

Re: [RFC PATCH 09/14] cxl/mem: Add basic IOCTL interface

Quoted 10 lines · Randy Dunlap

Review

Quoted 2 lines · Ben Widawsky
(quoted diff) +2 -0
+
+#define CXL_MEM_QUERY_COMMANDS _IOR('C', 1, struct cxl_mem_query_commands)

Hi,
I could have missed it, but IOCTL major "numbers" (like 'C') should be listed in Documentation/userspace-api/ioctl/ioctl-number.rst.

thanks.

I was unaware of this. Fixing for v3 without conflict.

Thanks.
Ben

#17 lore raw

RFC PATCH v2 10/14 cxl/mem: Add send command

In reply to Ben Widawsky

The send command allows userspace to issue mailbox commands directly to the hardware. The driver will verify basic properties of the command but otherwise pass any input payload untouched to the hardware, and return the output payload to userspace.

The caller of this IOCTL is required to allocate enough space for max(size_in, size_out) of the payload. The payload input data will be wiped out if any output payload exists.

---
 drivers/cxl/mem.c            | 127 +++++++++++++++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h |  35 ++++++++++
 2 files changed, 162 insertions(+)
drivers/cxl/mem.c +127 -0
index 2c4aadcea0e4..0bf03afc0c80 100644
@@ -324,6 +324,120 @@ static int cxl_mem_count_commands(void)
 	}
 
 	return n;
+};
+
+/**
+ * handle_mailbox_cmd_from_user() - Dispatch a mailbox command.
+ * @cxlmd: The CXL memory device to communicate with.
+ * @cmd: The validated command
+ * @u: The command submitted by userspace. Only useful for RAW commands.
+ *
+ * Return: 0 on success.
+ *
+ * This function packages up a &struct mbox_cmd on behalf of userspace,
+ * dispatches the command, and returns the results.
+ */
+static int handle_mailbox_cmd_from_user(struct cxl_memdev *cxlmd,
+					const struct cxl_mem_command *cmd,
+					struct cxl_send_command __user *u)
+{
+	struct mbox_cmd mbox_cmd;
+	ssize_t payload_size;
+	void *payload;
+	u32 size_out;
+	int rc;
+
+	if (get_user(size_out, &u->size_out))
+		return -EFAULT;
+
+	payload_size = max_t(ssize_t, cmd->info.size_in, size_out);
+	if (payload_size) {
+		payload =
+			memdup_user(u64_to_user_ptr(u->payload), payload_size);
+		if (IS_ERR(payload))
+			return PTR_ERR(payload);
+	}
+
+	rc = cxl_mem_mbox_get(cxlmd->cxlm);
+	if (rc)
+		return rc;
+
+	mbox_cmd = (struct mbox_cmd){
+		.opcode = cmd->opcode,
+		.payload = payload,
+		.size_in = cmd->info.size_in,
+		.size_out = size_out,
+	};
+	rc = cxl_mem_mbox_send_cmd(cxlmd->cxlm, &mbox_cmd);
+	cxl_mem_mbox_put(cxlmd->cxlm);
+	if (rc)
+		goto out;
+
+	rc = put_user(mbox_cmd.return_code, &u->retval);
+	if (rc)
+		goto out;
+
+	rc = put_user(mbox_cmd.size_out, &u->size_out);
+	if (rc)
+		goto out;
+
+	if (mbox_cmd.size_out)
+		if (copy_to_user(u64_to_user_ptr(u->payload), payload,
+				 mbox_cmd.size_out))
+			rc = -EFAULT;
+
+out:
+	if (payload_size)
+		kfree(payload);
+	return rc;
+}
+
+/**
+ * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND.
+ * @user_cmd: &struct cxl_send_command from userspace.
+ * @out_cmd: Sanitized and populared &struct cxl_mem_command.
+ *
+ * Return:
+ *  * %0	- Command dispatched successfully.
+ *  * %-EFAULT	- Something happened with copy_to/from_user.
+ *  * %-EINVAL	- Rerserved fields were used.
+ *  * %-EPERM	- Protected command used by the RAW interface.
+ *  * %-ENOMEM	- Input or output buffer wasn't large enough.
+ *
+ */
+static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd,
+				      struct cxl_mem_command *out_cmd)
+{
+	const struct cxl_command_info *info;
+	struct cxl_send_command cmd;
+	struct cxl_mem_command *c;
+
+	if (copy_from_user(&cmd, user_cmd, sizeof(cmd)))
+		return -EFAULT;
+
+	if (cmd.id == 0 || cmd.id >= CXL_MEM_COMMAND_ID_MAX)
+		return -EINVAL;
+
+	c = &mem_commands[cmd.id];
+	info = &c->info;
+
+	if (cmd.flags & CXL_MEM_COMMAND_FLAG_MASK)
+		return -EINVAL;
+
+	if (cmd.rsvd)
+		return -EINVAL;
+
+	/* Check the input buffer is the expected size */
+	if (info->size_in >= 0 && info->size_in != cmd.size_in)
+		return -ENOMEM;
+
+	/* Check the output buffer is at least large enough */
+	if (info->size_out >= 0 && cmd.size_out < info->size_out)
+		return -ENOMEM;
+
+	memcpy(out_cmd, c, sizeof(*c));
+
+	return 0;
 }
 
 static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
@@ -357,6 +471,19 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
 
 			j++;
 		}
+
+		return 0;
+	} else if (cmd == CXL_MEM_SEND_COMMAND) {
+		struct cxl_send_command __user *u = (void __user *)arg;
+		struct cxl_memdev *cxlmd = file->private_data;
+		struct cxl_mem_command c;
+		int rc;
+
+		rc = cxl_validate_cmd_from_user(u, &c);
+		if (rc)
+			return rc;
+
+		return handle_mailbox_cmd_from_user(cxlmd, &c, u);
 	}
 
 	return -ENOTTY;
include/uapi/linux/cxl_mem.h +35 -0
index 1d1e143f98ec..189d86a13637 100644
@@ -19,6 +19,7 @@ extern "C" {
  */
 
 #define CXL_MEM_QUERY_COMMANDS _IOR('C', 1, struct cxl_mem_query_commands)
+#define CXL_MEM_SEND_COMMAND _IOWR('C', 2, struct cxl_send_command)
 
 #define CXL_MEM_COMMAND_NAME_LENGTH 32
 
@@ -48,10 +49,12 @@ extern "C" {
 struct cxl_command_info {
 	__u32 id;
 #define CXL_MEM_COMMAND_ID_INVALID 0
+#define CXL_MEM_COMMAND_ID_MAX (CXL_MEM_COMMAND_ID_INVALID + 1)
 
 	__u32 flags;
 #define CXL_MEM_COMMAND_FLAG_NONE 0
 #define CXL_MEM_COMMAND_FLAG_TAINT BIT(0)
+#define CXL_MEM_COMMAND_FLAG_MASK ~BIT(0)
 
 	__s32 size_in;
 	__s32 size_out;
@@ -95,6 +98,38 @@ struct cxl_mem_query_commands {
 	struct cxl_command_info __user commands[]; /* out: supported commands */
 };
 
+/**
+ * struct cxl_send_command - Send a command to a memory device.
+ * @id: The command to send to the memory device. This must be one of the
+ *	commands returned by the query command.
+ * @flags: Flags for the command
+ * @rsvd: Reserved for future use.
+ * @retval: Return value from the memory device (output).
+ * @size_in: Size of the payload to provide to the device (input).
+ * @size_out: Size of the payload received from the device (input/output). This
+ *	      field is filled in my userspace to let the driver know how much
+ *	      space was allocated for output. It is populated by the driver to
+ *	      let userspace know how large the output payload actually was.
+ * @payload: Pointer to memory available for payload input/output.
+ *
+ * Mechanism for userspace to send a command to the hardware for processing. The
+ * driver will do basic validation on the command sizes, but the payload input
+ * and output are not introspected. Userspace is required to allocate large
+ * enough buffers for max(size_in, size_out).
+ */
+struct cxl_send_command {
+	__u32 id;
+	__u32 flags;
+	__u32 rsvd;
+	__u32 retval;
+
+	struct {
+		__s32 size_in;
+		__s32 size_out;
+		__u64 payload;
+	};
+};
+
 #if defined(__cplusplus)
 }
 #endif
Signature · 1 line
2.29.2
#18 lore raw

Re: [RFC PATCH 10/14] cxl/mem: Add send command

Quoted 9 lines · Ben Widawsky

The send command allows userspace to issue mailbox commands directly to the hardware. The driver will verify basic properties of the command but otherwise pass any input payload untouched to the hardware, and return the output payload to userspace.

The caller of this IOCTL is required to allocate enough space for max(size_in, size_out) of the payload. The payload input data will be wiped out if any output payload exists.

Seems awkward for the kernel to overwrite input payloads. I can see that potentially complicating userspace if it has the same payload to send to multiple devices. What's the rationale for overwriting input?

Quoted 35 lines
---
 drivers/cxl/mem.c            | 127 +++++++++++++++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h |  35 ++++++++++
 2 files changed, 162 insertions(+)
drivers/cxl/mem.c +20 -0
index 2c4aadcea0e4..0bf03afc0c80 100644
@@ -324,6 +324,120 @@ static int cxl_mem_count_commands(void)
        }
 
        return n;
+};
+
+/**
+ * handle_mailbox_cmd_from_user() - Dispatch a mailbox command.
+ * @cxlmd: The CXL memory device to communicate with.
+ * @cmd: The validated command
+ * @u: The command submitted by userspace. Only useful for RAW commands.
+ *
+ * Return: 0 on success.
+ *
+ * This function packages up a &struct mbox_cmd on behalf of userspace,
+ * dispatches the command, and returns the results.
+ */
+static int handle_mailbox_cmd_from_user(struct cxl_memdev *cxlmd,
+                                       const struct cxl_mem_command *cmd,
+                                       struct cxl_send_command __user *u)
+{
+       struct mbox_cmd mbox_cmd;
+       ssize_t payload_size;
+       void *payload;

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 356

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+       u32 size_out;
+       int rc;
+
+       if (get_user(size_out, &u->size_out))
+               return -EFAULT;
+
+       payload_size = max_t(ssize_t, cmd->info.size_in, size_out);
+       if (payload_size) {
+               payload =
+                       memdup_user(u64_to_user_ptr(u->payload), payload_size);

Me thinks this should be vmemdup_user() for payloads that exceed the kmalloc() max, and I think it would be worthwhile to clamp @size_out to some maximum and not let userspace ask for gigantic payloads. Return EINVAL for payloads greater than... 4MB? At least 4MB is the arbitrary max that libnvdimm picked.

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 365

Quoted 9 lines
drivers/cxl/mem.c +9 -0
+               if (IS_ERR(payload))
+                       return PTR_ERR(payload);
+       }
+
+       rc = cxl_mem_mbox_get(cxlmd->cxlm);
+       if (rc)
+               return rc;
+
+       mbox_cmd = (struct mbox_cmd){

I'd expect a space between: ){ ...looks like clang-format does not have that rule.

Quoted 22 lines
drivers/cxl/mem.c +22 -0
+               .opcode = cmd->opcode,
+               .payload = payload,
+               .size_in = cmd->info.size_in,
+               .size_out = size_out,
+       };
+       rc = cxl_mem_mbox_send_cmd(cxlmd->cxlm, &mbox_cmd);
+       cxl_mem_mbox_put(cxlmd->cxlm);
+       if (rc)
+               goto out;
+
+       rc = put_user(mbox_cmd.return_code, &u->retval);
+       if (rc)
+               goto out;
+
+       rc = put_user(mbox_cmd.size_out, &u->size_out);
+       if (rc)
+               goto out;
+
+       if (mbox_cmd.size_out)
+               if (copy_to_user(u64_to_user_ptr(u->payload), payload,
+                                mbox_cmd.size_out))
+                       rc = -EFAULT;

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 397

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+
+out:
+       if (payload_size)
+               kfree(payload);
+       return rc;
+}
+
+/**
+ * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND.
+ * @user_cmd: &struct cxl_send_command from userspace.

Ah cool, I did not realize kernel doc recognized type specifiers like that, nice.

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 398

Quoted 1 line
drivers/cxl/mem.c +1 -0
+ * @out_cmd: Sanitized and populared &struct cxl_mem_command.

s/populared/populated/

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 403

Quoted 5 lines
drivers/cxl/mem.c +5 -0
+ *
+ * Return:
+ *  * %0       - Command dispatched successfully.
+ *  * %-EFAULT - Something happened with copy_to/from_user.
+ *  * %-EINVAL - Rerserved fields were used.

s/Rerserved/Reserved/

Quoted 6 lines
drivers/cxl/mem.c +6 -0
+ *  * %-EPERM  - Protected command used by the RAW interface.
+ *  * %-ENOMEM - Input or output buffer wasn't large enough.
+ *
+ */
+static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd,
+                                     struct cxl_mem_command *out_cmd)

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 419

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+{
+       const struct cxl_command_info *info;
+       struct cxl_send_command cmd;
+       struct cxl_mem_command *c;
+
+       if (copy_from_user(&cmd, user_cmd, sizeof(cmd)))
+               return -EFAULT;
+
+       if (cmd.id == 0 || cmd.id >= CXL_MEM_COMMAND_ID_MAX)
+               return -EINVAL;

I wonder if the "cmd.id >= CXL_MEM_COMMAND_ID_MAX" case should return -ENOTTY. The command might be perfectly valid, just the kernel does not have that command specified which would be the case with older kernel with newer userspace.

Quoted 9 lines
drivers/cxl/mem.c +9 -0
+
+       c = &mem_commands[cmd.id];
+       info = &c->info;
+
+       if (cmd.flags & CXL_MEM_COMMAND_FLAG_MASK)
+               return -EINVAL;
+
+       if (cmd.rsvd)
+               return -EINVAL;

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 438

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+
+       /* Check the input buffer is the expected size */
+       if (info->size_in >= 0 && info->size_in != cmd.size_in)
+               return -ENOMEM;
+
+       /* Check the output buffer is at least large enough */
+       if (info->size_out >= 0 && cmd.size_out < info->size_out)
+               return -ENOMEM;
+
+       memcpy(out_cmd, c, sizeof(*c));

Why not do the copy_from_user() directly into out_cmd to save a copy operation?

Quoted 38 lines
drivers/cxl/mem.c +15 -0
+
+       return 0;
 }
 
 static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
@@ -357,6 +471,19 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
 
                        j++;
                }
+
+               return 0;
+       } else if (cmd == CXL_MEM_SEND_COMMAND) {
+               struct cxl_send_command __user *u = (void __user *)arg;
+               struct cxl_memdev *cxlmd = file->private_data;
+               struct cxl_mem_command c;
+               int rc;
+
+               rc = cxl_validate_cmd_from_user(u, &c);
+               if (rc)
+                       return rc;
+
+               return handle_mailbox_cmd_from_user(cxlmd, &c, u);
        }
 
        return -ENOTTY;
include/uapi/linux/cxl_mem.h +1 -0
index 1d1e143f98ec..189d86a13637 100644
@@ -19,6 +19,7 @@ extern "C" {
  */
 
 #define CXL_MEM_QUERY_COMMANDS _IOR('C', 1, struct cxl_mem_query_commands)
+#define CXL_MEM_SEND_COMMAND _IOWR('C', 2, struct cxl_send_command)
 
 #define CXL_MEM_COMMAND_NAME_LENGTH 32
 

include/uapi/linux/cxl_mem.h extern "C" { · line 56

Quoted 8 lines
include/uapi/linux/cxl_mem.h +1 -0
@@ -48,10 +49,12 @@ extern "C" {
 struct cxl_command_info {
        __u32 id;
 #define CXL_MEM_COMMAND_ID_INVALID 0
+#define CXL_MEM_COMMAND_ID_MAX (CXL_MEM_COMMAND_ID_INVALID + 1)
 
        __u32 flags;
 #define CXL_MEM_COMMAND_FLAG_NONE 0
 #define CXL_MEM_COMMAND_FLAG_TAINT BIT(0)

I guess I missed this in the last patch, why would userspace specify a taint flag? @flags is an output parameter?

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 105

Quoted 13 lines
include/uapi/linux/cxl_mem.h +6 -0
+#define CXL_MEM_COMMAND_FLAG_MASK ~BIT(0)
 
        __s32 size_in;
        __s32 size_out;
@@ -95,6 +98,38 @@ struct cxl_mem_query_commands {
        struct cxl_command_info __user commands[]; /* out: supported commands */
 };
 
+/**
+ * struct cxl_send_command - Send a command to a memory device.
+ * @id: The command to send to the memory device. This must be one of the
+ *     commands returned by the query command.
+ * @flags: Flags for the command

in / out field?

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 106

Quoted 1 line
include/uapi/linux/cxl_mem.h +1 -0
+ * @rsvd: Reserved for future use.

Document 'must be zero' to match the code?

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 107

Quoted 1 line
include/uapi/linux/cxl_mem.h +1 -0
+ * @retval: Return value from the memory device (output).

Is this a translated error code or the raw

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 110

Quoted 3 lines
include/uapi/linux/cxl_mem.h +3 -0
+ * @size_in: Size of the payload to provide to the device (input).
+ * @size_out: Size of the payload received from the device (input/output). This
+ *           field is filled in my userspace to let the driver know how much

s/my/by/

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 117

Quoted 7 lines
include/uapi/linux/cxl_mem.h +7 -0
+ *           space was allocated for output. It is populated by the driver to
+ *           let userspace know how large the output payload actually was.
+ * @payload: Pointer to memory available for payload input/output.
+ *
+ * Mechanism for userspace to send a command to the hardware for processing. The
+ * driver will do basic validation on the command sizes, but the payload input
+ * and output are not introspected. Userspace is required to allocate large

They might be introspected, at least there were some NVDIMM ioctls like "clear error" where the driver in addition to telling the device to clear the error also cleaned up the kernel's error tracking list. I'd say one of the benefits of Linux-commands that get translated to CXL-specific payloads is that they allow for kernel translation / interpretation / quirking vs 'raw' where the kernel does not care.

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 129

Quoted 12 lines
include/uapi/linux/cxl_mem.h +12 -0
+ * enough buffers for max(size_in, size_out).
+ */
+struct cxl_send_command {
+       __u32 id;
+       __u32 flags;
+       __u32 rsvd;
+       __u32 retval;
+
+       struct {
+               __s32 size_in;
+               __s32 size_out;
+               __u64 payload;

per the initial comment above a separate @payload_in and a @payload_out pointer would seem to be more flexible and get out of the situation where the kernel needs to clobber inputs.

#19 lore raw

Re: [RFC PATCH 10/14] cxl/mem: Add send command

Quoted 7 lines · Dan Williams
Quoted 6 lines · Ben Widawsky

The send command allows userspace to issue mailbox commands directly to the hardware. The driver will verify basic properties of the command but otherwise pass any input payload untouched to the hardware, and return the output payload to userspace.

Quoted 8 lines
Quoted 3 lines

The caller of this IOCTL is required to allocate enough space for max(size_in, size_out) of the payload. The payload input data will be wiped out if any output payload exists.

Seems awkward for the kernel to overwrite input payloads. I can see that potentially complicating userspace if it has the same payload to send to multiple devices. What's the rationale for overwriting input?

I couldn't think of a case where preserving the input payload would be beneficial and I actually think it's a bit simpler with just one payload for both sides - however, since you came up with a good usage, I will modify this.

Quoted 43 lines
Quoted 43 lines
---
 drivers/cxl/mem.c            | 127 +++++++++++++++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h |  35 ++++++++++
 2 files changed, 162 insertions(+)
drivers/cxl/mem.c +28 -0
index 2c4aadcea0e4..0bf03afc0c80 100644
@@ -324,6 +324,120 @@ static int cxl_mem_count_commands(void)
        }
 
        return n;
+};
+
+/**
+ * handle_mailbox_cmd_from_user() - Dispatch a mailbox command.
+ * @cxlmd: The CXL memory device to communicate with.
+ * @cmd: The validated command
+ * @u: The command submitted by userspace. Only useful for RAW commands.
+ *
+ * Return: 0 on success.
+ *
+ * This function packages up a &struct mbox_cmd on behalf of userspace,
+ * dispatches the command, and returns the results.
+ */
+static int handle_mailbox_cmd_from_user(struct cxl_memdev *cxlmd,
+                                       const struct cxl_mem_command *cmd,
+                                       struct cxl_send_command __user *u)
+{
+       struct mbox_cmd mbox_cmd;
+       ssize_t payload_size;
+       void *payload;
+       u32 size_out;
+       int rc;
+
+       if (get_user(size_out, &u->size_out))
+               return -EFAULT;
+
+       payload_size = max_t(ssize_t, cmd->info.size_in, size_out);
+       if (payload_size) {
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 356

Quoted 2 lines
drivers/cxl/mem.c +2 -0
+               payload =
+                       memdup_user(u64_to_user_ptr(u->payload), payload_size);

Me thinks this should be vmemdup_user() for payloads that exceed the kmalloc() max, and I think it would be worthwhile to clamp @size_out to some maximum and not let userspace ask for gigantic payloads. Return EINVAL for payloads greater than... 4MB? At least 4MB is the arbitrary max that libnvdimm picked.

This is handled in cxl_validate_cmd_from_user() currently. The current API allows the user to specify as large as they like with @size_out but the kernel only writes out as much as the hardware returns.... More on this below [1].

Quoted 74 lines

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 365

Quoted 9 lines
drivers/cxl/mem.c +9 -0
+               if (IS_ERR(payload))
+                       return PTR_ERR(payload);
+       }
+
+       rc = cxl_mem_mbox_get(cxlmd->cxlm);
+       if (rc)
+               return rc;
+
+       mbox_cmd = (struct mbox_cmd){

I'd expect a space between: ){ ...looks like clang-format does not have that rule.

Quoted 22 lines
drivers/cxl/mem.c +22 -0
+               .opcode = cmd->opcode,
+               .payload = payload,
+               .size_in = cmd->info.size_in,
+               .size_out = size_out,
+       };
+       rc = cxl_mem_mbox_send_cmd(cxlmd->cxlm, &mbox_cmd);
+       cxl_mem_mbox_put(cxlmd->cxlm);
+       if (rc)
+               goto out;
+
+       rc = put_user(mbox_cmd.return_code, &u->retval);
+       if (rc)
+               goto out;
+
+       rc = put_user(mbox_cmd.size_out, &u->size_out);
+       if (rc)
+               goto out;
+
+       if (mbox_cmd.size_out)
+               if (copy_to_user(u64_to_user_ptr(u->payload), payload,
+                                mbox_cmd.size_out))
+                       rc = -EFAULT;

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 397

Quoted 10 lines
drivers/cxl/mem.c +10 -0
+
+out:
+       if (payload_size)
+               kfree(payload);
+       return rc;
+}
+
+/**
+ * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND.
+ * @user_cmd: &struct cxl_send_command from userspace.

Ah cool, I did not realize kernel doc recognized type specifiers like that, nice.

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 398

Quoted 1 line
drivers/cxl/mem.c +1 -0
+ * @out_cmd: Sanitized and populared &struct cxl_mem_command.

s/populared/populated/

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 403

Quoted 5 lines
drivers/cxl/mem.c +5 -0
+ *
+ * Return:
+ *  * %0       - Command dispatched successfully.
+ *  * %-EFAULT - Something happened with copy_to/from_user.
+ *  * %-EINVAL - Rerserved fields were used.

s/Rerserved/Reserved/

Quoted 13 lines
drivers/cxl/mem.c +13 -0
+ *  * %-EPERM  - Protected command used by the RAW interface.
+ *  * %-ENOMEM - Input or output buffer wasn't large enough.
+ *
+ */
+static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd,
+                                     struct cxl_mem_command *out_cmd)
+{
+       const struct cxl_command_info *info;
+       struct cxl_send_command cmd;
+       struct cxl_mem_command *c;
+
+       if (copy_from_user(&cmd, user_cmd, sizeof(cmd)))
+               return -EFAULT;
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 419

Quoted 3 lines
drivers/cxl/mem.c +3 -0
+
+       if (cmd.id == 0 || cmd.id >= CXL_MEM_COMMAND_ID_MAX)
+               return -EINVAL;

I wonder if the "cmd.id >= CXL_MEM_COMMAND_ID_MAX" case should return -ENOTTY. The command might be perfectly valid, just the kernel does not have that command specified which would be the case with older kernel with newer userspace.

I think ENOTTY could be a bit confusing here as it could be confused for the SEND ioctl not being present.

What about ENOENT, or something else?

Quoted 13 lines
Quoted 13 lines
drivers/cxl/mem.c +13 -0
+
+       c = &mem_commands[cmd.id];
+       info = &c->info;
+
+       if (cmd.flags & CXL_MEM_COMMAND_FLAG_MASK)
+               return -EINVAL;
+
+       if (cmd.rsvd)
+               return -EINVAL;
+
+       /* Check the input buffer is the expected size */
+       if (info->size_in >= 0 && info->size_in != cmd.size_in)
+               return -ENOMEM;
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 438

Quoted 6 lines
drivers/cxl/mem.c +6 -0
+
+       /* Check the output buffer is at least large enough */
+       if (info->size_out >= 0 && cmd.size_out < info->size_out)
+               return -ENOMEM;
+
+       memcpy(out_cmd, c, sizeof(*c));

Why not do the copy_from_user() directly into out_cmd to save a copy operation?

[1]
cxl_validate_cmd_from_user() essentially translates a user's command into the internal kernel representation of the command via the lookup into the array. This will do things like adjust the output size or flags, to prevent userspace from doing nefarious things. An actual command, especially now that you had me remove the name[32] will be relatively small and I see the extra copy as being well worth being able to isolate this command sanitization.

Quoted 43 lines
Quoted 43 lines
drivers/cxl/mem.c +15 -0
+
+       return 0;
 }
 
 static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
@@ -357,6 +471,19 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
 
                        j++;
                }
+
+               return 0;
+       } else if (cmd == CXL_MEM_SEND_COMMAND) {
+               struct cxl_send_command __user *u = (void __user *)arg;
+               struct cxl_memdev *cxlmd = file->private_data;
+               struct cxl_mem_command c;
+               int rc;
+
+               rc = cxl_validate_cmd_from_user(u, &c);
+               if (rc)
+                       return rc;
+
+               return handle_mailbox_cmd_from_user(cxlmd, &c, u);
        }
 
        return -ENOTTY;
include/uapi/linux/cxl_mem.h +2 -0
index 1d1e143f98ec..189d86a13637 100644
@@ -19,6 +19,7 @@ extern "C" {
  */
 
 #define CXL_MEM_QUERY_COMMANDS _IOR('C', 1, struct cxl_mem_query_commands)
+#define CXL_MEM_SEND_COMMAND _IOWR('C', 2, struct cxl_send_command)
 
 #define CXL_MEM_COMMAND_NAME_LENGTH 32
 
@@ -48,10 +49,12 @@ extern "C" {
 struct cxl_command_info {
        __u32 id;
 #define CXL_MEM_COMMAND_ID_INVALID 0
+#define CXL_MEM_COMMAND_ID_MAX (CXL_MEM_COMMAND_ID_INVALID + 1)
 
Quoted 7 lines

include/uapi/linux/cxl_mem.h extern "C" { · line 56

Quoted 3 lines
include/uapi/linux/cxl_mem.h +0 -0
        __u32 flags;
 #define CXL_MEM_COMMAND_FLAG_NONE 0
 #define CXL_MEM_COMMAND_FLAG_TAINT BIT(0)

I guess I missed this in the last patch, why would userspace specify a taint flag? @flags is an output parameter?

The flag is from the kernel to userspace to let them know that using the command would taint the kernel.

Quoted 44 lines

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 105

Quoted 13 lines
include/uapi/linux/cxl_mem.h +6 -0
+#define CXL_MEM_COMMAND_FLAG_MASK ~BIT(0)
 
        __s32 size_in;
        __s32 size_out;
@@ -95,6 +98,38 @@ struct cxl_mem_query_commands {
        struct cxl_command_info __user commands[]; /* out: supported commands */
 };
 
+/**
+ * struct cxl_send_command - Send a command to a memory device.
+ * @id: The command to send to the memory device. This must be one of the
+ *     commands returned by the query command.
+ * @flags: Flags for the command

in / out field?

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 106

Quoted 1 line
include/uapi/linux/cxl_mem.h +1 -0
+ * @rsvd: Reserved for future use.

Document 'must be zero' to match the code?

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 107

Quoted 1 line
include/uapi/linux/cxl_mem.h +1 -0
+ * @retval: Return value from the memory device (output).

Is this a translated error code or the raw

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 110

Quoted 3 lines
include/uapi/linux/cxl_mem.h +3 -0
+ * @size_in: Size of the payload to provide to the device (input).
+ * @size_out: Size of the payload received from the device (input/output). This
+ *           field is filled in my userspace to let the driver know how much

s/my/by/

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 117

Quoted 7 lines
include/uapi/linux/cxl_mem.h +7 -0
+ *           space was allocated for output. It is populated by the driver to
+ *           let userspace know how large the output payload actually was.
+ * @payload: Pointer to memory available for payload input/output.
+ *
+ * Mechanism for userspace to send a command to the hardware for processing. The
+ * driver will do basic validation on the command sizes, but the payload input
+ * and output are not introspected. Userspace is required to allocate large

They might be introspected, at least there were some NVDIMM ioctls like "clear error" where the driver in addition to telling the device to clear the error also cleaned up the kernel's error tracking list. I'd say one of the benefits of Linux-commands that get translated to CXL-specific payloads is that they allow for kernel translation / interpretation / quirking vs 'raw' where the kernel does not care.

Quoted 16 lines

include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 129

Quoted 12 lines
include/uapi/linux/cxl_mem.h +12 -0
+ * enough buffers for max(size_in, size_out).
+ */
+struct cxl_send_command {
+       __u32 id;
+       __u32 flags;
+       __u32 rsvd;
+       __u32 retval;
+
+       struct {
+               __s32 size_in;
+               __s32 size_out;
+               __u64 payload;

per the initial comment above a separate @payload_in and a @payload_out pointer would seem to be more flexible and get out of the situation where the kernel needs to clobber inputs.

Anything I didn't specifically respond to, I'm working on fixing.

#20 lore raw

Re: [RFC PATCH 10/14] cxl/mem: Add send command

On Tue, Dec 15, 2020 at 1:44 PM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 28 lines
Quoted 15 lines
Quoted 15 lines
drivers/cxl/mem.c +15 -0
+static int handle_mailbox_cmd_from_user(struct cxl_memdev *cxlmd,
+                                       const struct cxl_mem_command *cmd,
+                                       struct cxl_send_command __user *u)
+{
+       struct mbox_cmd mbox_cmd;
+       ssize_t payload_size;
+       void *payload;
+       u32 size_out;
+       int rc;
+
+       if (get_user(size_out, &u->size_out))
+               return -EFAULT;
+
+       payload_size = max_t(ssize_t, cmd->info.size_in, size_out);
+       if (payload_size) {
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 356

Quoted 2 lines
drivers/cxl/mem.c +2 -0
+               payload =
+                       memdup_user(u64_to_user_ptr(u->payload), payload_size);

Me thinks this should be vmemdup_user() for payloads that exceed the kmalloc() max, and I think it would be worthwhile to clamp @size_out to some maximum and not let userspace ask for gigantic payloads. Return EINVAL for payloads greater than... 4MB? At least 4MB is the arbitrary max that libnvdimm picked.

This is handled in cxl_validate_cmd_from_user() currently. The current API allows the user to specify as large as they like with @size_out but the kernel only writes out as much as the hardware returns.... More on this below [1].

[..]

Quoted 23 lines
Quoted 13 lines
Quoted 13 lines

+ * * %-EPERM - Protected command used by the RAW interface.
+ * * %-ENOMEM - Input or output buffer wasn't large enough.
+ *
+ */

drivers/cxl/mem.c +9 -0
+static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd,
+                                     struct cxl_mem_command *out_cmd)
+{
+       const struct cxl_command_info *info;
+       struct cxl_send_command cmd;
+       struct cxl_mem_command *c;
+
+       if (copy_from_user(&cmd, user_cmd, sizeof(cmd)))
+               return -EFAULT;
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 419

Quoted 3 lines
drivers/cxl/mem.c +3 -0
+
+       if (cmd.id == 0 || cmd.id >= CXL_MEM_COMMAND_ID_MAX)
+               return -EINVAL;

I wonder if the "cmd.id >= CXL_MEM_COMMAND_ID_MAX" case should return -ENOTTY. The command might be perfectly valid, just the kernel does not have that command specified which would be the case with older kernel with newer userspace.

Quoted 4 lines

I think ENOTTY could be a bit confusing here as it could be confused for the SEND ioctl not being present.

What about ENOENT, or something else?

"No such file or directory" seems more confusing to me than "Inappropriate I/O control operation". What version of this driver, once it's upstream, will not support the SEND ioctl? I think it is safe to assume that the mere fact that the driver is attached means SEND is present.

Quoted 23 lines
Quoted 13 lines
Quoted 13 lines
drivers/cxl/mem.c +13 -0
+
+       c = &mem_commands[cmd.id];
+       info = &c->info;
+
+       if (cmd.flags & CXL_MEM_COMMAND_FLAG_MASK)
+               return -EINVAL;
+
+       if (cmd.rsvd)
+               return -EINVAL;
+
+       /* Check the input buffer is the expected size */
+       if (info->size_in >= 0 && info->size_in != cmd.size_in)
+               return -ENOMEM;
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 438

Quoted 6 lines
drivers/cxl/mem.c +6 -0
+
+       /* Check the output buffer is at least large enough */
+       if (info->size_out >= 0 && cmd.size_out < info->size_out)
+               return -ENOMEM;
+
+       memcpy(out_cmd, c, sizeof(*c));

Why not do the copy_from_user() directly into out_cmd to save a copy operation?

Quoted 7 lines

[1]
cxl_validate_cmd_from_user() essentially translates a user's command into the internal kernel representation of the command via the lookup into the array. This will do things like adjust the output size or flags, to prevent userspace from doing nefarious things. An actual command, especially now that you had me remove the name[32] will be relatively small and I see the extra copy as being well worth being able to isolate this command sanitization.

I can buy that, but it does not answer my other question above about cases where a valid command has size_{in,out} larger than the kmalloc max. vmemdup_user() lets you support arbitrary command payload sizes larger than KMALLOC_MAX_SIZE. For example, I might want to overwrite the entire label area at once in a single write.

Quoted 52 lines
Quoted 44 lines
Quoted 44 lines
drivers/cxl/mem.c +15 -0
+
+       return 0;
 }
 
 static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
@@ -357,6 +471,19 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
 
                        j++;
                }
+
+               return 0;
+       } else if (cmd == CXL_MEM_SEND_COMMAND) {
+               struct cxl_send_command __user *u = (void __user *)arg;
+               struct cxl_memdev *cxlmd = file->private_data;
+               struct cxl_mem_command c;
+               int rc;
+
+               rc = cxl_validate_cmd_from_user(u, &c);
+               if (rc)
+                       return rc;
+
+               return handle_mailbox_cmd_from_user(cxlmd, &c, u);
        }
 
        return -ENOTTY;
include/uapi/linux/cxl_mem.h +2 -0
index 1d1e143f98ec..189d86a13637 100644
@@ -19,6 +19,7 @@ extern "C" {
  */
 
 #define CXL_MEM_QUERY_COMMANDS _IOR('C', 1, struct cxl_mem_query_commands)
+#define CXL_MEM_SEND_COMMAND _IOWR('C', 2, struct cxl_send_command)
 
 #define CXL_MEM_COMMAND_NAME_LENGTH 32
 
@@ -48,10 +49,12 @@ extern "C" {
 struct cxl_command_info {
        __u32 id;
 #define CXL_MEM_COMMAND_ID_INVALID 0
+#define CXL_MEM_COMMAND_ID_MAX (CXL_MEM_COMMAND_ID_INVALID + 1)
 
Quoted 7 lines

include/uapi/linux/cxl_mem.h extern "C" { · line 56

Quoted 3 lines
include/uapi/linux/cxl_mem.h +0 -0
        __u32 flags;
 #define CXL_MEM_COMMAND_FLAG_NONE 0
 #define CXL_MEM_COMMAND_FLAG_TAINT BIT(0)

I guess I missed this in the last patch, why would userspace specify a taint flag? @flags is an output parameter?

Quoted 2 lines

The flag is from the kernel to userspace to let them know that using the command would taint the kernel.

...but they should know that by the fact that they are using the RAW send. I.e. document it once rather than include it in the ABI. I think a more useful flag would be to identify which commands may be blocked because they are exclusively owned by the kernel.

#21 lore raw

Re: [RFC PATCH 10/14] cxl/mem: Add send command

Quoted 57 lines · Dan Williams

On Tue, Dec 15, 2020 at 1:44 PM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 28 lines
Quoted 15 lines
Quoted 15 lines
drivers/cxl/mem.c +15 -0
+static int handle_mailbox_cmd_from_user(struct cxl_memdev *cxlmd,
+                                       const struct cxl_mem_command *cmd,
+                                       struct cxl_send_command __user *u)
+{
+       struct mbox_cmd mbox_cmd;
+       ssize_t payload_size;
+       void *payload;
+       u32 size_out;
+       int rc;
+
+       if (get_user(size_out, &u->size_out))
+               return -EFAULT;
+
+       payload_size = max_t(ssize_t, cmd->info.size_in, size_out);
+       if (payload_size) {
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 356

Quoted 2 lines
drivers/cxl/mem.c +2 -0
+               payload =
+                       memdup_user(u64_to_user_ptr(u->payload), payload_size);

Me thinks this should be vmemdup_user() for payloads that exceed the kmalloc() max, and I think it would be worthwhile to clamp @size_out to some maximum and not let userspace ask for gigantic payloads. Return EINVAL for payloads greater than... 4MB? At least 4MB is the arbitrary max that libnvdimm picked.

This is handled in cxl_validate_cmd_from_user() currently. The current API allows the user to specify as large as they like with @size_out but the kernel only writes out as much as the hardware returns.... More on this below [1].

[..]

Quoted 26 lines
Quoted 13 lines
Quoted 13 lines

+ * * %-EPERM - Protected command used by the RAW interface.
+ * * %-ENOMEM - Input or output buffer wasn't large enough.
+ *
+ */

drivers/cxl/mem.c +9 -0
+static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd,
+                                     struct cxl_mem_command *out_cmd)
+{
+       const struct cxl_command_info *info;
+       struct cxl_send_command cmd;
+       struct cxl_mem_command *c;
+
+       if (copy_from_user(&cmd, user_cmd, sizeof(cmd)))
+               return -EFAULT;
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 419

Quoted 3 lines
drivers/cxl/mem.c +3 -0
+
+       if (cmd.id == 0 || cmd.id >= CXL_MEM_COMMAND_ID_MAX)
+               return -EINVAL;

I wonder if the "cmd.id >= CXL_MEM_COMMAND_ID_MAX" case should return -ENOTTY. The command might be perfectly valid, just the kernel does not have that command specified which would be the case with older kernel with newer userspace.

I think ENOTTY could be a bit confusing here as it could be confused for the SEND ioctl not being present.

Quoted 8 lines
Quoted 1 line

What about ENOENT, or something else?

"No such file or directory" seems more confusing to me than "Inappropriate I/O control operation". What version of this driver, once it's upstream, will not support the SEND ioctl? I think it is safe to assume that the mere fact that the driver is attached means SEND is present.

No version. I had been thinking it gets confusing with a new IOCTL, but you're correct that it should be trivial to determine ENOTTY from Send is different than ENOTTY from another IOCTL.

I'll change it.

Quoted 31 lines
Quoted 23 lines
Quoted 13 lines
Quoted 13 lines
drivers/cxl/mem.c +13 -0
+
+       c = &mem_commands[cmd.id];
+       info = &c->info;
+
+       if (cmd.flags & CXL_MEM_COMMAND_FLAG_MASK)
+               return -EINVAL;
+
+       if (cmd.rsvd)
+               return -EINVAL;
+
+       /* Check the input buffer is the expected size */
+       if (info->size_in >= 0 && info->size_in != cmd.size_in)
+               return -ENOMEM;
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_count_commands(void) · line 438

Quoted 6 lines
drivers/cxl/mem.c +6 -0
+
+       /* Check the output buffer is at least large enough */
+       if (info->size_out >= 0 && cmd.size_out < info->size_out)
+               return -ENOMEM;
+
+       memcpy(out_cmd, c, sizeof(*c));

Why not do the copy_from_user() directly into out_cmd to save a copy operation?

Quoted 7 lines

[1]
cxl_validate_cmd_from_user() essentially translates a user's command into the internal kernel representation of the command via the lookup into the array. This will do things like adjust the output size or flags, to prevent userspace from doing nefarious things. An actual command, especially now that you had me remove the name[32] will be relatively small and I see the extra copy as being well worth being able to isolate this command sanitization.

Quoted 6 lines

I can buy that, but it does not answer my other question above about cases where a valid command has size_{in,out} larger than the kmalloc max. vmemdup_user() lets you support arbitrary command payload sizes larger than KMALLOC_MAX_SIZE. For example, I might want to overwrite the entire label area at once in a single write.

It does need to switch to vmemdup_user(), I didn't mean to disagree there.

For posterity btw, I'll point out CXL 2.0 max is 2M for payload.

Quoted 52 lines
Quoted 52 lines
Quoted 51 lines
Quoted 39 lines
drivers/cxl/mem.c +15 -0
+
+       return 0;
 }
 
 static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
@@ -357,6 +471,19 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
 
                        j++;
                }
+
+               return 0;
+       } else if (cmd == CXL_MEM_SEND_COMMAND) {
+               struct cxl_send_command __user *u = (void __user *)arg;
+               struct cxl_memdev *cxlmd = file->private_data;
+               struct cxl_mem_command c;
+               int rc;
+
+               rc = cxl_validate_cmd_from_user(u, &c);
+               if (rc)
+                       return rc;
+
+               return handle_mailbox_cmd_from_user(cxlmd, &c, u);
        }
 
        return -ENOTTY;
include/uapi/linux/cxl_mem.h +1 -0
index 1d1e143f98ec..189d86a13637 100644
@@ -19,6 +19,7 @@ extern "C" {
  */
 
 #define CXL_MEM_QUERY_COMMANDS _IOR('C', 1, struct cxl_mem_query_commands)
+#define CXL_MEM_SEND_COMMAND _IOWR('C', 2, struct cxl_send_command)
 
 #define CXL_MEM_COMMAND_NAME_LENGTH 32
 

include/uapi/linux/cxl_mem.h extern "C" { · line 56

Quoted 8 lines
include/uapi/linux/cxl_mem.h +1 -0
@@ -48,10 +49,12 @@ extern "C" {
 struct cxl_command_info {
        __u32 id;
 #define CXL_MEM_COMMAND_ID_INVALID 0
+#define CXL_MEM_COMMAND_ID_MAX (CXL_MEM_COMMAND_ID_INVALID + 1)
 
        __u32 flags;
 #define CXL_MEM_COMMAND_FLAG_NONE 0
 #define CXL_MEM_COMMAND_FLAG_TAINT BIT(0)

I guess I missed this in the last patch, why would userspace specify a taint flag? @flags is an output parameter?

Quoted 7 lines
Quoted 2 lines

The flag is from the kernel to userspace to let them know that using the command would taint the kernel.

...but they should know that by the fact that they are using the RAW send. I.e. document it once rather than include it in the ABI. I think a more useful flag would be to identify which commands may be blocked because they are exclusively owned by the kernel.

I think taint is something that could change between driver versions, and even based on the underlying device. However, since Raw command will always taint, we can save that for another day and I will drop it from this patch.

As an example, perhaps we know device X's active firmware update is flaky - we can make that a tainted command just for that device.

#22 lore raw

RFC PATCH v2 11/14 cxl/mem: Add a "RAW" send command

In reply to Ben Widawsky

The CXL memory device send interface will have a number of supported commands. The raw command is not such a command. Raw commands allow userspace to send a specified opcode to the underlying hardware and bypass all driver checks on the command. This is useful for a couple of usecases, mainly:
1. Undocumented vendor specific hardware commands
2. Prototyping new hardware commands not yet supported by the driver

While this all sounds very powerful it comes with a couple of caveats:
1. Bug reports using raw commands will not get the same level of attention as bug reports using supported commands (via taint).
2. Supported commands will be rejected by the RAW command.

---
 drivers/cxl/mem.c            | 32 ++++++++++++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h | 14 ++++++++++++--
 2 files changed, 44 insertions(+), 2 deletions(-)
drivers/cxl/mem.c +32 -0
index 0bf03afc0c80..a2cea7ac7cc6 100644
@@ -115,6 +115,7 @@ struct cxl_mem_command {
 
 static struct cxl_mem_command mem_commands[] = {
 	CXL_CMD(INVALID, NONE, 0, 0, "Reserved", false, 0),
+	CXL_CMD(RAW, TAINT, ~0, ~0, "Raw", true, 0),
 };
 
 static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
@@ -326,6 +327,20 @@ static int cxl_mem_count_commands(void)
 	return n;
 };
 
+static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(mem_commands); i++) {
+		struct cxl_mem_command *c = &mem_commands[i];
+
+		if (c->opcode == opcode)
+			return c;
+	}
+
+	return NULL;
+};
+
 /**
  * handle_mailbox_cmd_from_user() - Dispatch a mailbox command.
  * @cxlmd: The CXL memory device to communicate with.
@@ -421,6 +436,23 @@ static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd,
 	c = &mem_commands[cmd.id];
 	info = &c->info;
 
+	/* Checks are bypassed for raw commands but along comes the taint! */
+	if (cmd.id == CXL_MEM_COMMAND_ID_RAW) {
+		struct cxl_mem_command temp =
+			CXL_CMD(RAW, NONE, cmd.size_in, cmd.size_out, "Raw",
+				true, cmd.raw.opcode);
+
+		if (cmd.raw.rsvd)
+			return -EINVAL;
+
+		if (cxl_mem_find_command(cmd.raw.opcode))
+			return -EPERM;
+
+		add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
+		memcpy(out_cmd, &temp, sizeof(temp));
+		return 0;
+	}
+
 	if (cmd.flags & CXL_MEM_COMMAND_FLAG_MASK)
 		return -EINVAL;
 
include/uapi/linux/cxl_mem.h +12 -2
index 189d86a13637..f2fbb0dcda06 100644
@@ -49,7 +49,8 @@ extern "C" {
 struct cxl_command_info {
 	__u32 id;
 #define CXL_MEM_COMMAND_ID_INVALID 0
-#define CXL_MEM_COMMAND_ID_MAX (CXL_MEM_COMMAND_ID_INVALID + 1)
+#define CXL_MEM_COMMAND_ID_RAW 1
+#define CXL_MEM_COMMAND_ID_MAX (CXL_MEM_COMMAND_ID_RAW + 1)
 
 	__u32 flags;
 #define CXL_MEM_COMMAND_FLAG_NONE 0
@@ -103,6 +104,9 @@ struct cxl_mem_query_commands {
  * @id: The command to send to the memory device. This must be one of the
  *	commands returned by the query command.
  * @flags: Flags for the command
+ * @raw: Special fields for raw commands
+ * @raw.opcode: Opcode passed to hardware when using the RAW command.
+ * @raw.rsvd: Reserved for future use.
  * @rsvd: Reserved for future use.
  * @retval: Return value from the memory device (output).
  * @size_in: Size of the payload to provide to the device (input).
@@ -120,7 +124,13 @@ struct cxl_mem_query_commands {
 struct cxl_send_command {
 	__u32 id;
 	__u32 flags;
-	__u32 rsvd;
+	union {
+		struct {
+			__u16 opcode;
+			__u16 rsvd;
+		} raw;
+		__u32 rsvd;
+	};
 	__u32 retval;
 
 	struct {
Signature · 1 line
2.29.2
#23 lore raw

Re: [RFC PATCH 11/14] cxl/mem: Add a "RAW" send command

Quoted 25 lines · Ben Widawsky

The CXL memory device send interface will have a number of supported commands. The raw command is not such a command. Raw commands allow userspace to send a specified opcode to the underlying hardware and bypass all driver checks on the command. This is useful for a couple of usecases, mainly:
1. Undocumented vendor specific hardware commands
2. Prototyping new hardware commands not yet supported by the driver

While this all sounds very powerful it comes with a couple of caveats:
1. Bug reports using raw commands will not get the same level of attention as bug reports using supported commands (via taint).
2. Supported commands will be rejected by the RAW command.

---
 drivers/cxl/mem.c            | 32 ++++++++++++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h | 14 ++++++++++++--
 2 files changed, 44 insertions(+), 2 deletions(-)
drivers/cxl/mem.c +0 -0
index 0bf03afc0c80..a2cea7ac7cc6 100644

drivers/cxl/mem.c struct cxl_mem_command { · line 118

Quoted 4 lines
drivers/cxl/mem.c +1 -0
@@ -115,6 +115,7 @@ struct cxl_mem_command {
 
 static struct cxl_mem_command mem_commands[] = {
        CXL_CMD(INVALID, NONE, 0, 0, "Reserved", false, 0),
+       CXL_CMD(RAW, TAINT, ~0, ~0, "Raw", true, 0),

Why is the taint indication in the ABI? It seems like it only needs to be documented.

Quoted 25 lines
drivers/cxl/mem.c +14 -0
 };
 
 static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
@@ -326,6 +327,20 @@ static int cxl_mem_count_commands(void)
        return n;
 };
 
+static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(mem_commands); i++) {
+               struct cxl_mem_command *c = &mem_commands[i];
+
+               if (c->opcode == opcode)
+                       return c;
+       }
+
+       return NULL;
+};
+
 /**
  * handle_mailbox_cmd_from_user() - Dispatch a mailbox command.
  * @cxlmd: The CXL memory device to communicate with.

drivers/cxl/mem.c static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd, · line 443

Quoted 8 lines
drivers/cxl/mem.c +5 -0
@@ -421,6 +436,23 @@ static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd,
        c = &mem_commands[cmd.id];
        info = &c->info;
 
+       /* Checks are bypassed for raw commands but along comes the taint! */
+       if (cmd.id == CXL_MEM_COMMAND_ID_RAW) {
+               struct cxl_mem_command temp =
+                       CXL_CMD(RAW, NONE, cmd.size_in, cmd.size_out, "Raw",
+                               true, cmd.raw.opcode);

Oh, I thought CXL_CMD() was only used to populate the mem_commands array. Feels out of place to use it here when all it is doing is updating the size_{in,out} and opcode fields. Mainly I'm interested in CXL_CMD() enforcing that the command-id is the mem_commands index.

drivers/cxl/mem.c static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd, · line 451

Quoted 8 lines
drivers/cxl/mem.c +8 -0
+
+               if (cmd.raw.rsvd)
+                       return -EINVAL;
+
+               if (cxl_mem_find_command(cmd.raw.opcode))
+                       return -EPERM;
+
+               add_taint(TAINT_WARN, LOCKDEP_STILL_OK);

TAINT_WARN seems the wrong value, especially since no WARN has occurred. I feel that this is more in the spirit of TAINT_PROPRIETARY_MODULE, TAINT_OVERRIDDEN_ACPI_TABLE, and TAINT_OOT_MODULE. How about a new TAINT_RAW_PASSTHROUGH? I could use this for the acpi/nfit driver as well to disclaim responsibility for system errors that can result from not using the nominal kernel-provided commands.

#24 lore raw

Re: [RFC PATCH 11/14] cxl/mem: Add a "RAW" send command

Quoted 27 lines · Dan Williams
Quoted 26 lines · Ben Widawsky

The CXL memory device send interface will have a number of supported commands. The raw command is not such a command. Raw commands allow userspace to send a specified opcode to the underlying hardware and bypass all driver checks on the command. This is useful for a couple of usecases, mainly:
1. Undocumented vendor specific hardware commands
2. Prototyping new hardware commands not yet supported by the driver

While this all sounds very powerful it comes with a couple of caveats:
1. Bug reports using raw commands will not get the same level of attention as bug reports using supported commands (via taint).
2. Supported commands will be rejected by the RAW command.

---
 drivers/cxl/mem.c            | 32 ++++++++++++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h | 14 ++++++++++++--
 2 files changed, 44 insertions(+), 2 deletions(-)
drivers/cxl/mem.c +0 -0
index 0bf03afc0c80..a2cea7ac7cc6 100644
@@ -115,6 +115,7 @@ struct cxl_mem_command {
 
Quoted 7 lines

drivers/cxl/mem.c struct cxl_mem_command { · line 118

Quoted 3 lines
drivers/cxl/mem.c +1 -0
 static struct cxl_mem_command mem_commands[] = {
        CXL_CMD(INVALID, NONE, 0, 0, "Reserved", false, 0),
+       CXL_CMD(RAW, TAINT, ~0, ~0, "Raw", true, 0),

Why is the taint indication in the ABI? It seems like it only needs to be documented.

It's removed per the previous patch discussion.

Quoted 28 lines
Quoted 28 lines
drivers/cxl/mem.c +14 -0
 };
 
 static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
@@ -326,6 +327,20 @@ static int cxl_mem_count_commands(void)
        return n;
 };
 
+static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(mem_commands); i++) {
+               struct cxl_mem_command *c = &mem_commands[i];
+
+               if (c->opcode == opcode)
+                       return c;
+       }
+
+       return NULL;
+};
+
 /**
  * handle_mailbox_cmd_from_user() - Dispatch a mailbox command.
  * @cxlmd: The CXL memory device to communicate with.
@@ -421,6 +436,23 @@ static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd,
        c = &mem_commands[cmd.id];
        info = &c->info;
 
Quoted 11 lines

drivers/cxl/mem.c static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd, · line 443

Quoted 5 lines
drivers/cxl/mem.c +5 -0
+       /* Checks are bypassed for raw commands but along comes the taint! */
+       if (cmd.id == CXL_MEM_COMMAND_ID_RAW) {
+               struct cxl_mem_command temp =
+                       CXL_CMD(RAW, NONE, cmd.size_in, cmd.size_out, "Raw",
+                               true, cmd.raw.opcode);

Oh, I thought CXL_CMD() was only used to populate the mem_commands array. Feels out of place to use it here when all it is doing is updating the size_{in,out} and opcode fields. Mainly I'm interested in CXL_CMD() enforcing that the command-id is the mem_commands index.

Agreed and removed.

Quoted 9 lines

drivers/cxl/mem.c static int cxl_validate_cmd_from_user(struct cxl_send_command __user *user_cmd, · line 451

Quoted 8 lines
drivers/cxl/mem.c +8 -0
+
+               if (cmd.raw.rsvd)
+                       return -EINVAL;
+
+               if (cxl_mem_find_command(cmd.raw.opcode))
+                       return -EPERM;
+
+               add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Quoted 7 lines

TAINT_WARN seems the wrong value, especially since no WARN has occurred. I feel that this is more in the spirit of TAINT_PROPRIETARY_MODULE, TAINT_OVERRIDDEN_ACPI_TABLE, and TAINT_OOT_MODULE. How about a new TAINT_RAW_PASSTHROUGH? I could use this for the acpi/nfit driver as well to disclaim responsibility for system errors that can result from not using the nominal kernel-provided commands.

I like it.

#25 lore raw

RFC PATCH v2 12/14 cxl: Add basic debugging

In reply to Ben Widawsky

Provide a standard debug function for use throughout the driver.

---
 drivers/cxl/cxl.h |  3 +++
 drivers/cxl/mem.c | 26 +++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)
drivers/cxl/cxl.h +3 -0
index 77c2dee6843c..e5afb89dab0b 100644
@@ -9,6 +9,9 @@
 #include <linux/bitops.h>
 #include <linux/io.h>
 
+#define cxl_debug(fmt, ...)                                                    \
+	pr_debug("CXL DEBUG: %s: " fmt, __func__, ##__VA_ARGS__)
+
 #define CXL_SET_FIELD(value, field)                                            \
 	({                                                                     \
 		WARN_ON(!FIELD_FIT(field##_MASK, value));                      \
drivers/cxl/mem.c +25 -1
index a2cea7ac7cc6..6b2f8d3776b5 100644
@@ -122,9 +122,12 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
 {
 	const int timeout = msecs_to_jiffies(2000);
 	const unsigned long start = jiffies;
+	unsigned long end = start;
 
 	while (cxl_doorbell_busy(cxlm)) {
-		if (time_after(jiffies, start + timeout)) {
+		end = jiffies;
+
+		if (time_after(end, start + timeout)) {
 			/* Check again in case preempted before timeout test */
 			if (!cxl_doorbell_busy(cxlm))
 				break;
@@ -133,6 +136,8 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
 		cpu_relax();
 	}
 
+	cxl_debug("Doorbell wait took %dms",
+		  jiffies_to_msecs(end) - jiffies_to_msecs(start));
 	return 0;
 }
 
@@ -180,6 +185,8 @@ static int cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm,
 	}
 
 	/* #4 */
+	cxl_debug("Sending command to %s\n",
+		  dev_driver_string(&cxlm->pdev->dev));
 	cxl_write_mbox_reg32(cxlm, CXLDEV_MB_CTRL_OFFSET,
 			     CXLDEV_MB_CTRL_DOORBELL);
 
@@ -308,6 +315,8 @@ static int cxl_mem_open(struct inode *inode, struct file *file)
 	if (!cxlmd)
 		return -ENXIO;
 
+	cxl_debug("Opened %pD\n", file);
+
 	file->private_data = cxlmd;
 
 	return 0;
@@ -383,6 +392,10 @@ static int handle_mailbox_cmd_from_user(struct cxl_memdev *cxlmd,
 		.size_in = cmd->info.size_in,
 		.size_out = size_out,
 	};
+	cxl_debug("Submitting command for user\n"
+		  "\topcode: %x\n"
+		  "\tsize: %zub/%zub\n",
+		  mbox_cmd.opcode, mbox_cmd.size_in, mbox_cmd.size_out);
 	rc = cxl_mem_mbox_send_cmd(cxlmd->cxlm, &mbox_cmd);
 	cxl_mem_mbox_put(cxlmd->cxlm);
 	if (rc)
@@ -479,6 +492,8 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
 		u32 n_commands;
 		int i, j;
 
+		cxl_debug("Query IOCTL\n");
+
 		if (get_user(n_commands, (u32 __user *)arg))
 			return -EFAULT;
 
@@ -511,6 +526,8 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
 		struct cxl_mem_command c;
 		int rc;
 
+		cxl_debug("Send IOCTL\n");
+
 		rc = cxl_validate_cmd_from_user(u, &c);
 		if (rc)
 			return rc;
@@ -843,6 +860,13 @@ static int cxl_mem_identify(struct cxl_mem *cxlm)
 
 	id = (struct cxl_mbox_identify *)mbox_cmd.payload;
 
+	cxl_debug("Driver identify command\n"
+		  "\tFirmware Version: %s\n"
+		  "\tTotal Capacity: %llu (%llu persistent)\n"
+		  "\tLSA size: %u\n",
+		  id->fw_revision, id->total_capacity, id->persistent_capacity,
+		  id->lsa_size);
+
 	/*
 	 * TODO: enumerate DPA map, as 'ram' and 'pmem' do not alias.
 	 * For now, only the capacity is exported in sysfs
Signature · 1 line
2.29.2
#26 lore raw

Re: [RFC PATCH 12/14] cxl: Add basic debugging

Quoted 14 lines · Ben Widawsky

Provide a standard debug function for use throughout the driver.

---
 drivers/cxl/cxl.h |  3 +++
 drivers/cxl/mem.c | 26 +++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)
drivers/cxl/cxl.h +0 -0
index 77c2dee6843c..e5afb89dab0b 100644

drivers/cxl/cxl.h line 14

Quoted 6 lines
drivers/cxl/cxl.h +3 -0
@@ -9,6 +9,9 @@
 #include <linux/bitops.h>
 #include <linux/io.h>
 
+#define cxl_debug(fmt, ...)                                                    \
+       pr_debug("CXL DEBUG: %s: " fmt, __func__, ##__VA_ARGS__)
+

This should be dev_dbg(), then you don't need the CXL DEBUG prefix. In fact you don't need a cxl_debug() macro at all in that case. cxl_mem might need a ->dev attribute for this purpose.

Quoted 31 lines
drivers/cxl/cxl.h +0 -0
 #define CXL_SET_FIELD(value, field)                                            \
        ({                                                                     \
                WARN_ON(!FIELD_FIT(field##_MASK, value));                      \
drivers/cxl/mem.c +6 -1
index a2cea7ac7cc6..6b2f8d3776b5 100644
@@ -122,9 +122,12 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
 {
        const int timeout = msecs_to_jiffies(2000);
        const unsigned long start = jiffies;
+       unsigned long end = start;
 
        while (cxl_doorbell_busy(cxlm)) {
-               if (time_after(jiffies, start + timeout)) {
+               end = jiffies;
+
+               if (time_after(end, start + timeout)) {
                        /* Check again in case preempted before timeout test */
                        if (!cxl_doorbell_busy(cxlm))
                                break;
@@ -133,6 +136,8 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
                cpu_relax();
        }
 
+       cxl_debug("Doorbell wait took %dms",
+                 jiffies_to_msecs(end) - jiffies_to_msecs(start));
        return 0;
 }
 

drivers/cxl/mem.c static int cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm, · line 189

Quoted 5 lines
drivers/cxl/mem.c +2 -0
@@ -180,6 +185,8 @@ static int cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm,
        }
 
        /* #4 */
+       cxl_debug("Sending command to %s\n",
+                 dev_driver_string(&cxlm->pdev->dev));

dev_dbg() already includes dev_driver_string().

Quoted 42 lines
drivers/cxl/mem.c +10 -0
        cxl_write_mbox_reg32(cxlm, CXLDEV_MB_CTRL_OFFSET,
                             CXLDEV_MB_CTRL_DOORBELL);
 
@@ -308,6 +315,8 @@ static int cxl_mem_open(struct inode *inode, struct file *file)
        if (!cxlmd)
                return -ENXIO;
 
+       cxl_debug("Opened %pD\n", file);
+
        file->private_data = cxlmd;
 
        return 0;
@@ -383,6 +392,10 @@ static int handle_mailbox_cmd_from_user(struct cxl_memdev *cxlmd,
                .size_in = cmd->info.size_in,
                .size_out = size_out,
        };
+       cxl_debug("Submitting command for user\n"
+                 "\topcode: %x\n"
+                 "\tsize: %zub/%zub\n",
+                 mbox_cmd.opcode, mbox_cmd.size_in, mbox_cmd.size_out);
        rc = cxl_mem_mbox_send_cmd(cxlmd->cxlm, &mbox_cmd);
        cxl_mem_mbox_put(cxlmd->cxlm);
        if (rc)
@@ -479,6 +492,8 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
                u32 n_commands;
                int i, j;
 
+               cxl_debug("Query IOCTL\n");
+
                if (get_user(n_commands, (u32 __user *)arg))
                        return -EFAULT;
 
@@ -511,6 +526,8 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
                struct cxl_mem_command c;
                int rc;
 
+               cxl_debug("Send IOCTL\n");
+
                rc = cxl_validate_cmd_from_user(u, &c);
                if (rc)
                        return rc;

drivers/cxl/mem.c static int cxl_mem_identify(struct cxl_mem *cxlm) · line 869

Quoted 10 lines
drivers/cxl/mem.c +7 -0
@@ -843,6 +860,13 @@ static int cxl_mem_identify(struct cxl_mem *cxlm)
 
        id = (struct cxl_mbox_identify *)mbox_cmd.payload;
 
+       cxl_debug("Driver identify command\n"
+                 "\tFirmware Version: %s\n"
+                 "\tTotal Capacity: %llu (%llu persistent)\n"
+                 "\tLSA size: %u\n",
+                 id->fw_revision, id->total_capacity, id->persistent_capacity,
+                 id->lsa_size);
+

Seems not necessary for details that are published in sysfs?

#27 lore raw

Re: [RFC PATCH 12/14] cxl: Add basic debugging

Quoted 18 lines · Dan Williams
Quoted 17 lines · Ben Widawsky

Provide a standard debug function for use throughout the driver.

---
 drivers/cxl/cxl.h |  3 +++
 drivers/cxl/mem.c | 26 +++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)
drivers/cxl/cxl.h +0 -0
index 77c2dee6843c..e5afb89dab0b 100644
@@ -9,6 +9,9 @@
 #include <linux/bitops.h>
 #include <linux/io.h>
 
Quoted 8 lines

drivers/cxl/cxl.h line 14

Quoted 3 lines
drivers/cxl/cxl.h +3 -0
+#define cxl_debug(fmt, ...)                                                    \
+       pr_debug("CXL DEBUG: %s: " fmt, __func__, ##__VA_ARGS__)
+

This should be dev_dbg(), then you don't need the CXL DEBUG prefix. In fact you don't need a cxl_debug() macro at all in that case. cxl_mem might need a ->dev attribute for this purpose.

I really like the ability to turn specific messages on and off at will. (FWIW, __func__ is also redundant because pr_debug allows you to specify a flag to always print the function name). While it's not very frequent events here, in the future it likely will be and I think it can be really helpful to be able to have that level of control.

If you want to avoid creating a new debug functionality, I'm okay with that, but I'd really like to use pr_debug instead of dev_dbg for those messages going forward. Once you take that step, it seems giving contributors a macro named 'cxl_debug' so they don't have to figure out when to use what, makes sense. My mental separation is, dev_* is useful primarily for errors and initialization debug messaging, pr_debug/trace_printk is for runtime things.

I probably should have put that in the commit message...

Quoted 84 lines
Quoted 31 lines
drivers/cxl/cxl.h +0 -0
 #define CXL_SET_FIELD(value, field)                                            \
        ({                                                                     \
                WARN_ON(!FIELD_FIT(field##_MASK, value));                      \
drivers/cxl/mem.c +6 -1
index a2cea7ac7cc6..6b2f8d3776b5 100644
@@ -122,9 +122,12 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
 {
        const int timeout = msecs_to_jiffies(2000);
        const unsigned long start = jiffies;
+       unsigned long end = start;
 
        while (cxl_doorbell_busy(cxlm)) {
-               if (time_after(jiffies, start + timeout)) {
+               end = jiffies;
+
+               if (time_after(end, start + timeout)) {
                        /* Check again in case preempted before timeout test */
                        if (!cxl_doorbell_busy(cxlm))
                                break;
@@ -133,6 +136,8 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
                cpu_relax();
        }
 
+       cxl_debug("Doorbell wait took %dms",
+                 jiffies_to_msecs(end) - jiffies_to_msecs(start));
        return 0;
 }
 

drivers/cxl/mem.c static int cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm, · line 189

Quoted 5 lines
drivers/cxl/mem.c +2 -0
@@ -180,6 +185,8 @@ static int cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm,
        }
 
        /* #4 */
+       cxl_debug("Sending command to %s\n",
+                 dev_driver_string(&cxlm->pdev->dev));

dev_dbg() already includes dev_driver_string().

Quoted 45 lines
drivers/cxl/mem.c +10 -0
        cxl_write_mbox_reg32(cxlm, CXLDEV_MB_CTRL_OFFSET,
                             CXLDEV_MB_CTRL_DOORBELL);
 
@@ -308,6 +315,8 @@ static int cxl_mem_open(struct inode *inode, struct file *file)
        if (!cxlmd)
                return -ENXIO;
 
+       cxl_debug("Opened %pD\n", file);
+
        file->private_data = cxlmd;
 
        return 0;
@@ -383,6 +392,10 @@ static int handle_mailbox_cmd_from_user(struct cxl_memdev *cxlmd,
                .size_in = cmd->info.size_in,
                .size_out = size_out,
        };
+       cxl_debug("Submitting command for user\n"
+                 "\topcode: %x\n"
+                 "\tsize: %zub/%zub\n",
+                 mbox_cmd.opcode, mbox_cmd.size_in, mbox_cmd.size_out);
        rc = cxl_mem_mbox_send_cmd(cxlmd->cxlm, &mbox_cmd);
        cxl_mem_mbox_put(cxlmd->cxlm);
        if (rc)
@@ -479,6 +492,8 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
                u32 n_commands;
                int i, j;
 
+               cxl_debug("Query IOCTL\n");
+
                if (get_user(n_commands, (u32 __user *)arg))
                        return -EFAULT;
 
@@ -511,6 +526,8 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
                struct cxl_mem_command c;
                int rc;
 
+               cxl_debug("Send IOCTL\n");
+
                rc = cxl_validate_cmd_from_user(u, &c);
                if (rc)
                        return rc;
@@ -843,6 +860,13 @@ static int cxl_mem_identify(struct cxl_mem *cxlm)
 
        id = (struct cxl_mbox_identify *)mbox_cmd.payload;
 
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_identify(struct cxl_mem *cxlm) · line 869

Quoted 7 lines
drivers/cxl/mem.c +7 -0
+       cxl_debug("Driver identify command\n"
+                 "\tFirmware Version: %s\n"
+                 "\tTotal Capacity: %llu (%llu persistent)\n"
+                 "\tLSA size: %u\n",
+                 id->fw_revision, id->total_capacity, id->persistent_capacity,
+                 id->lsa_size);
+

Seems not necessary for details that are published in sysfs?

I was thinking for cases where driver doesn't bind, seeing the identify information could be useful. This is one case where dev_dbg would also serve IMO.

#28 lore raw

Re: [RFC PATCH 12/14] cxl: Add basic debugging

Quoted 35 lines · Ben Widawsky
Quoted 18 lines · Dan Williams
Quoted 17 lines · Ben Widawsky

Provide a standard debug function for use throughout the driver.

---
 drivers/cxl/cxl.h |  3 +++
 drivers/cxl/mem.c | 26 +++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)
drivers/cxl/cxl.h +0 -0
index 77c2dee6843c..e5afb89dab0b 100644
@@ -9,6 +9,9 @@
 #include <linux/bitops.h>
 #include <linux/io.h>
 
Quoted 8 lines

drivers/cxl/cxl.h line 14

Quoted 3 lines
drivers/cxl/cxl.h +3 -0
+#define cxl_debug(fmt, ...)                                                    \
+       pr_debug("CXL DEBUG: %s: " fmt, __func__, ##__VA_ARGS__)
+

This should be dev_dbg(), then you don't need the CXL DEBUG prefix. In fact you don't need a cxl_debug() macro at all in that case. cxl_mem might need a ->dev attribute for this purpose.

I really like the ability to turn specific messages on and off at will. (FWIW, __func__ is also redundant because pr_debug allows you to specify a flag to always print the function name). While it's not very frequent events here, in the future it likely will be and I think it can be really helpful to be able to have that level of control.

Quoted 8 lines

If you want to avoid creating a new debug functionality, I'm okay with that, but I'd really like to use pr_debug instead of dev_dbg for those messages going forward. Once you take that step, it seems giving contributors a macro named 'cxl_debug' so they don't have to figure out when to use what, makes sense. My mental separation is, dev_* is useful primarily for errors and initialization debug messaging, pr_debug/trace_printk is for runtime things.

I probably should have put that in the commit message...

I suspect you haven't taken a look at the backed and of pr_debug() and dev_dbg() in a while? They both use _dynamic_func_call and enjoy all the benefits afforded by /sys/debug/dynamic_debug/control and the "dyndbg" module option for adding __func__ and enable / disable by line number or format message . pr_debug() and this cxl_debug() macro are completely superseded by dev_dbg(). Even if a driver wanted a common prefix on all prints there is the "#define dev_fmt ..." mechanism for that.

Quoted 95 lines
Quoted 85 lines
Quoted 32 lines
drivers/cxl/cxl.h +0 -0
 #define CXL_SET_FIELD(value, field)                                            \
        ({                                                                     \
                WARN_ON(!FIELD_FIT(field##_MASK, value));                      \
drivers/cxl/mem.c +6 -1
index a2cea7ac7cc6..6b2f8d3776b5 100644
@@ -122,9 +122,12 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
 {
        const int timeout = msecs_to_jiffies(2000);
        const unsigned long start = jiffies;
+       unsigned long end = start;
 
        while (cxl_doorbell_busy(cxlm)) {
-               if (time_after(jiffies, start + timeout)) {
+               end = jiffies;
+
+               if (time_after(end, start + timeout)) {
                        /* Check again in case preempted before timeout test */
                        if (!cxl_doorbell_busy(cxlm))
                                break;
@@ -133,6 +136,8 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
                cpu_relax();
        }
 
+       cxl_debug("Doorbell wait took %dms",
+                 jiffies_to_msecs(end) - jiffies_to_msecs(start));
        return 0;
 }
 

drivers/cxl/mem.c static int cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm, · line 189

Quoted 5 lines
drivers/cxl/mem.c +2 -0
@@ -180,6 +185,8 @@ static int cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm,
        }
 
        /* #4 */
+       cxl_debug("Sending command to %s\n",
+                 dev_driver_string(&cxlm->pdev->dev));

dev_dbg() already includes dev_driver_string().

Quoted 45 lines
drivers/cxl/mem.c +10 -0
        cxl_write_mbox_reg32(cxlm, CXLDEV_MB_CTRL_OFFSET,
                             CXLDEV_MB_CTRL_DOORBELL);
 
@@ -308,6 +315,8 @@ static int cxl_mem_open(struct inode *inode, struct file *file)
        if (!cxlmd)
                return -ENXIO;
 
+       cxl_debug("Opened %pD\n", file);
+
        file->private_data = cxlmd;
 
        return 0;
@@ -383,6 +392,10 @@ static int handle_mailbox_cmd_from_user(struct cxl_memdev *cxlmd,
                .size_in = cmd->info.size_in,
                .size_out = size_out,
        };
+       cxl_debug("Submitting command for user\n"
+                 "\topcode: %x\n"
+                 "\tsize: %zub/%zub\n",
+                 mbox_cmd.opcode, mbox_cmd.size_in, mbox_cmd.size_out);
        rc = cxl_mem_mbox_send_cmd(cxlmd->cxlm, &mbox_cmd);
        cxl_mem_mbox_put(cxlmd->cxlm);
        if (rc)
@@ -479,6 +492,8 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
                u32 n_commands;
                int i, j;
 
+               cxl_debug("Query IOCTL\n");
+
                if (get_user(n_commands, (u32 __user *)arg))
                        return -EFAULT;
 
@@ -511,6 +526,8 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
                struct cxl_mem_command c;
                int rc;
 
+               cxl_debug("Send IOCTL\n");
+
                rc = cxl_validate_cmd_from_user(u, &c);
                if (rc)
                        return rc;
@@ -843,6 +860,13 @@ static int cxl_mem_identify(struct cxl_mem *cxlm)
 
        id = (struct cxl_mbox_identify *)mbox_cmd.payload;
 
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_identify(struct cxl_mem *cxlm) · line 869

Quoted 7 lines
drivers/cxl/mem.c +7 -0
+       cxl_debug("Driver identify command\n"
+                 "\tFirmware Version: %s\n"
+                 "\tTotal Capacity: %llu (%llu persistent)\n"
+                 "\tLSA size: %u\n",
+                 id->fw_revision, id->total_capacity, id->persistent_capacity,
+                 id->lsa_size);
+

Seems not necessary for details that are published in sysfs?

Quoted 3 lines

I was thinking for cases where driver doesn't bind, seeing the identify information could be useful. This is one case where dev_dbg would also serve IMO.

There's very little that can cause bind failures after cxl_mem_identify() succeeds. The bind failures themselves will dev_err() the reason.

#29 lore raw

Re: [RFC PATCH 12/14] cxl: Add basic debugging

Quoted 45 lines · Dan Williams
Quoted 35 lines · Ben Widawsky
Quoted 18 lines · Dan Williams
Quoted 17 lines · Ben Widawsky

Provide a standard debug function for use throughout the driver.

---
 drivers/cxl/cxl.h |  3 +++
 drivers/cxl/mem.c | 26 +++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)
drivers/cxl/cxl.h +0 -0
index 77c2dee6843c..e5afb89dab0b 100644
@@ -9,6 +9,9 @@
 #include <linux/bitops.h>
 #include <linux/io.h>
 
Quoted 8 lines

drivers/cxl/cxl.h line 14

Quoted 3 lines
drivers/cxl/cxl.h +3 -0
+#define cxl_debug(fmt, ...)                                                    \
+       pr_debug("CXL DEBUG: %s: " fmt, __func__, ##__VA_ARGS__)
+

This should be dev_dbg(), then you don't need the CXL DEBUG prefix. In fact you don't need a cxl_debug() macro at all in that case. cxl_mem might need a ->dev attribute for this purpose.

I really like the ability to turn specific messages on and off at will. (FWIW, __func__ is also redundant because pr_debug allows you to specify a flag to always print the function name). While it's not very frequent events here, in the future it likely will be and I think it can be really helpful to be able to have that level of control.

Quoted 8 lines

If you want to avoid creating a new debug functionality, I'm okay with that, but I'd really like to use pr_debug instead of dev_dbg for those messages going forward. Once you take that step, it seems giving contributors a macro named 'cxl_debug' so they don't have to figure out when to use what, makes sense. My mental separation is, dev_* is useful primarily for errors and initialization debug messaging, pr_debug/trace_printk is for runtime things.

I probably should have put that in the commit message...

Quoted 9 lines

I suspect you haven't taken a look at the backed and of pr_debug() and dev_dbg() in a while? They both use _dynamic_func_call and enjoy all the benefits afforded by /sys/debug/dynamic_debug/control and the "dyndbg" module option for adding __func__ and enable / disable by line number or format message . pr_debug() and this cxl_debug() macro are completely superseded by dev_dbg(). Even if a driver wanted a common prefix on all prints there is the "#define dev_fmt ..." mechanism for that.

In that case, I'm just going to squash in the debug messages into the various commits that need them.

Quoted 102 lines
Quoted 95 lines
Quoted 85 lines
Quoted 32 lines
drivers/cxl/cxl.h +0 -0
 #define CXL_SET_FIELD(value, field)                                            \
        ({                                                                     \
                WARN_ON(!FIELD_FIT(field##_MASK, value));                      \
drivers/cxl/mem.c +6 -1
index a2cea7ac7cc6..6b2f8d3776b5 100644
@@ -122,9 +122,12 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
 {
        const int timeout = msecs_to_jiffies(2000);
        const unsigned long start = jiffies;
+       unsigned long end = start;
 
        while (cxl_doorbell_busy(cxlm)) {
-               if (time_after(jiffies, start + timeout)) {
+               end = jiffies;
+
+               if (time_after(end, start + timeout)) {
                        /* Check again in case preempted before timeout test */
                        if (!cxl_doorbell_busy(cxlm))
                                break;
@@ -133,6 +136,8 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
                cpu_relax();
        }
 
+       cxl_debug("Doorbell wait took %dms",
+                 jiffies_to_msecs(end) - jiffies_to_msecs(start));
        return 0;
 }
 

drivers/cxl/mem.c static int cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm, · line 189

Quoted 5 lines
drivers/cxl/mem.c +2 -0
@@ -180,6 +185,8 @@ static int cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm,
        }
 
        /* #4 */
+       cxl_debug("Sending command to %s\n",
+                 dev_driver_string(&cxlm->pdev->dev));

dev_dbg() already includes dev_driver_string().

Quoted 45 lines
drivers/cxl/mem.c +10 -0
        cxl_write_mbox_reg32(cxlm, CXLDEV_MB_CTRL_OFFSET,
                             CXLDEV_MB_CTRL_DOORBELL);
 
@@ -308,6 +315,8 @@ static int cxl_mem_open(struct inode *inode, struct file *file)
        if (!cxlmd)
                return -ENXIO;
 
+       cxl_debug("Opened %pD\n", file);
+
        file->private_data = cxlmd;
 
        return 0;
@@ -383,6 +392,10 @@ static int handle_mailbox_cmd_from_user(struct cxl_memdev *cxlmd,
                .size_in = cmd->info.size_in,
                .size_out = size_out,
        };
+       cxl_debug("Submitting command for user\n"
+                 "\topcode: %x\n"
+                 "\tsize: %zub/%zub\n",
+                 mbox_cmd.opcode, mbox_cmd.size_in, mbox_cmd.size_out);
        rc = cxl_mem_mbox_send_cmd(cxlmd->cxlm, &mbox_cmd);
        cxl_mem_mbox_put(cxlmd->cxlm);
        if (rc)
@@ -479,6 +492,8 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
                u32 n_commands;
                int i, j;
 
+               cxl_debug("Query IOCTL\n");
+
                if (get_user(n_commands, (u32 __user *)arg))
                        return -EFAULT;
 
@@ -511,6 +526,8 @@ static long cxl_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg
                struct cxl_mem_command c;
                int rc;
 
+               cxl_debug("Send IOCTL\n");
+
                rc = cxl_validate_cmd_from_user(u, &c);
                if (rc)
                        return rc;
@@ -843,6 +860,13 @@ static int cxl_mem_identify(struct cxl_mem *cxlm)
 
        id = (struct cxl_mbox_identify *)mbox_cmd.payload;
 
Quoted 9 lines

drivers/cxl/mem.c static int cxl_mem_identify(struct cxl_mem *cxlm) · line 869

Quoted 7 lines
drivers/cxl/mem.c +7 -0
+       cxl_debug("Driver identify command\n"
+                 "\tFirmware Version: %s\n"
+                 "\tTotal Capacity: %llu (%llu persistent)\n"
+                 "\tLSA size: %u\n",
+                 id->fw_revision, id->total_capacity, id->persistent_capacity,
+                 id->lsa_size);
+

Seems not necessary for details that are published in sysfs?

Quoted 3 lines

I was thinking for cases where driver doesn't bind, seeing the identify information could be useful. This is one case where dev_dbg would also serve IMO.

There's very little that can cause bind failures after cxl_mem_identify() succeeds. The bind failures themselves will dev_err() the reason.

#30 lore raw

RFC PATCH v2 13/14 MAINTAINERS: Add maintainers of the CXL driver

In reply to Ben Widawsky
Cc Dan Williams <[email protected]>
Cc Vishal Verma <[email protected]>
Cc Ira Weiny <[email protected]>
---
 MAINTAINERS | 10 ++++++++++
 1 file changed, 10 insertions(+)
MAINTAINERS +10 -0
index b516bb34a8d5..d6aa4b684a1a 100644
@@ -4428,6 +4428,16 @@ F:	fs/configfs/
 F:	include/linux/configfs.h
 F:	samples/configfs/
 
+COMPUTE EXPRESS LINK (CXL)
+M:	Vishal Verma <[email protected]>
+M:	Ira Weiny <[email protected]>
+M:	Ben Widawsky <[email protected]>
+M:	Dan Williams <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/cxl/
+F:	include/uapi/linux/cxl_mem.h
+
 CONSOLE SUBSYSTEM
 M:	Greg Kroah-Hartman <[email protected]>
 S:	Supported
Signature · 1 line
2.29.2
#31 lore raw

RFC PATCH v2 14/14 WIP/cxl/mem: Add get firmware for testing

In reply to Ben Widawsky

This also serves as an example how to add a new command

---
 drivers/cxl/mem.c            | 22 ++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h |  3 ++-
 2 files changed, 24 insertions(+), 1 deletion(-)
drivers/cxl/mem.c +22 -0
index 6b2f8d3776b5..76aa1e6e4117 100644
@@ -116,6 +116,7 @@ struct cxl_mem_command {
 static struct cxl_mem_command mem_commands[] = {
 	CXL_CMD(INVALID, NONE, 0, 0, "Reserved", false, 0),
 	CXL_CMD(RAW, TAINT, ~0, ~0, "Raw", true, 0),
+	CXL_CMD(GET_FW_INFO, NONE, 0, 0x50, "Get FW Info", false, 0x0200),
 };
 
 static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
@@ -827,6 +828,23 @@ static int cxl_mem_add_memdev(struct cxl_mem *cxlm)
 	return cxl_register(dev);
 }
 
+static int cxl_mem_enable_commands(struct cxl_mem *cxlm)
+{
+	struct cxl_mem_command *c;
+
+	/*
+	 * For now we pretend Get FW info is supported.
+	 *
+	 * FIXME: Invoke GET LOG to get the Command Effect Logs (CEL).
+	 */
+	c = cxl_mem_find_command(0x200);
+	if (!c)
+		return -ENOENT;
+
+	c->enable = true;
+	return 0;
+}
+
 /**
  * cxl_mem_identify() - Send the IDENTIFY command to the device.
  * @cxlm: The device to identify.
@@ -936,6 +954,10 @@ static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (rc)
 		return rc;
 
+	rc = cxl_mem_enable_commands(cxlm);
+	if (rc)
+		return rc;
+
 	rc = cxl_mem_identify(cxlm);
 	if (rc)
 		return rc;
include/uapi/linux/cxl_mem.h +2 -1
index f2fbb0dcda06..3ac39acf8fa7 100644
@@ -50,7 +50,8 @@ struct cxl_command_info {
 	__u32 id;
 #define CXL_MEM_COMMAND_ID_INVALID 0
 #define CXL_MEM_COMMAND_ID_RAW 1
-#define CXL_MEM_COMMAND_ID_MAX (CXL_MEM_COMMAND_ID_RAW + 1)
+#define CXL_MEM_COMMAND_ID_GET_FW_INFO 2
+#define CXL_MEM_COMMAND_ID_MAX (CXL_MEM_COMMAND_ID_GET_FW_INFO + 1)
 
 	__u32 flags;
 #define CXL_MEM_COMMAND_FLAG_NONE 0
Signature · 1 line
2.29.2
#32 lore raw

Re: [RFC PATCH 14/14] WIP/cxl/mem: Add get firmware for testing

Quoted 14 lines · Ben Widawsky

This also serves as an example how to add a new command

---
 drivers/cxl/mem.c            | 22 ++++++++++++++++++++++
 include/uapi/linux/cxl_mem.h |  3 ++-
 2 files changed, 24 insertions(+), 1 deletion(-)
drivers/cxl/mem.c +0 -0
index 6b2f8d3776b5..76aa1e6e4117 100644

drivers/cxl/mem.c struct cxl_mem_command { · line 119

Quoted 4 lines
drivers/cxl/mem.c +1 -0
@@ -116,6 +116,7 @@ struct cxl_mem_command {
 static struct cxl_mem_command mem_commands[] = {
        CXL_CMD(INVALID, NONE, 0, 0, "Reserved", false, 0),
        CXL_CMD(RAW, TAINT, ~0, ~0, "Raw", true, 0),
+       CXL_CMD(GET_FW_INFO, NONE, 0, 0x50, "Get FW Info", false, 0x0200),

I think CXL_CMD() arguments can be put on a diet if the mem-command-id-to-name was moved to its own table, and the opcode was defined as something like:

#define CXL_MBOX_OP_GET_FW_INFO 0x200

...so that CXL_CMD can just do:

    .opcode = CXL_MBOX_OP_##_id

That @_enable arg wants a flag #define like CMD_ENABLE which reads better than 'true'. I would then add CMD_KERNEL_EXCL alongside that flag to indicate the commands that may be exclusive to the kernel when the device is active in a PMEM memory region, or ones that have an alternate ABI wrapped around them like the keys subsystem for security or the firwmare activation sysfs interface.

Quoted 43 lines
drivers/cxl/mem.c +21 -0
 };
 
 static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)
@@ -827,6 +828,23 @@ static int cxl_mem_add_memdev(struct cxl_mem *cxlm)
        return cxl_register(dev);
 }
 
+static int cxl_mem_enable_commands(struct cxl_mem *cxlm)
+{
+       struct cxl_mem_command *c;
+
+       /*
+        * For now we pretend Get FW info is supported.
+        *
+        * FIXME: Invoke GET LOG to get the Command Effect Logs (CEL).
+        */
+       c = cxl_mem_find_command(0x200);
+       if (!c)
+               return -ENOENT;
+
+       c->enable = true;
+       return 0;
+}
+
 /**
  * cxl_mem_identify() - Send the IDENTIFY command to the device.
  * @cxlm: The device to identify.
@@ -936,6 +954,10 @@ static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id)
        if (rc)
                return rc;
 
+       rc = cxl_mem_enable_commands(cxlm);
+       if (rc)
+               return rc;
+
        rc = cxl_mem_identify(cxlm);
        if (rc)
                return rc;
include/uapi/linux/cxl_mem.h +0 -0
index f2fbb0dcda06..3ac39acf8fa7 100644

include/uapi/linux/cxl_mem.h struct cxl_command_info { · line 54

Quoted 6 lines
include/uapi/linux/cxl_mem.h +2 -1
@@ -50,7 +50,8 @@ struct cxl_command_info {
        __u32 id;
 #define CXL_MEM_COMMAND_ID_INVALID 0
 #define CXL_MEM_COMMAND_ID_RAW 1
-#define CXL_MEM_COMMAND_ID_MAX (CXL_MEM_COMMAND_ID_RAW + 1)
+#define CXL_MEM_COMMAND_ID_GET_FW_INFO 2
+#define CXL_MEM_COMMAND_ID_MAX (CXL_MEM_COMMAND_ID_GET_FW_INFO + 1)

Seems like CXL_MEM_COMMAND_ID definitions want to be an enum so that CXL_MEM_COMMAND_ID_MAX does not need to be touched every time.

Quoted 6 lines
include/uapi/linux/cxl_mem.h +0 -1
        __u32 flags;
 #define CXL_MEM_COMMAND_FLAG_NONE 0
--

2.29.2

maintainer
#33 lore raw
In reply to Ben Widawsky
Quoted 32 lines · Ben Widawsky

Changes since v1 [1]

A few additions have been made:
- IOCTL (UAPI) interface has been added with commands
- Kernel docs have been created
- A new debug macro is introduced and sprinkled throughout.

A deletion was made:
- Removal of the non-standard _OSC UUID.

The detailed list of fixes is:
- fix cxl_register() no previous prototype warning (0day robot)
- s/REGLOG/REGLOC/ (Ben)
- Wait for doorbell on cxl_mem_mbox_get() and add comment on why (Ben)
- make "type-3" a proper adjective, add spec references, also did the same for the Kconfig (Bjorn)
- align some defines (Bjorn)
- s/bar/BAR (Bjorn)
- rename cxl_bus_prepared() to cxl_bus_acquire() (Bjorn)
- move definition of struct cxl_mem to "cxl/mem: Map memory device registers" (Bjorn)
- use consistent hex/decimal (Bjorn)
- use consistent upper/lower hex values (Bjorn)
- get rid of READ_ONCE (Bjorn)
- add offsets to debug messages (Bjorn)
- cleanup SPDX comment style (Bjorn, Christoph)
- change errors returned by case (Bjorn, Dan)
- 80 character violation cleanups (Christoph)
- cleanup CXL_BUS_PROVIDER dependencies (Christoph, Randy)
- remove "raw" from mmio functions (Dan)
- rename PCI_DVSEC_VENDOR_CXL to add _ID (Jonathan)
- combine introduction of mbox infrastruct and cxl_mem_identify() (Jonathan)
- add ABI documentation for sysfs attributes (Jonathan)

Quoted 8 lines

- document scope of cxl_memdev_lock (Jonathan)
- rework cxl_register() to have devm semantics (reaction to comments about cxl_mem_remove() and cxl_mem_add_memdev() semantics) (Jonathan)
- fix cxl_mem_exit() ordering (Jonathan)
- use GENMASK/GET_FIELD (Jonathan)
- fix and add comments for cap ids (Jonathan)
- use _OFFSET postfix in definitions (Jonathan)
- save pci_set_drvdata for later (Jonathan)

There are a few more change credits for the acpi patches:

- Remove unnecessary helpers and callbacks in drivers/cxl/acpi.c (Christoph, Jonathan)
- Remove some unnecessary variable initalizations (Bjorn, Jonathan)
- Convert to platform_driver (Rafael)

Quoted 135 lines

[1]: https://lore.kernel.org/linux-cxl/[email protected]/

---

Introduce support for “type-3” memory devices defined in the recently released Compute Express Link (CXL) 2.0 specification[2]. Specifically, these are the memory devices defined by section 8.2.8.5 of the CXL 2.0 spec. A reference implementation emulating these devices has been submitted to the QEMU mailing list and is available on gitlab [3]. “Type-3” is a CXL device that acts as a memory expander for RAM or PMEM. It might be interleaved with other CXL devices in a given physical address range.

These changes allow for foundational enumeration of CXL 2.0 memory devices as well as basic userspace interaction. The functionality present is:
- Initial driver bring-up
- Device enumeration and an initial sysfs representation
- Submit a basic firmware command via ‘mailbox’ to an emulated memory device with non-volatile capacity.
- Provide an interface to send "raw" commands to the hardware.

Some of the functionality that is still missing includes:
- Memory interleaving at the host bridge, root port, or switch level
- CXL 1.1 Root Complex Integrated Endpoint Support
- CXL 2.0 Hot plug support
- A bevy of supported device commands

In addition to the core functionality of discovering the spec defined registers and resources, introduce a CXL device model that will be the foundation for translating CXL capabilities into existing Linux infrastructure for Persistent Memory and other memory devices. For now, this only includes support for the management command mailbox that type-3 devices surface. These control devices fill the role of “DIMMs” / nmemX memory-devices in LIBNVDIMM terms.

Now, while implementing the driver some feedback for the specification was generated to cover perceived gaps and address conflicts. The feedback is presented as a reference implementation in the driver and QEMU emulation. Specifically the following concepts are original to the Linux implementation and feedback / collaboration is requested to develop these into specification proposals:
1. Top level ACPI object (ACPI0017)
2. HW imposed address space and interleave constraints

ACPI0017
--------
Introduce a new ACPI namespace device with an _HID of ACPI0017. The purpose of this object is twofold, support a legacy OS with a set of out-of-tree CXL modules, and establish an attach point for a driver that knows about interleaving. Both of these boil down to the same point, to centralize Operating System support for resources described by the CXL Early Discovery Table (CEDT).

The legacy OS problem stems from the spec's description of a host bridge, ACPI0016 is denoted as the _HID for host bridges, with a _CID of PNP0A08. In a CXL unaware version of Linux, the core ACPI subsystem will bind a driver to PNP0A08 and preclude a CXL-aware driver from binding to ACPI0016. An ACPI0017 device allows a standalone CXL-aware driver to register for handling / coordinating CEDT and CXL-specific _OSC control.

Similarly when managing interleaving there needs to be some management layer above the ACPI0016 device that is capable of assembling leaf nodes into interleave sets. As is the case with ACPI0012 that does this central coordination for NFIT defined resources, ACPI0017 does the same for CEDT described resources.

Memory Windows
-------
For CXL.mem capable platforms, there is a need for a mechanism for platform firmware to make the Operating System aware of any restrictions that hardware might have in address space. For example, in a system with 4 host bridges all participating in an interleave set, the firmware needs to provide some description of this. That information is missing from the CXL 2.0 spec as of today and it also is not implemented in the driver. A variety of ACPI based mechanisms, for example _CRS fields on the ACPI0017 device, were considered.

Next steps after this basic foundation is expanded command support and LIBNVDIMM integration. This is the initial “release early / release often” version of the Linux CXL enabling.

[2]: https://www.computeexpresslink.org/
[3]: https://gitlab.com/bwidawsk/qemu/-/tree/cxl-2.0v2

Ben Widawsky (10):
docs: cxl: Add basic documentation
cxl/mem: Map memory device registers
cxl/mem: Find device capabilities
cxl/mem: Implement polled mode mailbox
cxl/mem: Add basic IOCTL interface
cxl/mem: Add send command
cxl/mem: Add a "RAW" send command
cxl: Add basic debugging
MAINTAINERS: Add maintainers of the CXL driver
WIP/cxl/mem: Add get firmware for testing

Dan Williams (2):
cxl/mem: Introduce a driver for CXL-2.0-Type-3 endpoints cxl/mem: Register CXL memX devices

Vishal Verma (2):
cxl/acpi: Add an acpi_cxl module for the CXL interconnect cxl/acpi: add OSC support

 Documentation/ABI/testing/sysfs-bus-cxl |   26 +
 Documentation/cxl/index.rst             |   12 +
 Documentation/cxl/memory-devices.rst    |   51 ++
 Documentation/index.rst                 |    1 +
 MAINTAINERS                             |   10 +
 drivers/Kconfig                         |    1 +
 drivers/Makefile                        |    1 +
 drivers/cxl/Kconfig                     |   58 ++
 drivers/cxl/Makefile                    |    9 +
 drivers/cxl/acpi.c                      |  352 ++++++++
 drivers/cxl/acpi.h                      |   35 +
 drivers/cxl/bus.c                       |   54 ++
 drivers/cxl/bus.h                       |    8 +
 drivers/cxl/cxl.h                       |  188 +++++
 drivers/cxl/mem.c                       | 1022 +++++++++++++++++++++++
 drivers/cxl/pci.h                       |   34 +
 include/acpi/actbl1.h                   |   51 ++
 include/uapi/linux/cxl_mem.h            |  148 ++++
 18 files changed, 2061 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-cxl
 create mode 100644 Documentation/cxl/index.rst
 create mode 100644 Documentation/cxl/memory-devices.rst
 create mode 100644 drivers/cxl/Kconfig
 create mode 100644 drivers/cxl/Makefile
 create mode 100644 drivers/cxl/acpi.c
 create mode 100644 drivers/cxl/acpi.h
 create mode 100644 drivers/cxl/bus.c
 create mode 100644 drivers/cxl/bus.h
 create mode 100644 drivers/cxl/cxl.h
 create mode 100644 drivers/cxl/mem.c
 create mode 100644 drivers/cxl/pci.h
 create mode 100644 include/uapi/linux/cxl_mem.h