[PATCH v4 15/25] nvdimm/ocxl: Register a character device for userspace to interact with

Dan Williams dan.j.williams at intel.com
Thu Apr 2 11:27:45 AEDT 2020


On Sun, Mar 29, 2020 at 10:53 PM Alastair D'Silva <alastair at d-silva.org> wrote:
>
> This patch introduces a character device (/dev/ocxlpmemX) which further
> patches will use to interact with userspace, such as error logs,
> controller stats and card debug functionality.

This was asked earlier, but I'll reiterate, I do not see what
justifies an ocxlpmemX private device ABI vs routing through the
existing generic character ndbusX and nmemX character devices.

>
> Signed-off-by: Alastair D'Silva <alastair at d-silva.org>
> ---
>  drivers/nvdimm/ocxl/main.c     | 117 ++++++++++++++++++++++++++++++++-
>  drivers/nvdimm/ocxl/ocxlpmem.h |   2 +
>  2 files changed, 117 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/nvdimm/ocxl/main.c b/drivers/nvdimm/ocxl/main.c
> index 8db573036423..9b85fcd3f1c9 100644
> --- a/drivers/nvdimm/ocxl/main.c
> +++ b/drivers/nvdimm/ocxl/main.c
> @@ -10,6 +10,7 @@
>  #include <misc/ocxl.h>
>  #include <linux/delay.h>
>  #include <linux/ndctl.h>
> +#include <linux/fs.h>
>  #include <linux/mm_types.h>
>  #include <linux/memory_hotplug.h>
>  #include "ocxlpmem.h"
> @@ -356,6 +357,67 @@ static int ocxlpmem_register(struct ocxlpmem *ocxlpmem)
>         return device_register(&ocxlpmem->dev);
>  }
>
> +static void ocxlpmem_put(struct ocxlpmem *ocxlpmem)
> +{
> +       put_device(&ocxlpmem->dev);
> +}
> +
> +static struct ocxlpmem *ocxlpmem_get(struct ocxlpmem *ocxlpmem)
> +{
> +       return (!get_device(&ocxlpmem->dev)) ? NULL : ocxlpmem;
> +}
> +
> +static struct ocxlpmem *find_and_get_ocxlpmem(dev_t devno)
> +{
> +       struct ocxlpmem *ocxlpmem;
> +       int minor = MINOR(devno);
> +
> +       mutex_lock(&minors_idr_lock);
> +       ocxlpmem = idr_find(&minors_idr, minor);
> +       if (ocxlpmem)
> +               ocxlpmem_get(ocxlpmem);
> +       mutex_unlock(&minors_idr_lock);
> +
> +       return ocxlpmem;
> +}
> +
> +static int file_open(struct inode *inode, struct file *file)
> +{
> +       struct ocxlpmem *ocxlpmem;
> +
> +       ocxlpmem = find_and_get_ocxlpmem(inode->i_rdev);
> +       if (!ocxlpmem)
> +               return -ENODEV;
> +
> +       file->private_data = ocxlpmem;
> +       return 0;
> +}
> +
> +static int file_release(struct inode *inode, struct file *file)
> +{
> +       struct ocxlpmem *ocxlpmem = file->private_data;
> +
> +       ocxlpmem_put(ocxlpmem);
> +       return 0;
> +}
> +
> +static const struct file_operations fops = {
> +       .owner          = THIS_MODULE,
> +       .open           = file_open,
> +       .release        = file_release,
> +};
> +
> +/**
> + * create_cdev() - Create the chardev in /dev for the device
> + * @ocxlpmem: the SCM metadata
> + * Return: 0 on success, negative on failure
> + */
> +static int create_cdev(struct ocxlpmem *ocxlpmem)
> +{
> +       cdev_init(&ocxlpmem->cdev, &fops);
> +       return cdev_add(&ocxlpmem->cdev, ocxlpmem->dev.devt, 1);
> +}
> +
>  /**
>   * ocxlpmem_remove() - Free an OpenCAPI persistent memory device
>   * @pdev: the PCI device information struct
> @@ -376,6 +438,13 @@ static void remove(struct pci_dev *pdev)
>                 if (ocxlpmem->nvdimm_bus)
>                         nvdimm_bus_unregister(ocxlpmem->nvdimm_bus);
>
> +               /*
> +                * Remove the cdev early to prevent a race against userspace
> +                * via the char dev
> +                */
> +               if (ocxlpmem->cdev.owner)
> +                       cdev_del(&ocxlpmem->cdev);
> +
>                 device_unregister(&ocxlpmem->dev);
>         }
>  }
> @@ -527,11 +596,18 @@ static int probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>                 goto err;
>         }
>
> -       if (setup_command_metadata(ocxlpmem)) {
> +       rc = setup_command_metadata(ocxlpmem);
> +       if (rc) {
>                 dev_err(&pdev->dev, "Could not read command metadata\n");
>                 goto err;
>         }
>
> +       rc = create_cdev(ocxlpmem);
> +       if (rc) {
> +               dev_err(&pdev->dev, "Could not create character device\n");
> +               goto err;
> +       }
> +
>         elapsed = 0;
>         timeout = ocxlpmem->readiness_timeout +
>                   ocxlpmem->memory_available_timeout;
> @@ -599,6 +675,36 @@ static struct pci_driver pci_driver = {
>         .shutdown = remove,
>  };
>
> +static int file_init(void)
> +{
> +       int rc;
> +
> +       rc = alloc_chrdev_region(&ocxlpmem_dev, 0, NUM_MINORS, "ocxlpmem");
> +       if (rc) {
> +               idr_destroy(&minors_idr);
> +               pr_err("Unable to allocate OpenCAPI persistent memory major number: %d\n",
> +                      rc);
> +               return rc;
> +       }
> +
> +       ocxlpmem_class = class_create(THIS_MODULE, "ocxlpmem");
> +       if (IS_ERR(ocxlpmem_class)) {
> +               idr_destroy(&minors_idr);
> +               pr_err("Unable to create ocxlpmem class\n");
> +               unregister_chrdev_region(ocxlpmem_dev, NUM_MINORS);
> +               return PTR_ERR(ocxlpmem_class);
> +       }
> +
> +       return 0;
> +}
> +
> +static void file_exit(void)
> +{
> +       class_destroy(ocxlpmem_class);
> +       unregister_chrdev_region(ocxlpmem_dev, NUM_MINORS);
> +       idr_destroy(&minors_idr);
> +}
> +
>  static int __init ocxlpmem_init(void)
>  {
>         int rc;
> @@ -606,16 +712,23 @@ static int __init ocxlpmem_init(void)
>         mutex_init(&minors_idr_lock);
>         idr_init(&minors_idr);
>
> -       rc = pci_register_driver(&pci_driver);
> +       rc = file_init();
>         if (rc)
>                 return rc;
>
> +       rc = pci_register_driver(&pci_driver);
> +       if (rc) {
> +               file_exit();
> +               return rc;
> +       }
> +
>         return 0;
>  }
>
>  static void ocxlpmem_exit(void)
>  {
>         pci_unregister_driver(&pci_driver);
> +       file_exit();
>  }
>
>  module_init(ocxlpmem_init);
> diff --git a/drivers/nvdimm/ocxl/ocxlpmem.h b/drivers/nvdimm/ocxl/ocxlpmem.h
> index b72b3f909fc3..ee3bd651f254 100644
> --- a/drivers/nvdimm/ocxl/ocxlpmem.h
> +++ b/drivers/nvdimm/ocxl/ocxlpmem.h
> @@ -2,6 +2,7 @@
>  // Copyright 2020 IBM Corp.
>
>  #include <linux/pci.h>
> +#include <linux/cdev.h>
>  #include <misc/ocxl.h>
>  #include <linux/libnvdimm.h>
>  #include <linux/mm.h>
> @@ -103,6 +104,7 @@ struct command_metadata {
>  struct ocxlpmem {
>         struct device dev;
>         struct pci_dev *pdev;
> +       struct cdev cdev;
>         struct ocxl_fn *ocxl_fn;
>         struct nd_interleave_set nd_set;
>         struct nvdimm_bus_descriptor bus_desc;
> --
> 2.24.1
>


More information about the Linuxppc-dev mailing list