Skip to contentSkip to conversation

Enable/Disable NVMe quirks via module parameter

PATCH Superseded Maurizio Lombardi 3 participants 4 messages Jan 28 – Feb 2

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Enable/Disable NVMe quirks via module parameter

Recently, some of our customers were very vocal about wanting an easy way to enable or disable specific nvme quirks.

I recall that during the ALPSS conference this topic has been briefly discussed, and it was proposed to use the generic PCI sysfs "new_id" entry to manipulate the quirks.

However, in some cases a quirk may prevent the system from properly booting, the kernel may hang or fail to mount the root
filesystem before the system reaches a state where new_id can be written, this makes this solution not exactly user-friendly for end users.

In particular, boot-time hangs have been reported involving the bogus_nid quirk.

Therefore, I have been asked to re-propose this module parameter approach with a few modifications suggested by Daniel Wagner.

# dmesg | grep nvme
[    0.000000] Command line: root=/dev/vda1 console=ttyS0,115200n8 nvme.quirks=1b36:0010:^bogus_nxid
[    6.732692] nvme: unrecognized quirk bogus_nxid
# cat /sys/devices/pci0000:00/0000:00:05.0/nvme/nvme1/quirks
bogus_nid
# dmesg | grep nvme
[    0.000000] Command line: root=/dev/vda1 console=ttyS0,115200n8 nvme.quirks=1b36:0010:^bogus_nid,no_apst
# cat /sys/devices/pci0000:00/0000:00:05.0/nvme/nvme0/quirks                                                                                                                                             
no_apst

Maurizio Lombardi (1):
nvme: add support for dynamic quirk configuration via module parameter

 .../admin-guide/kernel-parameters.txt         |  13 ++
 drivers/nvme/host/pci.c                       | 154 ++++++++++++++++++
 2 files changed, 167 insertions(+)
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/1 nvme: add support for dynamic quirk configuration via module parameter

Introduce support for enabling or disabling specific NVMe quirks at module load time through the quirks module parameter. This mechanism allows users to apply known quirks dynamically based on the device's PCI vendor and device IDs,
without requiring to add hardcoded entries in the driver and recompiling the kernel.

While the generic PCI new_id sysfs interface exists for dynamic configuration, it is insufficient for scenarios where the system fails to boot (for example, this has been reported to happen because of the bogus_nid quirk). The new_id attribute is writable only after the system has booted and sysfs is mounted.

The quirks parameter accepts a list of quirk specifications separated by a '-' character in the following format:

<VID>:<DID>:<quirk_names>[-<VID>:<DID>:<quirk_names>-..]

Each quirk is represented by its name and can be prefixed with ^ to indicate that the quirk should be disabled. Quirk names are separated by a ',' character.

Example usage (enable BOGUS_NID and BROKEN_MSI, disable DEALLOCATE_ZEROES):

  $ modprobe nvme quirks=7170:2210:bogus_nid,broken_msi,^deallocate_zeroes
---
 .../admin-guide/kernel-parameters.txt         |  13 ++
 drivers/nvme/host/pci.c                       | 154 ++++++++++++++++++
 2 files changed, 167 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +13 -0
index a8d0afde7f85..e559648f97e6 100644
@@ -74,6 +74,7 @@
 	TPM	TPM drivers are enabled.
 	UMS	USB Mass Storage support is enabled.
 	USB	USB support is enabled.
+	NVME	NVMe support is enabled
 	USBHID	USB Human Interface Device support is enabled.
 	V4L	Video For Linux support is enabled.
 	VGA	The VGA console has been enabled.
@@ -4671,6 +4672,18 @@ Kernel parameters
 			This can be set from sysctl after boot.
 			See Documentation/admin-guide/sysctl/vm.rst for details.
 
