mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: musl@lists.openwall.com
Subject: Re: stdio self-synchronized destruction: does it need fixing?
Date: Mon, 10 Dec 2012 18:21:53 -0500	[thread overview]
Message-ID: <20121210232153.GN20323@brightrain.aerifal.cx> (raw)
In-Reply-To: <20121210225752.GI23126@port70.net>

On Mon, Dec 10, 2012 at 11:57:53PM +0100, Szabolcs Nagy wrote:
> * Rich Felker <dalias@aerifal.cx> [2012-12-10 13:05:09 -0500]:
> > memory. The allocation of FILE structues is always performed by libc,
> > and always happens via malloc with a small-size allocation, which
> > means the memory is managed as part of the heap and never unmapped
> > once it's mapped. Thus, as far as I can tell, the worst that can
> > happen is a read-only access to memory no longer owned by the FILE,
> 
> at least write a comment there that the invalid read is known

This sounds like a perfect solution for now.

> (btw at some point someone may rewrite malloc so small allocations
> can go to mmapped areas as well which may be reclaimed..)

I would like to add the ability to obtain more heap memory from mmap,
but I think unmapping unused parts has more cons than pros. The only
benefit is that it reclaims the virtual address space so that the
kernel can use it for honoring future mmap requests. The disadvantage
is that it fragments the heap; once a "hole" has been created in the
heap, it's impossible to put it back and merge the adjacent free
regions into one large free region in O(1) time.

Of course if you're talking about a _whole_ supplemental mmap zone
becoming free, then it would be safe to unmap it, and this may be a
worthwhile case to handle. In particular, one way of doing
supplemental heap via mmap would be to make each map roughly the same
size as the threshold for releasing it back to the kernel via
mprotect/mmap zeroing. The beginning and end of the region could have
specially-flagged chunks, and then, after merging on free(), if both
the previous and next neighbor are flagged as the mmap region
endpoints, the whole region could be unmapped rather than adding it
back to the free list.

The alternative would be to do supplemental mmap regions in much
larger increments, maybe on the order of 16MB, initially with
PROT_NONE, and grow the PROT_READ|PROT_WRITE portion like brk() does
for the main heap. Then the same code that releases parts of the main
brk heap via zeroing could also work well here. I think this
alternative approach would result in less fragmentation for malloc,
but more chance of filling up virtual address space and preventing
mmap from working.

Rich


      reply	other threads:[~2012-12-10 23:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-10 18:05 Rich Felker
2012-12-10 22:57 ` Szabolcs Nagy
2012-12-10 23:21   ` Rich Felker [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20121210232153.GN20323@brightrain.aerifal.cx \
    --to=dalias@aerifal.cx \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).