linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Adam Endrodi <adam.endrodi@nsn.com>
To: Hugh Dickins <hughd@google.com>
Cc: linux-mm@kvack.org
Subject: mmap()ing a size-extended file on a 100% full tmpfs
Date: Mon, 23 Jun 2014 15:35:37 +0200	[thread overview]
Message-ID: <20140623133537.GD12012@timmy> (raw)


Hello,


If you try to run the following program with /dev/shm being 100% full, it will
be terminated by a SIGBUS in memset():

"""
#define _GNU_SOURCE
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>

int main(void)
{
	int fd = shm_open("segg", O_CREAT|O_RDWR, 0666);
	printf("fd: %d\n", fd);
	printf("truncate: %d\n", ftruncate(fd, 1024*1024));
//	errno = posix_fallocate(fd, 0, 1024*1024);
//	printf("falloc: %s\n", strerror(errno));
	void *ptr = mmap(NULL, 1024*1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	printf("ptr: %p\n", ptr);

	memset(ptr, 0, 1024*1024);

	return 0;
}
"""

On a similarly full ext2 file system memset() completes successfully (though
I'm not sure whether it made through it by mere chance).

So the probelm is that the program may not know at all what the underlying
file system is, and in case of tmpfs it may be terminated for a completely
unexpected reason.

A portable solution could be to [posix_]fallocate() the file before trying to
mmap() it.  That works (except that perhaps tmpfs can deallocate memory if
it's under pressure).

Alternatively I could imagine such an ftruncate() implementation for tmpfs,
which would incorporate fallocate()ion.

In combination with this mmap() could refuse the operation if insufficient
backing store is available.  Ie. it would return MAP_FAILED if the programmer
didn't call ftruncate() which would include fallocate().

The wayland developers faced the same problem last year:
http://lists.freedesktop.org/archives/wayland-devel/2013-October/011501.html

My opinion is that either ftruncate() or mmap() should return an error.
What do you think?

-- 
adam

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

             reply	other threads:[~2014-06-23 13:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-23 13:35 Adam Endrodi [this message]
2014-06-23 23:23 ` mmap()ing a size-extended file on a 100% full tmpfs Hugh Dickins

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=20140623133537.GD12012@timmy \
    --to=adam.endrodi@nsn.com \
    --cc=hughd@google.com \
    --cc=linux-mm@kvack.org \
    /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.
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).