+	nvme.quirks=    [NVME] A list of quirk entries to augment the built-in
+			nvme quirk list. List entries are separated by a
+			'-' character.
+			Each entry has the form VendorID:ProductID:quirk_names.
+			The IDs are 4-digits hex numbers and quirk_names is a
+			list of quirk names separated by commas. A quirk name can
+			be prefixed by '^', meaning that the specified quirk must
+			be disabled.
+
+			Example:
+			nvme.quirks=7710:2267:bogus_nid,^identify_cns-9900:7711:broken_msi
+
 	ohci1394_dma=early	[HW,EARLY] enable debugging via the ohci1394 driver.
 			See Documentation/core-api/debugging-via-ohci1394.rst for more
 			info.
drivers/nvme/host/pci.c +154 -0

190 lines not on this page · raw message

Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH 1/1] nvme: add support for dynamic quirk configuration via module parameter

Quoted 6 lines · Maurizio Lombardi

Introduce support for enabling or disabling specific NVMe quirks at module load time through the quirks module parameter. This mechanism allows users to apply known quirks dynamically based on the device's PCI vendor and device IDs,
without requiring to add hardcoded entries in the driver and recompiling the kernel.

Nit: this commit message reads a little odd because line lengths are so inconsistent, please use up all 73/75 chracaters consistently.

Documentation/admin-guide/kernel-parameters.txt

Quoted 2 lines
Documentation/admin-guide/kernel-parameters.txt +0 -0

I thought those file only used to document always built-in paramters using __setup and not module parameters, but grabbing random ones that look modular disproves that.

Documentation/admin-guide/kernel-parameters.txt Kernel parameters · line 4681

Quoted 2 lines
Documentation/admin-guide/kernel-parameters.txt +2 -0
+			list of quirk names separated by commas. A quirk name can
+			be prefixed by '^', meaning that the specified quirk must

Overly long lines.

Quoted 1 line

+static int quirks_param_set(const char *value, const struct kernel_param *kp);

Can we get away without a forware declaration here?

Documentation/admin-guide/kernel-parameters.txt

Quoted 2 lines
Documentation/admin-guide/kernel-parameters.txt +2 -0
+static struct quirk_entry *quirk_list;
+static unsigned int quirk_count;

Please use nvme_pci_ prefixes for global variables and functions.

