Skip to content

Commit ba09c01

Browse files
committed
dax: convert to the cdev api
A goal of the device-DAX interface is to be able to support many exclusive allocations (partitions) of performance / feature differentiated memory. This count may exceed the default minors limit of 256. As a result of switching to an embedded cdev the inode-to-dax_dev conversion is simplified, as well as reference counting which can switch to the cdev kobject lifetime. Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Dan Williams <dan.j.williams@intel.com>
1 parent ebd84d7 commit ba09c01

2 files changed

Lines changed: 46 additions & 41 deletions

File tree

‎drivers/dax/Kconfig‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ config DEV_DAX_PMEM
2323

2424
Say Y if unsure
2525

26+
config NR_DEV_DAX
27+
int "Maximum number of Device-DAX instances"
28+
default 32768
29+
range 256 2147483647
30+
2631
endif

‎drivers/dax/dax.c‎

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@
1414
#include <linux/module.h>
1515
#include <linux/device.h>
1616
#include <linux/pfn_t.h>
17+
#include <linux/cdev.h>
1718
#include <linux/slab.h>
1819
#include <linux/dax.h>
1920
#include <linux/fs.h>
2021
#include <linux/mm.h>
2122
#include "dax.h"
2223

23-
static int dax_major;
24+
static dev_t dax_devt;
2425
static struct class *dax_class;
2526
static DEFINE_IDA(dax_minor_ida);
27+
static int nr_dax = CONFIG_NR_DEV_DAX;
28+
module_param(nr_dax, int, S_IRUGO);
29+
MODULE_PARM_DESC(nr_dax, "max number of device-dax instances");
2630

2731
/**
2832
* struct dax_region - mapping infrastructure for dax devices
@@ -49,6 +53,7 @@ struct dax_region {
4953
* struct dax_dev - subdivision of a dax region
5054
* @region - parent region
5155
* @dev - device backing the character device
56+
* @cdev - core chardev data
5257
* @alive - !alive + rcu grace period == no new mappings can be established
5358
* @id - child id in the region
5459
* @num_resources - number of physical address extents in this device
@@ -57,6 +62,7 @@ struct dax_region {
5762
struct dax_dev {
5863
struct dax_region *region;
5964
struct device dev;
65+
struct cdev cdev;
6066
bool alive;
6167
int id;
6268
int num_resources;
@@ -367,29 +373,12 @@ static unsigned long dax_get_unmapped_area(struct file *filp,
367373
return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
368374
}
369375

370-
static int __match_devt(struct device *dev, const void *data)
371-
{
372-
const dev_t *devt = data;
373-
374-
return dev->devt == *devt;
375-
}
376-
377-
static struct device *dax_dev_find(dev_t dev_t)
378-
{
379-
return class_find_device(dax_class, NULL, &dev_t, __match_devt);
380-
}
381-
382376
static int dax_open(struct inode *inode, struct file *filp)
383377
{
384-
struct dax_dev *dax_dev = NULL;
385-
struct device *dev;
386-
387-
dev = dax_dev_find(inode->i_rdev);
388-
if (!dev)
389-
return -ENXIO;
378+
struct dax_dev *dax_dev;
390379

391-
dax_dev = to_dax_dev(dev);
392-
dev_dbg(dev, "%s\n", __func__);
380+
dax_dev = container_of(inode->i_cdev, struct dax_dev, cdev);
381+
dev_dbg(&dax_dev->dev, "%s\n", __func__);
393382
filp->private_data = dax_dev;
394383
inode->i_flags = S_DAX;
395384

@@ -399,11 +388,8 @@ static int dax_open(struct inode *inode, struct file *filp)
399388
static int dax_release(struct inode *inode, struct file *filp)
400389
{
401390
struct dax_dev *dax_dev = filp->private_data;
402-
struct device *dev = &dax_dev->dev;
403-
404-
dev_dbg(dev, "%s\n", __func__);
405-
put_device(dev);
406391

392+
dev_dbg(&dax_dev->dev, "%s\n", __func__);
407393
return 0;
408394
}
409395

@@ -430,6 +416,7 @@ static void dax_dev_release(struct device *dev)
430416
static void unregister_dax_dev(void *dev)
431417
{
432418
struct dax_dev *dax_dev = to_dax_dev(dev);
419+
struct cdev *cdev = &dax_dev->cdev;
433420

434421
dev_dbg(dev, "%s\n", __func__);
435422

@@ -442,6 +429,7 @@ static void unregister_dax_dev(void *dev)
442429
*/
443430
dax_dev->alive = false;
444431
synchronize_rcu();
432+
cdev_del(cdev);
445433
device_unregister(dev);
446434
}
447435

