Skip to content

Commit 608cc4b

Browse files
author
Christoph Hellwig
committed
nvme: fix lightnvm check
nvme_nvm_ns_supported assumes every device is a pci_dev, which leads to reading an incorrect field, or possible even a dereference of unallocated memory for fabrics controllers. Fix this by introducing a quirk for lighnvm capable devices instead. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Matias Bjørling <[email protected]> Reviewed-by: Keith Busch <[email protected]> Reviewed-by: Sagi Grimberg <[email protected]>
1 parent 09c2c35 commit 608cc4b

File tree

4 files changed

+14
-35
lines changed

4 files changed

+14
-35
lines changed

drivers/nvme/host/core.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,10 +2377,11 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
23772377

23782378
nvme_report_ns_ids(ctrl, ns->ns_id, id, ns->eui, ns->nguid, &ns->uuid);
23792379

2380-
if (nvme_nvm_ns_supported(ns, id) &&
2381-
nvme_nvm_register(ns, disk_name, node)) {
2382-
dev_warn(ctrl->device, "%s: LightNVM init failure\n", __func__);
2383-
goto out_free_id;
2380+
if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
2381+
if (nvme_nvm_register(ns, disk_name, node)) {
2382+
dev_warn(ctrl->device, "LightNVM init failure\n");
2383+
goto out_free_id;
2384+
}
23842385
}
23852386

23862387
disk = alloc_disk_node(0, node);

drivers/nvme/host/lightnvm.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -955,29 +955,3 @@ void nvme_nvm_unregister_sysfs(struct nvme_ns *ns)
955955
sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
956956
&nvm_dev_attr_group);
957957
}
958-
959-
/* move to shared place when used in multiple places. */
960-
#define PCI_VENDOR_ID_CNEX 0x1d1d
961-
#define PCI_DEVICE_ID_CNEX_WL 0x2807
962-
#define PCI_DEVICE_ID_CNEX_QEMU 0x1f1f
963-
964-
int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id)
965-
{
966-
struct nvme_ctrl *ctrl = ns->ctrl;
967-
/* XXX: this is poking into PCI structures from generic code! */
968-
struct pci_dev *pdev = to_pci_dev(ctrl->dev);
969-
970-
/* QEMU NVMe simulator - PCI ID + Vendor specific bit */
971-
if (pdev->vendor == PCI_VENDOR_ID_CNEX &&
972-
pdev->device == PCI_DEVICE_ID_CNEX_QEMU &&
973-
id->vs[0] == 0x1)
974-
return 1;
975-
976-
/* CNEX Labs - PCI ID + Vendor specific bit */
977-
if (pdev->vendor == PCI_VENDOR_ID_CNEX &&
978-
pdev->device == PCI_DEVICE_ID_CNEX_WL &&
979-
id->vs[0] == 0x1)
980-
return 1;
981-
982-
return 0;
983-
}

drivers/nvme/host/nvme.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ enum nvme_quirks {
7575
* The deepest sleep state should not be used.
7676
*/
7777
NVME_QUIRK_NO_DEEPEST_PS = (1 << 5),
78+
79+
/*
80+
* Supports the LighNVM command set if indicated in vs[1].
81+
*/
82+
NVME_QUIRK_LIGHTNVM = (1 << 6),
7883
};
7984

8085
/*
@@ -320,7 +325,6 @@ void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
320325
int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
321326

322327
#ifdef CONFIG_NVM
323-
int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id);
324328
int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node);
325329
void nvme_nvm_unregister(struct nvme_ns *ns);
326330
int nvme_nvm_register_sysfs(struct nvme_ns *ns);
@@ -339,10 +343,6 @@ static inline int nvme_nvm_register_sysfs(struct nvme_ns *ns)
339343
return 0;
340344
}
341345
static inline void nvme_nvm_unregister_sysfs(struct nvme_ns *ns) {};
342-
static inline int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id)
343-
{
344-
return 0;
345-
}
346346
static inline int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd,
347347
unsigned long arg)
348348
{

drivers/nvme/host/pci.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,6 +2497,10 @@ static const struct pci_device_id nvme_id_table[] = {
24972497
.driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
24982498
{ PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */
24992499
.driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
2500+
{ PCI_DEVICE(0x1d1d, 0x1f1f), /* LighNVM qemu device */
2501+
.driver_data = NVME_QUIRK_LIGHTNVM, },
2502+
{ PCI_DEVICE(0x1d1d, 0x2807), /* CNEX WL */
2503+
.driver_data = NVME_QUIRK_LIGHTNVM, },
25002504
{ PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
25012505
{ PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
25022506
{ PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },

0 commit comments

Comments
 (0)