2

Despite continued interest there is still no way to create a “cow-copy” of a memory region on Linux. With the inception of the memfd_create(2) syscall, the situation has slightely improved, as one does not have to create an explicit file any more for shared memory.

I am wondering, why isn't there a thing like the following?

void *ptr = mmap((void *)0, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, (size_t)0);
void *ptr2= mremap(ptr, 0, size, MREMAP_MAYMOVE | MREMAP_COW);

The intended semantics are that ptr2 and ptr share the underlying memory, but a write from either will trigger a copy-on-write with page-granularity.

Is this just a case of “nobody bothered to implement this yet” or am I missing something technical?

Community
  • 1
  • 1
Fabian Klötzl
  • 407
  • 3
  • 10
  • With `memfd_create()` you can exactly achieve what you want, this is a very clean interface with intuitive semantics in my eyes. What is your issue with it? – Ctx Feb 28 '17 at 11:18
  • @Ctx [This post](https://blog.kloetzl.info/the-missing-cow-call/) describes, what I have achieved with `memfd_create` so far. But it has weird semantics and does not allow making a copy of a copy without creating yet another file. – Fabian Klötzl Feb 28 '17 at 11:35
  • Fabian, refering to your blog article, you could simply make both mappings private (not the first one shared) and populate the memory with `read()/write()`-calls. Or you could make a shared mapping to populate the memory, unmap it and make two private mappings. – Ctx Feb 28 '17 at 11:51
  • This would be a hideous hack, but... what about a `mmap()` of the process's own address space? `int fd = open( "/proc/self/as", O_RDWR ); void *ptr = mmap( 0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, offset );` I suspect `offset` would be the address of the memory region you want to create a COW copy of. – Andrew Henle Feb 28 '17 at 12:04
  • @Ctx I don't think your first option has a big advantage over a memcpy which I am trying to avoid. The second method again has the problem that one cannot simply create a copy of a copy without writing everything to another file (even if that file might be memfd created). – Fabian Klötzl Feb 28 '17 at 12:18
  • @AndrewHenle Yeah, that would be a nasty hack. There must be an easier way. After all, Linux supports this functionality on a `fork`, but there is just no way to explicitly call it‽ – Fabian Klötzl Feb 28 '17 at 12:21
  • @FabianKlötzl So your main issue is to make a COW-copy of memory data without having it in a file (be it virtual or on-disk) in the first place, correct? This is indeed not straight-forward, I wonder what the application might be... – Ctx Feb 28 '17 at 12:54
  • @Ctx I guess it would be useful for any kind of COW data structures. In my case MB-long strings (genomes). – Fabian Klötzl Feb 28 '17 at 13:09
  • Fabian, for most cases I do not see how the kernel could do this more efficient than the application itself. In your case, I would suggest to handle the COW manually in userspace with appropriate structures and access functions, I'd bet this is more efficient and can be tailored better to the specific applications needs. – Ctx Feb 28 '17 at 13:12

0 Answers0