title: How to avoid high cpu usage while reading/writing character device?
body: <p>I need to write a linux kernel driver for a PCIe device with SRAM.</p>
  
  <p>For the first attempt, I've written a driver to access SRAM from PCIe with a character device.</p>
  
  <p>Everything works as expected, but there is one problem. SRAM is slow 1MB takes about 2 secs to read/write, this is a hardware limitation. The CPU is 100% busy while reading/writing. Witch is a problem. I don't need speed, reading/writing can be slow, but why it takes so much CPU?</p>
  
  <p>The buffer is initialized with <code>pci_iomap</code>:</p>
  
  <pre><code>  g_mmio_buffer[0] = pci_iomap(pdev, SRAM_BAR_H, g_mmio_length);
  </code></pre>
  
  <p>read/write functions looks like this:</p>
  
  <pre><code>static ssize_t dev_read(struct file *fp, char *buf, size_t len, loff_t *off) {
    unsigned long rval;
    size_t copied;
  
    rval = copy_to_user(buf, g_mmio_buffer[SRAM_BAR] + *off, len);
  
    if (rval &lt; 0) return -EFAULT;
  
    copied = len - rval;
    *off += copied;
  
    return copied;
  }
  
  static ssize_t dev_write(struct file *fp, const char *buf, size_t len, loff_t *off) {
    unsigned long rval;
    size_t copied;
  
    rval = copy_from_user(g_mmio_buffer[SRAM_BAR] + *off, buf, len);
  
    if (rval &lt; 0) return -EFAULT;
  
    copied = len - rval;
    *off += copied;
  
    return copied;
  }
  </code></pre>
  
  <p>The question is what can I do with high CPU usage?</p>
  
  <p>Should I rewrite the driver to use a block device instead of a character?</p>
  
  <p>Allow CPU to work on another process while reading/saving data?</p>