Memory mirroring for a ring buffer on Linux
Asked Answered
R

1

6

I use an anonymous mmap to allocate a giant chunk of memory. There are several contiguous pages in this that I'd like to turn into a ring buffer, using virtual memory mirroring.

This example on Wikipedia shows what I'm meaning by virtual memory mirroring.

Say the first 14 blocks below are the pages in my giant chunk. I'd like to virtually map pages 6 and 7 to another two consecutive locations.

[0][1][2][3][4][5][6][7][8][9][10][11][12][13].......[6][7][6][7]

Mike Ash gives a rundown of what I want to do, but using mach specific APIs.

How can this be done on Linux?

Rupp answered 14/1, 2014 at 11:41 Comment(6)
What's wrong with that example on Wikipedia?Wristwatch
@Wristwatch I already have a big chunk of memory, a couple of pages from within I want to mirror. The Wikipedia example requires allocating new memory.Rupp
Not really, all it requires is a file descriptor that it can mmap twice, it doesn't care where it came from. The idea being that you want the complete buffer to be mirrored (which is obviously what most people want). If you only want to mirror a part of the memory block, simply use something other than zero for offset. The only other possible way of mirroring pages is using remap_file_pages, and this, too, requires a memory mapping (so you gain nothing, really). You lose something though... portability.Pragmatist
@Pragmatist thanks, remap_file_pages is what I was looking forRupp
@max @damon Alas, remap_file_pages is marked deprecated in recent Linux versions. Probably better to use mmap.Radmilla
@Rupp here is what you were looking for github.com/vitalyvch/rng_bufHakeem
H
0

This is not possible to achieve with anonymous mappings. You can, however, use memfd_create(2) or create a temporary file on tmpfs (so that it won't ever be stored to disk, except by "swapping") and do shared mappings on that file descriptor, adjacent to each other.

remap_file_pages(2) is just a performance hack and not any more powerful than classic mmap/mremap calls, and it can't operate on anonymous mappings either.

Related: Linux mremap without freeing the old mapping?

Honeyhoneybee answered 24/1, 2020 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.