Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1302238 > unrolled thread

cdev_del(), and when is it safe to free memory

Started byRajat Jain <rajatxjain@gmail.com>
First post2016-01-05 23:00 +0100
Last post2016-01-05 23:00 +0100
Articles 1 — 1 participant

Back to article view | Back to linux.kernel


Contents

  cdev_del(), and when is it safe to free memory Rajat Jain <rajatxjain@gmail.com> - 2016-01-05 23:00 +0100

#1302238 — cdev_del(), and when is it safe to free memory

FromRajat Jain <rajatxjain@gmail.com>
Date2016-01-05 23:00 +0100
Subjectcdev_del(), and when is it safe to free memory
Message-ID<qNHO3-gg-25@gated-at.bofh.it>
Hi,

I'm trying to understand the cdev_del() behavior while the device is
continuously accessed from user space.

struct my_dev{
 ...
 struct cdev cdev;
}

struct file_operations fops {
 .open = fun_open,
 . read = fun_read,
 .mmap = fun_mmap,
 . release = fun_release,
}

fun_register()
{
    struct my_dev *md = kmalloc(sizeof(struct my_dev), ...);
    alloc_chrdev_region(&devnum,...);

    cdev_init(&md->cdev, fops);
    cdev_add(&md->cdev, devnum, 1)l
     ....
}

fun_unregister(struct my_dev * md)
{
    cdev_del(&md->cdev);
    unregister_chrdev_region(devnum,...)
    kfree(md);
}

My user space program does something like this:

fd = open("/dev/my_dev", O_RDWR);
ptr = mmap(fd,...);
while (1) {
    read(fd, buf, 1);
    printf("%x", *ptr);
    printf("%c", buf);

}
munmap(ptr,...);
close(fd);

My driver tries to do cdev_del() while user is continuously using the
character device node.

My questions:

1) If my driver calls cdev_del() while the above user space program is
running, does the cdev_del() wait for any pending calls (read() here)
to complete before returning?

2) Once cdev_del() returns, am I guaranteed that my file operations
will never be called again (even though user space is issuing calls)
and holding file references at that point?

3) Since the user space does not ever give up references to the
character device node file, what is cdev_del() behaviour here? After
it returns, can I kfree all memory? (Note that I may have mmapped
memory to user space via mmap). Also, am I correct in releasing the
chrdev_region irrespective of user space accessing it?

Thanks,

Rajat
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [standalone]


Back to top | Article view | linux.kernel


csiph-web