@@ -451,17 +439,13 @@ int devm_create_dax_dev(struct dax_region *dax_region, struct resource *res,
451439
struct device *parent = dax_region->dev;
452440
struct dax_dev *dax_dev;
453441
struct device *dev;
442+
struct cdev *cdev;
454443
int rc, minor;
455444
dev_t dev_t;
456445

457446
dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
458447
if (!dax_dev)
459448
return -ENOMEM;
460-
memcpy(dax_dev->res, res, sizeof(*res) * count);
461-
dax_dev->num_resources = count;
462-
dax_dev->alive = true;
463-
dax_dev->region = dax_region;
464-
kref_get(&dax_region->kref);
465449

466450
dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
467451
if (dax_dev->id < 0) {
@@ -475,10 +459,26 @@ int devm_create_dax_dev(struct dax_region *dax_region, struct resource *res,
475459
goto err_minor;
476460
}
477461

478-
dev_t = MKDEV(dax_major, minor);
479-
462+
/* device_initialize() so cdev can reference kobj parent */
463+
dev_t = MKDEV(MAJOR(dax_devt), minor);
480464
dev = &dax_dev->dev;
481465
device_initialize(dev);
466+
467+
cdev = &dax_dev->cdev;
468+
cdev_init(cdev, &dax_fops);
469+
cdev->owner = parent->driver->owner;
470+
cdev->kobj.parent = &dev->kobj;
471+
rc = cdev_add(&dax_dev->cdev, dev_t, 1);
472+
if (rc)
473+
goto err_cdev;
474+
475+
/* from here on we're committed to teardown via dax_dev_release() */
476+
memcpy(dax_dev->res, res, sizeof(*res) * count);
477+
dax_dev->num_resources = count;
478+
dax_dev->alive = true;
479+
dax_dev->region = dax_region;
480+
kref_get(&dax_region->kref);
481+
482482
dev->devt = dev_t;
483483
dev->class = dax_class;
484484
dev->parent = parent;
@@ -493,6 +493,8 @@ int devm_create_dax_dev(struct dax_region *dax_region, struct resource *res,
493493

494494
return devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev);
495495

496+
err_cdev:
497+
ida_simple_remove(&dax_minor_ida, minor);
496498
err_minor:
497499
ida_simple_remove(&dax_region->ida, dax_dev->id);
498500
err_id:
@@ -506,24 +508,22 @@ static int __init dax_init(void)
506508
{
507509
int rc;
508510

509-
rc = register_chrdev(0, "dax", &dax_fops);
510-
if (rc < 0)
511+
nr_dax = max(nr_dax, 256);
512+
rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
513+
if (rc)
511514
return rc;
512-
dax_major = rc;
513515

514516
dax_class = class_create(THIS_MODULE, "dax");
515-
if (IS_ERR(dax_class)) {
516-
unregister_chrdev(dax_major, "dax");
517-
return PTR_ERR(dax_class);
518-
}
517+
if (IS_ERR(dax_class))
518+
unregister_chrdev_region(dax_devt, nr_dax);
519519

520-
return 0;
520+
return PTR_ERR_OR_ZERO(dax_class);
521521
}
522522

523523
static void __exit dax_exit(void)
524524
{
525525
class_destroy(dax_class);
526-
unregister_chrdev(dax_major, "dax");
526+
unregister_chrdev_region(dax_devt, nr_dax);
527527
ida_destroy(&dax_minor_ida);
528528
}
529529

0 commit comments

Comments
 (0)