خرید بک لینک

Vote count: 0

I am writing a keel driver to send/receive data with a PCI Express device. For this first version of the driver I am creating a character device interface where the user can read data using a file.

Background

I want to implement a blocking read where the user request data and the driver populates a user buffer. In order to block the user's read call I am using a completion structure.

When the driver is loaded and the user requests a read the driver blocks as expected. If I were to finish the read then everything runs fine.

Here is my problem

In order to be safe, whenever the module is removed I call the 'complete_all' function just in case someone removes the module or device in the middle of a read transaction.

Neither the remove or exit function is called and both the module and user application is blocked. I've tried the following three functions (shown with their associated result).

  1. wait_completion(&dev->read_complete); //Blocks indefinitely, I need to reset the computer
  2. retval = wait_for_completion_interruptible(&dev->read_complete); //I can kill the user application manually and then remove the driver
  3. retval = wait_for_completion_killable(&dev->read_complete); //Same as interruptible

My expectation is that when the remove function is called I can call 'complete_all(&dev->read_complete)' and the read function will retu an error.

In order to remove exteal factors I've made a repo on github so if anyone wants to see the behavior for themselves they just need to clone and follow the instructions:

Keel Module Completion Test

The relevant parts of the module are here (/src/mymodule.c)

typedef struct {
  struct cdev cdv;
  struct class *cls;
  struct device *dev;
  struct completion complete;
} mymodule_t;
mymodule_t mymod;


//Sysfs 'mymodule_test' attribute (all but the actual function is left out for brevity)

static ssize_t mymodule_test_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
  int retval = 0;
  int value = 0;
  if (sscanf(buf, "%d", &value) == 1)
  {
    retval = strlen(buf);
  }
  if (value)
  {
    printk("Value is: %dn", value);
    if (!completion_done(&mymod.complete))
    {
      complete(&mymod.complete);
    }
    printk("Sent Completio");
  }

  retu retval;
}

//FOPS (all but 'read' function is left out for brevity)

ssize_t mymodule_read(struct file *filp, char * buf, size_t count, loff_t *f_pos)
{
  printk("Read!n");
  if (completion_done(&mymod.complete))
  {
    reinit_completion(&mymod.complete);
  }

  printk("Wait for Completio");
  wait_for_completion_interruptible(&mymod.complete);
  printk("After Completio");
  retu 0;
}


static int __init mymodule_init(void)
{
  ...
  //Register class and device
  //Configure character driver with fops
  init_completion(&mymod.complete);
  ...
}
static void __exit mymodule_exit(void)
{
  ...
  if (!completion_done(&mymod.complete))
  {
    printk("Send a completion!n");
    complete(&mymod.complete);
  }
  //Clean up the rest of the module
  ...
}
module_init(mymodule_init);
module_exit(mymodule_exit);

Here is the userland application I use to exercise this:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>

#include "mymodule.h"


#define FILEPATH "/dev/mymodule0"

#define TEST_SIZE 10

int main(void)
{
  int fn = -1;
  char buf[TEST_SIZE];
  printf("Attempting to open file module file...n");
  fn = open(FILEPATH, O_RDWR);
  if (fn < 0)
  {
    printf("Failed to open file!n");
    retu -1;
  }
  printf("Attempting to read from the file...n");
  read(fn, &buf, TEST_SIZE);
  printf("Finished reading from filen");
  retu 0;
}

Here is the dmesg output when I

  1. Load the module
  2. Run the user application (basically it opens the file, attempts to read 10 characters then exits
  3. Write '1' to the sysfs attribute
  4. Unload the module
[3217633.993937] Registering Driver
[3217633.993995] Driver Initialized!
[3217643.747791] Opened!
[3217643.747800] Read!
[3217643.747801] Wait for Completion
[3217646.436780] Value is: 1
[3217646.436792] Sent Completion
[3217646.436806] After Completion
[3217646.437010] Closed!
[3217727.378388] Cleanup Module
[3217727.378393] Check if we need to complete anything
[3217727.378395] Send a completion!
[3217727.378397] Unregistering Character Driver
[3217727.378400] Give back all the numbers we requested
[3217727.378402] Remove the class driver
[3217727.378571] Release the class
[3217727.378593] Finished Cleanup Module, Exiting

If I run the following commands:

  1. Load the module
  2. Run the user application
  3. Unload the module
[3218223.442777] Registering Driver
[3218223.442934] Driver Initialized!
[3218229.378396] Opened!
[3218229.378419] Read!
[3218229.378422] Wait for Completion

The module doesn't unload. If this were a real device, like a USB hard drive it is possible that the user could remove the device in the middle of a read transaction. It seems like something is wrong, or perhaps I'm missing something... Am I missing something?

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: سه شنبه 7 ارديبهشت 1395 ساعت: 4:44

صفحه بندی