title: mmap for direct IO: bad address?
body: <p>I allocated some memory with anonymous mmap:</p>
  
  <pre><code>buff-&gt;addr = mmap(NULL, length, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS -1, 0);
  fprintf(stderr, "allocated buffer: %p, %lu\n", buff-&gt;addr, (unsigned long)length);
  </code></pre>
  
  <p>then I'm writing to it using fd opened with O_DIRECT:</p>
  
  <pre><code>int fd = open(name, O_CREAT | O_TRUNC | O_WRONLY | O_DIRECT, 00300);
  if(fd == -1)
  {
      perror("failed to open for write");
      return;
  }
  
  fprintf(stderr, "writing to address: %p size: %lu\n", buff.addr, buff.length);
  if(write(fd, buff.addr, buff.length) != size)
  {
      perror("failed to write");
  }
  </code></pre>
  
  <p>but get bad address error:</p>
  
  <pre><code>allocated buffer: 0x7f096ddff000, 512000
  writing to address: 0x7f096ddff000 size: 512000
  failed to write: Bad address
  </code></pre>
  
  <p>The address looks good to me. </p>
  
  <p>If I allocate the buffer in .data and align to 512, the write works, so the open() and write() are ok, I have all permissions, etc.</p>
  
  <p>What's wrong? Are there other requirements for direct IO than 512 alignment?</p>
  
  <p>UPDATE:</p>
  
  <p>for example, if I change mmap for this:</p>
  
  <pre><code>char buffer[ 1024000 ];
  for(buff-&gt;addr = buffer; ((unsigned long)buff-&gt;addr % 512) != 0; buff-&gt;addr = buff-&gt;addr + 1);
  </code></pre>
  
  <p>it works:</p>
  
  <pre><code>allocated buffer: 0x500e00, 512000
  writing to address: 0x500e00 size: 512000
  </code></pre>