CXL 2.0 Support
Subsystems (6)
v4 cover CXL 2.0 Support
# Changes since v3 [1]
* Fix use of GET_SUPPORTED_LOGS (Ben)
* Reported by Dan
* Rework userspace commands (Al, Dan)
* Don't get_user twice (Al)
* Don't pass __user @u to handle_mailbox_cmd_from_user() (Dan)
* Use void * in cxl_mem_mbox_send_cmd() (Dan)
* Fix for 32b builds (Stephen, Randy, more)
* Include io-64-nonatomic-lo-hi.h in mem.c
* Use GENMASK_ULL where appropriate
---
In addition to the mailing list, please feel free to use #cxl on oftc IRC for discussion.
---
# Summary
Introduce support for “type-3” memory devices defined in the 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 [3] and is available on gitlab [4], but will move to a shared tree on kernel.org after initial acceptance. “Type-3” is a CXL device that acts as a memory expander for RAM or Persistent Memory. The device might be interleaved with other CXL devices in a given physical address range.
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 the surfacing of type-3 devices. These control devices fill the role of “DIMMs” / nmemX memory-devices in LIBNVDIMM terms.
## Userspace Interaction
Interaction with the driver and type-3 devices via the CXL drivers is introduced in this patch series and considered stable ABI. They include
* sysfs - Documentation/ABI/testing/sysfs-bus-cxl
* IOCTL - Documentation/driver-api/cxl/memory-devices.rst
* debugfs - Documentation/ABI/testing/debugfs-debug
Work is in process to add support for CXL interactions to the ndctl project [5]
### Development plans
One of the unique challenges that CXL imposes on the Linux driver model is that it requires the operating system to perform physical address space management interleaved across devices and bridges. Whereas LIBNVDIMM handles a list of established static persistent memory address ranges (for example from the ACPI NFIT), CXL introduces hotplug and the concept of allocating address space to instantiate persistent memory ranges. This is similar to PCI in the sense that the platform establishes the MMIO range for PCI BARs to be allocated, but it is significantly complicated by the fact that a given device can optionally be interleaved with other devices and can participate in several interleave-sets at once. LIBNVDIMM handled something like this with the aliasing between PMEM and BLOCK-WINDOW mode, but CXL adds flexibility to alias DEVICE MEMORY through up to 10 decoders per device.
All of the above needs to be enabled with respect to PCI hotplug events on Type-3 memory device which needs hooks to determine if a given device is contributing to a "System RAM" address range that is unable to be unplugged. In other words CXL ties PCI hotplug to Memory Hotplug and PCI hotplug needs to be able to negotiate with memory hotplug. In the medium term the implications of CXL hotplug vs ACPI SRAT/SLIT/HMAT need to be reconciled. One capability that seems to be needed is either the dynamic allocation of new memory nodes, or default initializing extra pgdat instances beyond what is enumerated in ACPI SRAT to accommodate hot-added CXL memory.
Patches welcome, questions welcome as the development effort on the post v5.12 capabilities proceeds.
## Running in QEMU
The incantation to get CXL support in QEMU [4] is considered unstable at this time. Future readers of this cover letter should verify if any changes are needed. For the novice QEMU user, the following can be copy/pasted into a working QEMU commandline. It is enough to make the simplest topology possible. The topology would consist of a single memory window, single type3 device, single root port, and single host bridge.
+-------------+
| CXL PXB |
| |
| +-------+ |<----------+
| |CXL RP | | |
+--+-------+--+ v
| +----------+
| | "window" |
| +----------+
v ^
+-------------+ |
| CXL Type 3 | |
| Device |<----------+
+-------------+// Memory backend for "window"
-object memory-backend-file,id=cxl-mem1,share,mem-path=cxl-type3,size=512M
// Memory backend for LSA
-object memory-backend-file,id=cxl-mem1-lsa,share,mem-path=cxl-mem1-lsa,size=1K
// Host Bridge
-device pxb-cxl id=cxl.0,bus=pcie.0,bus_nr=52,uid=0 len-window-base=1,window-base[0]=0x4c0000000 memdev[0]=cxl-mem1
// Single root port
-device cxl rp,id=rp0,bus=cxl.0,addr=0.0,chassis=0,slot=0,memdev=cxl-mem1
// Single type3 device
-device cxl-type3,bus=rp0,memdev=cxl-mem1,id=cxl-pmem0,size=256M -device cxl-type3,bus=rp1,memdev=cxl-mem1,id=cxl-pmem1,size=256M,lsa=cxl-mem1-lsa
---
[1]: https://lore.kernel.org/linux-cxl/[email protected]/
[2]: https://www.computeexpresslink.org/](https://www.computeexpresslink.org/
[3]: https://lore.kernel.org/qemu-devel/[email protected]/
[4]: https://gitlab.com/bwidawsk/qemu/-/tree/cxl-2.0v4
[5]: https://github.com/pmem/ndctl/tree/cxl-2.0v2
---
Ben Widawsky (7):
cxl/mem: Find device capabilities
cxl/mem: Add basic IOCTL interface
cxl/mem: Add a "RAW" send command
cxl/mem: Enable commands via CEL
cxl/mem: Add set of informational commands
MAINTAINERS: Add maintainers of the CXL driver
cxl/mem: Add payload dumping for debug
Dan Williams (2):
cxl/mem: Introduce a driver for CXL-2.0-Type-3 endpoints cxl/mem: Register CXL memX devices
.clang-format | 1 + Documentation/ABI/testing/sysfs-bus-cxl | 26 + Documentation/driver-api/cxl/index.rst | 12 + .../driver-api/cxl/memory-devices.rst | 46 + Documentation/driver-api/index.rst | 1 + .../userspace-api/ioctl/ioctl-number.rst | 1 + MAINTAINERS | 11 + drivers/Kconfig | 1 + drivers/Makefile | 1 + drivers/cxl/Kconfig | 66 + drivers/cxl/Makefile | 7 + drivers/cxl/bus.c | 29 + drivers/cxl/cxl.h | 93 + drivers/cxl/mem.c | 1540 +++++++++++++++++ drivers/cxl/pci.h | 31 + include/linux/pci_ids.h | 1 + include/uapi/linux/cxl_mem.h | 170 ++ 17 files changed, 2037 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-cxl create mode 100644 Documentation/driver-api/cxl/index.rst create mode 100644 Documentation/driver-api/cxl/memory-devices.rst create mode 100644 drivers/cxl/Kconfig create mode 100644 drivers/cxl/Makefile create mode 100644 drivers/cxl/bus.c 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 · 4 lines
2.30.1 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
PATCH v4 1/9 cxl/mem: Introduce a driver for CXL-2.0-Type-3 endpoints
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 initial driver boiler-plate and Documentation/ infrastructure.
--- Documentation/driver-api/cxl/index.rst | 12 ++++ .../driver-api/cxl/memory-devices.rst | 29 +++++++++ Documentation/driver-api/index.rst | 1 + drivers/Kconfig | 1 + drivers/Makefile | 1 + drivers/cxl/Kconfig | 35 +++++++++++ drivers/cxl/Makefile | 4 ++ drivers/cxl/mem.c | 62 +++++++++++++++++++ drivers/cxl/pci.h | 17 +++++ include/linux/pci_ids.h | 1 + 10 files changed, 163 insertions(+) create mode 100644 Documentation/driver-api/cxl/index.rst create mode 100644 Documentation/driver-api/cxl/memory-devices.rst create mode 100644 drivers/cxl/Kconfig create mode 100644 drivers/cxl/Makefile create mode 100644 drivers/cxl/mem.c create mode 100644 drivers/cxl/pci.h
Documentation/driver-api/cxl/index.rst new file +12 -0
@@ -0,0 +1,12 @@ +.. SPDX-License-Identifier: GPL-2.0 + +==================== +Compute Express Link +==================== + +.. toctree:: + :maxdepth: 1 + + memory-devices + +.. only:: subproject and html
Documentation/driver-api/cxl/memory-devices.rst new file +29 -0
@@ -0,0 +1,29 @@ +.. 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. It is enumerated as a PCI device for configuration and passing +messages over an MMIO mailbox. Its contribution to the System Physical +Address space is handled via HDM (Host Managed Device Memory) decoders +that optionally define a device's contribution to an interleaved address +range across multiple devices underneath a host-bridge or interleaved +across host-bridges. + +Driver Infrastructure +===================== + +This section covers the driver infrastructure for a CXL memory device. + +CXL Memory Device +----------------- + +.. kernel-doc:: drivers/cxl/mem.c + :doc: cxl mem + +.. kernel-doc:: drivers/cxl/mem.c + :internal:
Documentation/driver-api/index.rst +1 -0
@@ -35,6 +35,7 @@ available subsections can be seen below. usb/index firewire pci/index + cxl/index spi i2c ipmb
drivers/Kconfig +1 -0
@@ -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
@@ -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 +35 -0
@@ -0,0 +1,35 @@ +# SPDX-License-Identifier: GPL-2.0-only +menuconfig CXL_BUS + tristate "CXL (Compute Express Link) Devices Support" + depends on PCI + help + CXL is a bus that is electrically compatible with PCI Express, 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 Express. + Say 'y' to enable support for the configuration and management of + devices supporting these protocols. + +if CXL_BUS + +config CXL_MEM + tristate "CXL.mem: Memory Devices" + 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 prerequisite 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 new file +4 -0
@@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_CXL_MEM) += cxl_mem.o + +cxl_mem-y := mem.o
drivers/cxl/mem.c new file +62 -0
62 lines not on this page · raw message
drivers/cxl/pci.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright(c) 2020 Intel Corporation. All rights reserved. */ +#ifndef __CXL_PCI_H__ +#define __CXL_PCI_H__ + +#define CXL_MEMORY_PROGIF 0x10 + +/* +* See section 8.1 Configuration Space Registers in the CXL 2.0 +* Specification +*/ +#define PCI_DVSEC_VENDOR_ID_CXL 0x1E98 +#define PCI_DVSEC_ID_CXL 0x0 + +#define PCI_DVSEC_ID_CXL_REGLOC_OFFSET 0x8 + +#endif /* __CXL_PCI_H__ */
include/linux/pci_ids.h +1 -0
@@ -51,6 +51,7 @@#define PCI_BASE_CLASS_MEMORY 0x05#define PCI_CLASS_MEMORY_RAM 0x0500#define PCI_CLASS_MEMORY_FLASH 0x0501 +#define PCI_CLASS_MEMORY_CXL 0x0502#define PCI_CLASS_MEMORY_OTHER 0x0580#define PCI_BASE_CLASS_BRIDGE 0x06
Signature · 4 lines
2.30.1 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
PATCH v4 2/9 cxl/mem: Find device capabilities
In reply to Ben WidawskyProvide 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 flow is proven with one implemented command, "identify". Because the class code has already told the driver this is a memory device and the identify command is mandatory.
CXL devices contain an array of capabilities that describe the interactions software can have with the device or firmware running on the device. A CXL compliant device must implement the device status and the mailbox capability. Additionally, 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.
The capabilities tell the driver how to find and map the register space for CXL Memory Devices. The registers are required to utilize the CXL spec defined mailbox interface. The spec outlines two mailboxes, primary and secondary. The secondary mailbox is earmarked for system firmware, and not handled in this driver.
Primary mailboxes are capable of generating an interrupt when submitting a background command. That implementation is saved for a later time.
--- drivers/cxl/cxl.h | 88 ++++++++ drivers/cxl/mem.c | 543 +++++++++++++++++++++++++++++++++++++++++++++- drivers/cxl/pci.h | 14 ++ 3 files changed, 643 insertions(+), 2 deletions(-) create mode 100644 drivers/cxl/cxl.h
drivers/cxl/cxl.h new file +88 -0
@@ -0,0 +1,88 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright(c) 2020 Intel Corporation. */ + +#ifndef __CXL_H__ +#define __CXL_H__ + +#include <linux/bitfield.h> +#include <linux/bitops.h> +#include <linux/io.h> + +/* CXL 2.0 8.2.8.1 Device Capabilities Array Register */ +#define CXLDEV_CAP_ARRAY_OFFSET 0x0 +#define CXLDEV_CAP_ARRAY_CAP_ID 0 +#define CXLDEV_CAP_ARRAY_ID_MASK GENMASK_ULL(15, 0) +#define CXLDEV_CAP_ARRAY_COUNT_MASK GENMASK_ULL(47, 32) +/* CXL 2.0 8.2.8.2.1 CXL Device Capabilities */ +#define CXLDEV_CAP_CAP_ID_DEVICE_STATUS 0x1 +#define CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX 0x2 +#define CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX 0x3 +#define CXLDEV_CAP_CAP_ID_MEMDEV 0x4000 + +/* CXL 2.0 8.2.8.4 Mailbox Registers */ +#define CXLDEV_MBOX_CAPS_OFFSET 0x00 +#define CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK GENMASK(4, 0) +#define CXLDEV_MBOX_CTRL_OFFSET 0x04 +#define CXLDEV_MBOX_CTRL_DOORBELL BIT(0) +#define CXLDEV_MBOX_CMD_OFFSET 0x08 +#define CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0) +#define CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK GENMASK_ULL(36, 16) +#define CXLDEV_MBOX_STATUS_OFFSET 0x10 +#define CXLDEV_MBOX_STATUS_RET_CODE_MASK GENMASK_ULL(47, 32) +#define CXLDEV_MBOX_BG_CMD_STATUS_OFFSET 0x18 +#define CXLDEV_MBOX_PAYLOAD_OFFSET 0x20 + +/* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */ +#define CXLMDEV_STATUS_OFFSET 0x0 +#define CXLMDEV_DEV_FATAL BIT(0) +#define CXLMDEV_FW_HALT BIT(1) +#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) \ + (FIELD_GET(CXLMDEV_STATUS_MEDIA_STATUS_MASK, status) == \ + CXLMDEV_MS_READY) +#define CXLMDEV_MBOX_IF_READY BIT(4) +#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) \ + (FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) != \ + CXLMDEV_RESET_NEEDED_NOT) + +/** +* struct cxl_mem - A CXL memory device +* @pdev: The PCI device associated with this CXL device. +* @regs: IO mappings to the device's MMIO +* @status_regs: CXL 2.0 8.2.8.3 Device Status Registers +* @mbox_regs: CXL 2.0 8.2.8.4 Mailbox Registers +* @memdev_regs: CXL 2.0 8.2.8.5 Memory Device Registers +* @payload_size: Size of space for payload +* (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register) +* @mbox_mutex: Mutex to synchronize mailbox access. +* @firmware_version: Firmware version for the memory device. +* @pmem: Persistent memory capacity information. +* @ram: Volatile memory capacity information. +*/ +struct cxl_mem { +struct pci_dev *pdev; +void __iomem *regs; + +void __iomem *status_regs; +void __iomem *mbox_regs; +void __iomem *memdev_regs; + +size_t payload_size; +struct mutex mbox_mutex;/* Protects device mailbox and firmware */ +char firmware_version[0x10 ]; + +struct range pmem_range; +struct range ram_range; +}; + +#endif /* __CXL_H__ */
drivers/cxl/mem.c +541 -2
565 lines not on this page · raw message
drivers/cxl/pci.h +14 -0
@@ -9,9 +9,23 @@* See section 8.1 Configuration Space Registers in the CXL 2.0 * Specification */ +#define PCI_DVSEC_HEADER1_LENGTH_MASK GENMASK(31, 20)#define PCI_DVSEC_VENDOR_ID_CXL 0x1E98#define PCI_DVSEC_ID_CXL 0x0#define PCI_DVSEC_ID_CXL_REGLOC_OFFSET 0x8 +#define PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET 0xC + +/* BAR Indicator Register (BIR) */ +#define CXL_REGLOC_BIR_MASK GENMASK(2, 0) + +/* Register Block Identifier (RBI) */ +#define CXL_REGLOC_RBI_MASK GENMASK(15, 8) +#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 GENMASK(31, 16)#endif /* __CXL_PCI_H__ */
Signature · 4 lines
2.30.1 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 2/9] cxl/mem: Find device capabilities
Quoted 20 lines · On Mon, 15 Feb 2021 17:45:31 -0800 Ben Widawsky <[email protected]> wrote:
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 flow is proven with one implemented command, "identify". Because the class code has already told the driver this is a memory device and the identify command is mandatory.
CXL devices contain an array of capabilities that describe the interactions software can have with the device or firmware running on the device. A CXL compliant device must implement the device status and the mailbox capability. Additionally, 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.
The capabilities tell the driver how to find and map the register space for CXL Memory Devices. The registers are required to utilize the CXL spec defined mailbox interface. The spec outlines two mailboxes, primary and secondary. The secondary mailbox is earmarked for system firmware, and not handled in this driver.
Quoted 6 lines
Primary mailboxes are capable of generating an interrupt when submitting a background command. That implementation is saved for a later time.
Looks like an off by one error in the register locator iteration.
The potential buffer overrun from memcpy_fromio is still there as well as far as I can see.
If the software provides storage for a payload of size n and the hardware reports a size of n + d, code will happily write beyond the end of the storage provided.
Obviously, this shouldn't happen, but I'm not that trusting of both hardware and software never having bugs.
Jonathan
Quoted 7 lines
--- drivers/cxl/cxl.h | 88 ++++++++ drivers/cxl/mem.c | 543 +++++++++++++++++++++++++++++++++++++++++++++- drivers/cxl/pci.h | 14 ++ 3 files changed, 643 insertions(+), 2 deletions(-) create mode 100644 drivers/cxl/cxl.h
...
Quoted 143 lines
(quoted diff) +2
-0+ +#endif /* __CXL_H__ */drivers/cxl/mem.c +130
-0@@ -3,7 +3,458 @@#include <linux/module.h> #include <linux/pci.h> #include <linux/io.h> +#include <linux/io-64-nonatomic-lo-hi.h> #include "pci.h" +#include "cxl.h" + +#define cxl_doorbell_busy (cxlm) \ + (readl((cxlm)->mbox_regs + CXLDEV_MBOX_CTRL_OFFSET) & \ + CXLDEV_MBOX_CTRL_DOORBELL) + +/* CXL 2.0 - 8.2.8.4 */ +#define CXL_MAILBOX_TIMEOUT_MS (2 * HZ) + +enum opcode { +CXL_MBOX_OP_IDENTIFY =0x4000 , +CXL_MBOX_OP_MAX =0x10000 +}; + +/** +* struct mbox_cmd - A command to be submitted to hardware. +* @opcode: (input) The command set and command submitted to hardware. +* @payload_in: (input) Pointer to the input payload. +* @payload_out: (output) Pointer to the output payload. Must be allocated by +* the caller. +* @size_in: (input) Number of bytes to load from @payload. +* @size_out: (output) Number of bytes loaded into @payload. +* @return_code: (output) Error code returned from hardware. +* +* This is the primary mechanism used to send commands to the hardware. +* All the fields except @payload_* correspond exactly to the fields described in +* Command Register section of the CXL 2.0 8.2.8.4.5. @payload_in and +* @payload_out are written to, and read from the Command Payload Registers +* defined in CXL 2.0 8.2.8.4.8. +*/ +struct mbox_cmd { +u16 opcode; +void *payload_in; +void *payload_out; +size_t size_in; +size_t size_out; +u16 return_code; +#define CXL_MBOX_SUCCESS 0 +}; + +/** +* __cxl_mem_mbox_send_cmd() - Execute a mailbox command +* @cxlm: The CXL memory device to communicate with. +* @mbox_cmd: Command to send to the memory device. +* +* Context: Any context. Expects mbox_mutex to be held. +* Return: -ETIMEDOUT if timeout occurred waiting for completion. 0 on success. +* Caller should check the return code in @mbox_cmd to make sure it +* succeeded. +* +* This is a generic form of the CXL mailbox send command thus only using the +* registers defined by the mailbox capability ID - CXL 2.0 8.2.8.4. Memory +* devices, and perhaps other types of CXL devices may have further information +* available upon error conditions. Driver facilities wishing to send mailbox +* commands should use the wrapper command. +* +* The CXL spec allows for up to two mailboxes. The intention is for the primary +* mailbox to be OS controlled and the secondary mailbox to be used by system +* firmware. This allows the OS and firmware to communicate with the device and +* not need to coordinate with each other. The driver only uses the primary +* mailbox. +*/ +static int __cxl_mem_mbox_send_cmd (struct cxl_mem *cxlm, +struct mbox_cmd *mbox_cmd) +{ +void __iomem *payload = cxlm->mbox_regs +CXLDEV_MBOX_PAYLOAD_OFFSET ; +u64 cmd_reg, status_reg; +size_t out_len; +int rc; + +lockdep_assert_held (&cxlm->mbox_mutex); + +/* +* 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 +* +* Hardware is free to do whatever it wants before the doorbell is rung, +* and isn't allowed to change anything after it clears the doorbell. As +* such, steps 2 and 3 can happen in any order, and steps 6, 7, 8 can +* also happen in any order (though some orders might not make sense). +*/ + +/* #1 */ +if (cxl_doorbell_busy (cxlm)) { +dev_err_ratelimited (&cxlm->pdev->dev, +"Mailbox re-busy after acquiring\n" ); +return -EBUSY ; + } + + cmd_reg =FIELD_PREP (CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK , + mbox_cmd->opcode); +if (mbox_cmd->size_in) { +if (WARN_ON (!mbox_cmd->payload_in)) +return -EINVAL ; + + cmd_reg |=FIELD_PREP (CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK , + mbox_cmd->size_in); +memcpy_toio (payload, mbox_cmd->payload_in, mbox_cmd->size_in); + } + +/* #2, #3 */ +writeq (cmd_reg, cxlm->mbox_regs +CXLDEV_MBOX_CMD_OFFSET ); + +/* #4 */ +dev_dbg (&cxlm->pdev->dev,"Sending command\n" ); +writel (CXLDEV_MBOX_CTRL_DOORBELL , + cxlm->mbox_regs +CXLDEV_MBOX_CTRL_OFFSET ); + +/* #5 */ + rc =cxl_mem_wait_for_doorbell (cxlm); +if (rc == -ETIMEDOUT ) { +cxl_mem_mbox_timeout (cxlm, mbox_cmd); +return rc; + } + +/* #6 */ + status_reg =readq (cxlm->mbox_regs +CXLDEV_MBOX_STATUS_OFFSET ); + mbox_cmd->return_code = +FIELD_GET (CXLDEV_MBOX_STATUS_RET_CODE_MASK , status_reg); + +if (mbox_cmd->return_code !=0 ) { +dev_dbg (&cxlm->pdev->dev,"Mailbox operation had an error\n" );
drivers/cxl/mem.c line 148
Quoted 10 lines
drivers/cxl/mem.c +10
-0+return 0 ; + } + +/* #7 */ + cmd_reg =readq (cxlm->mbox_regs +CXLDEV_MBOX_CMD_OFFSET ); + out_len =FIELD_GET (CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK , cmd_reg); + +/* #8 */ +if (out_len && mbox_cmd->payload_out) { +size_t n =min_t (size_t, cxlm->payload_size, out_len);
This doesn't protect us from the case where the hardware returns a larger payload than the caller is expecting.
i.e. payload_out is too small. We need to pass in the size of that buffer as
well. This currently clamps to the size of the source buffer but does not
check if there is enough space at the destination (mbox_cmd->payload_out).
Quoted 10 lines
drivers/cxl/mem.c +10
-0+ +memcpy_fromio (mbox_cmd->payload_out, payload, n); + mbox_cmd->size_out = n; + }else { + mbox_cmd->size_out =0 ; + } + +return 0 ; +} +
...
Quoted 77 lines
drivers/cxl/mem.c +77
-0+ +/** +* cxl_mem_mbox_send_cmd() - Send a mailbox command to a memory device. +* @cxlm: The CXL memory device to communicate with. +* @opcode: Opcode for the mailbox command. +* @in: The input payload for the mailbox command. +* @in_size: The length of the input payload +* @out: Caller allocated buffer for the output. +* @out_min_size: Minimum expected size of output. +* +* Context: Any context. Will acquire and release mbox_mutex. +* Return: +* * %>=0 - Number of bytes returned in @out. +* * %-E2BIG - Payload is too large for hardware. +* * %-EBUSY - Couldn't acquire exclusive mailbox access. +* * %-EFAULT - Hardware error occurred. +* * %-ENXIO - Command completed, but device reported an error. +* * %-ENODATA - Not enough payload data returned by hardware. +* +* Mailbox commands may execute successfully yet the device itself reported an +* error. While this distinction can be useful for commands from userspace, the +* kernel will only be able to use results when both are successful. +* +* See __cxl_mem_mbox_send_cmd() +*/ +static int cxl_mem_mbox_send_cmd (struct cxl_mem *cxlm,u16 opcode,void *in, +size_t in_size,void *out,size_t out_min_size) +{ +struct mbox_cmd mbox_cmd = { + .opcode = opcode, + .payload_in = in, + .size_in = in_size, + .payload_out = out, + }; +int rc; + +if (out_min_size > cxlm->payload_size) +return -E2BIG ; + + rc =cxl_mem_mbox_get (cxlm); +if (rc) +return rc; + + rc =__cxl_mem_mbox_send_cmd (cxlm, &mbox_cmd); +cxl_mem_mbox_put (cxlm); +if (rc) +return rc; + +/* TODO: Map return code to proper kernel style errno */ +if (mbox_cmd.return_code !=CXL_MBOX_SUCCESS ) +return -ENXIO ; + +if (mbox_cmd.size_out < out_min_size) +return -ENODATA ; + +return mbox_cmd.size_out; +} + +/** +* cxl_mem_setup_regs() - Setup necessary MMIO. +* @cxlm: The CXL memory device to communicate with. +* +* Return: 0 if all necessary registers mapped. +* +* A memory device is required by spec to implement a certain set of MMIO +* regions. The purpose of this function is to enumerate and map those +* registers. +*/ +static int cxl_mem_setup_regs (struct cxl_mem *cxlm) +{ +struct device *dev = &cxlm->pdev->dev; +int cap, cap_count; +u64 cap_array; + + cap_array =readq (cxlm->regs +CXLDEV_CAP_ARRAY_OFFSET ); +if (FIELD_GET (CXLDEV_CAP_ARRAY_ID_MASK , cap_array) != +CXLDEV_CAP_ARRAY_CAP_ID )
drivers/cxl/mem.c line 354
Quoted 10 lines
drivers/cxl/mem.c +10
-0+return -ENODEV ; + + cap_count =FIELD_GET (CXLDEV_CAP_ARRAY_COUNT_MASK , cap_array); + +for (cap =1 ; cap <= cap_count; cap++) { +void __iomem *register_block; +u32 offset; +u16 cap_id; + + cap_id =readl (cxlm->regs + cap *0x10 ) &0xffff ;
Slight preference for FIELD_GET just for consistency.
Quoted 36 lines
drivers/cxl/mem.c +36
-0+ offset =readl (cxlm->regs + cap *0x10 +0x4 ); + register_block = cxlm->regs + offset; + +switch (cap_id) { +case CXLDEV_CAP_CAP_ID_DEVICE_STATUS : +dev_dbg (dev,"found Status capability (0x%x)\n" , offset); + cxlm->status_regs = register_block; +break ; +case CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX : +dev_dbg (dev,"found Mailbox capability (0x%x)\n" , offset); + cxlm->mbox_regs = register_block; +break ; +case CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX : +dev_dbg (dev,"found Secondary Mailbox capability (0x%x)\n" , offset); +break ; +case CXLDEV_CAP_CAP_ID_MEMDEV : +dev_dbg (dev,"found Memory Device capability (0x%x)\n" , offset); + cxlm->memdev_regs = register_block; +break ; +default : +dev_dbg (dev,"Unknown cap ID: %d (0x%x)\n" , cap_id, offset); +break ; + } + } + +if (!cxlm->status_regs || !cxlm->mbox_regs || !cxlm->memdev_regs) { +dev_err (dev,"registers not found: %s%s%s\n" , + !cxlm->status_regs ?"status " :"" , + !cxlm->mbox_regs ?"mbox " :"" , + !cxlm->memdev_regs ?"memdev" :"" ); +return -ENXIO ; + } + +return 0 ; +} +
...
drivers/cxl/mem.c line 419
Quoted 3 lines
drivers/cxl/mem.c +3
-0+ +static struct cxl_mem *cxl_mem_create (struct pci_dev *pdev,u32 reg_lo, +u32 reg_hi)
I'm not really suggesting you change it at this point, but I'd have done the splitting of reg_lo up and the building of the offset at the call site rather than in here. I think that would have been slightly easier to follow.
Quoted 109 lines
drivers/cxl/mem.c +94
-1+{ +struct device *dev = &pdev->dev; +struct cxl_mem *cxlm; +void __iomem *regs; +u64 offset; +u8 bar; +int rc; + + cxlm =devm_kzalloc (&pdev->dev,sizeof (*cxlm),GFP_KERNEL ); +if (!cxlm) { +dev_err (dev,"No memory available\n" ); +return NULL ; + } + + offset = ((u64 )reg_hi <<32 ) |FIELD_GET (CXL_REGLOC_ADDR_MASK , reg_lo); + bar =FIELD_GET (CXL_REGLOC_BIR_MASK , reg_lo); + +/* Basic sanity check that BAR is big enough */ +if (pci_resource_len (pdev, bar) < offset) { +dev_err (dev,"BAR%d: %pr: too small (offset: %#llx)\n" , bar, + &pdev->resource[bar], (unsigned long long )offset); +return NULL ; + } + + rc =pcim_iomap_regions (pdev,BIT (bar),pci_name (pdev)); +if (rc) { +dev_err (dev,"failed to map registers\n" ); +return NULL ; + } + regs =pcim_iomap_table (pdev)[bar]; + +mutex_init (&cxlm->mbox_mutex); + cxlm->pdev = pdev; + cxlm->regs = regs + offset; + +dev_dbg (dev,"Mapped CXL Memory Device resource\n" ); +return cxlm; +}static int cxl_mem_dvsec (struct pci_dev *pdev,int dvsec) {@@ -28,10 +479,65 @@ static int cxl_mem_dvsec(struct pci_dev *pdev, int dvsec)return 0 ; } +/** +* cxl_mem_identify() - Send the IDENTIFY command to the device. +* @cxlm: The device to identify. +* +* Return: 0 if identify was executed successfully. +* +* This will dispatch the identify command to the device and on success populate +* structures to be exported to sysfs. +*/ +static int cxl_mem_identify (struct cxl_mem *cxlm) +{ +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 id; +int rc; + + rc =cxl_mem_mbox_send_cmd (cxlm,CXL_MBOX_OP_IDENTIFY ,NULL ,0 , &id, +sizeof (id)); +if (rc <0 ) +return rc; + +/* +* TODO: enumerate DPA map, as 'ram' and 'pmem' do not alias. +* For now, only the capacity is exported in sysfs +*/ + cxlm->ram_range.start =0 ; + cxlm->ram_range.end =le64_to_cpu (id.volatile_capacity) -1 ; + + cxlm->pmem_range.start =0 ; + cxlm->pmem_range.end =le64_to_cpu (id.persistent_capacity) -1 ; + +memcpy (cxlm->firmware_version, id.fw_revision,sizeof (id.fw_revision)); + +return 0 ; +} +static int cxl_mem_probe (struct pci_dev *pdev,const struct pci_device_id *id) {struct device *dev = &pdev->dev; -int regloc; +struct cxl_mem *cxlm =NULL ; +int rc, regloc, i; +u32 regloc_size; + + rc =pcim_enable_device (pdev); +if (rc) +return rc; regloc =cxl_mem_dvsec (pdev,PCI_DVSEC_ID_CXL_REGLOC_OFFSET );if (!regloc) {
drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 550
Quoted 7 lines
drivers/cxl/mem.c +3
-1@@ -39,7 +545,40 @@ static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id)return -ENXIO ; } -return 0 ; +/* Get the size of the Register Locator DVSEC */ +pci_read_config_dword (pdev, regloc +PCI_DVSEC_HEADER1 , ®loc_size); + regloc_size =FIELD_GET (PCI_DVSEC_HEADER1_LENGTH_MASK , regloc_size);
The size field here is the dvsec length.. Let's say we only have one register block entry at +0x0c and +0x10
From PCI spec :
"DVSEC Length - This field indicates the number of bytes in the entire DVSEC structure, including the PCI Express Extended Capability Header, the DVSEC Header 1, DVSEC Header 2, and DVSEC vendor-specific registers."
So here it would be 0x14
drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 552
Quoted 2 lines
drivers/cxl/mem.c +2
-0+ + regloc +=PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET ;
We then shift regloc forward by +0xc
drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 554
Quoted 2 lines
drivers/cxl/mem.c +2
-0+ +for (i = regloc; i < regloc + regloc_size; i +=8 ) {
This loop will then index form
i= +0xc to i < +0c + 0x14 (0x20)
i = 0xc, 0x14
So that's indexing one more entry than is actually present. Should be something like
for (i = regloc; i < regloc + regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET; i++)
which will mean the only iteration for this example is the one with i == +0xC
Quoted 48 lines
drivers/cxl/mem.c +27
-0+u32 reg_lo, reg_hi; +u8 reg_type; + +/* "register low and high" contain other bits */ +pci_read_config_dword (pdev, i, ®_lo); +pci_read_config_dword (pdev, i +4 , ®_hi); + + reg_type =FIELD_GET (CXL_REGLOC_RBI_MASK , reg_lo); + +if (reg_type ==CXL_REGLOC_RBI_MEMDEV ) { + cxlm =cxl_mem_create (pdev, reg_lo, reg_hi); +break ; + } + } + +if (!cxlm) +return -ENODEV ; + + rc =cxl_mem_setup_regs (cxlm); +if (rc) +return rc; + + rc =cxl_mem_setup_mailbox (cxlm); +if (rc) +return rc; + +return cxl_mem_identify (cxlm); }static const struct pci_device_id cxl_mem_pci_tbl[] = {drivers/cxl/pci.h +6
-0@@ -9,9 +9,23 @@* See section 8.1 Configuration Space Registers in the CXL 2.0 * Specification */ +#define PCI_DVSEC_HEADER1_LENGTH_MASK GENMASK(31, 20)#define PCI_DVSEC_VENDOR_ID_CXL 0x1E98#define PCI_DVSEC_ID_CXL 0x0#define PCI_DVSEC_ID_CXL_REGLOC_OFFSET 0x8 +#define PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET 0xC + +/* BAR Indicator Register (BIR) */ +#define CXL_REGLOC_BIR_MASK GENMASK(2, 0) +
drivers/cxl/pci.h line 31
Quoted 10 lines
drivers/cxl/pci.h +8
-0+/* Register Block Identifier (RBI) */ +#define CXL_REGLOC_RBI_MASK GENMASK(15, 8) +#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 GENMASK(31, 16)#endif /* __CXL_PCI_H__ */
_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 2/9] cxl/mem: Find device capabilities
Quoted 210 lines · Jonathan Cameron
Quoted 20 lines · On Mon, 15 Feb 2021 17:45:31 -0800 Ben Widawsky <[email protected]> wrote:
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 flow is proven with one implemented command, "identify". Because the class code has already told the driver this is a memory device and the identify command is mandatory.
CXL devices contain an array of capabilities that describe the interactions software can have with the device or firmware running on the device. A CXL compliant device must implement the device status and the mailbox capability. Additionally, 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.
The capabilities tell the driver how to find and map the register space for CXL Memory Devices. The registers are required to utilize the CXL spec defined mailbox interface. The spec outlines two mailboxes, primary and secondary. The secondary mailbox is earmarked for system firmware, and not handled in this driver.
Quoted 6 lines
Primary mailboxes are capable of generating an interrupt when submitting a background command. That implementation is saved for a later time.
Looks like an off by one error in the register locator iteration.
The potential buffer overrun from
memcpy_fromiois still there as well as far as I can see.If the software provides storage for a payload of size n and the hardware reports a size of n + d, code will happily write beyond the end of the storage provided.
Obviously, this shouldn't happen, but I'm not that trusting of both hardware and software never having bugs.
Jonathan
Quoted 7 lines
--- drivers/cxl/cxl.h | 88 ++++++++ drivers/cxl/mem.c | 543 +++++++++++++++++++++++++++++++++++++++++++++- drivers/cxl/pci.h | 14 ++ 3 files changed, 643 insertions(+), 2 deletions(-) create mode 100644 drivers/cxl/cxl.h...
Quoted 143 lines
(quoted diff) +2
-0+ +#endif /* __CXL_H__ */drivers/cxl/mem.c +130
-0@@ -3,7 +3,458 @@#include <linux/module.h> #include <linux/pci.h> #include <linux/io.h> +#include <linux/io-64-nonatomic-lo-hi.h> #include "pci.h" +#include "cxl.h" + +#define cxl_doorbell_busy (cxlm) \ + (readl((cxlm)->mbox_regs + CXLDEV_MBOX_CTRL_OFFSET) & \ + CXLDEV_MBOX_CTRL_DOORBELL) + +/* CXL 2.0 - 8.2.8.4 */ +#define CXL_MAILBOX_TIMEOUT_MS (2 * HZ) + +enum opcode { +CXL_MBOX_OP_IDENTIFY =0x4000 , +CXL_MBOX_OP_MAX =0x10000 +}; + +/** +* struct mbox_cmd - A command to be submitted to hardware. +* @opcode: (input) The command set and command submitted to hardware. +* @payload_in: (input) Pointer to the input payload. +* @payload_out: (output) Pointer to the output payload. Must be allocated by +* the caller. +* @size_in: (input) Number of bytes to load from @payload. +* @size_out: (output) Number of bytes loaded into @payload. +* @return_code: (output) Error code returned from hardware. +* +* This is the primary mechanism used to send commands to the hardware. +* All the fields except @payload_* correspond exactly to the fields described in +* Command Register section of the CXL 2.0 8.2.8.4.5. @payload_in and +* @payload_out are written to, and read from the Command Payload Registers +* defined in CXL 2.0 8.2.8.4.8. +*/ +struct mbox_cmd { +u16 opcode; +void *payload_in; +void *payload_out; +size_t size_in; +size_t size_out; +u16 return_code; +#define CXL_MBOX_SUCCESS 0 +}; + +/** +* __cxl_mem_mbox_send_cmd() - Execute a mailbox command +* @cxlm: The CXL memory device to communicate with. +* @mbox_cmd: Command to send to the memory device. +* +* Context: Any context. Expects mbox_mutex to be held. +* Return: -ETIMEDOUT if timeout occurred waiting for completion. 0 on success. +* Caller should check the return code in @mbox_cmd to make sure it +* succeeded. +* +* This is a generic form of the CXL mailbox send command thus only using the +* registers defined by the mailbox capability ID - CXL 2.0 8.2.8.4. Memory +* devices, and perhaps other types of CXL devices may have further information +* available upon error conditions. Driver facilities wishing to send mailbox +* commands should use the wrapper command. +* +* The CXL spec allows for up to two mailboxes. The intention is for the primary +* mailbox to be OS controlled and the secondary mailbox to be used by system +* firmware. This allows the OS and firmware to communicate with the device and +* not need to coordinate with each other. The driver only uses the primary +* mailbox. +*/ +static int __cxl_mem_mbox_send_cmd (struct cxl_mem *cxlm, +struct mbox_cmd *mbox_cmd) +{ +void __iomem *payload = cxlm->mbox_regs +CXLDEV_MBOX_PAYLOAD_OFFSET ; +u64 cmd_reg, status_reg; +size_t out_len; +int rc; + +lockdep_assert_held (&cxlm->mbox_mutex); + +/* +* 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 +* +* Hardware is free to do whatever it wants before the doorbell is rung, +* and isn't allowed to change anything after it clears the doorbell. As +* such, steps 2 and 3 can happen in any order, and steps 6, 7, 8 can +* also happen in any order (though some orders might not make sense). +*/ + +/* #1 */ +if (cxl_doorbell_busy (cxlm)) { +dev_err_ratelimited (&cxlm->pdev->dev, +"Mailbox re-busy after acquiring\n" ); +return -EBUSY ; + } + + cmd_reg =FIELD_PREP (CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK , + mbox_cmd->opcode); +if (mbox_cmd->size_in) { +if (WARN_ON (!mbox_cmd->payload_in)) +return -EINVAL ; + + cmd_reg |=FIELD_PREP (CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK , + mbox_cmd->size_in); +memcpy_toio (payload, mbox_cmd->payload_in, mbox_cmd->size_in); + } + +/* #2, #3 */ +writeq (cmd_reg, cxlm->mbox_regs +CXLDEV_MBOX_CMD_OFFSET ); + +/* #4 */ +dev_dbg (&cxlm->pdev->dev,"Sending command\n" ); +writel (CXLDEV_MBOX_CTRL_DOORBELL , + cxlm->mbox_regs +CXLDEV_MBOX_CTRL_OFFSET ); + +/* #5 */ + rc =cxl_mem_wait_for_doorbell (cxlm); +if (rc == -ETIMEDOUT ) { +cxl_mem_mbox_timeout (cxlm, mbox_cmd); +return rc; + } + +/* #6 */ + status_reg =readq (cxlm->mbox_regs +CXLDEV_MBOX_STATUS_OFFSET ); + mbox_cmd->return_code = +FIELD_GET (CXLDEV_MBOX_STATUS_RET_CODE_MASK , status_reg); + +if (mbox_cmd->return_code !=0 ) { +dev_dbg (&cxlm->pdev->dev,"Mailbox operation had an error\n" );drivers/cxl/mem.c line 148
Quoted 10 lines
drivers/cxl/mem.c +10
-0+return 0 ; + } + +/* #7 */ + cmd_reg =readq (cxlm->mbox_regs +CXLDEV_MBOX_CMD_OFFSET ); + out_len =FIELD_GET (CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK , cmd_reg); + +/* #8 */ +if (out_len && mbox_cmd->payload_out) { +size_t n =min_t (size_t, cxlm->payload_size, out_len);This doesn't protect us from the case where the hardware returns a larger payload than the caller is expecting.
Quoted 4 lines
i.e.
payload_outis too small. We need to pass in the size of that buffer as
well. This currently clamps to the size of the source buffer but does not
check if there is enough space at the destination (mbox_cmd->payload_out).
Let me articulate the issue a bit. The userspace call chain should be fine:cxl_send_cmd() -> ioctl handlers
cxl_validate_cmd_from_user -> converts to internal command
handle_mailbox_cmd_from_user -> dispatches mbox command.
cxl_send_cmd():
if (c.info.size_out < 0)
c.info.size_out = cxlm->payload_size;
handle_mailbox_cmd_from_user():
if (cmd->info.size_out) {
mbox_cmd.payload_out = kvzalloc(cmd->info.size_out, GFP_KERNEL);The kernel call chain could have issues:
cxl_mem_identify/*() -> kernel caller allocates just enough space
cxl_mem_mbox_send_cmd() -> internal wrapper we created for v3
blows up in the spot you mention.
The driver allocates enough space on the stack for all these calls, but yes, if hardware is out of spec it would be problematic. In previous versions of this series, there has been a check there. However, the ability to have hardware return more data than expected is I believe the correct functionality here.
So my proposal is for now, since no real hardware exists, and the command set here is so benign, we leave fixing this as a TODO.
I can post a patch on top of this series to address this issue in a manner I believe warrants discussing (kvzalloc max payload size buffers on open() and for each driver instance).
Quoted 142 lines
Quoted 10 lines
drivers/cxl/mem.c +10
-0+ +memcpy_fromio (mbox_cmd->payload_out, payload, n); + mbox_cmd->size_out = n; + }else { + mbox_cmd->size_out =0 ; + } + +return 0 ; +} +...
Quoted 77 lines
drivers/cxl/mem.c +77
-0+ +/** +* cxl_mem_mbox_send_cmd() - Send a mailbox command to a memory device. +* @cxlm: The CXL memory device to communicate with. +* @opcode: Opcode for the mailbox command. +* @in: The input payload for the mailbox command. +* @in_size: The length of the input payload +* @out: Caller allocated buffer for the output. +* @out_min_size: Minimum expected size of output. +* +* Context: Any context. Will acquire and release mbox_mutex. +* Return: +* * %>=0 - Number of bytes returned in @out. +* * %-E2BIG - Payload is too large for hardware. +* * %-EBUSY - Couldn't acquire exclusive mailbox access. +* * %-EFAULT - Hardware error occurred. +* * %-ENXIO - Command completed, but device reported an error. +* * %-ENODATA - Not enough payload data returned by hardware. +* +* Mailbox commands may execute successfully yet the device itself reported an +* error. While this distinction can be useful for commands from userspace, the +* kernel will only be able to use results when both are successful. +* +* See __cxl_mem_mbox_send_cmd() +*/ +static int cxl_mem_mbox_send_cmd (struct cxl_mem *cxlm,u16 opcode,void *in, +size_t in_size,void *out,size_t out_min_size) +{ +struct mbox_cmd mbox_cmd = { + .opcode = opcode, + .payload_in = in, + .size_in = in_size, + .payload_out = out, + }; +int rc; + +if (out_min_size > cxlm->payload_size) +return -E2BIG ; + + rc =cxl_mem_mbox_get (cxlm); +if (rc) +return rc; + + rc =__cxl_mem_mbox_send_cmd (cxlm, &mbox_cmd); +cxl_mem_mbox_put (cxlm); +if (rc) +return rc; + +/* TODO: Map return code to proper kernel style errno */ +if (mbox_cmd.return_code !=CXL_MBOX_SUCCESS ) +return -ENXIO ; + +if (mbox_cmd.size_out < out_min_size) +return -ENODATA ; + +return mbox_cmd.size_out; +} + +/** +* cxl_mem_setup_regs() - Setup necessary MMIO. +* @cxlm: The CXL memory device to communicate with. +* +* Return: 0 if all necessary registers mapped. +* +* A memory device is required by spec to implement a certain set of MMIO +* regions. The purpose of this function is to enumerate and map those +* registers. +*/ +static int cxl_mem_setup_regs (struct cxl_mem *cxlm) +{ +struct device *dev = &cxlm->pdev->dev; +int cap, cap_count; +u64 cap_array; + + cap_array =readq (cxlm->regs +CXLDEV_CAP_ARRAY_OFFSET ); +if (FIELD_GET (CXLDEV_CAP_ARRAY_ID_MASK , cap_array) != +CXLDEV_CAP_ARRAY_CAP_ID )drivers/cxl/mem.c line 354
Quoted 10 lines
drivers/cxl/mem.c +10
-0+return -ENODEV ; + + cap_count =FIELD_GET (CXLDEV_CAP_ARRAY_COUNT_MASK , cap_array); + +for (cap =1 ; cap <= cap_count; cap++) { +void __iomem *register_block; +u32 offset; +u16 cap_id; + + cap_id =readl (cxlm->regs + cap *0x10 ) &0xffff ;Slight preference for
FIELD_GETjust for consistency.Quoted 36 lines
drivers/cxl/mem.c +36
-0+ offset =readl (cxlm->regs + cap *0x10 +0x4 ); + register_block = cxlm->regs + offset; + +switch (cap_id) { +case CXLDEV_CAP_CAP_ID_DEVICE_STATUS : +dev_dbg (dev,"found Status capability (0x%x)\n" , offset); + cxlm->status_regs = register_block; +break ; +case CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX : +dev_dbg (dev,"found Mailbox capability (0x%x)\n" , offset); + cxlm->mbox_regs = register_block; +break ; +case CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX : +dev_dbg (dev,"found Secondary Mailbox capability (0x%x)\n" , offset); +break ; +case CXLDEV_CAP_CAP_ID_MEMDEV : +dev_dbg (dev,"found Memory Device capability (0x%x)\n" , offset); + cxlm->memdev_regs = register_block; +break ; +default : +dev_dbg (dev,"Unknown cap ID: %d (0x%x)\n" , cap_id, offset); +break ; + } + } + +if (!cxlm->status_regs || !cxlm->mbox_regs || !cxlm->memdev_regs) { +dev_err (dev,"registers not found: %s%s%s\n" , + !cxlm->status_regs ?"status " :"" , + !cxlm->mbox_regs ?"mbox " :"" , + !cxlm->memdev_regs ?"memdev" :"" ); +return -ENXIO ; + } + +return 0 ; +} +...
Quoted 8 lines
drivers/cxl/mem.c line 419
Quoted 3 lines
drivers/cxl/mem.c +3
-0+ +static struct cxl_mem *cxl_mem_create (struct pci_dev *pdev,u32 reg_lo, +u32 reg_hi)I'm not really suggesting you change it at this point, but I'd have done the splitting of
reg_loup and the building of the offset at the call site rather than in here. I think that would have been slightly easier to follow.
Noted. In future patches this is going to get reworked somewhat to support more flexibility with register blocks.
Quoted 139 lines
Quoted 109 lines
drivers/cxl/mem.c +94
-1+{ +struct device *dev = &pdev->dev; +struct cxl_mem *cxlm; +void __iomem *regs; +u64 offset; +u8 bar; +int rc; + + cxlm =devm_kzalloc (&pdev->dev,sizeof (*cxlm),GFP_KERNEL ); +if (!cxlm) { +dev_err (dev,"No memory available\n" ); +return NULL ; + } + + offset = ((u64 )reg_hi <<32 ) |FIELD_GET (CXL_REGLOC_ADDR_MASK , reg_lo); + bar =FIELD_GET (CXL_REGLOC_BIR_MASK , reg_lo); + +/* Basic sanity check that BAR is big enough */ +if (pci_resource_len (pdev, bar) < offset) { +dev_err (dev,"BAR%d: %pr: too small (offset: %#llx)\n" , bar, + &pdev->resource[bar], (unsigned long long )offset); +return NULL ; + } + + rc =pcim_iomap_regions (pdev,BIT (bar),pci_name (pdev)); +if (rc) { +dev_err (dev,"failed to map registers\n" ); +return NULL ; + } + regs =pcim_iomap_table (pdev)[bar]; + +mutex_init (&cxlm->mbox_mutex); + cxlm->pdev = pdev; + cxlm->regs = regs + offset; + +dev_dbg (dev,"Mapped CXL Memory Device resource\n" ); +return cxlm; +}static int cxl_mem_dvsec (struct pci_dev *pdev,int dvsec) {@@ -28,10 +479,65 @@ static int cxl_mem_dvsec(struct pci_dev *pdev, int dvsec)return 0 ; } +/** +* cxl_mem_identify() - Send the IDENTIFY command to the device. +* @cxlm: The device to identify. +* +* Return: 0 if identify was executed successfully. +* +* This will dispatch the identify command to the device and on success populate +* structures to be exported to sysfs. +*/ +static int cxl_mem_identify (struct cxl_mem *cxlm) +{ +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 id; +int rc; + + rc =cxl_mem_mbox_send_cmd (cxlm,CXL_MBOX_OP_IDENTIFY ,NULL ,0 , &id, +sizeof (id)); +if (rc <0 ) +return rc; + +/* +* TODO: enumerate DPA map, as 'ram' and 'pmem' do not alias. +* For now, only the capacity is exported in sysfs +*/ + cxlm->ram_range.start =0 ; + cxlm->ram_range.end =le64_to_cpu (id.volatile_capacity) -1 ; + + cxlm->pmem_range.start =0 ; + cxlm->pmem_range.end =le64_to_cpu (id.persistent_capacity) -1 ; + +memcpy (cxlm->firmware_version, id.fw_revision,sizeof (id.fw_revision)); + +return 0 ; +} +static int cxl_mem_probe (struct pci_dev *pdev,const struct pci_device_id *id) {struct device *dev = &pdev->dev; -int regloc; +struct cxl_mem *cxlm =NULL ; +int rc, regloc, i; +u32 regloc_size; + + rc =pcim_enable_device (pdev); +if (rc) +return rc; regloc =cxl_mem_dvsec (pdev,PCI_DVSEC_ID_CXL_REGLOC_OFFSET );if (!regloc) {drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 550
Quoted 7 lines
drivers/cxl/mem.c +3
-1@@ -39,7 +545,40 @@ static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id)return -ENXIO ; } -return 0 ; +/* Get the size of the Register Locator DVSEC */ +pci_read_config_dword (pdev, regloc +PCI_DVSEC_HEADER1 , ®loc_size); + regloc_size =FIELD_GET (PCI_DVSEC_HEADER1_LENGTH_MASK , regloc_size);The size field here is the dvsec length.. Let's say we only have one register block entry at +0x0c and +0x10
From PCI spec :"DVSEC Length - This field indicates the number of bytes in the entire DVSEC structure, including the PCI Express Extended Capability Header, the DVSEC Header 1, DVSEC Header 2, and DVSEC vendor-specific registers."
So here it would be
0x14drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 552
Quoted 2 lines
drivers/cxl/mem.c +2
-0+ + regloc +=PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET ;We then shift regloc forward by +0xc
drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 554
Quoted 2 lines
drivers/cxl/mem.c +2
-0+ +for (i = regloc; i < regloc + regloc_size; i +=8 ) {This loop will then index form
i= +0xc to i < +0c +0x14(0x20)
i =0xc,0x14
Quoted 9 lines
So that's indexing one more entry than is actually present. Should be something like
for (i = regloc; i < regloc + regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET; i++)which will mean the only iteration for this example is the one with i == +0xC
Good catch. I think this warrants rewriting a bit, let me know what you think?
regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
for (i = 0; i < regblocks; i++, regloc+=8) {
pci_read_config_dword(pdev, regloc, ®_lo);
pci_read_config_dword(pdev, regloc + 4, ®_hi);
...
}Quoted 57 lines
Quoted 57 lines
drivers/cxl/mem.c +27
-0+u32 reg_lo, reg_hi; +u8 reg_type; + +/* "register low and high" contain other bits */ +pci_read_config_dword (pdev, i, ®_lo); +pci_read_config_dword (pdev, i +4 , ®_hi); + + reg_type =FIELD_GET (CXL_REGLOC_RBI_MASK , reg_lo); + +if (reg_type ==CXL_REGLOC_RBI_MEMDEV ) { + cxlm =cxl_mem_create (pdev, reg_lo, reg_hi); +break ; + } + } + +if (!cxlm) +return -ENODEV ; + + rc =cxl_mem_setup_regs (cxlm); +if (rc) +return rc; + + rc =cxl_mem_setup_mailbox (cxlm); +if (rc) +return rc; + +return cxl_mem_identify (cxlm); }static const struct pci_device_id cxl_mem_pci_tbl[] = {drivers/cxl/pci.h +14
-0@@ -9,9 +9,23 @@* See section 8.1 Configuration Space Registers in the CXL 2.0 * Specification */ +#define PCI_DVSEC_HEADER1_LENGTH_MASK GENMASK(31, 20)#define PCI_DVSEC_VENDOR_ID_CXL 0x1E98#define PCI_DVSEC_ID_CXL 0x0#define PCI_DVSEC_ID_CXL_REGLOC_OFFSET 0x8 +#define PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET 0xC + +/* BAR Indicator Register (BIR) */ +#define CXL_REGLOC_BIR_MASK GENMASK(2, 0) + +/* Register Block Identifier (RBI) */ +#define CXL_REGLOC_RBI_MASK GENMASK(15, 8) +#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 GENMASK(31, 16)
Quoted 2 lines
drivers/cxl/pci.h line 31
Quoted 1 line
drivers/cxl/pci.h +0
-0#endif /* __CXL_PCI_H__ */
_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 2/9] cxl/mem: Find device capabilities
Quoted 216 lines · On Tue, 16 Feb 2021 08:43:03 -0800 Ben Widawsky <[email protected]> wrote:
Quoted 210 lines · Jonathan Cameron
Quoted 20 lines · On Mon, 15 Feb 2021 17:45:31 -0800 Ben Widawsky <[email protected]> wrote:
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 flow is proven with one implemented command, "identify". Because the class code has already told the driver this is a memory device and the identify command is mandatory.
CXL devices contain an array of capabilities that describe the interactions software can have with the device or firmware running on the device. A CXL compliant device must implement the device status and the mailbox capability. Additionally, 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.
The capabilities tell the driver how to find and map the register space for CXL Memory Devices. The registers are required to utilize the CXL spec defined mailbox interface. The spec outlines two mailboxes, primary and secondary. The secondary mailbox is earmarked for system firmware, and not handled in this driver.
Quoted 6 lines
Primary mailboxes are capable of generating an interrupt when submitting a background command. That implementation is saved for a later time.
Looks like an off by one error in the register locator iteration.
The potential buffer overrun from
memcpy_fromiois still there as well as far as I can see.If the software provides storage for a payload of size n and the hardware reports a size of n + d, code will happily write beyond the end of the storage provided.
Obviously, this shouldn't happen, but I'm not that trusting of both hardware and software never having bugs.
Jonathan
Quoted 7 lines
--- drivers/cxl/cxl.h | 88 ++++++++ drivers/cxl/mem.c | 543 +++++++++++++++++++++++++++++++++++++++++++++- drivers/cxl/pci.h | 14 ++ 3 files changed, 643 insertions(+), 2 deletions(-) create mode 100644 drivers/cxl/cxl.h...
Quoted 143 lines
(quoted diff) +2
-0+ +#endif /* __CXL_H__ */drivers/cxl/mem.c +130
-0@@ -3,7 +3,458 @@#include <linux/module.h> #include <linux/pci.h> #include <linux/io.h> +#include <linux/io-64-nonatomic-lo-hi.h> #include "pci.h" +#include "cxl.h" + +#define cxl_doorbell_busy (cxlm) \ + (readl((cxlm)->mbox_regs + CXLDEV_MBOX_CTRL_OFFSET) & \ + CXLDEV_MBOX_CTRL_DOORBELL) + +/* CXL 2.0 - 8.2.8.4 */ +#define CXL_MAILBOX_TIMEOUT_MS (2 * HZ) + +enum opcode { +CXL_MBOX_OP_IDENTIFY =0x4000 , +CXL_MBOX_OP_MAX =0x10000 +}; + +/** +* struct mbox_cmd - A command to be submitted to hardware. +* @opcode: (input) The command set and command submitted to hardware. +* @payload_in: (input) Pointer to the input payload. +* @payload_out: (output) Pointer to the output payload. Must be allocated by +* the caller. +* @size_in: (input) Number of bytes to load from @payload. +* @size_out: (output) Number of bytes loaded into @payload. +* @return_code: (output) Error code returned from hardware. +* +* This is the primary mechanism used to send commands to the hardware. +* All the fields except @payload_* correspond exactly to the fields described in +* Command Register section of the CXL 2.0 8.2.8.4.5. @payload_in and +* @payload_out are written to, and read from the Command Payload Registers +* defined in CXL 2.0 8.2.8.4.8. +*/ +struct mbox_cmd { +u16 opcode; +void *payload_in; +void *payload_out; +size_t size_in; +size_t size_out; +u16 return_code; +#define CXL_MBOX_SUCCESS 0 +}; + +/** +* __cxl_mem_mbox_send_cmd() - Execute a mailbox command +* @cxlm: The CXL memory device to communicate with. +* @mbox_cmd: Command to send to the memory device. +* +* Context: Any context. Expects mbox_mutex to be held. +* Return: -ETIMEDOUT if timeout occurred waiting for completion. 0 on success. +* Caller should check the return code in @mbox_cmd to make sure it +* succeeded. +* +* This is a generic form of the CXL mailbox send command thus only using the +* registers defined by the mailbox capability ID - CXL 2.0 8.2.8.4. Memory +* devices, and perhaps other types of CXL devices may have further information +* available upon error conditions. Driver facilities wishing to send mailbox +* commands should use the wrapper command. +* +* The CXL spec allows for up to two mailboxes. The intention is for the primary +* mailbox to be OS controlled and the secondary mailbox to be used by system +* firmware. This allows the OS and firmware to communicate with the device and +* not need to coordinate with each other. The driver only uses the primary +* mailbox. +*/ +static int __cxl_mem_mbox_send_cmd (struct cxl_mem *cxlm, +struct mbox_cmd *mbox_cmd) +{ +void __iomem *payload = cxlm->mbox_regs +CXLDEV_MBOX_PAYLOAD_OFFSET ; +u64 cmd_reg, status_reg; +size_t out_len; +int rc; + +lockdep_assert_held (&cxlm->mbox_mutex); + +/* +* 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 +* +* Hardware is free to do whatever it wants before the doorbell is rung, +* and isn't allowed to change anything after it clears the doorbell. As +* such, steps 2 and 3 can happen in any order, and steps 6, 7, 8 can +* also happen in any order (though some orders might not make sense). +*/ + +/* #1 */ +if (cxl_doorbell_busy (cxlm)) { +dev_err_ratelimited (&cxlm->pdev->dev, +"Mailbox re-busy after acquiring\n" ); +return -EBUSY ; + } + + cmd_reg =FIELD_PREP (CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK , + mbox_cmd->opcode); +if (mbox_cmd->size_in) { +if (WARN_ON (!mbox_cmd->payload_in)) +return -EINVAL ; + + cmd_reg |=FIELD_PREP (CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK , + mbox_cmd->size_in); +memcpy_toio (payload, mbox_cmd->payload_in, mbox_cmd->size_in); + } + +/* #2, #3 */ +writeq (cmd_reg, cxlm->mbox_regs +CXLDEV_MBOX_CMD_OFFSET ); + +/* #4 */ +dev_dbg (&cxlm->pdev->dev,"Sending command\n" ); +writel (CXLDEV_MBOX_CTRL_DOORBELL , + cxlm->mbox_regs +CXLDEV_MBOX_CTRL_OFFSET ); + +/* #5 */ + rc =cxl_mem_wait_for_doorbell (cxlm); +if (rc == -ETIMEDOUT ) { +cxl_mem_mbox_timeout (cxlm, mbox_cmd); +return rc; + } + +/* #6 */ + status_reg =readq (cxlm->mbox_regs +CXLDEV_MBOX_STATUS_OFFSET ); + mbox_cmd->return_code = +FIELD_GET (CXLDEV_MBOX_STATUS_RET_CODE_MASK , status_reg); + +if (mbox_cmd->return_code !=0 ) { +dev_dbg (&cxlm->pdev->dev,"Mailbox operation had an error\n" );drivers/cxl/mem.c line 148
Quoted 10 lines
drivers/cxl/mem.c +10
-0+return 0 ; + } + +/* #7 */ + cmd_reg =readq (cxlm->mbox_regs +CXLDEV_MBOX_CMD_OFFSET ); + out_len =FIELD_GET (CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK , cmd_reg); + +/* #8 */ +if (out_len && mbox_cmd->payload_out) { +size_t n =min_t (size_t, cxlm->payload_size, out_len);This doesn't protect us from the case where the hardware returns a larger payload than the caller is expecting.
Quoted 4 lines
i.e.
payload_outis too small. We need to pass in the size of that buffer as
well. This currently clamps to the size of the source buffer but does not
check if there is enough space at the destination (mbox_cmd->payload_out).
Quoted 4 lines
Let me articulate the issue a bit. The userspace call chain should be fine:
cxl_send_cmd()-> ioctl handlers
cxl_validate_cmd_from_user-> converts to internal commandhandle_mailbox_cmd_from_user -> dispatches mbox command.
There is a sanity check in there against info->size_out, that will return
an error if the buffer isn't big enough. However, that test passes
for a variable length command. It is then followed by
out_cmd->info.size_out = send_cmd->out.size;
(perhaps that is meant to be = info->size_out so as to pick up the -1?)
handle_mailbox_cmd_from_user() then uses that size in
mbox_cmd.payload_out = kvzalloc(cmd->info.size_out, GFP_KERNEL);
Quoted 5 lines
cxl_send_cmd():
if (c.info.size_out < 0)c.info.size_out = cxlm->payload_size;
(c == out_cmd above)
So this doesn't apply because c.info.size_out is whatever userspace set it to.
Quoted 3 lines
handle_mailbox_cmd_from_user():if (cmd->info.size_out) { mbox_cmd.payload_out = kvzalloc(cmd->info.size_out, GFP_KERNEL);
__cxl_mem_mbox_send_cmd() called with that payload size and blindly copies whatever size of data the hardware receives into the the buffer we allocated above. If it's not big enough you now have a userspace triggered buffer overflow in the kernel.
All userspace needs to do is issue an ioctl for a raw command with the out.size set too small but not set to -1.
Quoted 11 lines
The kernel call chain could have issues:
cxl_mem_identify/*() -> kernel caller allocates just enough space
cxl_mem_mbox_send_cmd()-> internal wrapper we created for v3
blows up in the spot you mention.The driver allocates enough space on the stack for all these calls, but yes, if hardware is out of spec it would be problematic. In previous versions of this series, there has been a check there. However, the ability to have hardware return more data than expected is I believe the correct functionality here.
It's absolutely fine to return more data, but we shouldn't copy it from the mailbox into memory that isn't big enough. We should be extremely paranoid about that.
Quoted 7 lines
So my proposal is for now, since no real hardware exists, and the command set here is so benign, we leave fixing this as a TODO.
I can post a patch on top of this series to address this issue in a manner I believe warrants discussing (kvzalloc max payload size buffers on
open()and for each driver instance).
Or just sanity check the size against available buffer size before using it in mempcy_fromio.
Quoted 309 lines
Quoted 143 lines
Quoted 11 lines
drivers/cxl/mem.c +10
-0+ +memcpy_fromio (mbox_cmd->payload_out, payload, n); + mbox_cmd->size_out = n; + }else { + mbox_cmd->size_out =0 ; + } + +return 0 ; +} +...
Quoted 77 lines
drivers/cxl/mem.c +77
-0+ +/** +* cxl_mem_mbox_send_cmd() - Send a mailbox command to a memory device. +* @cxlm: The CXL memory device to communicate with. +* @opcode: Opcode for the mailbox command. +* @in: The input payload for the mailbox command. +* @in_size: The length of the input payload +* @out: Caller allocated buffer for the output. +* @out_min_size: Minimum expected size of output. +* +* Context: Any context. Will acquire and release mbox_mutex. +* Return: +* * %>=0 - Number of bytes returned in @out. +* * %-E2BIG - Payload is too large for hardware. +* * %-EBUSY - Couldn't acquire exclusive mailbox access. +* * %-EFAULT - Hardware error occurred. +* * %-ENXIO - Command completed, but device reported an error. +* * %-ENODATA - Not enough payload data returned by hardware. +* +* Mailbox commands may execute successfully yet the device itself reported an +* error. While this distinction can be useful for commands from userspace, the +* kernel will only be able to use results when both are successful. +* +* See __cxl_mem_mbox_send_cmd() +*/ +static int cxl_mem_mbox_send_cmd (struct cxl_mem *cxlm,u16 opcode,void *in, +size_t in_size,void *out,size_t out_min_size) +{ +struct mbox_cmd mbox_cmd = { + .opcode = opcode, + .payload_in = in, + .size_in = in_size, + .payload_out = out, + }; +int rc; + +if (out_min_size > cxlm->payload_size) +return -E2BIG ; + + rc =cxl_mem_mbox_get (cxlm); +if (rc) +return rc; + + rc =__cxl_mem_mbox_send_cmd (cxlm, &mbox_cmd); +cxl_mem_mbox_put (cxlm); +if (rc) +return rc; + +/* TODO: Map return code to proper kernel style errno */ +if (mbox_cmd.return_code !=CXL_MBOX_SUCCESS ) +return -ENXIO ; + +if (mbox_cmd.size_out < out_min_size) +return -ENODATA ; + +return mbox_cmd.size_out; +} + +/** +* cxl_mem_setup_regs() - Setup necessary MMIO. +* @cxlm: The CXL memory device to communicate with. +* +* Return: 0 if all necessary registers mapped. +* +* A memory device is required by spec to implement a certain set of MMIO +* regions. The purpose of this function is to enumerate and map those +* registers. +*/ +static int cxl_mem_setup_regs (struct cxl_mem *cxlm) +{ +struct device *dev = &cxlm->pdev->dev; +int cap, cap_count; +u64 cap_array; + + cap_array =readq (cxlm->regs +CXLDEV_CAP_ARRAY_OFFSET ); +if (FIELD_GET (CXLDEV_CAP_ARRAY_ID_MASK , cap_array) != +CXLDEV_CAP_ARRAY_CAP_ID )drivers/cxl/mem.c line 354
Quoted 10 lines
drivers/cxl/mem.c +10
-0+return -ENODEV ; + + cap_count =FIELD_GET (CXLDEV_CAP_ARRAY_COUNT_MASK , cap_array); + +for (cap =1 ; cap <= cap_count; cap++) { +void __iomem *register_block; +u32 offset; +u16 cap_id; + + cap_id =readl (cxlm->regs + cap *0x10 ) &0xffff ;Slight preference for
FIELD_GETjust for consistency.Quoted 36 lines
drivers/cxl/mem.c +36
-0+ offset =readl (cxlm->regs + cap *0x10 +0x4 ); + register_block = cxlm->regs + offset; + +switch (cap_id) { +case CXLDEV_CAP_CAP_ID_DEVICE_STATUS : +dev_dbg (dev,"found Status capability (0x%x)\n" , offset); + cxlm->status_regs = register_block; +break ; +case CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX : +dev_dbg (dev,"found Mailbox capability (0x%x)\n" , offset); + cxlm->mbox_regs = register_block; +break ; +case CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX : +dev_dbg (dev,"found Secondary Mailbox capability (0x%x)\n" , offset); +break ; +case CXLDEV_CAP_CAP_ID_MEMDEV : +dev_dbg (dev,"found Memory Device capability (0x%x)\n" , offset); + cxlm->memdev_regs = register_block; +break ; +default : +dev_dbg (dev,"Unknown cap ID: %d (0x%x)\n" , cap_id, offset); +break ; + } + } + +if (!cxlm->status_regs || !cxlm->mbox_regs || !cxlm->memdev_regs) { +dev_err (dev,"registers not found: %s%s%s\n" , + !cxlm->status_regs ?"status " :"" , + !cxlm->mbox_regs ?"mbox " :"" , + !cxlm->memdev_regs ?"memdev" :"" ); +return -ENXIO ; + } + +return 0 ; +} +...
Quoted 8 lines
drivers/cxl/mem.c line 419
Quoted 3 lines
drivers/cxl/mem.c +3
-0+ +static struct cxl_mem *cxl_mem_create (struct pci_dev *pdev,u32 reg_lo, +u32 reg_hi)I'm not really suggesting you change it at this point, but I'd have done the splitting of
reg_loup and the building of the offset at the call site rather than in here. I think that would have been slightly easier to follow.Noted. In future patches this is going to get reworked somewhat to support more flexibility with register blocks.
Quoted 139 lines
Quoted 109 lines
drivers/cxl/mem.c +94
-1+{ +struct device *dev = &pdev->dev; +struct cxl_mem *cxlm; +void __iomem *regs; +u64 offset; +u8 bar; +int rc; + + cxlm =devm_kzalloc (&pdev->dev,sizeof (*cxlm),GFP_KERNEL ); +if (!cxlm) { +dev_err (dev,"No memory available\n" ); +return NULL ; + } + + offset = ((u64 )reg_hi <<32 ) |FIELD_GET (CXL_REGLOC_ADDR_MASK , reg_lo); + bar =FIELD_GET (CXL_REGLOC_BIR_MASK , reg_lo); + +/* Basic sanity check that BAR is big enough */ +if (pci_resource_len (pdev, bar) < offset) { +dev_err (dev,"BAR%d: %pr: too small (offset: %#llx)\n" , bar, + &pdev->resource[bar], (unsigned long long )offset); +return NULL ; + } + + rc =pcim_iomap_regions (pdev,BIT (bar),pci_name (pdev)); +if (rc) { +dev_err (dev,"failed to map registers\n" ); +return NULL ; + } + regs =pcim_iomap_table (pdev)[bar]; + +mutex_init (&cxlm->mbox_mutex); + cxlm->pdev = pdev; + cxlm->regs = regs + offset; + +dev_dbg (dev,"Mapped CXL Memory Device resource\n" ); +return cxlm; +}static int cxl_mem_dvsec (struct pci_dev *pdev,int dvsec) {@@ -28,10 +479,65 @@ static int cxl_mem_dvsec(struct pci_dev *pdev, int dvsec)return 0 ; } +/** +* cxl_mem_identify() - Send the IDENTIFY command to the device. +* @cxlm: The device to identify. +* +* Return: 0 if identify was executed successfully. +* +* This will dispatch the identify command to the device and on success populate +* structures to be exported to sysfs. +*/ +static int cxl_mem_identify (struct cxl_mem *cxlm) +{ +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 id; +int rc; + + rc =cxl_mem_mbox_send_cmd (cxlm,CXL_MBOX_OP_IDENTIFY ,NULL ,0 , &id, +sizeof (id)); +if (rc <0 ) +return rc; + +/* +* TODO: enumerate DPA map, as 'ram' and 'pmem' do not alias. +* For now, only the capacity is exported in sysfs +*/ + cxlm->ram_range.start =0 ; + cxlm->ram_range.end =le64_to_cpu (id.volatile_capacity) -1 ; + + cxlm->pmem_range.start =0 ; + cxlm->pmem_range.end =le64_to_cpu (id.persistent_capacity) -1 ; + +memcpy (cxlm->firmware_version, id.fw_revision,sizeof (id.fw_revision)); + +return 0 ; +} +static int cxl_mem_probe (struct pci_dev *pdev,const struct pci_device_id *id) {struct device *dev = &pdev->dev; -int regloc; +struct cxl_mem *cxlm =NULL ; +int rc, regloc, i; +u32 regloc_size; + + rc =pcim_enable_device (pdev); +if (rc) +return rc; regloc =cxl_mem_dvsec (pdev,PCI_DVSEC_ID_CXL_REGLOC_OFFSET );if (!regloc) {drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 550
Quoted 7 lines
drivers/cxl/mem.c +3
-1@@ -39,7 +545,40 @@ static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id)return -ENXIO ; } -return 0 ; +/* Get the size of the Register Locator DVSEC */ +pci_read_config_dword (pdev, regloc +PCI_DVSEC_HEADER1 , ®loc_size); + regloc_size =FIELD_GET (PCI_DVSEC_HEADER1_LENGTH_MASK , regloc_size);The size field here is the dvsec length.. Let's say we only have one register block entry at +0x0c and +0x10
From PCI spec :"DVSEC Length - This field indicates the number of bytes in the entire DVSEC structure, including the PCI Express Extended Capability Header, the DVSEC Header 1, DVSEC Header 2, and DVSEC vendor-specific registers."
So here it would be
0x14drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 552
Quoted 2 lines
drivers/cxl/mem.c +2
-0+ + regloc +=PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET ;We then shift regloc forward by +0xc
drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 554
Quoted 2 lines
drivers/cxl/mem.c +2
-0+ +for (i = regloc; i < regloc + regloc_size; i +=8 ) {This loop will then index form
i= +0xc to i < +0c +0x14(0x20)
i =0xc,0x14Quoted 9 lines
So that's indexing one more entry than is actually present. Should be something like
for (i = regloc; i < regloc + regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET; i++)which will mean the only iteration for this example is the one with i == +0xC
Good catch. I think this warrants rewriting a bit, let me know what you think?
regloc +=
PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
regblocks = (regloc_size-PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
Quoted 6 lines
for (i = 0; i < regblocks; i++, regloc+=8) {
pci_read_config_dword(pdev, regloc, ®_lo);
pci_read_config_dword(pdev, regloc + 4, ®_hi);
...
}That's fine.
_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 2/9] cxl/mem: Find device capabilities
Quoted 246 lines · Jonathan Cameron
Quoted 216 lines · On Tue, 16 Feb 2021 08:43:03 -0800 Ben Widawsky <[email protected]> wrote:
Quoted 210 lines · Jonathan Cameron
Quoted 20 lines · On Mon, 15 Feb 2021 17:45:31 -0800 Ben Widawsky <[email protected]> wrote:
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 flow is proven with one implemented command, "identify". Because the class code has already told the driver this is a memory device and the identify command is mandatory.
CXL devices contain an array of capabilities that describe the interactions software can have with the device or firmware running on the device. A CXL compliant device must implement the device status and the mailbox capability. Additionally, 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.
The capabilities tell the driver how to find and map the register space for CXL Memory Devices. The registers are required to utilize the CXL spec defined mailbox interface. The spec outlines two mailboxes, primary and secondary. The secondary mailbox is earmarked for system firmware, and not handled in this driver.
Quoted 6 lines
Primary mailboxes are capable of generating an interrupt when submitting a background command. That implementation is saved for a later time.
Looks like an off by one error in the register locator iteration.
The potential buffer overrun from
memcpy_fromiois still there as well as far as I can see.If the software provides storage for a payload of size n and the hardware reports a size of n + d, code will happily write beyond the end of the storage provided.
Obviously, this shouldn't happen, but I'm not that trusting of both hardware and software never having bugs.
Jonathan
Quoted 7 lines
--- drivers/cxl/cxl.h | 88 ++++++++ drivers/cxl/mem.c | 543 +++++++++++++++++++++++++++++++++++++++++++++- drivers/cxl/pci.h | 14 ++ 3 files changed, 643 insertions(+), 2 deletions(-) create mode 100644 drivers/cxl/cxl.h...
Quoted 143 lines
(quoted diff) +2
-0+ +#endif /* __CXL_H__ */drivers/cxl/mem.c +130
-0@@ -3,7 +3,458 @@#include <linux/module.h> #include <linux/pci.h> #include <linux/io.h> +#include <linux/io-64-nonatomic-lo-hi.h> #include "pci.h" +#include "cxl.h" + +#define cxl_doorbell_busy (cxlm) \ + (readl((cxlm)->mbox_regs + CXLDEV_MBOX_CTRL_OFFSET) & \ + CXLDEV_MBOX_CTRL_DOORBELL) + +/* CXL 2.0 - 8.2.8.4 */ +#define CXL_MAILBOX_TIMEOUT_MS (2 * HZ) + +enum opcode { +CXL_MBOX_OP_IDENTIFY =0x4000 , +CXL_MBOX_OP_MAX =0x10000 +}; + +/** +* struct mbox_cmd - A command to be submitted to hardware. +* @opcode: (input) The command set and command submitted to hardware. +* @payload_in: (input) Pointer to the input payload. +* @payload_out: (output) Pointer to the output payload. Must be allocated by +* the caller. +* @size_in: (input) Number of bytes to load from @payload. +* @size_out: (output) Number of bytes loaded into @payload. +* @return_code: (output) Error code returned from hardware. +* +* This is the primary mechanism used to send commands to the hardware. +* All the fields except @payload_* correspond exactly to the fields described in +* Command Register section of the CXL 2.0 8.2.8.4.5. @payload_in and +* @payload_out are written to, and read from the Command Payload Registers +* defined in CXL 2.0 8.2.8.4.8. +*/ +struct mbox_cmd { +u16 opcode; +void *payload_in; +void *payload_out; +size_t size_in; +size_t size_out; +u16 return_code; +#define CXL_MBOX_SUCCESS 0 +}; + +/** +* __cxl_mem_mbox_send_cmd() - Execute a mailbox command +* @cxlm: The CXL memory device to communicate with. +* @mbox_cmd: Command to send to the memory device. +* +* Context: Any context. Expects mbox_mutex to be held. +* Return: -ETIMEDOUT if timeout occurred waiting for completion. 0 on success. +* Caller should check the return code in @mbox_cmd to make sure it +* succeeded. +* +* This is a generic form of the CXL mailbox send command thus only using the +* registers defined by the mailbox capability ID - CXL 2.0 8.2.8.4. Memory +* devices, and perhaps other types of CXL devices may have further information +* available upon error conditions. Driver facilities wishing to send mailbox +* commands should use the wrapper command. +* +* The CXL spec allows for up to two mailboxes. The intention is for the primary +* mailbox to be OS controlled and the secondary mailbox to be used by system +* firmware. This allows the OS and firmware to communicate with the device and +* not need to coordinate with each other. The driver only uses the primary +* mailbox. +*/ +static int __cxl_mem_mbox_send_cmd (struct cxl_mem *cxlm, +struct mbox_cmd *mbox_cmd) +{ +void __iomem *payload = cxlm->mbox_regs +CXLDEV_MBOX_PAYLOAD_OFFSET ; +u64 cmd_reg, status_reg; +size_t out_len; +int rc; + +lockdep_assert_held (&cxlm->mbox_mutex); + +/* +* 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 +* +* Hardware is free to do whatever it wants before the doorbell is rung, +* and isn't allowed to change anything after it clears the doorbell. As +* such, steps 2 and 3 can happen in any order, and steps 6, 7, 8 can +* also happen in any order (though some orders might not make sense). +*/ + +/* #1 */ +if (cxl_doorbell_busy (cxlm)) { +dev_err_ratelimited (&cxlm->pdev->dev, +"Mailbox re-busy after acquiring\n" ); +return -EBUSY ; + } + + cmd_reg =FIELD_PREP (CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK , + mbox_cmd->opcode); +if (mbox_cmd->size_in) { +if (WARN_ON (!mbox_cmd->payload_in)) +return -EINVAL ; + + cmd_reg |=FIELD_PREP (CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK , + mbox_cmd->size_in); +memcpy_toio (payload, mbox_cmd->payload_in, mbox_cmd->size_in); + } + +/* #2, #3 */ +writeq (cmd_reg, cxlm->mbox_regs +CXLDEV_MBOX_CMD_OFFSET ); + +/* #4 */ +dev_dbg (&cxlm->pdev->dev,"Sending command\n" ); +writel (CXLDEV_MBOX_CTRL_DOORBELL , + cxlm->mbox_regs +CXLDEV_MBOX_CTRL_OFFSET ); + +/* #5 */ + rc =cxl_mem_wait_for_doorbell (cxlm); +if (rc == -ETIMEDOUT ) { +cxl_mem_mbox_timeout (cxlm, mbox_cmd); +return rc; + } + +/* #6 */ + status_reg =readq (cxlm->mbox_regs +CXLDEV_MBOX_STATUS_OFFSET ); + mbox_cmd->return_code = +FIELD_GET (CXLDEV_MBOX_STATUS_RET_CODE_MASK , status_reg); + +if (mbox_cmd->return_code !=0 ) { +dev_dbg (&cxlm->pdev->dev,"Mailbox operation had an error\n" );drivers/cxl/mem.c line 148
Quoted 10 lines
drivers/cxl/mem.c +10
-0+return 0 ; + } + +/* #7 */ + cmd_reg =readq (cxlm->mbox_regs +CXLDEV_MBOX_CMD_OFFSET ); + out_len =FIELD_GET (CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK , cmd_reg); + +/* #8 */ +if (out_len && mbox_cmd->payload_out) { +size_t n =min_t (size_t, cxlm->payload_size, out_len);This doesn't protect us from the case where the hardware returns a larger payload than the caller is expecting.
Quoted 4 lines
i.e.
payload_outis too small. We need to pass in the size of that buffer as
well. This currently clamps to the size of the source buffer but does not
check if there is enough space at the destination (mbox_cmd->payload_out).Quoted 4 lines
Let me articulate the issue a bit. The userspace call chain should be fine:
cxl_send_cmd()-> ioctl handlers
cxl_validate_cmd_from_user-> converts to internal commandhandle_mailbox_cmd_from_user -> dispatches mbox command.There is a sanity check in there against
info->size_out, that will return
an error if the buffer isn't big enough. However, that test passes
for a variable length command. It is then followed by
out_cmd->info.size_out=send_cmd->out.size;
(perhaps that is meant to be =info->size_outso as to pick up the -1?)
handle_mailbox_cmd_from_user()then uses that size inmbox_cmd.payload_out = kvzalloc(cmd->info.size_out, GFP_KERNEL);Quoted 5 lines
cxl_send_cmd():
if (c.info.size_out < 0)c.info.size_out = cxlm->payload_size;(c ==
out_cmdabove)
So this doesn't apply because c.info.size_out is whatever userspace set it to.Quoted 3 lines
handle_mailbox_cmd_from_user():if (cmd->info.size_out) { mbox_cmd.payload_out = kvzalloc(cmd->info.size_out, GFP_KERNEL);
Quoted 6 lines
__cxl_mem_mbox_send_cmd()called with that payload size and blindly copies whatever size of data the hardware receives into the the buffer we allocated above. If it's not big enough you now have a userspace triggered buffer overflow in the kernel.
All userspace needs to do is issue an ioctl for a raw command with the out.size set too small but not set to -1.
You're right.. See my email just now in 4/9. I didn't understand when I read this earlier, and I shouldn't have brought up the userspace side in this patch review anyway.
Quoted 22 lines
Quoted 8 lines
The kernel call chain could have issues:
cxl_mem_identify/*() -> kernel caller allocates just enough space
cxl_mem_mbox_send_cmd()-> internal wrapper we created for v3
blows up in the spot you mention.Quoted 4 lines
The driver allocates enough space on the stack for all these calls, but yes, if hardware is out of spec it would be problematic. In previous versions of this series, there has been a check there. However, the ability to have hardware return more data than expected is I believe the correct functionality here.
It's absolutely fine to return more data, but we shouldn't copy it from the mailbox into memory that isn't big enough. We should be extremely paranoid about that.
Quoted 4 lines
So my proposal is for now, since no real hardware exists, and the command set here is so benign, we leave fixing this as a TODO.
Quoted 6 lines
Quoted 3 lines
I can post a patch on top of this series to address this issue in a manner I believe warrants discussing (kvzalloc max payload size buffers on
open()and for each driver instance).Or just sanity check the size against available buffer size before using it in
mempcy_fromio.
I'll rework this...
Quoted 310 lines
Quoted 310 lines
Quoted 144 lines
Quoted 12 lines
drivers/cxl/mem.c +10
-0+ +memcpy_fromio (mbox_cmd->payload_out, payload, n); + mbox_cmd->size_out = n; + }else { + mbox_cmd->size_out =0 ; + } + +return 0 ; +} +...
Quoted 77 lines
drivers/cxl/mem.c +77
-0+ +/** +* cxl_mem_mbox_send_cmd() - Send a mailbox command to a memory device. +* @cxlm: The CXL memory device to communicate with. +* @opcode: Opcode for the mailbox command. +* @in: The input payload for the mailbox command. +* @in_size: The length of the input payload +* @out: Caller allocated buffer for the output. +* @out_min_size: Minimum expected size of output. +* +* Context: Any context. Will acquire and release mbox_mutex. +* Return: +* * %>=0 - Number of bytes returned in @out. +* * %-E2BIG - Payload is too large for hardware. +* * %-EBUSY - Couldn't acquire exclusive mailbox access. +* * %-EFAULT - Hardware error occurred. +* * %-ENXIO - Command completed, but device reported an error. +* * %-ENODATA - Not enough payload data returned by hardware. +* +* Mailbox commands may execute successfully yet the device itself reported an +* error. While this distinction can be useful for commands from userspace, the +* kernel will only be able to use results when both are successful. +* +* See __cxl_mem_mbox_send_cmd() +*/ +static int cxl_mem_mbox_send_cmd (struct cxl_mem *cxlm,u16 opcode,void *in, +size_t in_size,void *out,size_t out_min_size) +{ +struct mbox_cmd mbox_cmd = { + .opcode = opcode, + .payload_in = in, + .size_in = in_size, + .payload_out = out, + }; +int rc; + +if (out_min_size > cxlm->payload_size) +return -E2BIG ; + + rc =cxl_mem_mbox_get (cxlm); +if (rc) +return rc; + + rc =__cxl_mem_mbox_send_cmd (cxlm, &mbox_cmd); +cxl_mem_mbox_put (cxlm); +if (rc) +return rc; + +/* TODO: Map return code to proper kernel style errno */ +if (mbox_cmd.return_code !=CXL_MBOX_SUCCESS ) +return -ENXIO ; + +if (mbox_cmd.size_out < out_min_size) +return -ENODATA ; + +return mbox_cmd.size_out; +} + +/** +* cxl_mem_setup_regs() - Setup necessary MMIO. +* @cxlm: The CXL memory device to communicate with. +* +* Return: 0 if all necessary registers mapped. +* +* A memory device is required by spec to implement a certain set of MMIO +* regions. The purpose of this function is to enumerate and map those +* registers. +*/ +static int cxl_mem_setup_regs (struct cxl_mem *cxlm) +{ +struct device *dev = &cxlm->pdev->dev; +int cap, cap_count; +u64 cap_array; + + cap_array =readq (cxlm->regs +CXLDEV_CAP_ARRAY_OFFSET ); +if (FIELD_GET (CXLDEV_CAP_ARRAY_ID_MASK , cap_array) != +CXLDEV_CAP_ARRAY_CAP_ID )drivers/cxl/mem.c line 354
Quoted 10 lines
drivers/cxl/mem.c +10
-0+return -ENODEV ; + + cap_count =FIELD_GET (CXLDEV_CAP_ARRAY_COUNT_MASK , cap_array); + +for (cap =1 ; cap <= cap_count; cap++) { +void __iomem *register_block; +u32 offset; +u16 cap_id; + + cap_id =readl (cxlm->regs + cap *0x10 ) &0xffff ;Slight preference for
FIELD_GETjust for consistency.Quoted 36 lines
drivers/cxl/mem.c +36
-0+ offset =readl (cxlm->regs + cap *0x10 +0x4 ); + register_block = cxlm->regs + offset; + +switch (cap_id) { +case CXLDEV_CAP_CAP_ID_DEVICE_STATUS : +dev_dbg (dev,"found Status capability (0x%x)\n" , offset); + cxlm->status_regs = register_block; +break ; +case CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX : +dev_dbg (dev,"found Mailbox capability (0x%x)\n" , offset); + cxlm->mbox_regs = register_block; +break ; +case CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX : +dev_dbg (dev,"found Secondary Mailbox capability (0x%x)\n" , offset); +break ; +case CXLDEV_CAP_CAP_ID_MEMDEV : +dev_dbg (dev,"found Memory Device capability (0x%x)\n" , offset); + cxlm->memdev_regs = register_block; +break ; +default : +dev_dbg (dev,"Unknown cap ID: %d (0x%x)\n" , cap_id, offset); +break ; + } + } + +if (!cxlm->status_regs || !cxlm->mbox_regs || !cxlm->memdev_regs) { +dev_err (dev,"registers not found: %s%s%s\n" , + !cxlm->status_regs ?"status " :"" , + !cxlm->mbox_regs ?"mbox " :"" , + !cxlm->memdev_regs ?"memdev" :"" ); +return -ENXIO ; + } + +return 0 ; +} +...
Quoted 8 lines
drivers/cxl/mem.c line 419
Quoted 3 lines
drivers/cxl/mem.c +3
-0+ +static struct cxl_mem *cxl_mem_create (struct pci_dev *pdev,u32 reg_lo, +u32 reg_hi)I'm not really suggesting you change it at this point, but I'd have done the splitting of
reg_loup and the building of the offset at the call site rather than in here. I think that would have been slightly easier to follow.Noted. In future patches this is going to get reworked somewhat to support more flexibility with register blocks.
Quoted 139 lines
Quoted 109 lines
drivers/cxl/mem.c +94
-1+{ +struct device *dev = &pdev->dev; +struct cxl_mem *cxlm; +void __iomem *regs; +u64 offset; +u8 bar; +int rc; + + cxlm =devm_kzalloc (&pdev->dev,sizeof (*cxlm),GFP_KERNEL ); +if (!cxlm) { +dev_err (dev,"No memory available\n" ); +return NULL ; + } + + offset = ((u64 )reg_hi <<32 ) |FIELD_GET (CXL_REGLOC_ADDR_MASK , reg_lo); + bar =FIELD_GET (CXL_REGLOC_BIR_MASK , reg_lo); + +/* Basic sanity check that BAR is big enough */ +if (pci_resource_len (pdev, bar) < offset) { +dev_err (dev,"BAR%d: %pr: too small (offset: %#llx)\n" , bar, + &pdev->resource[bar], (unsigned long long )offset); +return NULL ; + } + + rc =pcim_iomap_regions (pdev,BIT (bar),pci_name (pdev)); +if (rc) { +dev_err (dev,"failed to map registers\n" ); +return NULL ; + } + regs =pcim_iomap_table (pdev)[bar]; + +mutex_init (&cxlm->mbox_mutex); + cxlm->pdev = pdev; + cxlm->regs = regs + offset; + +dev_dbg (dev,"Mapped CXL Memory Device resource\n" ); +return cxlm; +}static int cxl_mem_dvsec (struct pci_dev *pdev,int dvsec) {@@ -28,10 +479,65 @@ static int cxl_mem_dvsec(struct pci_dev *pdev, int dvsec)return 0 ; } +/** +* cxl_mem_identify() - Send the IDENTIFY command to the device. +* @cxlm: The device to identify. +* +* Return: 0 if identify was executed successfully. +* +* This will dispatch the identify command to the device and on success populate +* structures to be exported to sysfs. +*/ +static int cxl_mem_identify (struct cxl_mem *cxlm) +{ +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 id; +int rc; + + rc =cxl_mem_mbox_send_cmd (cxlm,CXL_MBOX_OP_IDENTIFY ,NULL ,0 , &id, +sizeof (id)); +if (rc <0 ) +return rc; + +/* +* TODO: enumerate DPA map, as 'ram' and 'pmem' do not alias. +* For now, only the capacity is exported in sysfs +*/ + cxlm->ram_range.start =0 ; + cxlm->ram_range.end =le64_to_cpu (id.volatile_capacity) -1 ; + + cxlm->pmem_range.start =0 ; + cxlm->pmem_range.end =le64_to_cpu (id.persistent_capacity) -1 ; + +memcpy (cxlm->firmware_version, id.fw_revision,sizeof (id.fw_revision)); + +return 0 ; +} +static int cxl_mem_probe (struct pci_dev *pdev,const struct pci_device_id *id) {struct device *dev = &pdev->dev; -int regloc; +struct cxl_mem *cxlm =NULL ; +int rc, regloc, i; +u32 regloc_size; + + rc =pcim_enable_device (pdev); +if (rc) +return rc; regloc =cxl_mem_dvsec (pdev,PCI_DVSEC_ID_CXL_REGLOC_OFFSET );if (!regloc) {drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 550
Quoted 7 lines
drivers/cxl/mem.c +3
-1@@ -39,7 +545,40 @@ static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id)return -ENXIO ; } -return 0 ; +/* Get the size of the Register Locator DVSEC */ +pci_read_config_dword (pdev, regloc +PCI_DVSEC_HEADER1 , ®loc_size); + regloc_size =FIELD_GET (PCI_DVSEC_HEADER1_LENGTH_MASK , regloc_size);The size field here is the dvsec length.. Let's say we only have one register block entry at +0x0c and +0x10
From PCI spec :"DVSEC Length - This field indicates the number of bytes in the entire DVSEC structure, including the PCI Express Extended Capability Header, the DVSEC Header 1, DVSEC Header 2, and DVSEC vendor-specific registers."
So here it would be
0x14drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 552
Quoted 2 lines
drivers/cxl/mem.c +2
-0+ + regloc +=PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET ;We then shift regloc forward by +0xc
drivers/cxl/mem.c static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 554
Quoted 2 lines
drivers/cxl/mem.c +2
-0+ +for (i = regloc; i < regloc + regloc_size; i +=8 ) {This loop will then index form
i= +0xc to i < +0c +0x14(0x20)
i =0xc,0x14Quoted 9 lines
So that's indexing one more entry than is actually present. Should be something like
for (i = regloc; i < regloc + regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET; i++)which will mean the only iteration for this example is the one with i == +0xC
Good catch. I think this warrants rewriting a bit, let me know what you think?
regloc +=
PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
regblocks = (regloc_size-PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
Quoted 10 lines
Quoted 6 lines
for (i = 0; i < regblocks; i++, regloc+=8) { pci_read_config_dword(pdev, regloc, ®_lo); pci_read_config_dword(pdev, regloc + 4, ®_hi); ... }That's fine.
_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
PATCH v4 3/9 cxl/mem: Register CXL memX devices
In reply to Ben WidawskyFrom: 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)
* 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. However, going forward it will provide a mechanism to coordinate cross-device interleave.
--- Documentation/ABI/testing/sysfs-bus-cxl | 26 ++ .../driver-api/cxl/memory-devices.rst | 17 + drivers/cxl/Makefile | 3 + drivers/cxl/bus.c | 29 ++ drivers/cxl/cxl.h | 3 + drivers/cxl/mem.c | 301 +++++++++++++++++- 6 files changed, 377 insertions(+), 2 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-cxl create mode 100644 drivers/cxl/bus.c
Documentation/ABI/testing/sysfs-bus-cxl new file +26 -0
@@ -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 bytes. Represents the + identically named field in 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 bytes. Represents the + identically named field in the Identify Memory Device Output + Payload in the CXL-2.0 specification.
Documentation/driver-api/cxl/memory-devices.rst +17 -0
@@ -27,3 +27,20 @@ CXL Memory Device .. kernel-doc:: drivers/cxl/mem.c :internal: + +CXL Bus +------- +.. kernel-doc:: drivers/cxl/bus.c + :doc: cxl bus + +External Interfaces +=================== + +CXL IOCTL Interface +------------------- + +.. kernel-doc:: include/uapi/linux/cxl_mem.h + :doc: UAPI + +.. kernel-doc:: include/uapi/linux/cxl_mem.h + :internal:
drivers/cxl/Makefile +3 -0
@@ -1,4 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_CXL_BUS) += cxl_bus.o obj-$(CONFIG_CXL_MEM) += cxl_mem.o +ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=CXL +cxl_bus-y := bus.o cxl_mem-y := mem.o
drivers/cxl/bus.c new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright(c) 2020 Intel Corporation. All rights reserved. */ +#include <linux/device.h> +#include <linux/module.h> + +/** +* DOC: cxl bus +* +* The CXL bus provides namespace for control devices and a rendezvous +* point for cross-device interleave coordination. +*/ +struct bus_type cxl_bus_type = { + .name ="cxl" , +}; +EXPORT_SYMBOL_GPL (cxl_bus_type); + +static __init int cxl_bus_init (void ) +{ +return bus_register (&cxl_bus_type); +} + +static void cxl_bus_exit (void ) +{ +bus_unregister (&cxl_bus_type); +} + +module_init (cxl_bus_init); +module_exit (cxl_bus_exit); +MODULE_LICENSE ("GPL v2" );
drivers/cxl/cxl.h +3 -0
@@ -55,6 +55,7 @@ (FIELD_GET (CXLMDEV_RESET_NEEDED_MASK , status) != \ CXLMDEV_RESET_NEEDED_NOT) +struct cxl_memdev ;/** * struct cxl_mem - A CXL memory device * @pdev: The PCI device associated with this CXL device.
@@ -72,6 +73,7 @@struct cxl_mem {struct pci_dev *pdev;void __iomem *regs; +struct cxl_memdev *cxlmd;void __iomem *status_regs;void __iomem *mbox_regs;
@@ -85,4 +87,5 @@ struct cxl_mem { struct range ram_range; }; +extern struct bus_type cxl_bus_type;#endif /* __CXL_H__ */
drivers/cxl/mem.c +299 -2
341 lines not on this page · raw message
Signature · 4 lines
2.30.1 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
PATCH v4 4/9 cxl/mem: Add basic IOCTL interface
In reply to Ben WidawskyAdd a straightforward IOCTL that provides a mechanism for userspace to query the supported memory device commands. CXL commands as they appear to userspace are described as part of the UAPI kerneldoc. The command list returned via this IOCTL will contain the full set of commands that the driver supports, however, some of those commands may not be available for use by userspace.
Memory device commands first appear in the CXL 2.0 specification. They are submitted through a mailbox mechanism specified in the CXL 2.0 specification.
The send command allows userspace to issue mailbox commands directly to the hardware. The list of available commands to send are the output of the query command. The driver verifies basic properties of the command and possibly inspect the input (or output) payload to determine whether or not the command is allowed (or might taint the kernel).
--- .clang-format | 1 + .../userspace-api/ioctl/ioctl-number.rst | 1 + drivers/cxl/mem.c | 288 +++++++++++++++++- include/uapi/linux/cxl_mem.h | 154 ++++++++++ 4 files changed, 443 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/cxl_mem.h
.clang-format +1 -0
@@ -109,6 +109,7 @@ ForEachMacros: - 'css_for_each_child' - 'css_for_each_descendant_post' - 'css_for_each_descendant_pre' + - 'cxl_for_each_cmd' - 'device_for_each_child_node' - 'dma_fence_chain_for_each' - 'do_for_each_ftrace_op'
Documentation/userspace-api/ioctl/ioctl-number.rst +1 -0
@@ -352,6 +352,7 @@ Code Seq# Include File Comments <mailto:[email protected]> 0xCC 00-0F drivers/misc/ibmvmc.h pseries VMC driver 0xCD 01 linux/reiserfs_fs.h +0xCE 01-02 uapi/linux/cxl_mem.h Compute Express Link Memory Devices 0xCF 02 fs/cifs/ioctl.c 0xDB 00-0F drivers/char/mwave/mwavepub.h 0xDD 00-3F ZFCP device driver see drivers/s390/scsi/
drivers/cxl/mem.c +287 -1
317 lines not on this page · raw message
include/uapi/linux/cxl_mem.h new file +154 -0
154 lines not on this page · raw message
Signature · 4 lines
2.30.1 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 4/9] cxl/mem: Add basic IOCTL interface
Quoted 17 lines · On Mon, 15 Feb 2021 17:45:33 -0800 Ben Widawsky <[email protected]> wrote:
Add a straightforward IOCTL that provides a mechanism for userspace to query the supported memory device commands. CXL commands as they appear to userspace are described as part of the UAPI kerneldoc. The command list returned via this IOCTL will contain the full set of commands that the driver supports, however, some of those commands may not be available for use by userspace.
Memory device commands first appear in the CXL 2.0 specification. They are submitted through a mailbox mechanism specified in the CXL 2.0 specification.
The send command allows userspace to issue mailbox commands directly to the hardware. The list of available commands to send are the output of the query command. The driver verifies basic properties of the command and possibly inspect the input (or output) payload to determine whether or not the command is allowed (or might taint the kernel).
Quoted 3 lines
Signed-off-by Ben Widawsky <[email protected]>Reviewed-by Dan Williams <[email protected]> (v2)
I may be missreading this but I think the logic to ensure commands using a variable sized buffer have enough space is broken.
Jonathan
Quoted 174 lines
--- .clang-format | 1 + .../userspace-api/ioctl/ioctl-number.rst | 1 + drivers/cxl/mem.c | 288 +++++++++++++++++- include/uapi/linux/cxl_mem.h | 154 ++++++++++ 4 files changed, 443 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/cxl_mem.h.clang-format +1
-0@@ -109,6 +109,7 @@ ForEachMacros: - 'css_for_each_child' - 'css_for_each_descendant_post' - 'css_for_each_descendant_pre' + - 'cxl_for_each_cmd' - 'device_for_each_child_node' - 'dma_fence_chain_for_each' - 'do_for_each_ftrace_op'Documentation/userspace-api/ioctl/ioctl-number.rst +1
-0@@ -352,6 +352,7 @@ Code Seq# Include File Comments <mailto:[email protected]> 0xCC 00-0F drivers/misc/ibmvmc.h pseries VMC driver 0xCD 01 linux/reiserfs_fs.h +0xCE 01-02 uapi/linux/cxl_mem.h Compute Express Link Memory Devices 0xCF 02 fs/cifs/ioctl.c 0xDB 00-0F drivers/char/mwave/mwavepub.h 0xDD 00-3F ZFCP device driver see drivers/s390/scsi/drivers/cxl/mem.c +114
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ +#include <uapi/linux/cxl_mem.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/cdev.h> @@ -40,6 +41,7 @@#define CXL_MAILBOX_TIMEOUT_MS (2 * HZ)enum opcode { +CXL_MBOX_OP_INVALID =0x0000 ,CXL_MBOX_OP_IDENTIFY =0x4000 ,CXL_MBOX_OP_MAX =0x10000 };@@ -91,6 +93,49 @@ struct cxl_memdev {static int cxl_mem_major;static DEFINE_IDA (cxl_memdev_ida); +/** +* 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 +* +* 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. 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 { +struct cxl_command_info info; +enum opcode opcode; +}; + +#define CXL_CMD (_id, sin, sout) \ + [CXL_MEM_COMMAND_ID_##_id] = { \ + .info = { \ + .id = CXL_MEM_COMMAND_ID_##_id, \ + .size_in = sin, \ + .size_out = sout, \ + }, \ + .opcode = CXL_MBOX_OP_##_id, \ + } + +/* +* This table defines the supported mailbox commands for the driver. This table +* is made up of a UAPI structure. Non-negative values as parameters 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. +*/ +static struct cxl_mem_command mem_commands[] = { +CXL_CMD (IDENTIFY ,0 ,0x43 ), +}; + +#define cxl_for_each_cmd (cmd) \ + for ((cmd) = &mem_commands[0]; \ + ((cmd) - mem_commands) < ARRAY_SIZE(mem_commands); (cmd)++) + +#define cxl_cmd_count ARRAY_SIZE(mem_commands) +static int cxl_mem_wait_for_doorbell (struct cxl_mem *cxlm) {const unsigned long start = jiffies;@@ -312,6 +357,247 @@ static void cxl_mem_mbox_put(struct cxl_mem *cxlm)mutex_unlock (&cxlm->mbox_mutex); } +/** +* handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. +* @cxlm: The CXL memory device to communicate with. +* @cmd: The validated command. +* @in_payload: Pointer to userspace's input payload. +* @out_payload: Pointer to userspace's output payload. +* @size_out: (Input) Max payload size to copy out. +* (Output) Payload size hardware generated. +* @retval: Hardware generated return code from the operation. +* +* Return: +* * %0 - Mailbox transaction succeeded. This implies the mailbox +* protocol completed successfully not that the operation itself +* was successful. +* * %-ENOMEM - Couldn't allocate a bounce buffer. +* * %-EFAULT - Something happened with copy_to/from_user. +* * %-EINTR - Mailbox acquisition interrupted. +* * %-* - Transaction level failures. +* +* Creates the appropriate mailbox command and dispatches it on behalf of a +* userspace request. The input and output payloads are copied between +* userspace. +* +* See cxl_send_cmd(). +*/ +static int handle_mailbox_cmd_from_user (struct cxl_mem *cxlm, +const struct cxl_mem_command *cmd, +u64 in_payload,u64 out_payload, +s32 *size_out,u32 *retval) +{ +struct device *dev = &cxlm->pdev->dev; +struct mbox_cmd mbox_cmd = { + .opcode = cmd->opcode, + .size_in = cmd->info.size_in, + }; +int rc; + +if (cmd->info.size_out) { + mbox_cmd.payload_out =kvzalloc (cmd->info.size_out,GFP_KERNEL ); +if (!mbox_cmd.payload_out) +return -ENOMEM ; + } + +if (cmd->info.size_in) { + mbox_cmd.payload_in =kvzalloc (cmd->info.size_in,GFP_KERNEL ); +if (!mbox_cmd.payload_in) { + rc = -ENOMEM ; + goto out; + } + +if (copy_from_user (mbox_cmd.payload_in, +u64_to_user_ptr (in_payload), + cmd->info.size_in)) { + rc = -EFAULT ; + goto out; + } + } + + rc =cxl_mem_mbox_get (cxlm); +if (rc) + goto out; + +dev_dbg (dev, +"Submitting %s command for user\n" +"\topcode: %x\n" +"\tsize: %ub\n" , + cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, + cmd->info.size_in); +
drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 438
Quoted 10 lines
drivers/cxl/mem.c +10
-0+ rc =__cxl_mem_mbox_send_cmd (cxlm, &mbox_cmd); +cxl_mem_mbox_put (cxlm); +if (rc) + goto out; + +/* +* @size_out contains the max size that's allowed to be written back out +* to userspace. While the payload may have written more output than +* this it will have to be ignored. +*/
See below for why I don't think this works. The size of mbox_cmd.payload_out seems to always be the size userspace specified, never the 1MB this code is assuming. So if the hardware returns more than userspace asks for you have a buffer overrun.
Quoted 125 lines
drivers/cxl/mem.c +125
-0+if (mbox_cmd.size_out) { +if (copy_to_user (u64_to_user_ptr (out_payload), + mbox_cmd.payload_out, *size_out)) { + rc = -EFAULT ; + goto out; + } + } + +/* +* Reporting the actual size, even if it was greater than @size_out +* allows userspace to try the command again with a bigger buffer. +*/ + *size_out = mbox_cmd.size_out; + *retval = mbox_cmd.return_code; + +out: +kvfree (mbox_cmd.payload_in); +kvfree (mbox_cmd.payload_out); +return rc; +} + +/** +* cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. +* @cxlm: &struct cxl_mem device whose mailbox will be used. +* @send_cmd: &struct cxl_send_command copied in from userspace. +* @out_cmd: Sanitized and populated &struct cxl_mem_command. +* +* Return: +* * %0 - @out_cmd is ready to send. +* * %-ENOTTY - Invalid command specified. +* * %-EINVAL - Reserved fields or invalid values were used. +* * %-ENOMEM - Input or output buffer wasn't sized properly. +* +* The result of this command is a fully validated command in @out_cmd that is +* safe to send to the hardware. +* +* See handle_mailbox_cmd_from_user() +*/ +static int cxl_validate_cmd_from_user (struct cxl_mem *cxlm, +const struct cxl_send_command *send_cmd, +struct cxl_mem_command *out_cmd) +{ +const struct cxl_command_info *info; +struct cxl_mem_command *c; + +if (send_cmd->id ==0 || send_cmd->id >=CXL_MEM_COMMAND_ID_MAX ) +return -ENOTTY ; + +/* +* The user can never specify an input payload larger than what hardware +* supports, but output can be arbitrarily large (simply write out as +* much data as the hardware provides). +*/ +if (send_cmd->in.size > cxlm->payload_size) +return -EINVAL ; + +if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK ) +return -EINVAL ; + +if (send_cmd->rsvd) +return -EINVAL ; + +if (send_cmd->in.rsvd || send_cmd->out.rsvd) +return -EINVAL ; + +/* Convert user's command into the internal representation */ + c = &mem_commands[send_cmd->id]; + info = &c->info; + +/* Check the input buffer is the expected size */ +if (info->size_in >=0 && info->size_in != send_cmd->in.size) +return -ENOMEM ; + +/* Check the output buffer is at least large enough */ +if (info->size_out >=0 && send_cmd->out.size < info->size_out) +return -ENOMEM ; + +memcpy (out_cmd, c,sizeof (*c)); + out_cmd->info.size_in = send_cmd->in.size; + out_cmd->info.size_out = send_cmd->out.size; + +return 0 ; +} + +static int cxl_query_cmd (struct cxl_memdev *cxlmd, +struct cxl_mem_query_commands __user *q) +{ +struct device *dev = &cxlmd->dev; +struct cxl_mem_command *cmd; +u32 n_commands; +int j =0 ; + +dev_dbg (dev,"Query IOCTL\n" ); + +if (get_user (n_commands, &q->n_commands)) +return -EFAULT ; + +/* returns the total number if 0 elements are requested. */ +if (n_commands ==0 ) +return put_user (cxl_cmd_count, &q->n_commands); + +/* +* otherwise, return max(n_commands, total commands) cxl_command_info +* structures. +*/ +cxl_for_each_cmd (cmd) { +const struct cxl_command_info *info = &cmd->info; + +if (copy_to_user (&q->commands[j++], info,sizeof (*info))) +return -EFAULT ; + +if (j == n_commands) +break ; + } + +return 0 ; +} + +static int cxl_send_cmd (struct cxl_memdev *cxlmd, +struct cxl_send_command __user *s) +{ +struct cxl_mem *cxlm = cxlmd->cxlm; +struct device *dev = &cxlmd->dev; +struct cxl_send_command send; +struct cxl_mem_command c;
drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 573
Quoted 10 lines
drivers/cxl/mem.c +10
-0+int rc; + +dev_dbg (dev,"Send IOCTL\n" ); + +if (copy_from_user (&send, s,sizeof (send))) +return -EFAULT ; + + rc =cxl_validate_cmd_from_user (cxlmd->cxlm, &send, &c); +if (rc) +return rc;
Userspace will pass in send.out set to the size of it's available buffer. Then cxl_validate_cmd_from_user() will fill
c.info.size_out with send.out.size
drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 576
Quoted 3 lines
drivers/cxl/mem.c +3
-0+ +/* Prepare to handle a full payload for variable sized output */ +if (c.info.size_out <0 )
So this check only works if userspace set the command to have variable size. That's not what the docs below suggest should happen.
Quoted 76 lines
drivers/cxl/mem.c +25
-1+ c.info.size_out = cxlm->payload_size; + + rc =handle_mailbox_cmd_from_user (cxlm, &c, send.in.payload, + send.out.payload, &send.out.size, + &send.retval); +if (rc) +return rc; + +return copy_to_user (s, &send,sizeof (send)); +} + +static long __cxl_memdev_ioctl (struct cxl_memdev *cxlmd,unsigned int cmd, +unsigned long arg) +{ +switch (cmd) { +case CXL_MEM_QUERY_COMMANDS : +return cxl_query_cmd (cxlmd, (void __user *)arg); +case CXL_MEM_SEND_COMMAND : +return cxl_send_cmd (cxlmd, (void __user *)arg); +default : +return -ENOTTY ; + } +} +static long cxl_memdev_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {@@ -325,7 +611,7 @@ static long cxl_memdev_ioctl(struct file *file, unsigned int cmd,if (!percpu_ref_tryget_live (&cxlmd->ops_active))return -ENXIO ; -/* TODO: ioctl body */ + rc =__cxl_memdev_ioctl (cxlmd, cmd, arg);percpu_ref_put (&cxlmd->ops_active);include/uapi/linux/cxl_mem.h new file +34
-0@@ -0,0 +1,154 @@ +/* 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_ + +#include <linux/types.h> + +/** +* DOC: UAPI +* +* Not all of all commands that the driver supports are always available for use +* by userspace. Userspace must check the results from the QUERY command in +* order to determine the live set of commands. +*/ + +#define CXL_MEM_QUERY_COMMANDS _IOR(0xCE, 1, struct cxl_mem_query_commands) +#define CXL_MEM_SEND_COMMAND _IOWR(0xCE, 2, struct cxl_send_command) + +#define CXL_CMDS \ + ___C(INVALID, "Invalid Command"), \ + ___C(IDENTIFY, "Identify Command"), \ + ___C(MAX, "invalid / last command") + +#define ___C (a, b) CXL_MEM_COMMAND_ID_##a +enum {CXL_CMDS }; + +#undef ___C +#define ___C (a, b) { b } +static const struct { +const char *name; +} cxl_command_names[] = {CXL_CMDS };
include/uapi/linux/cxl_mem.h line 44
Quoted 10 lines
include/uapi/linux/cxl_mem.h new file +10
-0+ +/* +* Here's how this actually breaks out: +* cxl_command_names[] = { +* [CXL_MEM_COMMAND_ID_INVALID] = { "Invalid Command" }, +* [CXL_MEM_COMMAND_ID_IDENTIFY] = { "Identify Command" }, +* ... +* [CXL_MEM_COMMAND_ID_MAX] = { "invalid / last command" }, +* }; +*/
Thanks, this is great.
Quoted 52 lines
include/uapi/linux/cxl_mem.h new file +52
-0+ +#undef ___C + +/** +* struct cxl_command_info - Command information returned from a query. +* @id: ID number for the command. +* @flags: Flags that specify command behavior. +* @size_in: Expected input size, or -1 if variable length. +* @size_out: Expected output size, or -1 if variable length. +* +* Represents a single command that is supported by both the driver and the +* hardware. This is returned as part of an array from the query ioctl. The +* following would be a command that takes a variable length input and returns 0 +* bytes of output. +* +* - @id = 10 +* - @flags = 0 +* - @size_in = -1 +* - @size_out = 0 +* +* See struct cxl_mem_query_commands. +*/ +struct cxl_command_info { +__u32 id; + +__u32 flags; +#define CXL_MEM_COMMAND_FLAG_MASK GENMASK(0, 0) + +__s32 size_in; +__s32 size_out; +}; + +/** +* 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.
include/uapi/linux/cxl_mem.h line 106
Quoted 10 lines
include/uapi/linux/cxl_mem.h new file +10
-0+*/ +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 +*/
This is fairly well described in the docs above the structure. Perhaps combine the two.
Quoted 38 lines
include/uapi/linux/cxl_mem.h new file +38
-0+__u32 n_commands; +__u32 rsvd; + +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 (input). +* @rsvd: Must be zero. +* @retval: Return value from the memory device (output). +* @in.size: Size of the payload to provide to the device (input). +* @in.rsvd: Must be zero. +* @in.payload: Pointer to memory for payload input, payload is little endian. +* @out.size: Size of the payload received from the device (input/output). This +* field is filled in by 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. +* @out.rsvd: Must be zero. +* @out.payload: Pointer to memory for payload output, payload is little endian. +* +* Mechanism for userspace to send a command to the hardware for processing. The +* driver will do basic validation on the command sizes. In some cases even the +* payload may be introspected. Userspace is required to allocate large enough +* buffers for size_out which can be variable length in certain situations. +*/ +struct cxl_send_command { +__u32 id; +__u32 flags; +__u32 rsvd; +__u32 retval; + +struct { +__s32 size; +__u32 rsvd; +__u64 payload;
include/uapi/linux/cxl_mem.h line 154
Quoted 10 lines
include/uapi/linux/cxl_mem.h new file +10
-0+ } in; + +struct { +__s32 size; +__u32 rsvd; +__u64 payload; + } out; +}; + +#endif
_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 4/9] cxl/mem: Add basic IOCTL interface
Quoted 360 lines · Jonathan Cameron
Quoted 17 lines · On Mon, 15 Feb 2021 17:45:33 -0800 Ben Widawsky <[email protected]> wrote:
Add a straightforward IOCTL that provides a mechanism for userspace to query the supported memory device commands. CXL commands as they appear to userspace are described as part of the UAPI kerneldoc. The command list returned via this IOCTL will contain the full set of commands that the driver supports, however, some of those commands may not be available for use by userspace.
Memory device commands first appear in the CXL 2.0 specification. They are submitted through a mailbox mechanism specified in the CXL 2.0 specification.
The send command allows userspace to issue mailbox commands directly to the hardware. The list of available commands to send are the output of the query command. The driver verifies basic properties of the command and possibly inspect the input (or output) payload to determine whether or not the command is allowed (or might taint the kernel).
Quoted 3 lines
Signed-off-by Ben Widawsky <[email protected]>Reviewed-by Dan Williams <[email protected]> (v2)I may be missreading this but I think the logic to ensure commands using a variable sized buffer have enough space is broken.
Jonathan
Quoted 174 lines
--- .clang-format | 1 + .../userspace-api/ioctl/ioctl-number.rst | 1 + drivers/cxl/mem.c | 288 +++++++++++++++++- include/uapi/linux/cxl_mem.h | 154 ++++++++++ 4 files changed, 443 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/cxl_mem.h.clang-format +1
-0@@ -109,6 +109,7 @@ ForEachMacros: - 'css_for_each_child' - 'css_for_each_descendant_post' - 'css_for_each_descendant_pre' + - 'cxl_for_each_cmd' - 'device_for_each_child_node' - 'dma_fence_chain_for_each' - 'do_for_each_ftrace_op'Documentation/userspace-api/ioctl/ioctl-number.rst +1
-0@@ -352,6 +352,7 @@ Code Seq# Include File Comments <mailto:[email protected]> 0xCC 00-0F drivers/misc/ibmvmc.h pseries VMC driver 0xCD 01 linux/reiserfs_fs.h +0xCE 01-02 uapi/linux/cxl_mem.h Compute Express Link Memory Devices 0xCF 02 fs/cifs/ioctl.c 0xDB 00-0F drivers/char/mwave/mwavepub.h 0xDD 00-3F ZFCP device driver see drivers/s390/scsi/drivers/cxl/mem.c +114
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ +#include <uapi/linux/cxl_mem.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/cdev.h> @@ -40,6 +41,7 @@#define CXL_MAILBOX_TIMEOUT_MS (2 * HZ)enum opcode { +CXL_MBOX_OP_INVALID =0x0000 ,CXL_MBOX_OP_IDENTIFY =0x4000 ,CXL_MBOX_OP_MAX =0x10000 };@@ -91,6 +93,49 @@ struct cxl_memdev {static int cxl_mem_major;static DEFINE_IDA (cxl_memdev_ida); +/** +* 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 +* +* 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. 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 { +struct cxl_command_info info; +enum opcode opcode; +}; + +#define CXL_CMD (_id, sin, sout) \ + [CXL_MEM_COMMAND_ID_##_id] = { \ + .info = { \ + .id = CXL_MEM_COMMAND_ID_##_id, \ + .size_in = sin, \ + .size_out = sout, \ + }, \ + .opcode = CXL_MBOX_OP_##_id, \ + } + +/* +* This table defines the supported mailbox commands for the driver. This table +* is made up of a UAPI structure. Non-negative values as parameters 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. +*/ +static struct cxl_mem_command mem_commands[] = { +CXL_CMD (IDENTIFY ,0 ,0x43 ), +}; + +#define cxl_for_each_cmd (cmd) \ + for ((cmd) = &mem_commands[0]; \ + ((cmd) - mem_commands) < ARRAY_SIZE(mem_commands); (cmd)++) + +#define cxl_cmd_count ARRAY_SIZE(mem_commands) +static int cxl_mem_wait_for_doorbell (struct cxl_mem *cxlm) {const unsigned long start = jiffies;@@ -312,6 +357,247 @@ static void cxl_mem_mbox_put(struct cxl_mem *cxlm)mutex_unlock (&cxlm->mbox_mutex); } +/** +* handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. +* @cxlm: The CXL memory device to communicate with. +* @cmd: The validated command. +* @in_payload: Pointer to userspace's input payload. +* @out_payload: Pointer to userspace's output payload. +* @size_out: (Input) Max payload size to copy out. +* (Output) Payload size hardware generated. +* @retval: Hardware generated return code from the operation. +* +* Return: +* * %0 - Mailbox transaction succeeded. This implies the mailbox +* protocol completed successfully not that the operation itself +* was successful. +* * %-ENOMEM - Couldn't allocate a bounce buffer. +* * %-EFAULT - Something happened with copy_to/from_user. +* * %-EINTR - Mailbox acquisition interrupted. +* * %-* - Transaction level failures. +* +* Creates the appropriate mailbox command and dispatches it on behalf of a +* userspace request. The input and output payloads are copied between +* userspace. +* +* See cxl_send_cmd(). +*/ +static int handle_mailbox_cmd_from_user (struct cxl_mem *cxlm, +const struct cxl_mem_command *cmd, +u64 in_payload,u64 out_payload, +s32 *size_out,u32 *retval) +{ +struct device *dev = &cxlm->pdev->dev; +struct mbox_cmd mbox_cmd = { + .opcode = cmd->opcode, + .size_in = cmd->info.size_in, + }; +int rc; + +if (cmd->info.size_out) { + mbox_cmd.payload_out =kvzalloc (cmd->info.size_out,GFP_KERNEL ); +if (!mbox_cmd.payload_out) +return -ENOMEM ; + } + +if (cmd->info.size_in) { + mbox_cmd.payload_in =kvzalloc (cmd->info.size_in,GFP_KERNEL ); +if (!mbox_cmd.payload_in) { + rc = -ENOMEM ; + goto out; + } + +if (copy_from_user (mbox_cmd.payload_in, +u64_to_user_ptr (in_payload), + cmd->info.size_in)) { + rc = -EFAULT ; + goto out; + } + } + + rc =cxl_mem_mbox_get (cxlm); +if (rc) + goto out; + +dev_dbg (dev, +"Submitting %s command for user\n" +"\topcode: %x\n" +"\tsize: %ub\n" , + cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, + cmd->info.size_in); +drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 438
Quoted 10 lines
drivers/cxl/mem.c +10
-0+ rc =__cxl_mem_mbox_send_cmd (cxlm, &mbox_cmd); +cxl_mem_mbox_put (cxlm); +if (rc) + goto out; + +/* +* @size_out contains the max size that's allowed to be written back out +* to userspace. While the payload may have written more output than +* this it will have to be ignored. +*/ See below for why I don't think this works. The size of mbox_cmd.payload_out seems to always be the size userspace specified, never the 1MB this code is assuming. So if the hardware returns more than userspace asks for you have a buffer overrun.
Quoted 125 lines
drivers/cxl/mem.c +125
-0+if (mbox_cmd.size_out) { +if (copy_to_user (u64_to_user_ptr (out_payload), + mbox_cmd.payload_out, *size_out)) { + rc = -EFAULT ; + goto out; + } + } + +/* +* Reporting the actual size, even if it was greater than @size_out +* allows userspace to try the command again with a bigger buffer. +*/ + *size_out = mbox_cmd.size_out; + *retval = mbox_cmd.return_code; + +out: +kvfree (mbox_cmd.payload_in); +kvfree (mbox_cmd.payload_out); +return rc; +} + +/** +* cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. +* @cxlm: &struct cxl_mem device whose mailbox will be used. +* @send_cmd: &struct cxl_send_command copied in from userspace. +* @out_cmd: Sanitized and populated &struct cxl_mem_command. +* +* Return: +* * %0 - @out_cmd is ready to send. +* * %-ENOTTY - Invalid command specified. +* * %-EINVAL - Reserved fields or invalid values were used. +* * %-ENOMEM - Input or output buffer wasn't sized properly. +* +* The result of this command is a fully validated command in @out_cmd that is +* safe to send to the hardware. +* +* See handle_mailbox_cmd_from_user() +*/ +static int cxl_validate_cmd_from_user (struct cxl_mem *cxlm, +const struct cxl_send_command *send_cmd, +struct cxl_mem_command *out_cmd) +{ +const struct cxl_command_info *info; +struct cxl_mem_command *c; + +if (send_cmd->id ==0 || send_cmd->id >=CXL_MEM_COMMAND_ID_MAX ) +return -ENOTTY ; + +/* +* The user can never specify an input payload larger than what hardware +* supports, but output can be arbitrarily large (simply write out as +* much data as the hardware provides). +*/ +if (send_cmd->in.size > cxlm->payload_size) +return -EINVAL ; + +if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK ) +return -EINVAL ; + +if (send_cmd->rsvd) +return -EINVAL ; + +if (send_cmd->in.rsvd || send_cmd->out.rsvd) +return -EINVAL ; + +/* Convert user's command into the internal representation */ + c = &mem_commands[send_cmd->id]; + info = &c->info; + +/* Check the input buffer is the expected size */ +if (info->size_in >=0 && info->size_in != send_cmd->in.size) +return -ENOMEM ; + +/* Check the output buffer is at least large enough */ +if (info->size_out >=0 && send_cmd->out.size < info->size_out) +return -ENOMEM ; + +memcpy (out_cmd, c,sizeof (*c)); + out_cmd->info.size_in = send_cmd->in.size; + out_cmd->info.size_out = send_cmd->out.size; + +return 0 ; +} + +static int cxl_query_cmd (struct cxl_memdev *cxlmd, +struct cxl_mem_query_commands __user *q) +{ +struct device *dev = &cxlmd->dev; +struct cxl_mem_command *cmd; +u32 n_commands; +int j =0 ; + +dev_dbg (dev,"Query IOCTL\n" ); + +if (get_user (n_commands, &q->n_commands)) +return -EFAULT ; + +/* returns the total number if 0 elements are requested. */ +if (n_commands ==0 ) +return put_user (cxl_cmd_count, &q->n_commands); + +/* +* otherwise, return max(n_commands, total commands) cxl_command_info +* structures. +*/ +cxl_for_each_cmd (cmd) { +const struct cxl_command_info *info = &cmd->info; + +if (copy_to_user (&q->commands[j++], info,sizeof (*info))) +return -EFAULT ; + +if (j == n_commands) +break ; + } + +return 0 ; +} + +static int cxl_send_cmd (struct cxl_memdev *cxlmd, +struct cxl_send_command __user *s) +{ +struct cxl_mem *cxlm = cxlmd->cxlm; +struct device *dev = &cxlmd->dev; +struct cxl_send_command send; +struct cxl_mem_command c;drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 573
Quoted 10 lines
drivers/cxl/mem.c +10
-0+int rc; + +dev_dbg (dev,"Send IOCTL\n" ); + +if (copy_from_user (&send, s,sizeof (send))) +return -EFAULT ; + + rc =cxl_validate_cmd_from_user (cxlmd->cxlm, &send, &c); +if (rc) +return rc;Userspace will pass in send.out set to the size of it's available buffer. Then
cxl_validate_cmd_from_user()will fill
c.info.size_out with send.out.size
Quoted 7 lines
drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 576
Quoted 3 lines
drivers/cxl/mem.c +3
-0+ +/* Prepare to handle a full payload for variable sized output */ +if (c.info.size_out <0 )So this check only works if userspace set the command to have variable size. That's not what the docs below suggest should happen.
Another good catch. This bug was introduced after the last change was made. I still don't want to have a size_out as part of the mailbox command.
Validate should not alter the size_out field except in the case of RAW commands.
I believe this is the right fix, handle_mailbox_cmd_from_user() already will only copy_to the right number of byte, but your eyes on it are appreciated.
drivers/cxl/mem.c +5 -1
@@ -686,7 +686,11 @@ static int cxl_validate_cmd_from_user(struct cxl_mem *cxlm,memcpy (out_cmd, c,sizeof (*c)); out_cmd->info.size_in = send_cmd->in.size; - out_cmd->info.size_out = send_cmd->out.size; +/* +* XXX: out_cmd->info.size_out will be controlled by the driver, and the +* specified number of bytes @send_cmd->out.size will be copied back out +* to userspace. +*/ return 0 ; }
Quoted 195 lines
Quoted 76 lines
drivers/cxl/mem.c +25
-1+ c.info.size_out = cxlm->payload_size; + + rc =handle_mailbox_cmd_from_user (cxlm, &c, send.in.payload, + send.out.payload, &send.out.size, + &send.retval); +if (rc) +return rc; + +return copy_to_user (s, &send,sizeof (send)); +} + +static long __cxl_memdev_ioctl (struct cxl_memdev *cxlmd,unsigned int cmd, +unsigned long arg) +{ +switch (cmd) { +case CXL_MEM_QUERY_COMMANDS : +return cxl_query_cmd (cxlmd, (void __user *)arg); +case CXL_MEM_SEND_COMMAND : +return cxl_send_cmd (cxlmd, (void __user *)arg); +default : +return -ENOTTY ; + } +} +static long cxl_memdev_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {@@ -325,7 +611,7 @@ static long cxl_memdev_ioctl(struct file *file, unsigned int cmd,if (!percpu_ref_tryget_live (&cxlmd->ops_active))return -ENXIO ; -/* TODO: ioctl body */ + rc =__cxl_memdev_ioctl (cxlmd, cmd, arg);percpu_ref_put (&cxlmd->ops_active);include/uapi/linux/cxl_mem.h new file +34
-0@@ -0,0 +1,154 @@ +/* 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_ + +#include <linux/types.h> + +/** +* DOC: UAPI +* +* Not all of all commands that the driver supports are always available for use +* by userspace. Userspace must check the results from the QUERY command in +* order to determine the live set of commands. +*/ + +#define CXL_MEM_QUERY_COMMANDS _IOR(0xCE, 1, struct cxl_mem_query_commands) +#define CXL_MEM_SEND_COMMAND _IOWR(0xCE, 2, struct cxl_send_command) + +#define CXL_CMDS \ + ___C(INVALID, "Invalid Command"), \ + ___C(IDENTIFY, "Identify Command"), \ + ___C(MAX, "invalid / last command") + +#define ___C (a, b) CXL_MEM_COMMAND_ID_##a +enum {CXL_CMDS }; + +#undef ___C +#define ___C (a, b) { b } +static const struct { +const char *name; +} cxl_command_names[] = {CXL_CMDS };include/uapi/linux/cxl_mem.h line 44
Quoted 10 lines
include/uapi/linux/cxl_mem.h new file +10
-0+ +/* +* Here's how this actually breaks out: +* cxl_command_names[] = { +* [CXL_MEM_COMMAND_ID_INVALID] = { "Invalid Command" }, +* [CXL_MEM_COMMAND_ID_IDENTIFY] = { "Identify Command" }, +* ... +* [CXL_MEM_COMMAND_ID_MAX] = { "invalid / last command" }, +* }; +*/ Thanks, this is great.
Quoted 52 lines
include/uapi/linux/cxl_mem.h new file +52
-0+ +#undef ___C + +/** +* struct cxl_command_info - Command information returned from a query. +* @id: ID number for the command. +* @flags: Flags that specify command behavior. +* @size_in: Expected input size, or -1 if variable length. +* @size_out: Expected output size, or -1 if variable length. +* +* Represents a single command that is supported by both the driver and the +* hardware. This is returned as part of an array from the query ioctl. The +* following would be a command that takes a variable length input and returns 0 +* bytes of output. +* +* - @id = 10 +* - @flags = 0 +* - @size_in = -1 +* - @size_out = 0 +* +* See struct cxl_mem_query_commands. +*/ +struct cxl_command_info { +__u32 id; + +__u32 flags; +#define CXL_MEM_COMMAND_FLAG_MASK GENMASK(0, 0) + +__s32 size_in; +__s32 size_out; +}; + +/** +* 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. include/uapi/linux/cxl_mem.h line 106
Quoted 10 lines
include/uapi/linux/cxl_mem.h new file +10
-0+*/ +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 +*/ This is fairly well described in the docs above the structure. Perhaps combine the two.
Quoted 40 lines
include/uapi/linux/cxl_mem.h new file +40
-0+__u32 n_commands; +__u32 rsvd; + +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 (input). +* @rsvd: Must be zero. +* @retval: Return value from the memory device (output). +* @in.size: Size of the payload to provide to the device (input). +* @in.rsvd: Must be zero. +* @in.payload: Pointer to memory for payload input, payload is little endian. +* @out.size: Size of the payload received from the device (input/output). This +* field is filled in by 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. +* @out.rsvd: Must be zero. +* @out.payload: Pointer to memory for payload output, payload is little endian. +* +* Mechanism for userspace to send a command to the hardware for processing. The +* driver will do basic validation on the command sizes. In some cases even the +* payload may be introspected. Userspace is required to allocate large enough +* buffers for size_out which can be variable length in certain situations. +*/ +struct cxl_send_command { +__u32 id; +__u32 flags; +__u32 rsvd; +__u32 retval; + +struct { +__s32 size; +__u32 rsvd; +__u64 payload; + } in; +
Quoted 9 lines
include/uapi/linux/cxl_mem.h line 154
Quoted 8 lines
include/uapi/linux/cxl_mem.h new file +8
-0+struct { +__s32 size; +__u32 rsvd; +__u64 payload; + } out; +}; + +#endif
_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 4/9] cxl/mem: Add basic IOCTL interface
Quoted 384 lines · On Tue, 16 Feb 2021 09:53:14 -0800 Ben Widawsky <[email protected]> wrote:
Quoted 360 lines · Jonathan Cameron
Quoted 17 lines · On Mon, 15 Feb 2021 17:45:33 -0800 Ben Widawsky <[email protected]> wrote:
Add a straightforward IOCTL that provides a mechanism for userspace to query the supported memory device commands. CXL commands as they appear to userspace are described as part of the UAPI kerneldoc. The command list returned via this IOCTL will contain the full set of commands that the driver supports, however, some of those commands may not be available for use by userspace.
Memory device commands first appear in the CXL 2.0 specification. They are submitted through a mailbox mechanism specified in the CXL 2.0 specification.
The send command allows userspace to issue mailbox commands directly to the hardware. The list of available commands to send are the output of the query command. The driver verifies basic properties of the command and possibly inspect the input (or output) payload to determine whether or not the command is allowed (or might taint the kernel).
Quoted 3 lines
Signed-off-by Ben Widawsky <[email protected]>Reviewed-by Dan Williams <[email protected]> (v2)I may be missreading this but I think the logic to ensure commands using a variable sized buffer have enough space is broken.
Jonathan
Quoted 174 lines
--- .clang-format | 1 + .../userspace-api/ioctl/ioctl-number.rst | 1 + drivers/cxl/mem.c | 288 +++++++++++++++++- include/uapi/linux/cxl_mem.h | 154 ++++++++++ 4 files changed, 443 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/cxl_mem.h.clang-format +1
-0@@ -109,6 +109,7 @@ ForEachMacros: - 'css_for_each_child' - 'css_for_each_descendant_post' - 'css_for_each_descendant_pre' + - 'cxl_for_each_cmd' - 'device_for_each_child_node' - 'dma_fence_chain_for_each' - 'do_for_each_ftrace_op'Documentation/userspace-api/ioctl/ioctl-number.rst +1
-0@@ -352,6 +352,7 @@ Code Seq# Include File Comments <mailto:[email protected]> 0xCC 00-0F drivers/misc/ibmvmc.h pseries VMC driver 0xCD 01 linux/reiserfs_fs.h +0xCE 01-02 uapi/linux/cxl_mem.h Compute Express Link Memory Devices 0xCF 02 fs/cifs/ioctl.c 0xDB 00-0F drivers/char/mwave/mwavepub.h 0xDD 00-3F ZFCP device driver see drivers/s390/scsi/drivers/cxl/mem.c +114
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ +#include <uapi/linux/cxl_mem.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/cdev.h> @@ -40,6 +41,7 @@#define CXL_MAILBOX_TIMEOUT_MS (2 * HZ)enum opcode { +CXL_MBOX_OP_INVALID =0x0000 ,CXL_MBOX_OP_IDENTIFY =0x4000 ,CXL_MBOX_OP_MAX =0x10000 };@@ -91,6 +93,49 @@ struct cxl_memdev {static int cxl_mem_major;static DEFINE_IDA (cxl_memdev_ida); +/** +* 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 +* +* 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. 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 { +struct cxl_command_info info; +enum opcode opcode; +}; + +#define CXL_CMD (_id, sin, sout) \ + [CXL_MEM_COMMAND_ID_##_id] = { \ + .info = { \ + .id = CXL_MEM_COMMAND_ID_##_id, \ + .size_in = sin, \ + .size_out = sout, \ + }, \ + .opcode = CXL_MBOX_OP_##_id, \ + } + +/* +* This table defines the supported mailbox commands for the driver. This table +* is made up of a UAPI structure. Non-negative values as parameters 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. +*/ +static struct cxl_mem_command mem_commands[] = { +CXL_CMD (IDENTIFY ,0 ,0x43 ), +}; + +#define cxl_for_each_cmd (cmd) \ + for ((cmd) = &mem_commands[0]; \ + ((cmd) - mem_commands) < ARRAY_SIZE(mem_commands); (cmd)++) + +#define cxl_cmd_count ARRAY_SIZE(mem_commands) +static int cxl_mem_wait_for_doorbell (struct cxl_mem *cxlm) {const unsigned long start = jiffies;@@ -312,6 +357,247 @@ static void cxl_mem_mbox_put(struct cxl_mem *cxlm)mutex_unlock (&cxlm->mbox_mutex); } +/** +* handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. +* @cxlm: The CXL memory device to communicate with. +* @cmd: The validated command. +* @in_payload: Pointer to userspace's input payload. +* @out_payload: Pointer to userspace's output payload. +* @size_out: (Input) Max payload size to copy out. +* (Output) Payload size hardware generated. +* @retval: Hardware generated return code from the operation. +* +* Return: +* * %0 - Mailbox transaction succeeded. This implies the mailbox +* protocol completed successfully not that the operation itself +* was successful. +* * %-ENOMEM - Couldn't allocate a bounce buffer. +* * %-EFAULT - Something happened with copy_to/from_user. +* * %-EINTR - Mailbox acquisition interrupted. +* * %-* - Transaction level failures. +* +* Creates the appropriate mailbox command and dispatches it on behalf of a +* userspace request. The input and output payloads are copied between +* userspace. +* +* See cxl_send_cmd(). +*/ +static int handle_mailbox_cmd_from_user (struct cxl_mem *cxlm, +const struct cxl_mem_command *cmd, +u64 in_payload,u64 out_payload, +s32 *size_out,u32 *retval) +{ +struct device *dev = &cxlm->pdev->dev; +struct mbox_cmd mbox_cmd = { + .opcode = cmd->opcode, + .size_in = cmd->info.size_in, + }; +int rc; + +if (cmd->info.size_out) { + mbox_cmd.payload_out =kvzalloc (cmd->info.size_out,GFP_KERNEL ); +if (!mbox_cmd.payload_out) +return -ENOMEM ; + } + +if (cmd->info.size_in) { + mbox_cmd.payload_in =kvzalloc (cmd->info.size_in,GFP_KERNEL ); +if (!mbox_cmd.payload_in) { + rc = -ENOMEM ; + goto out; + } + +if (copy_from_user (mbox_cmd.payload_in, +u64_to_user_ptr (in_payload), + cmd->info.size_in)) { + rc = -EFAULT ; + goto out; + } + } + + rc =cxl_mem_mbox_get (cxlm); +if (rc) + goto out; + +dev_dbg (dev, +"Submitting %s command for user\n" +"\topcode: %x\n" +"\tsize: %ub\n" , + cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, + cmd->info.size_in); +drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 438
Quoted 10 lines
drivers/cxl/mem.c +10
-0+ rc =__cxl_mem_mbox_send_cmd (cxlm, &mbox_cmd); +cxl_mem_mbox_put (cxlm); +if (rc) + goto out; + +/* +* @size_out contains the max size that's allowed to be written back out +* to userspace. While the payload may have written more output than +* this it will have to be ignored. +*/ See below for why I don't think this works. The size of mbox_cmd.payload_out seems to always be the size userspace specified, never the 1MB this code is assuming. So if the hardware returns more than userspace asks for you have a buffer overrun.
Quoted 125 lines
drivers/cxl/mem.c +125
-0+if (mbox_cmd.size_out) { +if (copy_to_user (u64_to_user_ptr (out_payload), + mbox_cmd.payload_out, *size_out)) { + rc = -EFAULT ; + goto out; + } + } + +/* +* Reporting the actual size, even if it was greater than @size_out +* allows userspace to try the command again with a bigger buffer. +*/ + *size_out = mbox_cmd.size_out; + *retval = mbox_cmd.return_code; + +out: +kvfree (mbox_cmd.payload_in); +kvfree (mbox_cmd.payload_out); +return rc; +} + +/** +* cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. +* @cxlm: &struct cxl_mem device whose mailbox will be used. +* @send_cmd: &struct cxl_send_command copied in from userspace. +* @out_cmd: Sanitized and populated &struct cxl_mem_command. +* +* Return: +* * %0 - @out_cmd is ready to send. +* * %-ENOTTY - Invalid command specified. +* * %-EINVAL - Reserved fields or invalid values were used. +* * %-ENOMEM - Input or output buffer wasn't sized properly. +* +* The result of this command is a fully validated command in @out_cmd that is +* safe to send to the hardware. +* +* See handle_mailbox_cmd_from_user() +*/ +static int cxl_validate_cmd_from_user (struct cxl_mem *cxlm, +const struct cxl_send_command *send_cmd, +struct cxl_mem_command *out_cmd) +{ +const struct cxl_command_info *info; +struct cxl_mem_command *c; + +if (send_cmd->id ==0 || send_cmd->id >=CXL_MEM_COMMAND_ID_MAX ) +return -ENOTTY ; + +/* +* The user can never specify an input payload larger than what hardware +* supports, but output can be arbitrarily large (simply write out as +* much data as the hardware provides). +*/ +if (send_cmd->in.size > cxlm->payload_size) +return -EINVAL ; + +if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK ) +return -EINVAL ; + +if (send_cmd->rsvd) +return -EINVAL ; + +if (send_cmd->in.rsvd || send_cmd->out.rsvd) +return -EINVAL ; + +/* Convert user's command into the internal representation */ + c = &mem_commands[send_cmd->id]; + info = &c->info; + +/* Check the input buffer is the expected size */ +if (info->size_in >=0 && info->size_in != send_cmd->in.size) +return -ENOMEM ; + +/* Check the output buffer is at least large enough */ +if (info->size_out >=0 && send_cmd->out.size < info->size_out) +return -ENOMEM ; + +memcpy (out_cmd, c,sizeof (*c)); + out_cmd->info.size_in = send_cmd->in.size; + out_cmd->info.size_out = send_cmd->out.size; + +return 0 ; +} + +static int cxl_query_cmd (struct cxl_memdev *cxlmd, +struct cxl_mem_query_commands __user *q) +{ +struct device *dev = &cxlmd->dev; +struct cxl_mem_command *cmd; +u32 n_commands; +int j =0 ; + +dev_dbg (dev,"Query IOCTL\n" ); + +if (get_user (n_commands, &q->n_commands)) +return -EFAULT ; + +/* returns the total number if 0 elements are requested. */ +if (n_commands ==0 ) +return put_user (cxl_cmd_count, &q->n_commands); + +/* +* otherwise, return max(n_commands, total commands) cxl_command_info +* structures. +*/ +cxl_for_each_cmd (cmd) { +const struct cxl_command_info *info = &cmd->info; + +if (copy_to_user (&q->commands[j++], info,sizeof (*info))) +return -EFAULT ; + +if (j == n_commands) +break ; + } + +return 0 ; +} + +static int cxl_send_cmd (struct cxl_memdev *cxlmd, +struct cxl_send_command __user *s) +{ +struct cxl_mem *cxlm = cxlmd->cxlm; +struct device *dev = &cxlmd->dev; +struct cxl_send_command send; +struct cxl_mem_command c;drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 573
Quoted 10 lines
drivers/cxl/mem.c +10
-0+int rc; + +dev_dbg (dev,"Send IOCTL\n" ); + +if (copy_from_user (&send, s,sizeof (send))) +return -EFAULT ; + + rc =cxl_validate_cmd_from_user (cxlmd->cxlm, &send, &c); +if (rc) +return rc;Userspace will pass in send.out set to the size of it's available buffer. Then
cxl_validate_cmd_from_user()will fill
c.info.size_out with send.out.sizeQuoted 7 lines
drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 576
Quoted 3 lines
drivers/cxl/mem.c +3
-0+ +/* Prepare to handle a full payload for variable sized output */ +if (c.info.size_out <0 )So this check only works if userspace set the command to have variable size. That's not what the docs below suggest should happen.
Another good catch. This bug was introduced after the last change was made. I still don't want to have a
size_outas part of the mailbox command.Validate should not alter the
size_outfield except in the case of RAW commands.I believe this is the right fix,
handle_mailbox_cmd_from_user()already will onlycopy_tothe right number of byte, but your eyes on it are appreciated.drivers/cxl/mem.c +0
-0@@ -686,7 +686,11 @@ static int cxl_validate_cmd_from_user(struct cxl_mem *cxlm,memcpy (out_cmd, c,sizeof (*c));
drivers/cxl/mem.c static int cxl_validate_cmd_from_user(struct cxl_mem *cxlm, · line 696
Quoted 10 lines
drivers/cxl/mem.c +5
-1out_cmd->info.size_in = send_cmd->in.size; - out_cmd->info.size_out = send_cmd->out.size; +/* +* XXX: out_cmd->info.size_out will be controlled by the driver, and the +* specified number of bytes @send_cmd->out.size will be copied back out +* to userspace. +*/ return 0 ; }
This deals with the buffer overflow being triggered from userspace.
I'm still nervous. I really don't like assuming hardware will do the right thing and never send us more data than we expect.
Given the check that it will fit in the target buffer is simple, I'd prefer to harden it and know we can't have a problem.
Jonathan
Quoted 205 lines
Quoted 205 lines
Quoted 77 lines
drivers/cxl/mem.c +25
-1+ c.info.size_out = cxlm->payload_size; + + rc =handle_mailbox_cmd_from_user (cxlm, &c, send.in.payload, + send.out.payload, &send.out.size, + &send.retval); +if (rc) +return rc; + +return copy_to_user (s, &send,sizeof (send)); +} + +static long __cxl_memdev_ioctl (struct cxl_memdev *cxlmd,unsigned int cmd, +unsigned long arg) +{ +switch (cmd) { +case CXL_MEM_QUERY_COMMANDS : +return cxl_query_cmd (cxlmd, (void __user *)arg); +case CXL_MEM_SEND_COMMAND : +return cxl_send_cmd (cxlmd, (void __user *)arg); +default : +return -ENOTTY ; + } +} +static long cxl_memdev_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {@@ -325,7 +611,7 @@ static long cxl_memdev_ioctl(struct file *file, unsigned int cmd,if (!percpu_ref_tryget_live (&cxlmd->ops_active))return -ENXIO ; -/* TODO: ioctl body */ + rc =__cxl_memdev_ioctl (cxlmd, cmd, arg);percpu_ref_put (&cxlmd->ops_active);include/uapi/linux/cxl_mem.h new file +34
-0@@ -0,0 +1,154 @@ +/* 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_ + +#include <linux/types.h> + +/** +* DOC: UAPI +* +* Not all of all commands that the driver supports are always available for use +* by userspace. Userspace must check the results from the QUERY command in +* order to determine the live set of commands. +*/ + +#define CXL_MEM_QUERY_COMMANDS _IOR(0xCE, 1, struct cxl_mem_query_commands) +#define CXL_MEM_SEND_COMMAND _IOWR(0xCE, 2, struct cxl_send_command) + +#define CXL_CMDS \ + ___C(INVALID, "Invalid Command"), \ + ___C(IDENTIFY, "Identify Command"), \ + ___C(MAX, "invalid / last command") + +#define ___C (a, b) CXL_MEM_COMMAND_ID_##a +enum {CXL_CMDS }; + +#undef ___C +#define ___C (a, b) { b } +static const struct { +const char *name; +} cxl_command_names[] = {CXL_CMDS };include/uapi/linux/cxl_mem.h line 44
Quoted 10 lines
include/uapi/linux/cxl_mem.h new file +10
-0+ +/* +* Here's how this actually breaks out: +* cxl_command_names[] = { +* [CXL_MEM_COMMAND_ID_INVALID] = { "Invalid Command" }, +* [CXL_MEM_COMMAND_ID_IDENTIFY] = { "Identify Command" }, +* ... +* [CXL_MEM_COMMAND_ID_MAX] = { "invalid / last command" }, +* }; +*/ Thanks, this is great.
Quoted 52 lines
include/uapi/linux/cxl_mem.h new file +52
-0+ +#undef ___C + +/** +* struct cxl_command_info - Command information returned from a query. +* @id: ID number for the command. +* @flags: Flags that specify command behavior. +* @size_in: Expected input size, or -1 if variable length. +* @size_out: Expected output size, or -1 if variable length. +* +* Represents a single command that is supported by both the driver and the +* hardware. This is returned as part of an array from the query ioctl. The +* following would be a command that takes a variable length input and returns 0 +* bytes of output. +* +* - @id = 10 +* - @flags = 0 +* - @size_in = -1 +* - @size_out = 0 +* +* See struct cxl_mem_query_commands. +*/ +struct cxl_command_info { +__u32 id; + +__u32 flags; +#define CXL_MEM_COMMAND_FLAG_MASK GENMASK(0, 0) + +__s32 size_in; +__s32 size_out; +}; + +/** +* 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. include/uapi/linux/cxl_mem.h line 106
Quoted 10 lines
include/uapi/linux/cxl_mem.h new file +10
-0+*/ +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 +*/ This is fairly well described in the docs above the structure. Perhaps combine the two.
Quoted 48 lines
include/uapi/linux/cxl_mem.h new file +48
-0+__u32 n_commands; +__u32 rsvd; + + 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 (input). +* @rsvd: Must be zero. +* @retval: Return value from the memory device (output). +* @in.size: Size of the payload to provide to the device (input). +* @in.rsvd: Must be zero. +* @in.payload: Pointer to memory for payload input, payload is little endian. +* @out.size: Size of the payload received from the device (input/output). This +* field is filled in by 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. +* @out.rsvd: Must be zero. +* @out.payload: Pointer to memory for payload output, payload is little endian. +* +* Mechanism for userspace to send a command to the hardware for processing. The +* driver will do basic validation on the command sizes. In some cases even the +* payload may be introspected. Userspace is required to allocate large enough +* buffers for size_out which can be variable length in certain situations. +*/ +struct cxl_send_command { +__u32 id; +__u32 flags; +__u32 rsvd; +__u32 retval; + +struct { +__s32 size; +__u32 rsvd; +__u64 payload; + } in; + +struct { +__s32 size; +__u32 rsvd; +__u64 payload; + } out; +}; + +#endif
Re: [PATCH v4 4/9] cxl/mem: Add basic IOCTL interface
Quoted 400 lines · Jonathan Cameron
Quoted 384 lines · On Tue, 16 Feb 2021 09:53:14 -0800 Ben Widawsky <[email protected]> wrote:
Quoted 360 lines · Jonathan Cameron
Quoted 17 lines · On Mon, 15 Feb 2021 17:45:33 -0800 Ben Widawsky <[email protected]> wrote:
Add a straightforward IOCTL that provides a mechanism for userspace to query the supported memory device commands. CXL commands as they appear to userspace are described as part of the UAPI kerneldoc. The command list returned via this IOCTL will contain the full set of commands that the driver supports, however, some of those commands may not be available for use by userspace.
Memory device commands first appear in the CXL 2.0 specification. They are submitted through a mailbox mechanism specified in the CXL 2.0 specification.
The send command allows userspace to issue mailbox commands directly to the hardware. The list of available commands to send are the output of the query command. The driver verifies basic properties of the command and possibly inspect the input (or output) payload to determine whether or not the command is allowed (or might taint the kernel).
Quoted 3 lines
Signed-off-by Ben Widawsky <[email protected]>Reviewed-by Dan Williams <[email protected]> (v2)I may be missreading this but I think the logic to ensure commands using a variable sized buffer have enough space is broken.
Jonathan
Quoted 174 lines
--- .clang-format | 1 + .../userspace-api/ioctl/ioctl-number.rst | 1 + drivers/cxl/mem.c | 288 +++++++++++++++++- include/uapi/linux/cxl_mem.h | 154 ++++++++++ 4 files changed, 443 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/cxl_mem.h.clang-format +1
-0@@ -109,6 +109,7 @@ ForEachMacros: - 'css_for_each_child' - 'css_for_each_descendant_post' - 'css_for_each_descendant_pre' + - 'cxl_for_each_cmd' - 'device_for_each_child_node' - 'dma_fence_chain_for_each' - 'do_for_each_ftrace_op'Documentation/userspace-api/ioctl/ioctl-number.rst +1
-0@@ -352,6 +352,7 @@ Code Seq# Include File Comments <mailto:[email protected]> 0xCC 00-0F drivers/misc/ibmvmc.h pseries VMC driver 0xCD 01 linux/reiserfs_fs.h +0xCE 01-02 uapi/linux/cxl_mem.h Compute Express Link Memory Devices 0xCF 02 fs/cifs/ioctl.c 0xDB 00-0F drivers/char/mwave/mwavepub.h 0xDD 00-3F ZFCP device driver see drivers/s390/scsi/drivers/cxl/mem.c +114
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ +#include <uapi/linux/cxl_mem.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/cdev.h> @@ -40,6 +41,7 @@#define CXL_MAILBOX_TIMEOUT_MS (2 * HZ)enum opcode { +CXL_MBOX_OP_INVALID =0x0000 ,CXL_MBOX_OP_IDENTIFY =0x4000 ,CXL_MBOX_OP_MAX =0x10000 };@@ -91,6 +93,49 @@ struct cxl_memdev {static int cxl_mem_major;static DEFINE_IDA (cxl_memdev_ida); +/** +* 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 +* +* 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. 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 { +struct cxl_command_info info; +enum opcode opcode; +}; + +#define CXL_CMD (_id, sin, sout) \ + [CXL_MEM_COMMAND_ID_##_id] = { \ + .info = { \ + .id = CXL_MEM_COMMAND_ID_##_id, \ + .size_in = sin, \ + .size_out = sout, \ + }, \ + .opcode = CXL_MBOX_OP_##_id, \ + } + +/* +* This table defines the supported mailbox commands for the driver. This table +* is made up of a UAPI structure. Non-negative values as parameters 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. +*/ +static struct cxl_mem_command mem_commands[] = { +CXL_CMD (IDENTIFY ,0 ,0x43 ), +}; + +#define cxl_for_each_cmd (cmd) \ + for ((cmd) = &mem_commands[0]; \ + ((cmd) - mem_commands) < ARRAY_SIZE(mem_commands); (cmd)++) + +#define cxl_cmd_count ARRAY_SIZE(mem_commands) +static int cxl_mem_wait_for_doorbell (struct cxl_mem *cxlm) {const unsigned long start = jiffies;@@ -312,6 +357,247 @@ static void cxl_mem_mbox_put(struct cxl_mem *cxlm)mutex_unlock (&cxlm->mbox_mutex); } +/** +* handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. +* @cxlm: The CXL memory device to communicate with. +* @cmd: The validated command. +* @in_payload: Pointer to userspace's input payload. +* @out_payload: Pointer to userspace's output payload. +* @size_out: (Input) Max payload size to copy out. +* (Output) Payload size hardware generated. +* @retval: Hardware generated return code from the operation. +* +* Return: +* * %0 - Mailbox transaction succeeded. This implies the mailbox +* protocol completed successfully not that the operation itself +* was successful. +* * %-ENOMEM - Couldn't allocate a bounce buffer. +* * %-EFAULT - Something happened with copy_to/from_user. +* * %-EINTR - Mailbox acquisition interrupted. +* * %-* - Transaction level failures. +* +* Creates the appropriate mailbox command and dispatches it on behalf of a +* userspace request. The input and output payloads are copied between +* userspace. +* +* See cxl_send_cmd(). +*/ +static int handle_mailbox_cmd_from_user (struct cxl_mem *cxlm, +const struct cxl_mem_command *cmd, +u64 in_payload,u64 out_payload, +s32 *size_out,u32 *retval) +{ +struct device *dev = &cxlm->pdev->dev; +struct mbox_cmd mbox_cmd = { + .opcode = cmd->opcode, + .size_in = cmd->info.size_in, + }; +int rc; + +if (cmd->info.size_out) { + mbox_cmd.payload_out =kvzalloc (cmd->info.size_out,GFP_KERNEL ); +if (!mbox_cmd.payload_out) +return -ENOMEM ; + } + +if (cmd->info.size_in) { + mbox_cmd.payload_in =kvzalloc (cmd->info.size_in,GFP_KERNEL ); +if (!mbox_cmd.payload_in) { + rc = -ENOMEM ; + goto out; + } + +if (copy_from_user (mbox_cmd.payload_in, +u64_to_user_ptr (in_payload), + cmd->info.size_in)) { + rc = -EFAULT ; + goto out; + } + } + + rc =cxl_mem_mbox_get (cxlm); +if (rc) + goto out; + +dev_dbg (dev, +"Submitting %s command for user\n" +"\topcode: %x\n" +"\tsize: %ub\n" , + cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, + cmd->info.size_in); +drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 438
Quoted 10 lines
drivers/cxl/mem.c +10
-0+ rc =__cxl_mem_mbox_send_cmd (cxlm, &mbox_cmd); +cxl_mem_mbox_put (cxlm); +if (rc) + goto out; + +/* +* @size_out contains the max size that's allowed to be written back out +* to userspace. While the payload may have written more output than +* this it will have to be ignored. +*/ See below for why I don't think this works. The size of mbox_cmd.payload_out seems to always be the size userspace specified, never the 1MB this code is assuming. So if the hardware returns more than userspace asks for you have a buffer overrun.
Quoted 125 lines
drivers/cxl/mem.c +125
-0+if (mbox_cmd.size_out) { +if (copy_to_user (u64_to_user_ptr (out_payload), + mbox_cmd.payload_out, *size_out)) { + rc = -EFAULT ; + goto out; + } + } + +/* +* Reporting the actual size, even if it was greater than @size_out +* allows userspace to try the command again with a bigger buffer. +*/ + *size_out = mbox_cmd.size_out; + *retval = mbox_cmd.return_code; + +out: +kvfree (mbox_cmd.payload_in); +kvfree (mbox_cmd.payload_out); +return rc; +} + +/** +* cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. +* @cxlm: &struct cxl_mem device whose mailbox will be used. +* @send_cmd: &struct cxl_send_command copied in from userspace. +* @out_cmd: Sanitized and populated &struct cxl_mem_command. +* +* Return: +* * %0 - @out_cmd is ready to send. +* * %-ENOTTY - Invalid command specified. +* * %-EINVAL - Reserved fields or invalid values were used. +* * %-ENOMEM - Input or output buffer wasn't sized properly. +* +* The result of this command is a fully validated command in @out_cmd that is +* safe to send to the hardware. +* +* See handle_mailbox_cmd_from_user() +*/ +static int cxl_validate_cmd_from_user (struct cxl_mem *cxlm, +const struct cxl_send_command *send_cmd, +struct cxl_mem_command *out_cmd) +{ +const struct cxl_command_info *info; +struct cxl_mem_command *c; + +if (send_cmd->id ==0 || send_cmd->id >=CXL_MEM_COMMAND_ID_MAX ) +return -ENOTTY ; + +/* +* The user can never specify an input payload larger than what hardware +* supports, but output can be arbitrarily large (simply write out as +* much data as the hardware provides). +*/ +if (send_cmd->in.size > cxlm->payload_size) +return -EINVAL ; + +if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK ) +return -EINVAL ; + +if (send_cmd->rsvd) +return -EINVAL ; + +if (send_cmd->in.rsvd || send_cmd->out.rsvd) +return -EINVAL ; + +/* Convert user's command into the internal representation */ + c = &mem_commands[send_cmd->id]; + info = &c->info; + +/* Check the input buffer is the expected size */ +if (info->size_in >=0 && info->size_in != send_cmd->in.size) +return -ENOMEM ; + +/* Check the output buffer is at least large enough */ +if (info->size_out >=0 && send_cmd->out.size < info->size_out) +return -ENOMEM ; + +memcpy (out_cmd, c,sizeof (*c)); + out_cmd->info.size_in = send_cmd->in.size; + out_cmd->info.size_out = send_cmd->out.size; + +return 0 ; +} + +static int cxl_query_cmd (struct cxl_memdev *cxlmd, +struct cxl_mem_query_commands __user *q) +{ +struct device *dev = &cxlmd->dev; +struct cxl_mem_command *cmd; +u32 n_commands; +int j =0 ; + +dev_dbg (dev,"Query IOCTL\n" ); + +if (get_user (n_commands, &q->n_commands)) +return -EFAULT ; + +/* returns the total number if 0 elements are requested. */ +if (n_commands ==0 ) +return put_user (cxl_cmd_count, &q->n_commands); + +/* +* otherwise, return max(n_commands, total commands) cxl_command_info +* structures. +*/ +cxl_for_each_cmd (cmd) { +const struct cxl_command_info *info = &cmd->info; + +if (copy_to_user (&q->commands[j++], info,sizeof (*info))) +return -EFAULT ; + +if (j == n_commands) +break ; + } + +return 0 ; +} + +static int cxl_send_cmd (struct cxl_memdev *cxlmd, +struct cxl_send_command __user *s) +{ +struct cxl_mem *cxlm = cxlmd->cxlm; +struct device *dev = &cxlmd->dev; +struct cxl_send_command send; +struct cxl_mem_command c;drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 573
Quoted 10 lines
drivers/cxl/mem.c +10
-0+int rc; + +dev_dbg (dev,"Send IOCTL\n" ); + +if (copy_from_user (&send, s,sizeof (send))) +return -EFAULT ; + + rc =cxl_validate_cmd_from_user (cxlmd->cxlm, &send, &c); +if (rc) +return rc;Userspace will pass in send.out set to the size of it's available buffer. Then
cxl_validate_cmd_from_user()will fill
c.info.size_out with send.out.sizeQuoted 7 lines
drivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 576
Quoted 3 lines
drivers/cxl/mem.c +3
-0+ +/* Prepare to handle a full payload for variable sized output */ +if (c.info.size_out <0 )So this check only works if userspace set the command to have variable size. That's not what the docs below suggest should happen.
Another good catch. This bug was introduced after the last change was made. I still don't want to have a
size_outas part of the mailbox command.Validate should not alter the
size_outfield except in the case of RAW commands.I believe this is the right fix,
handle_mailbox_cmd_from_user()already will onlycopy_tothe right number of byte, but your eyes on it are appreciated.drivers/cxl/mem.c +0
-0@@ -686,7 +686,11 @@ static int cxl_validate_cmd_from_user(struct cxl_mem *cxlm,memcpy (out_cmd, c,sizeof (*c));drivers/cxl/mem.c static int cxl_validate_cmd_from_user(struct cxl_mem *cxlm, · line 696
Quoted 10 lines
drivers/cxl/mem.c +5
-1out_cmd->info.size_in = send_cmd->in.size; - out_cmd->info.size_out = send_cmd->out.size; +/* +* XXX: out_cmd->info.size_out will be controlled by the driver, and the +* specified number of bytes @send_cmd->out.size will be copied back out +* to userspace. +*/ return 0 ; }This deals with the buffer overflow being triggered from userspace.
Quoted 7 lines
I'm still nervous. I really don't like assuming hardware will do the right thing and never send us more data than we expect.
Given the check that it will fit in the target buffer is simple, I'd prefer to harden it and know we can't have a problem.
Jonathan
I'm working on hardening __cxl_mem_mbox_send_cmd now per your request. With that, I think this solves the issue, right?
Quoted 208 lines
Quoted 207 lines
Quoted 207 lines
Quoted 79 lines
drivers/cxl/mem.c +25
-1+ c.info.size_out = cxlm->payload_size; + + rc =handle_mailbox_cmd_from_user (cxlm, &c, send.in.payload, + send.out.payload, &send.out.size, + &send.retval); +if (rc) +return rc; + +return copy_to_user (s, &send,sizeof (send)); +} + +static long __cxl_memdev_ioctl (struct cxl_memdev *cxlmd,unsigned int cmd, +unsigned long arg) +{ +switch (cmd) { +case CXL_MEM_QUERY_COMMANDS : +return cxl_query_cmd (cxlmd, (void __user *)arg); +case CXL_MEM_SEND_COMMAND : +return cxl_send_cmd (cxlmd, (void __user *)arg); +default : +return -ENOTTY ; + } +} +static long cxl_memdev_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {@@ -325,7 +611,7 @@ static long cxl_memdev_ioctl(struct file *file, unsigned int cmd,if (!percpu_ref_tryget_live (&cxlmd->ops_active))return -ENXIO ; -/* TODO: ioctl body */ + rc =__cxl_memdev_ioctl (cxlmd, cmd, arg);percpu_ref_put (&cxlmd->ops_active);include/uapi/linux/cxl_mem.h new file +34
-0@@ -0,0 +1,154 @@ +/* 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_ + +#include <linux/types.h> + +/** +* DOC: UAPI +* +* Not all of all commands that the driver supports are always available for use +* by userspace. Userspace must check the results from the QUERY command in +* order to determine the live set of commands. +*/ + +#define CXL_MEM_QUERY_COMMANDS _IOR(0xCE, 1, struct cxl_mem_query_commands) +#define CXL_MEM_SEND_COMMAND _IOWR(0xCE, 2, struct cxl_send_command) + +#define CXL_CMDS \ + ___C(INVALID, "Invalid Command"), \ + ___C(IDENTIFY, "Identify Command"), \ + ___C(MAX, "invalid / last command") + +#define ___C (a, b) CXL_MEM_COMMAND_ID_##a +enum {CXL_CMDS }; + +#undef ___C +#define ___C (a, b) { b } +static const struct { +const char *name; +} cxl_command_names[] = {CXL_CMDS };include/uapi/linux/cxl_mem.h line 44
Quoted 10 lines
include/uapi/linux/cxl_mem.h new file +10
-0+ +/* +* Here's how this actually breaks out: +* cxl_command_names[] = { +* [CXL_MEM_COMMAND_ID_INVALID] = { "Invalid Command" }, +* [CXL_MEM_COMMAND_ID_IDENTIFY] = { "Identify Command" }, +* ... +* [CXL_MEM_COMMAND_ID_MAX] = { "invalid / last command" }, +* }; +*/ Thanks, this is great.
Quoted 52 lines
include/uapi/linux/cxl_mem.h new file +52
-0+ +#undef ___C + +/** +* struct cxl_command_info - Command information returned from a query. +* @id: ID number for the command. +* @flags: Flags that specify command behavior. +* @size_in: Expected input size, or -1 if variable length. +* @size_out: Expected output size, or -1 if variable length. +* +* Represents a single command that is supported by both the driver and the +* hardware. This is returned as part of an array from the query ioctl. The +* following would be a command that takes a variable length input and returns 0 +* bytes of output. +* +* - @id = 10 +* - @flags = 0 +* - @size_in = -1 +* - @size_out = 0 +* +* See struct cxl_mem_query_commands. +*/ +struct cxl_command_info { +__u32 id; + +__u32 flags; +#define CXL_MEM_COMMAND_FLAG_MASK GENMASK(0, 0) + +__s32 size_in; +__s32 size_out; +}; + +/** +* 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. include/uapi/linux/cxl_mem.h line 106
Quoted 10 lines
include/uapi/linux/cxl_mem.h new file +10
-0+*/ +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 +*/ This is fairly well described in the docs above the structure. Perhaps combine the two.
Quoted 48 lines
include/uapi/linux/cxl_mem.h new file +48
-0+__u32 n_commands; +__u32 rsvd; + + 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 (input). +* @rsvd: Must be zero. +* @retval: Return value from the memory device (output). +* @in.size: Size of the payload to provide to the device (input). +* @in.rsvd: Must be zero. +* @in.payload: Pointer to memory for payload input, payload is little endian. +* @out.size: Size of the payload received from the device (input/output). This +* field is filled in by 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. +* @out.rsvd: Must be zero. +* @out.payload: Pointer to memory for payload output, payload is little endian. +* +* Mechanism for userspace to send a command to the hardware for processing. The +* driver will do basic validation on the command sizes. In some cases even the +* payload may be introspected. Userspace is required to allocate large enough +* buffers for size_out which can be variable length in certain situations. +*/ +struct cxl_send_command { +__u32 id; +__u32 flags; +__u32 rsvd; +__u32 retval; + +struct { +__s32 size; +__u32 rsvd; +__u64 payload; + } in; + +struct { +__s32 size; +__u32 rsvd; +__u64 payload; + } out; +}; + +#endif
Re: [PATCH v4 4/9] cxl/mem: Add basic IOCTL interface
On Tue, 16 Feb 2021 10:34:32 -0800
Ben Widawsky <[email protected]> wrote:
...
Quoted 23 lines
Quoted 23 lines
Quoted 7 lines
drivers/cxl/mem.c +0
-0@@ -686,7 +686,11 @@ static int cxl_validate_cmd_from_user(struct cxl_mem *cxlm,memcpy (out_cmd, c,sizeof (*c));drivers/cxl/mem.c static int cxl_validate_cmd_from_user(struct cxl_mem *cxlm, · line 696
Quoted 10 lines
drivers/cxl/mem.c +5
-1out_cmd->info.size_in = send_cmd->in.size; - out_cmd->info.size_out = send_cmd->out.size; +/* +* XXX: out_cmd->info.size_out will be controlled by the driver, and the +* specified number of bytes @send_cmd->out.size will be copied back out +* to userspace. +*/ return 0 ; }This deals with the buffer overflow being triggered from userspace.
I'm still nervous. I really don't like assuming hardware will do the right thing and never send us more data than we expect.
Quoted 7 lines
Quoted 4 lines
Given the check that it will fit in the target buffer is simple, I'd prefer to harden it and know we can't have a problem.
Jonathan
I'm working on hardening
__cxl_mem_mbox_send_cmdnow per your request. With that, I think this solves the issue, right?
Should do. Thanks,
Jonathan
Re: [PATCH v4 4/9] cxl/mem: Add basic IOCTL interface
In reply to Ben Widawskydrivers/cxl/mem.c static void cxl_mem_mbox_put(struct cxl_mem *cxlm) · line 415
Quoted 13 lines · Ben Widawsky
drivers/cxl/mem.c +13
-0+if (cmd->info.size_in) { + mbox_cmd.payload_in =kvzalloc (cmd->info.size_in,GFP_KERNEL ); +if (!mbox_cmd.payload_in) { + rc = -ENOMEM ; + goto out; + } + +if (copy_from_user (mbox_cmd.payload_in, +u64_to_user_ptr (in_payload), + cmd->info.size_in)) { + rc = -EFAULT ; + goto out; + }
Umm... Do you need to open-code vmemdup_user()? The only difference is GFP_KERNEL allocation instead of GFP_USER one, and the latter is arguably saner here... Zeroing is definitely pointless - you either overwrite the entire buffer with copy_from_user(), or you fail and free the damn thing.
Re: [PATCH v4 4/9] cxl/mem: Add basic IOCTL interface
Quoted 12 lines · Al Viro
Quoted 11 lines · Ben Widawsky
drivers/cxl/mem.c +11
-0+if (cmd->info.size_in) { + mbox_cmd.payload_in =kvzalloc (cmd->info.size_in,GFP_KERNEL ); +if (!mbox_cmd.payload_in) { + rc = -ENOMEM ; + goto out; + } + +if (copy_from_user (mbox_cmd.payload_in, +u64_to_user_ptr (in_payload), + cmd->info.size_in)) { + rc = -EFAULT ;
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ goto out; + }Umm... Do you need to open-code
vmemdup_user()? The only difference isGFP_KERNELallocation instead ofGFP_USERone, and the latter is arguably saner here... Zeroing is definitely pointless - you either overwrite the entire buffer withcopy_from_user(), or you fail and free the damn thing.
mea culpa. In fact it was previously memdup_user and Dan suggested I switch to vmemdup_user.
https://lore.kernel.org/linux-cxl/CAPcyv4j+ixVgEo5q2OhV4kdkBZbnohZj3KDovReQJjPBsREugw@mail.gmail.com/
Will fix for the next version.
Thanks.
PATCH v4 5/9 cxl/mem: Add a "RAW" send command
In reply to Ben WidawskyThe 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. The primary use for this command is to [begrudgingly] allow undocumented vendor specific hardware commands.
While not the main motivation, it also allows prototyping new hardware commands without a driver patch and rebuild.
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.
With this comes new debugfs knob to allow full access to your toes with your weapon of choice.
--- drivers/cxl/Kconfig | 18 +++++ drivers/cxl/mem.c | 132 +++++++++++++++++++++++++++++++++++ include/uapi/linux/cxl_mem.h | 12 +++- 3 files changed, 161 insertions(+), 1 deletion(-)
drivers/cxl/Kconfig +18 -0
@@ -32,4 +32,22 @@ config CXL_MEM Chapter 2.3 Type 3 CXL Device in the CXL 2.0 specification. If unsure say 'm'. + +config CXL_MEM_RAW_COMMANDS + bool "RAW Command Interface for Memory Devices" + depends on CXL_MEM + help + Enable CXL RAW command interface. + + The CXL driver ioctl interface may assign a kernel ioctl command + number for each specification defined opcode. At any given point in + time the number of opcodes that the specification defines and a device + may implement may exceed the kernel's set of associated ioctl function + numbers. The mismatch is either by omission, specification is too new, + or by design. When prototyping new hardware, or developing / debugging + the driver it is useful to be able to submit any possible command to + the hardware, even commands that may crash the kernel due to their + potential impact to memory currently in use by the kernel. + + If developing CXL hardware or the driver say Y, otherwise say N. endif
drivers/cxl/mem.c +132 -0
204 lines not on this page · raw message
include/uapi/linux/cxl_mem.h +11 -1
@@ -22,6 +22,7 @@#define CXL_CMDS \ ___C(INVALID, "Invalid Command"), \ ___C(IDENTIFY, "Identify Command"), \ + ___C(RAW, "Raw device command"), \ ___C(MAX, "invalid / last command")#define ___C (a, b) CXL_MEM_COMMAND_ID_##a
@@ -115,6 +116,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 (input). +* @raw: Special fields for raw commands +* @raw.opcode: Opcode passed to hardware when using the RAW command. +* @raw.rsvd: Must be zero. * @rsvd: Must be zero. * @retval: Return value from the memory device (output). * @in.size: Size of the payload to provide to the device (input).
@@ -135,7 +139,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 · 4 lines
2.30.1 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 5/9] cxl/mem: Add a "RAW" send command
Quoted 15 lines · On Mon, 15 Feb 2021 17:45:34 -0800 Ben Widawsky <[email protected]> wrote:
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. The primary use for this command is to [begrudgingly] allow undocumented vendor specific hardware commands.
While not the main motivation, it also allows prototyping new hardware commands without a driver patch and rebuild.
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.
Quoted 6 lines
With this comes new debugfs knob to allow full access to your toes with your weapon of choice.
Cc Ariel Sibley <[email protected]>Signed-off-by Ben Widawsky <[email protected]>Reviewed-by Dan Williams <[email protected]> (v2)
Whilst I'm definitely dubious about introducing this interface so early in development, I haven't found any problems with 'how' it has been done.
I guess it's now just up to us to hassle our hardware colleagues into only using this facility when absolutely necessary...
Quoted 279 lines
--- drivers/cxl/Kconfig | 18 +++++ drivers/cxl/mem.c | 132 +++++++++++++++++++++++++++++++++++ include/uapi/linux/cxl_mem.h | 12 +++- 3 files changed, 161 insertions(+), 1 deletion(-)drivers/cxl/Kconfig +18
-0@@ -32,4 +32,22 @@ config CXL_MEM Chapter 2.3 Type 3 CXL Device in the CXL 2.0 specification. If unsure say 'm'. + +config CXL_MEM_RAW_COMMANDS + bool "RAW Command Interface for Memory Devices" + depends on CXL_MEM + help + Enable CXL RAW command interface. + + The CXL driver ioctl interface may assign a kernel ioctl command + number for each specification defined opcode. At any given point in + time the number of opcodes that the specification defines and a device + may implement may exceed the kernel's set of associated ioctl function + numbers. The mismatch is either by omission, specification is too new, + or by design. When prototyping new hardware, or developing / debugging + the driver it is useful to be able to submit any possible command to + the hardware, even commands that may crash the kernel due to their + potential impact to memory currently in use by the kernel. + + If developing CXL hardware or the driver say Y, otherwise say N. endifdrivers/cxl/mem.c +132
-0@@ -1,6 +1,8 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ #include <uapi/linux/cxl_mem.h> +#include <linux/security.h> +#include <linux/debugfs.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/cdev.h> @@ -42,7 +44,14 @@enum opcode {CXL_MBOX_OP_INVALID =0x0000 , +CXL_MBOX_OP_RAW =CXL_MBOX_OP_INVALID , +CXL_MBOX_OP_ACTIVATE_FW =0x0202 ,CXL_MBOX_OP_IDENTIFY =0x4000 , +CXL_MBOX_OP_SET_PARTITION_INFO =0x4101 , +CXL_MBOX_OP_SET_LSA =0x4103 , +CXL_MBOX_OP_SET_SHUTDOWN_STATE =0x4204 , +CXL_MBOX_OP_SCAN_MEDIA =0x4304 , +CXL_MBOX_OP_GET_SCAN_MEDIA =0x4305 ,CXL_MBOX_OP_MAX =0x10000 };@@ -92,6 +101,8 @@ struct cxl_memdev {static int cxl_mem_major;static DEFINE_IDA (cxl_memdev_ida); +static struct dentry *cxl_debugfs; +static bool cxl_raw_allow_all;/** * struct cxl_mem_command - Driver representation of a memory device command @@ -128,6 +139,49 @@ struct cxl_mem_command {*/ static struct cxl_mem_command mem_commands[] = {CXL_CMD (IDENTIFY ,0 ,0x43 ), +#ifdef CONFIG_CXL_MEM_RAW_COMMANDS +CXL_CMD (RAW , ~0 , ~0 ), +#endif +}; + +/* +* Commands that RAW doesn't permit. The rationale for each: +* +* CXL_MBOX_OP_ACTIVATE_FW: Firmware activation requires adjustment / +* coordination of transaction timeout values at the root bridge level. +* +* CXL_MBOX_OP_SET_PARTITION_INFO: The device memory map may change live +* and needs to be coordinated with HDM updates. +* +* CXL_MBOX_OP_SET_LSA: The label storage area may be cached by the +* driver and any writes from userspace invalidates those contents. +* +* CXL_MBOX_OP_SET_SHUTDOWN_STATE: Set shutdown state assumes no writes +* to the device after it is marked clean, userspace can not make that +* assertion. +* +* CXL_MBOX_OP_[GET_]SCAN_MEDIA: The kernel provides a native error list that +* is kept up to date with patrol notifications and error management. +*/ +static u16 cxl_disabled_raw_commands[] = { +CXL_MBOX_OP_ACTIVATE_FW , +CXL_MBOX_OP_SET_PARTITION_INFO , +CXL_MBOX_OP_SET_LSA , +CXL_MBOX_OP_SET_SHUTDOWN_STATE , +CXL_MBOX_OP_SCAN_MEDIA , +CXL_MBOX_OP_GET_SCAN_MEDIA , +}; + +/* +* Command sets that RAW doesn't permit. All opcodes in this set are +* disabled because they pass plain text security payloads over the +* user/kernel boundary. This functionality is intended to be wrapped +* behind the keys ABI which allows for encrypted payloads in the UAPI +*/ +static u8 security_command_sets[] = { +0x44 ,/* Sanitize */ +0x45 ,/* Persistent Memory Data-at-rest Security */ +0x46 ,/* Security Passthrough */ };#define cxl_for_each_cmd (cmd) \@@ -158,6 +212,16 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)return 0 ; } +static bool cxl_is_security_command (u16 opcode) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (security_command_sets); i++) +if (security_command_sets[i] == (opcode >>8 )) +return true; +return false; +} +static void cxl_mem_mbox_timeout (struct cxl_mem *cxlm,struct mbox_cmd *mbox_cmd) {@@ -426,6 +490,9 @@ static int handle_mailbox_cmd_from_user(struct cxl_mem *cxlm, cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, cmd->info.size_in); +dev_WARN_ONCE (dev, cmd->info.id ==CXL_MEM_COMMAND_ID_RAW , +"raw command path used\n" ); + rc =__cxl_mem_mbox_send_cmd (cxlm, &mbox_cmd);cxl_mem_mbox_put (cxlm);if (rc)@@ -457,6 +524,29 @@ static int handle_mailbox_cmd_from_user(struct cxl_mem *cxlm,return rc; } +static bool cxl_mem_raw_command_allowed (u16 opcode) +{ +int i; + +if (!IS_ENABLED (CONFIG_CXL_MEM_RAW_COMMANDS )) +return false; + +if (security_locked_down (LOCKDOWN_NONE )) +return false; + +if (cxl_raw_allow_all) +return true; + +if (cxl_is_security_command (opcode)) +return false; + +for (i =0 ; i <ARRAY_SIZE (cxl_disabled_raw_commands); i++) +if (cxl_disabled_raw_commands[i] == opcode) +return false; + +return true; +} +/** * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. * @cxlm: &struct cxl_mem device whose mailbox will be used. @@ -468,6 +558,7 @@ static int handle_mailbox_cmd_from_user(struct cxl_mem *cxlm,* * %-ENOTTY - Invalid command specified. * * %-EINVAL - Reserved fields or invalid values were used. * * %-ENOMEM - Input or output buffer wasn't sized properly. +* * %-EPERM - Attempted to use a protected command. * * The result of this command is a fully validated command in @out_cmd that is * safe to send to the hardware. @@ -492,6 +583,40 @@ static int cxl_validate_cmd_from_user(struct cxl_mem *cxlm,if (send_cmd->in.size > cxlm->payload_size)return -EINVAL ; +/* +* Checks are bypassed for raw commands but a WARN/taint will occur +* later in the callchain +*/ +if (send_cmd->id ==CXL_MEM_COMMAND_ID_RAW ) { +const struct cxl_mem_command temp = { + .info = { + .id =CXL_MEM_COMMAND_ID_RAW , + .flags =0 , + .size_in = send_cmd->in.size, + .size_out = send_cmd->out.size, + }, + .opcode = send_cmd->raw.opcode + }; + +if (send_cmd->raw.rsvd) +return -EINVAL ; + +/* +* Unlike supported commands, the output size of RAW commands +* gets passed along without further checking, so it must be +* validated here. +*/ +if (send_cmd->out.size > cxlm->payload_size) +return -EINVAL ; + +if (!cxl_mem_raw_command_allowed (send_cmd->raw.opcode)) +return -EPERM ; + +memcpy (out_cmd, &temp,sizeof (temp)); + +return 0 ; + } +if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK )return -EINVAL ;@@ -1153,6 +1278,7 @@ static struct pci_driver cxl_mem_driver = {static __init int cxl_mem_init (void ) { +struct dentry *mbox_debugfs;dev_t devt;int rc;@@ -1169,11 +1295,17 @@ static __init int cxl_mem_init(void)return rc; } + cxl_debugfs =debugfs_create_dir ("cxl" ,NULL ); + mbox_debugfs =debugfs_create_dir ("mbox" , cxl_debugfs); +debugfs_create_bool ("raw_allow_all" ,0600 , mbox_debugfs, + &cxl_raw_allow_all); +return 0 ; } static __exit void cxl_mem_exit(void) { +debugfs_remove_recursive (cxl_debugfs);pci_unregister_driver (&cxl_mem_driver);unregister_chrdev_region (MKDEV (cxl_mem_major,0 ),CXL_MEM_MAX_DEVS ); }include/uapi/linux/cxl_mem.h +4
-1@@ -22,6 +22,7 @@#define CXL_CMDS \ ___C(INVALID, "Invalid Command"), \ ___C(IDENTIFY, "Identify Command"), \ + ___C(RAW, "Raw device command"), \ ___C(MAX, "invalid / last command")#define ___C (a, b) CXL_MEM_COMMAND_ID_##a@@ -115,6 +116,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 (input). +* @raw: Special fields for raw commands +* @raw.opcode: Opcode passed to hardware when using the RAW command. +* @raw.rsvd: Must be zero. * @rsvd: Must be zero. * @retval: Return value from the memory device (output). * @in.size: Size of the payload to provide to the device (input). @@ -135,7 +139,13 @@ struct cxl_mem_query_commands {struct cxl_send_command {__u32 id;__u32 flags; -__u32 rsvd;
include/uapi/linux/cxl_mem.h struct cxl_mem_query_commands { · line 151
Quoted 10 lines
include/uapi/linux/cxl_mem.h +7
-0+union { +struct { +__u16 opcode; +__u16 rsvd; + } raw; +__u32 rsvd; + };__u32 retval;struct {
_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 5/9] cxl/mem: Add a "RAW" send command
Quoted 21 lines · Jonathan Cameron
Quoted 18 lines · On Mon, 15 Feb 2021 17:45:34 -0800 Ben Widawsky <[email protected]> wrote:
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. The primary use for this command is to [begrudgingly] allow undocumented vendor specific hardware commands.
While not the main motivation, it also allows prototyping new hardware commands without a driver patch and rebuild.
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.With this comes new debugfs knob to allow full access to your toes with your weapon of choice.
Quoted 8 lines
Quoted 3 lines
Cc Ariel Sibley <[email protected]>Signed-off-by Ben Widawsky <[email protected]>Reviewed-by Dan Williams <[email protected]> (v2)Whilst I'm definitely dubious about introducing this interface so early in development, I haven't found any problems with 'how' it has been done.
FWIW, it's already helpful for regression testing. ndctl/cxl(1) will make use of it for validating driver internals and our QEMU emulation. I don't mean to imply that's the only usage.
Quoted 2 lines
I guess it's now just up to us to hassle our hardware colleagues into only using this facility when absolutely necessary...
Yes. I think having distros not enable the Kconfig option is going to be the best path in the early days so they don't get used to having it available.
Quoted 293 lines
Reviewed-by Jonathan Cameron <[email protected]>Quoted 289 lines
--- drivers/cxl/Kconfig | 18 +++++ drivers/cxl/mem.c | 132 +++++++++++++++++++++++++++++++++++ include/uapi/linux/cxl_mem.h | 12 +++- 3 files changed, 161 insertions(+), 1 deletion(-)drivers/cxl/Kconfig +18
-0@@ -32,4 +32,22 @@ config CXL_MEM Chapter 2.3 Type 3 CXL Device in the CXL 2.0 specification. If unsure say 'm'. + +config CXL_MEM_RAW_COMMANDS + bool "RAW Command Interface for Memory Devices" + depends on CXL_MEM + help + Enable CXL RAW command interface. + + The CXL driver ioctl interface may assign a kernel ioctl command + number for each specification defined opcode. At any given point in + time the number of opcodes that the specification defines and a device + may implement may exceed the kernel's set of associated ioctl function + numbers. The mismatch is either by omission, specification is too new, + or by design. When prototyping new hardware, or developing / debugging + the driver it is useful to be able to submit any possible command to + the hardware, even commands that may crash the kernel due to their + potential impact to memory currently in use by the kernel. + + If developing CXL hardware or the driver say Y, otherwise say N. endifdrivers/cxl/mem.c +132
-0@@ -1,6 +1,8 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ #include <uapi/linux/cxl_mem.h> +#include <linux/security.h> +#include <linux/debugfs.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/cdev.h> @@ -42,7 +44,14 @@enum opcode {CXL_MBOX_OP_INVALID =0x0000 , +CXL_MBOX_OP_RAW =CXL_MBOX_OP_INVALID , +CXL_MBOX_OP_ACTIVATE_FW =0x0202 ,CXL_MBOX_OP_IDENTIFY =0x4000 , +CXL_MBOX_OP_SET_PARTITION_INFO =0x4101 , +CXL_MBOX_OP_SET_LSA =0x4103 , +CXL_MBOX_OP_SET_SHUTDOWN_STATE =0x4204 , +CXL_MBOX_OP_SCAN_MEDIA =0x4304 , +CXL_MBOX_OP_GET_SCAN_MEDIA =0x4305 ,CXL_MBOX_OP_MAX =0x10000 };@@ -92,6 +101,8 @@ struct cxl_memdev {static int cxl_mem_major;static DEFINE_IDA (cxl_memdev_ida); +static struct dentry *cxl_debugfs; +static bool cxl_raw_allow_all;/** * struct cxl_mem_command - Driver representation of a memory device command @@ -128,6 +139,49 @@ struct cxl_mem_command {*/ static struct cxl_mem_command mem_commands[] = {CXL_CMD (IDENTIFY ,0 ,0x43 ), +#ifdef CONFIG_CXL_MEM_RAW_COMMANDS +CXL_CMD (RAW , ~0 , ~0 ), +#endif +}; + +/* +* Commands that RAW doesn't permit. The rationale for each: +* +* CXL_MBOX_OP_ACTIVATE_FW: Firmware activation requires adjustment / +* coordination of transaction timeout values at the root bridge level. +* +* CXL_MBOX_OP_SET_PARTITION_INFO: The device memory map may change live +* and needs to be coordinated with HDM updates. +* +* CXL_MBOX_OP_SET_LSA: The label storage area may be cached by the +* driver and any writes from userspace invalidates those contents. +* +* CXL_MBOX_OP_SET_SHUTDOWN_STATE: Set shutdown state assumes no writes +* to the device after it is marked clean, userspace can not make that +* assertion. +* +* CXL_MBOX_OP_[GET_]SCAN_MEDIA: The kernel provides a native error list that +* is kept up to date with patrol notifications and error management. +*/ +static u16 cxl_disabled_raw_commands[] = { +CXL_MBOX_OP_ACTIVATE_FW , +CXL_MBOX_OP_SET_PARTITION_INFO , +CXL_MBOX_OP_SET_LSA , +CXL_MBOX_OP_SET_SHUTDOWN_STATE , +CXL_MBOX_OP_SCAN_MEDIA , +CXL_MBOX_OP_GET_SCAN_MEDIA , +}; + +/* +* Command sets that RAW doesn't permit. All opcodes in this set are +* disabled because they pass plain text security payloads over the +* user/kernel boundary. This functionality is intended to be wrapped +* behind the keys ABI which allows for encrypted payloads in the UAPI +*/ +static u8 security_command_sets[] = { +0x44 ,/* Sanitize */ +0x45 ,/* Persistent Memory Data-at-rest Security */ +0x46 ,/* Security Passthrough */ };#define cxl_for_each_cmd (cmd) \@@ -158,6 +212,16 @@ static int cxl_mem_wait_for_doorbell(struct cxl_mem *cxlm)return 0 ; } +static bool cxl_is_security_command (u16 opcode) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (security_command_sets); i++) +if (security_command_sets[i] == (opcode >>8 )) +return true; +return false; +} +static void cxl_mem_mbox_timeout (struct cxl_mem *cxlm,struct mbox_cmd *mbox_cmd) {@@ -426,6 +490,9 @@ static int handle_mailbox_cmd_from_user(struct cxl_mem *cxlm, cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, cmd->info.size_in); +dev_WARN_ONCE (dev, cmd->info.id ==CXL_MEM_COMMAND_ID_RAW , +"raw command path used\n" ); + rc =__cxl_mem_mbox_send_cmd (cxlm, &mbox_cmd);cxl_mem_mbox_put (cxlm);if (rc)@@ -457,6 +524,29 @@ static int handle_mailbox_cmd_from_user(struct cxl_mem *cxlm,return rc; } +static bool cxl_mem_raw_command_allowed (u16 opcode) +{ +int i; + +if (!IS_ENABLED (CONFIG_CXL_MEM_RAW_COMMANDS )) +return false; + +if (security_locked_down (LOCKDOWN_NONE )) +return false; + +if (cxl_raw_allow_all) +return true; + +if (cxl_is_security_command (opcode)) +return false; + +for (i =0 ; i <ARRAY_SIZE (cxl_disabled_raw_commands); i++) +if (cxl_disabled_raw_commands[i] == opcode) +return false; + +return true; +} +/** * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. * @cxlm: &struct cxl_mem device whose mailbox will be used. @@ -468,6 +558,7 @@ static int handle_mailbox_cmd_from_user(struct cxl_mem *cxlm,* * %-ENOTTY - Invalid command specified. * * %-EINVAL - Reserved fields or invalid values were used. * * %-ENOMEM - Input or output buffer wasn't sized properly. +* * %-EPERM - Attempted to use a protected command. * * The result of this command is a fully validated command in @out_cmd that is * safe to send to the hardware. @@ -492,6 +583,40 @@ static int cxl_validate_cmd_from_user(struct cxl_mem *cxlm,if (send_cmd->in.size > cxlm->payload_size)return -EINVAL ; +/* +* Checks are bypassed for raw commands but a WARN/taint will occur +* later in the callchain +*/ +if (send_cmd->id ==CXL_MEM_COMMAND_ID_RAW ) { +const struct cxl_mem_command temp = { + .info = { + .id =CXL_MEM_COMMAND_ID_RAW , + .flags =0 , + .size_in = send_cmd->in.size, + .size_out = send_cmd->out.size, + }, + .opcode = send_cmd->raw.opcode + }; + +if (send_cmd->raw.rsvd) +return -EINVAL ; + +/* +* Unlike supported commands, the output size of RAW commands +* gets passed along without further checking, so it must be +* validated here. +*/ +if (send_cmd->out.size > cxlm->payload_size) +return -EINVAL ; + +if (!cxl_mem_raw_command_allowed (send_cmd->raw.opcode)) +return -EPERM ; + +memcpy (out_cmd, &temp,sizeof (temp)); + +return 0 ; + } +if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK )return -EINVAL ;@@ -1153,6 +1278,7 @@ static struct pci_driver cxl_mem_driver = {static __init int cxl_mem_init (void ) { +struct dentry *mbox_debugfs;dev_t devt;int rc;@@ -1169,11 +1295,17 @@ static __init int cxl_mem_init(void)return rc; } + cxl_debugfs =debugfs_create_dir ("cxl" ,NULL ); + mbox_debugfs =debugfs_create_dir ("mbox" , cxl_debugfs); +debugfs_create_bool ("raw_allow_all" ,0600 , mbox_debugfs, + &cxl_raw_allow_all); +return 0 ; } static __exit void cxl_mem_exit(void) { +debugfs_remove_recursive (cxl_debugfs);pci_unregister_driver (&cxl_mem_driver);unregister_chrdev_region (MKDEV (cxl_mem_major,0 ),CXL_MEM_MAX_DEVS ); }include/uapi/linux/cxl_mem.h +11
-1@@ -22,6 +22,7 @@#define CXL_CMDS \ ___C(INVALID, "Invalid Command"), \ ___C(IDENTIFY, "Identify Command"), \ + ___C(RAW, "Raw device command"), \ ___C(MAX, "invalid / last command")#define ___C (a, b) CXL_MEM_COMMAND_ID_##a@@ -115,6 +116,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 (input). +* @raw: Special fields for raw commands +* @raw.opcode: Opcode passed to hardware when using the RAW command. +* @raw.rsvd: Must be zero. * @rsvd: Must be zero. * @retval: Return value from the memory device (output). * @in.size: Size of the payload to provide to the device (input). @@ -135,7 +139,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 {
PATCH v4 6/9 cxl/mem: Enable commands via CEL
In reply to Ben WidawskyCXL devices identified by the memory-device class code must implement the Device Command Interface (described in 8.2.9 of the CXL 2.0 spec). While the driver already maintains a list of commands it supports, there is still a need to be able to distinguish between commands that the driver knows about from commands that are optionally supported by the hardware.
The Command Effects Log (CEL) is specified in the CXL 2.0 specification. The CEL is one of two types of logs, the other being vendor specific. They are distinguished in hardware/spec via UUID. The CEL is useful for 2 things:
1. Determine which optional commands are supported by the CXL device.
2. Enumerate any vendor specific commands
The CEL is used by the driver to determine which commands are available in the hardware and therefore which commands userspace is allowed to execute. The set of enabled commands might be a subset of commands which are advertised in UAPI via CXL_MEM_SEND_COMMAND IOCTL.
With the CEL enabling comes a internal flag to indicate a base set of commands that are enabled regardless of CEL. Such commands are required for basic interaction with the hardware and thus can be useful in debug cases, for example if the CEL is corrupted.
The implementation leaves the statically defined table of commands and supplements it with a bitmap to determine commands that are enabled. This organization was chosen for the following reasons:
- Smaller memory footprint. Doesn't need a table per device.
- Reduce memory allocation complexity.
- Fixed command IDs to opcode mapping for all devices makes development and debugging easier.
- Certain helpers are easily achievable, like cxl_for_each_cmd().
--- drivers/cxl/cxl.h | 2 + drivers/cxl/mem.c | 213 ++++++++++++++++++++++++++++++++++- include/uapi/linux/cxl_mem.h | 1 + 3 files changed, 213 insertions(+), 3 deletions(-)
drivers/cxl/cxl.h +2 -0
@@ -67,6 +67,7 @@ struct cxl_memdev;* (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register) * @mbox_mutex: Mutex to synchronize mailbox access. * @firmware_version: Firmware version for the memory device. +* @enabled_commands: Hardware commands found enabled in CEL. * @pmem: Persistent memory capacity information. * @ram: Volatile memory capacity information. */
@@ -82,6 +83,7 @@ struct cxl_mem {size_t payload_size;struct mutex mbox_mutex;/* Protects device mailbox and firmware */ char firmware_version[0x10 ]; +unsigned long *enabled_cmds;struct range pmem_range;struct range ram_range;
drivers/cxl/mem.c +210 -3
275 lines not on this page · raw message
include/uapi/linux/cxl_mem.h +1 -0
@@ -23,6 +23,7 @@___C (INVALID ,"Invalid Command" ), \___C (IDENTIFY ,"Identify Command" ), \___C (RAW ,"Raw device command" ), \ +___C (GET_SUPPORTED_LOGS ,"Get Supported Logs" ), \ ___C(MAX, "invalid / last command")#define ___C (a, b) CXL_MEM_COMMAND_ID_##a
Signature · 4 lines
2.30.1 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 6/9] cxl/mem: Enable commands via CEL
Quoted 33 lines · On Mon, 15 Feb 2021 17:45:35 -0800 Ben Widawsky <[email protected]> wrote:
CXL devices identified by the memory-device class code must implement the Device Command Interface (described in 8.2.9 of the CXL 2.0 spec). While the driver already maintains a list of commands it supports, there is still a need to be able to distinguish between commands that the driver knows about from commands that are optionally supported by the hardware.
The Command Effects Log (CEL) is specified in the CXL 2.0 specification. The CEL is one of two types of logs, the other being vendor specific. They are distinguished in hardware/spec via UUID. The CEL is useful for 2 things:
1. Determine which optional commands are supported by the CXL device.
2. Enumerate any vendor specific commandsThe CEL is used by the driver to determine which commands are available in the hardware and therefore which commands userspace is allowed to execute. The set of enabled commands might be a subset of commands which are advertised in UAPI via
CXL_MEM_SEND_COMMANDIOCTL.With the CEL enabling comes a internal flag to indicate a base set of commands that are enabled regardless of CEL. Such commands are required for basic interaction with the hardware and thus can be useful in debug cases, for example if the CEL is corrupted.
The implementation leaves the statically defined table of commands and supplements it with a bitmap to determine commands that are enabled. This organization was chosen for the following reasons:
- Smaller memory footprint. Doesn't need a table per device.
- Reduce memory allocation complexity.
- Fixed command IDs to opcode mapping for all devices makes development and debugging easier.
- Certain helpers are easily achievable, likecxl_for_each_cmd().
Quoted 2 lines
Signed-off-by Ben Widawsky <[email protected]>Reviewed-by Dan Williams <[email protected]>
A few trivial, now unnecessary casts following on from you changing the types of the buffer pointers passed to cxl_mem_mbox_send_cmd() to void *
Otherwise looks good to me.
Quoted 144 lines
--- drivers/cxl/cxl.h | 2 + drivers/cxl/mem.c | 213 ++++++++++++++++++++++++++++++++++- include/uapi/linux/cxl_mem.h | 1 + 3 files changed, 213 insertions(+), 3 deletions(-)drivers/cxl/cxl.h +2
-0@@ -67,6 +67,7 @@ struct cxl_memdev;* (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register) * @mbox_mutex: Mutex to synchronize mailbox access. * @firmware_version: Firmware version for the memory device. +* @enabled_commands: Hardware commands found enabled in CEL. * @pmem: Persistent memory capacity information. * @ram: Volatile memory capacity information. */ @@ -82,6 +83,7 @@ struct cxl_mem {size_t payload_size;struct mutex mbox_mutex;/* Protects device mailbox and firmware */ char firmware_version[0x10 ]; +unsigned long *enabled_cmds;struct range pmem_range;struct range ram_range;drivers/cxl/mem.c +50
-3@@ -46,6 +46,8 @@ enum opcode {CXL_MBOX_OP_INVALID =0x0000 ,CXL_MBOX_OP_RAW =CXL_MBOX_OP_INVALID ,CXL_MBOX_OP_ACTIVATE_FW =0x0202 , +CXL_MBOX_OP_GET_SUPPORTED_LOGS =0x0400 , +CXL_MBOX_OP_GET_LOG =0x0401 ,CXL_MBOX_OP_IDENTIFY =0x4000 ,CXL_MBOX_OP_SET_PARTITION_INFO =0x4101 , CXL_MBOX_OP_SET_LSA = 0x4103,@@ -104,10 +106,28 @@ static DEFINE_IDA(cxl_memdev_ida);static struct dentry *cxl_debugfs;static bool cxl_raw_allow_all; +enum { +CEL_UUID , +VENDOR_DEBUG_UUID , +}; + +/* See CXL 2.0 Table 170. Get Log Input Payload */ +static const uuid_t log_uuid[] = { + [CEL_UUID ] =UUID_INIT (0xda9c0b5 ,0xbf41 ,0x4b78 ,0x8f ,0x79 ,0x96 , +0xb1 ,0x62 ,0x3b ,0x3f ,0x17 ), + [VENDOR_DEBUG_UUID ] =UUID_INIT (0xe1819d9 ,0x11a9 ,0x400c ,0x81 ,0x1f , +0xd6 ,0x07 ,0x19 ,0x40 ,0x3d ,0x86 ), +}; +/** * 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 +* @flags: Set of flags effecting driver behavior. +* +* * %CXL_CMD_FLAG_FORCE_ENABLE: In cases of error, commands with this flag +* will be enabled by the driver regardless of what hardware may have +* advertised. * * 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 @@ -119,9 +139,12 @@ static bool cxl_raw_allow_all;struct cxl_mem_command {struct cxl_command_info info;enum opcode opcode; +u32 flags; +#define CXL_CMD_FLAG_NONE 0 +#define CXL_CMD_FLAG_FORCE_ENABLE BIT(0) }; -#define CXL_CMD (_id, sin, sout) \ +#define CXL_CMD (_id, sin, sout, _flags) \ [CXL_MEM_COMMAND_ID_##_id] = { \ .info = { \ .id = CXL_MEM_COMMAND_ID_##_id, \@@ -129,6 +152,7 @@ struct cxl_mem_command { .size_out = sout, \ }, \ .opcode = CXL_MBOX_OP_##_id, \ + .flags = _flags, \ }/* @@ -138,10 +162,11 @@ struct cxl_mem_command {* 0, and the user passed in 1, it is an error. */ static struct cxl_mem_command mem_commands[] = { -CXL_CMD (IDENTIFY ,0 ,0x43 ), +CXL_CMD (IDENTIFY ,0 ,0x43 ,CXL_CMD_FLAG_FORCE_ENABLE ), #ifdef CONFIG_CXL_MEM_RAW_COMMANDS -CXL_CMD (RAW , ~0 , ~0 ), +CXL_CMD (RAW , ~0 , ~0 ,0 ), #endif +CXL_CMD (GET_SUPPORTED_LOGS ,0 , ~0 ,CXL_CMD_FLAG_FORCE_ENABLE ), };/* @@ -630,6 +655,10 @@ static int cxl_validate_cmd_from_user(struct cxl_mem *cxlm, c = &mem_commands[send_cmd->id]; info = &c->info; +/* Check that the command is enabled for hardware */ +if (!test_bit (info->id, cxlm->enabled_cmds)) +return -ENOTTY ; +/* Check the input buffer is the expected size */ if (info->size_in >=0 && info->size_in != send_cmd->in.size)return -ENOMEM ;@@ -935,6 +964,14 @@ static struct cxl_mem *cxl_mem_create(struct pci_dev *pdev, u32 reg_lo,mutex_init (&cxlm->mbox_mutex); cxlm->pdev = pdev; cxlm->regs = regs + offset; + cxlm->enabled_cmds = +devm_kmalloc_array (dev,BITS_TO_LONGS (cxl_cmd_count), +sizeof (unsigned long ), +GFP_KERNEL | __GFP_ZERO); +if (!cxlm->enabled_cmds) { +dev_err (dev,"No memory available for bitmap\n" ); +return NULL ; + }dev_dbg (dev,"Mapped CXL Memory Device resource\n" );return cxlm;@@ -1154,6 +1191,172 @@ static int cxl_mem_add_memdev(struct cxl_mem *cxlm)return rc; } +static int cxl_xfer_log (struct cxl_mem *cxlm,uuid_t *uuid,u32 size,u8 *out) +{ +u32 remaining = size; +u32 offset =0 ; + +while (remaining) { +u32 xfer_size =min_t (u32, remaining, cxlm->payload_size); + struct cxl_mbox_get_log { + uuid_t uuid; + __le32 offset;
drivers/cxl/mem.c static int cxl_mem_add_memdev(struct cxl_mem *cxlm) · line 1213
Quoted 10 lines
drivers/cxl/mem.c +10
-0+__le32 length; + }__packed log = { + .uuid = *uuid, + .offset =cpu_to_le32 (offset), + .length =cpu_to_le32 (xfer_size) + }; +int rc; + + rc = cxl_mem_mbox_send_cmd(cxlm, CXL_MBOX_OP_GET_LOG, + (u8 *)&log, sizeof(log), out,
Shouldn't need that u8 * cast any more now parameter is void *
Quoted 67 lines
drivers/cxl/mem.c +67
-0+ xfer_size); +if (rc <0 ) +return rc; + + out += xfer_size; + remaining -= xfer_size; + offset += xfer_size; + } + +return 0 ; +} + +static inline struct cxl_mem_command *cxl_mem_find_command (u16 opcode) +{ +struct cxl_mem_command *c; + +cxl_for_each_cmd (c) +if (c->opcode == opcode) +return c; + +return NULL ; +} + +/** +* cxl_walk_cel() - Walk through the Command Effects Log. +* @cxlm: Device. +* @size: Length of the Command Effects Log. +* @cel: CEL +* +* Iterate over each entry in the CEL and determine if the driver supports the +* command. If so, the command is enabled for the device and can be used later. +*/ +static void cxl_walk_cel (struct cxl_mem *cxlm,size_t size,u8 *cel) +{ +struct cel_entry { +__le16 opcode; +__le16 effect; + } __packed * cel_entry; +const int cel_entries = size /sizeof (*cel_entry); +int i; + + cel_entry = (struct cel_entry *)cel; + +for (i =0 ; i < cel_entries; i++) { +u16 opcode =le16_to_cpu (cel_entry[i].opcode); +struct cxl_mem_command *cmd =cxl_mem_find_command (opcode); + +if (!cmd) { +dev_dbg (&cxlm->pdev->dev, +"Opcode 0x%04x unsupported by driver" , opcode); +continue ; + } + +set_bit (cmd->info.id, cxlm->enabled_cmds); + } +} + +struct cxl_mbox_get_supported_logs { +__le16 entries; +u8 rsvd[6 ]; +struct gsl_entry { +uuid_t uuid; +__le32 size; + } __packed entry[]; +} __packed; + +static struct cxl_mbox_get_supported_logs *cxl_get_gsl (struct cxl_mem *cxlm)
drivers/cxl/mem.c static int cxl_mem_add_memdev(struct cxl_mem *cxlm) · line 1290
Quoted 10 lines
drivers/cxl/mem.c +10
-0+{ +struct cxl_mbox_get_supported_logs *ret; +int rc; + + ret =kvmalloc (cxlm->payload_size,GFP_KERNEL ); +if (!ret) +return ERR_PTR (-ENOMEM ); + + rc =cxl_mem_mbox_send_cmd (cxlm,CXL_MBOX_OP_GET_SUPPORTED_LOGS ,NULL , +0 , (u8 *)ret,sizeof (*ret));
Parameter type for out is now void *, so should be no need to cast another type to it explicitly.
Quoted 46 lines
drivers/cxl/mem.c +46
-0+if (rc <0 ) { +kvfree (ret); +return ERR_PTR (rc); + } + +return ret; +} + +/** +* cxl_mem_enumerate_cmds() - Enumerate commands for a device. +* @cxlm: The device. +* +* Returns 0 if enumerate completed successfully. +* +* CXL devices have optional support for certain commands. This function will +* determine the set of supported commands for the hardware and update the +* enabled_cmds bitmap in the @cxlm. +*/ +static int cxl_mem_enumerate_cmds (struct cxl_mem *cxlm) +{ +struct cxl_mbox_get_supported_logs *gsl; +struct device *dev = &cxlm->pdev->dev; +struct cxl_mem_command *cmd; +int i, rc; + + gsl =cxl_get_gsl (cxlm); +if (IS_ERR (gsl)) +return PTR_ERR (gsl); + + rc = -ENOENT ; +for (i =0 ; i <le16_to_cpu (gsl->entries); i++) { +u32 size =le32_to_cpu (gsl->entry[i].size); +uuid_t uuid = gsl->entry[i].uuid; +u8 *log; + +dev_dbg (dev,"Found LOG type %pU of size %d" , &uuid, size); + +if (!uuid_equal (&uuid, &log_uuid[CEL_UUID ])) +continue ; + + log =kvmalloc (size,GFP_KERNEL ); +if (!log) { + rc = -ENOMEM ; + goto out; + } +
drivers/cxl/mem.c static int cxl_mem_add_memdev(struct cxl_mem *cxlm) · line 1346
Quoted 10 lines
drivers/cxl/mem.c +10
-0+ rc =cxl_xfer_log (cxlm, &uuid, size, log); +if (rc) { +kvfree (log); + goto out; + } + +cxl_walk_cel (cxlm, size, log); +kvfree (log); + +/* In case CEL was bogus, enable some default commands. */
Worth a warning if it is bogus?
drivers/cxl/mem.c static int cxl_mem_add_memdev(struct cxl_mem *cxlm) · line 1352
Quoted 6 lines
drivers/cxl/mem.c +6
-0+cxl_for_each_cmd (cmd) +if (cmd->flags &CXL_CMD_FLAG_FORCE_ENABLE ) +set_bit (cmd->info.id, cxlm->enabled_cmds); + +/* Found the required CEL */ + rc =0 ;
Is that not already set appropriately? I guess this kind of acts as documentation.
Quoted 26 lines
drivers/cxl/mem.c +11
-0+ } + +out: +kvfree (gsl); +return rc; +} +/** * cxl_mem_identify() - Send the IDENTIFY command to the device. * @cxlm: The device to identify. @@ -1253,6 +1456,10 @@ static int cxl_mem_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (rc)return rc; + rc =cxl_mem_enumerate_cmds (cxlm); +if (rc) +return rc; + rc =cxl_mem_identify (cxlm);if (rc)return rc;include/uapi/linux/cxl_mem.h +0
-0
include/uapi/linux/cxl_mem.h line 29
Quoted 7 lines
include/uapi/linux/cxl_mem.h +1
-0@@ -23,6 +23,7 @@___C (INVALID ,"Invalid Command" ), \___C (IDENTIFY ,"Identify Command" ), \___C (RAW ,"Raw device command" ), \ +___C (GET_SUPPORTED_LOGS ,"Get Supported Logs" ), \ ___C(MAX, "invalid / last command")#define ___C (a, b) CXL_MEM_COMMAND_ID_##a
_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
PATCH v4 7/9 cxl/mem: Add set of informational commands
In reply to Ben WidawskyAdd initial set of formal commands beyond basic identify and command enumeration.
--- drivers/cxl/mem.c | 9 +++++++++ include/uapi/linux/cxl_mem.h | 5 +++++ 2 files changed, 14 insertions(+)
drivers/cxl/mem.c +9 -0
@@ -45,12 +45,16 @@enum opcode {CXL_MBOX_OP_INVALID =0x0000 ,CXL_MBOX_OP_RAW =CXL_MBOX_OP_INVALID , +CXL_MBOX_OP_GET_FW_INFO =0x0200 ,CXL_MBOX_OP_ACTIVATE_FW =0x0202 ,CXL_MBOX_OP_GET_SUPPORTED_LOGS =0x0400 ,CXL_MBOX_OP_GET_LOG =0x0401 ,CXL_MBOX_OP_IDENTIFY =0x4000 , +CXL_MBOX_OP_GET_PARTITION_INFO =0x4100 ,CXL_MBOX_OP_SET_PARTITION_INFO =0x4101 , +CXL_MBOX_OP_GET_LSA =0x4102 ,CXL_MBOX_OP_SET_LSA =0x4103 , +CXL_MBOX_OP_GET_HEALTH_INFO =0x4200 ,CXL_MBOX_OP_SET_SHUTDOWN_STATE =0x4204 ,CXL_MBOX_OP_SCAN_MEDIA =0x4304 ,CXL_MBOX_OP_GET_SCAN_MEDIA =0x4305 ,
@@ -167,6 +171,11 @@ static struct cxl_mem_command mem_commands[] = {CXL_CMD (RAW , ~0 , ~0 ,0 ), #endifCXL_CMD (GET_SUPPORTED_LOGS ,0 , ~0 ,CXL_CMD_FLAG_FORCE_ENABLE ), +CXL_CMD (GET_FW_INFO ,0 ,0x50 ,0 ), +CXL_CMD (GET_PARTITION_INFO ,0 ,0x20 ,0 ), +CXL_CMD (GET_LSA ,0x8 , ~0 ,0 ), +CXL_CMD (GET_HEALTH_INFO ,0 ,0x12 ,0 ), + CXL_CMD(GET_LOG, 0x18, ~0, CXL_CMD_FLAG_FORCE_ENABLE), };/*
include/uapi/linux/cxl_mem.h +5 -0
@@ -24,6 +24,11 @@___C (IDENTIFY ,"Identify Command" ), \___C (RAW ,"Raw device command" ), \___C (GET_SUPPORTED_LOGS ,"Get Supported Logs" ), \ +___C (GET_FW_INFO ,"Get FW Info" ), \ +___C (GET_PARTITION_INFO ,"Get Partition Information" ), \ +___C (GET_LSA ,"Get Label Storage Area" ), \ +___C (GET_HEALTH_INFO ,"Get Health Info" ), \ +___C (GET_LOG ,"Get Log" ), \ ___C(MAX, "invalid / last command") #define ___C(a, b) CXL_MEM_COMMAND_ID_##a
Signature · 4 lines
2.30.1 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
PATCH v4 8/9 MAINTAINERS: Add maintainers of the CXL driver
In reply to Ben Widawsky--- MAINTAINERS | 11 +++++++++++ 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
@@ -4444,6 +4444,17 @@ M: Miguel Ojeda <[email protected]> S: Maintained F: include/linux/compiler_attributes.h +COMPUTE EXPRESS LINK (CXL) +M: Alison Schofield <[email protected]> +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 + CONEXANT ACCESSRUNNER USB DRIVER L: [email protected] S: Orphan
Signature · 4 lines
2.30.1 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
PATCH v4 9/9 cxl/mem: Add payload dumping for debug
In reply to Ben WidawskyIt's often useful in debug scenarios to see what the hardware has dumped out. As it stands today, any device error will result in the payload not being copied out, so there is no way to triage commands which weren't expected to fail (and sometimes the payload may have that information).
The functionality is protected by normal kernel security mechanisms as well as a CONFIG option in the CXL driver.
This was extracted from the original version of the CXL enabling patch series.
--- drivers/cxl/Kconfig | 13 +++++++++++++ drivers/cxl/mem.c | 8 ++++++++ 2 files changed, 21 insertions(+)
drivers/cxl/Kconfig +13 -0
@@ -50,4 +50,17 @@ config CXL_MEM_RAW_COMMANDS potential impact to memory currently in use by the kernel. If developing CXL hardware or the driver say Y, otherwise say N. + +config CXL_MEM_INSECURE_DEBUG + bool "CXL.mem debugging" + depends on CXL_MEM + help + Enable debug of all CXL command payloads. + + Some CXL devices and controllers support encryption and other + security features. The payloads for the commands that enable + those features may contain sensitive clear-text security + material. Disable debug of those command payloads by default. + If you are a kernel developer actively working on CXL + security enabling say Y, otherwise say N. endif
drivers/cxl/mem.c +8 -0
@@ -342,6 +342,14 @@ static int __cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm,/* #5 */ rc =cxl_mem_wait_for_doorbell (cxlm); + +if (!cxl_is_security_command (mbox_cmd->opcode) || +IS_ENABLED (CONFIG_CXL_MEM_INSECURE_DEBUG )) { +print_hex_dump_debug ("Payload " ,DUMP_PREFIX_OFFSET ,16 ,1 , + mbox_cmd->payload_in, mbox_cmd->size_in, + true); + } +if (rc == -ETIMEDOUT ) {cxl_mem_mbox_timeout (cxlm, mbox_cmd);return rc;
Signature · 4 lines
2.30.1 _______________________________________________ Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 9/9] cxl/mem: Add payload dumping for debug
Quoted 5 lines · On Mon, 15 Feb 2021 17:45:38 -0800 Ben Widawsky <[email protected]> wrote:
It's often useful in debug scenarios to see what the hardware has dumped out. As it stands today, any device error will result in the payload not being copied out, so there is no way to triage commands which weren't expected to fail (and sometimes the payload may have that information).
Quoted 7 lines
The functionality is protected by normal kernel security mechanisms as well as a CONFIG option in the CXL driver.
This was extracted from the original version of the CXL enabling patch series.
Signed-off-by Ben Widawsky <[email protected]>
My gut feeling here is use a tracepoint rather than spamming the kernel log. Alternatively just don't bother merging this patch - it's on the list now anyway so trivial for anyone doing such debug to pick it up.
Jonathan
Quoted 36 lines
--- drivers/cxl/Kconfig | 13 +++++++++++++ drivers/cxl/mem.c | 8 ++++++++ 2 files changed, 21 insertions(+)drivers/cxl/Kconfig +13
-0@@ -50,4 +50,17 @@ config CXL_MEM_RAW_COMMANDS potential impact to memory currently in use by the kernel. If developing CXL hardware or the driver say Y, otherwise say N. + +config CXL_MEM_INSECURE_DEBUG + bool "CXL.mem debugging" + depends on CXL_MEM + help + Enable debug of all CXL command payloads. + + Some CXL devices and controllers support encryption and other + security features. The payloads for the commands that enable + those features may contain sensitive clear-text security + material. Disable debug of those command payloads by default. + If you are a kernel developer actively working on CXL + security enabling say Y, otherwise say N. endifdrivers/cxl/mem.c +1
-0@@ -342,6 +342,14 @@ static int __cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm,/* #5 */ rc =cxl_mem_wait_for_doorbell (cxlm); +
drivers/cxl/mem.c static int __cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm, · line 355
Quoted 10 lines
drivers/cxl/mem.c +7
-0+if (!cxl_is_security_command (mbox_cmd->opcode) || +IS_ENABLED (CONFIG_CXL_MEM_INSECURE_DEBUG )) { +print_hex_dump_debug ("Payload " ,DUMP_PREFIX_OFFSET ,16 ,1 , + mbox_cmd->payload_in, mbox_cmd->size_in, + true); + } +if (rc == -ETIMEDOUT ) {cxl_mem_mbox_timeout (cxlm, mbox_cmd);return rc;
_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: [PATCH v4 9/9] cxl/mem: Add payload dumping for debug
Quoted 12 lines · Jonathan Cameron
Quoted 8 lines · On Mon, 15 Feb 2021 17:45:38 -0800 Ben Widawsky <[email protected]> wrote:
It's often useful in debug scenarios to see what the hardware has dumped out. As it stands today, any device error will result in the payload not being copied out, so there is no way to triage commands which weren't expected to fail (and sometimes the payload may have that information).
The functionality is protected by normal kernel security mechanisms as well as a CONFIG option in the CXL driver.
Quoted 9 lines
Quoted 4 lines
This was extracted from the original version of the CXL enabling patch series.
Signed-off-by Ben Widawsky <[email protected]>My gut feeling here is use a tracepoint rather than spamming the kernel log. Alternatively just don't bother merging this patch - it's on the list now anyway so trivial for anyone doing such debug to pick it up.
I've long wanted to make dev_dbg() a way to declare trace-points. With that, enabling it as a log message, or a trace-point is user policy. The value of this is having it readily available, not digging up a patch off the list for a debug session.
_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]