Kiobufs and userspace memory

From: Steffen Persvold (
Date: Sun Apr 29 2001 - 12:29:53 EST

Hi all,

I'm writing a device driver for a shared memory adapter from which I plan to
support DMA directly from userspace memory (zero copy). I have already
implemented a version which I think works, but I'm not sure if I get the IO
addresses calculated correctly. The case is as follows :

The userspace application has allocated some memory with malloc() and use write
to the device's /dev entry.

The drivers write() function looks something like this :

ssize_t my_write(struct file* file, char* userbuf, size_t len, loff_t* poff)
  struct kiobuf * iobuf;
  size_t size;
  struct my_sglist *sglist = NULL;
  int i, err;

  /* Pin user memory */
  err = alloc_kiovec(1, &iobuf);
  if (err)
    return err;

  err = map_user_kiobuf(WRITE, userbuf, len);
  if (err)
    goto out;

  /* Traverse the iobuf to get the IO address for each page,
   * building up the SG table for my DMA machine */

  sglist = kmalloc(sizeof(struct my_sglist) * iobuf->nr_pages, GFP_ATOMIC)
  if (!sglist)
    goto out_unmap;

  totlen = 0;
  for (i = 0; i < iobuf->nr_pages; ++i)
    struct page *page = iobuf->maplist[i];
    void *vaddr = page_address(page) + iobuf->offset;
    unsigned long ioaddr = virt_to_bus(vaddr);

    sglist[i].start = ioaddr;

    if ((totlen + PAGE_SIZE) > len)
      sglist[i].len = len - totlen;
      sglist[i].len = PAGE_SIZE;

    totlen + = PAGE_SIZE;

  /* Start the synchronous DMA engine */

  /* Unpin user memory */

  free_kiovec(1, &iobuf);

Is this use of kiobufs sensible to you ? If not, what should I really be doing
in order to acheive zero copy DMA ?

I also have a question regarding a DMA read to userspace memory :

If the application didn't initialize the buffer by memset or anything, all the
pages maps to the same page (the zero page) right ? So if a map_user_kiobuf()
call is made on this buffer, will it sort this out and map the pages to real
ones ?

Any response greatly appreciated,

 Steffen Persvold                        Systems Engineer
 Email  :            Scali AS (
 Norway : Tel  : (+47) 2262 8950         Olaf Helsets vei 6
          Fax  : (+47) 2262 8951         N-0621 Oslo, Norway

USA : Tel : (+1) 713 706 0544 10500 Richmond Avenue, Suite 190 Houston, Texas 77042, USA - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to More majordomo info at Please read the FAQ at

This archive was generated by hypermail 2b29 : Mon Apr 30 2001 - 21:00:22 EST