Quoted 6 lines
Documentation/admin-guide/kernel-parameters.txt +6 -0
+static int quirks_param_set(const char *value, const struct kernel_param *kp)
+{
+	char *val, *outer_ptr, *inner_ptr, *field;
+	char *q_name;
+	u16 vid, did;
+	u32 *flags;

Documentation/admin-guide/kernel-parameters.txt

Quoted 10 lines
Documentation/admin-guide/kernel-parameters.txt +10 -0
+	size_t i = 0, q_len, field_len;
+	int err, b;
+
+	val = kstrdup(value, GFP_KERNEL);
+	if (!val)
+		return -ENOMEM;
+
+	err = param_set_copystring(val, kp);
+	if (err)
+		goto exit;

All other callers of param_set_copystring except for usbhid set it to the passsed in value and not a copy. Given that this function has no documentation I'm not sure what is right, but this smells a bit fishy.

Documentation/admin-guide/kernel-parameters.txt

Quoted 2 lines
Documentation/admin-guide/kernel-parameters.txt +2 -0
+	kfree(quirk_list);
+	quirk_list = NULL;

How can quirk_list be non-NULL here given that we don't allow runtime-changes to the paramter?

Documentation/admin-guide/kernel-parameters.txt

Quoted 8 lines
Documentation/admin-guide/kernel-parameters.txt +7 -0
+	if (!*val) {
+		quirk_count = 0;
+		goto exit;
+	}
 
+	for (quirk_count = 1, i = 0; val[i]; i++)
+		if (val[i] == '-')
+			quirk_count++;

initializing the quirk_count in the loop looks odd, compare to:

	quirk_count = 1;
	for (i = 0; val[i]; i++)
		if (val[i] == '-')
			quirk_count++;

Documentation/admin-guide/kernel-parameters.txt

Quoted 8 lines
Documentation/admin-guide/kernel-parameters.txt +8 -0
+
+	quirk_list = kcalloc(quirk_count, sizeof(struct quirk_entry),
+			     GFP_KERNEL);
+	if (!quirk_list) {
+		quirk_count = 0;
+		err = -ENOMEM;
+		goto exit;
+	}

Instead of zeroing the global variable on error, please only set it from a local one on success to make the code more robust.

Documentation/admin-guide/kernel-parameters.txt

Quoted 7 lines
Documentation/admin-guide/kernel-parameters.txt +7 -0
+	i = 0;
+	outer_ptr = val;
+	while ((field = strsep(&outer_ptr, "-"))) {
+		inner_ptr = field;
+
+		/* Each entry consists of VID:DID:quirk_names */
+		field = strsep(&inner_ptr, ":");

This juggling between field and innter_ptr looks odd. I'd scan directly into innter_ptr and keep field declarated locally in the loop. Same for any other variable not used globally. Also splitting out the loop bodies into well named helpers would help readability a lot here.

Documentation/admin-guide/kernel-parameters.txt

Quoted 3 lines
Documentation/admin-guide/kernel-parameters.txt +3 -0
+			field_len = strlen(field);
+			for (b = 0; ; ++b) {
+				q_name = nvme_quirk_name(BIT(b));

Urrg. Please avoid use of BIT in general, but especially here where you're actually doing arithmetics amd just declare constants.

Given that we're unlikely to add tons of quirk lines, simply reallocing the array vs pre-sizing it might also be simpler.

Documentation/admin-guide/kernel-parameters.txt

Quoted 5 lines
Documentation/admin-guide/kernel-parameters.txt +5 -0
+static struct quirk_entry *detect_dynamic_quirks(struct pci_dev *pdev)
+{
+	int i;
+
+	for (i = 0; i < quirk_count; ++i) {

The nvme driver like most of the kernel uses post-increments in for loops. (We already had at least one more instance above).

Documentation/admin-guide/kernel-parameters.txt

Quoted 5 lines
Documentation/admin-guide/kernel-parameters.txt +5 -0
+		if (pdev->vendor == quirk_list[i].vendor_id &&
+		    pdev->device == quirk_list[i].dev_id) {
+			return &quirk_list[i];
+		}
+	}

No need for either braces, and at least the inner ones are counter to the usual kernel style.

Documentation/admin-guide/kernel-parameters.txt

Quoted 4 lines
Documentation/admin-guide/kernel-parameters.txt +4 -0
+	qentry = detect_dynamic_quirks(pdev);
+	if (qentry) {
+		quirks |= qentry->enabled_quirks;
+		quirks &= ~(qentry->disabled_quirks);

No need for the braces after the ~.

#3 lore raw

Re: [PATCH 1/1] nvme: add support for dynamic quirk configuration via module parameter

Hello Christoph,

Quoted 10 lines · Christoph Hellwig

Nit: this commit message reads a little odd because line lengths are so inconsistent, please use up all 73/75 chracaters consistently.

Documentation/admin-guide/kernel-parameters.txt Kernel parameters · line 4681

Quoted 2 lines
Documentation/admin-guide/kernel-parameters.txt +2 -0
+			list of quirk names separated by commas. A quirk name can
+			be prefixed by '^', meaning that the specified quirk must

Overly long lines.

Quoted 8 lines
Quoted 1 line

+static int quirks_param_set(const char *value, const struct kernel_param *kp);

Can we get away without a forware declaration here?

drivers/nvme/host/pci.c static unsigned int io_queue_depth = 1024; · line 125

Quoted 2 lines
drivers/nvme/host/pci.c +2 -0
+static struct quirk_entry *quirk_list;
+static unsigned int quirk_count;

Please use nvme_pci_ prefixes for global variables and functions.

Ok

Quoted 14 lines
Quoted 14 lines
drivers/nvme/host/pci.c +13 -0
+static int quirks_param_set(const char *value, const struct kernel_param *kp)
+{
+	char *val, *outer_ptr, *inner_ptr, *field;
+	char *q_name;
+	u16 vid, did;
+	u32 *flags;
+	size_t i = 0, q_len, field_len;
+	int err, b;
+
+	val = kstrdup(value, GFP_KERNEL);
+	if (!val)
+		return -ENOMEM;
+
Quoted 8 lines

drivers/nvme/host/pci.c static unsigned int io_queue_depth = 1024; · line 144

Quoted 3 lines
drivers/nvme/host/pci.c +3 -0
+	err = param_set_copystring(val, kp);
+	if (err)
+		goto exit;

All other callers of param_set_copystring except for usbhid set it to the passsed in value and not a copy. Given that this function has no documentation I'm not sure what is right, but this smells a bit fishy.

Changed it to used the passed value

Quoted 6 lines

drivers/nvme/host/pci.c static unsigned int io_queue_depth = 1024; · line 147

Quoted 3 lines
drivers/nvme/host/pci.c +2 -0
+	kfree(quirk_list);
+	quirk_list = NULL;

How can quirk_list be non-NULL here given that we don't allow runtime-changes to the paramter?

That's right, I cleaned it up

Quoted 17 lines

drivers/nvme/host/pci.c static unsigned int io_queue_depth = 1024; · line 156

Quoted 9 lines
drivers/nvme/host/pci.c +7 -0
+	if (!*val) {
+		quirk_count = 0;
+		goto exit;
+	}
 
+	for (quirk_count = 1, i = 0; val[i]; i++)
+		if (val[i] == '-')
+			quirk_count++;

initializing the quirk_count in the loop looks odd, compare to:

	quirk_count = 1;
	for (i = 0; val[i]; i++)
		if (val[i] == '-')
			quirk_count++;
Quoted 11 lines

drivers/nvme/host/pci.c static unsigned int io_queue_depth = 1024; · line 164

Quoted 8 lines
drivers/nvme/host/pci.c +8 -0
+
+	quirk_list = kcalloc(quirk_count, sizeof(struct quirk_entry),
+			     GFP_KERNEL);
+	if (!quirk_list) {
+		quirk_count = 0;
+		err = -ENOMEM;
+		goto exit;
+	}

Instead of zeroing the global variable on error, please only set it from a local one on success to make the code more robust.

Ok

Quoted 2 lines

Given that we're unlikely to add tons of quirk lines, simply reallocing the array vs pre-sizing it might also be simpler.

I tried but IMO doesn't make it much simpler.

Quoted 19 lines

drivers/nvme/host/pci.c static unsigned long check_vendor_combination_bug(struct pci_dev *pdev) · line 3579

Quoted 6 lines
drivers/nvme/host/pci.c +5 -0
+static struct quirk_entry *detect_dynamic_quirks(struct pci_dev *pdev)
+{
+	int i;
+
+	for (i = 0; i < quirk_count; ++i) {

The nvme driver like most of the kernel uses post-increments in for loops. (We already had at least one more instance above).

drivers/nvme/host/pci.c static unsigned long check_vendor_combination_bug(struct pci_dev *pdev) · line 3584

Quoted 5 lines
drivers/nvme/host/pci.c +5 -0
+		if (pdev->vendor == quirk_list[i].vendor_id &&
+		    pdev->device == quirk_list[i].dev_id) {
+			return &quirk_list[i];
+		}
+	}

No need for either braces, and at least the inner ones are counter to the usual kernel style.

Quoted 6 lines

drivers/nvme/host/pci.c static struct nvme_dev *nvme_pci_alloc_dev(struct pci_dev *pdev, · line 3630

Quoted 4 lines
drivers/nvme/host/pci.c +4 -0
+	qentry = detect_dynamic_quirks(pdev);
+	if (qentry) {
+		quirks |= qentry->enabled_quirks;
+		quirks &= ~(qentry->disabled_quirks);

No need for the braces after the ~.

Should all be fixed in V2, please take a look and let me know

Thanks,
Maurizio