mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chris Wright <chrisw@osdl.org>
To: Andrew Morton <akpm@osdl.org>
Cc: Jeff Garzik <jgarzik@pobox.com>, linux-kernel@vger.kernel.org
Subject: Re: mlock(1)
Date: Fri, 24 Sep 2004 17:26:11 -0700	[thread overview]
Message-ID: <20040924172611.Y1973@build.pdx.osdl.net> (raw)
In-Reply-To: <20040924141731.7ced31f7.akpm@osdl.org>; from akpm@osdl.org on Fri, Sep 24, 2004 at 02:17:31PM -0700

[-- Attachment #1: Type: text/plain, Size: 2616 bytes --]

* Andrew Morton (akpm@osdl.org) wrote:
> Jeff Garzik <jgarzik@pobox.com> wrote:
> >
> > How feasible is it to create an mlock(1) utility, that would allow 
> >  priveleged users to execute a daemon such that none of the memory the 
> >  daemon allocates will ever be swapped out?
> 
> It used to be the case that mlockall(MCL_FUTURE) was inherited across fork,
> which would do what you want.
> 
> But that was a bug and we fixed it a couple of years back, sadly.
> 
> I guess we could add a new mlockall mode which _is_ inherited across fork.

Here's a simple (bit ugly) hack that does it (esp. the exec part).
Seems to miss one page, probably from the arguments or so.  Not sure if
it's worth pursuing, but here it is ;-)

$ ./mlock /bin/cat /proc/self/status | grep Vm
VmSize:     3436 kB
VmLck:      3432 kB
VmRSS:      3436 kB
VmData:      144 kB
VmStk:         8 kB
VmExe:        13 kB
VmLib:      1195 kB

thanks,
-chris
-- 
Linux Security Modules     http://lsm.immunix.org     http://lsm.bkbits.net

===== fs/binfmt_elf.c 1.88 vs edited =====
--- 1.88/fs/binfmt_elf.c	2004-09-22 13:34:05 -07:00
+++ edited/fs/binfmt_elf.c	2004-09-24 17:15:53 -07:00
@@ -83,6 +83,9 @@
 
 #define BAD_ADDR(x)	((unsigned long)(x) > TASK_SIZE)
 
+#define MCL_FOREVER	4
+#define PF_MEMLOCK	0x00400000
+
 static int set_brk(unsigned long start, unsigned long end)
 {
 	start = ELF_PAGEALIGN(start);
@@ -704,7 +707,7 @@
 	current->mm->end_code = 0;
 	current->mm->mmap = NULL;
 	current->flags &= ~PF_FORKNOEXEC;
-	current->mm->def_flags = def_flags;
+	current->mm->def_flags = (current->flags & PF_MEMLOCK) ? VM_LOCKED : 0;
 
 	/* Do this immediately, since STACK_TOP as used in setup_arg_pages
 	   may depend on the personality.  */
===== mm/mlock.c 1.9 vs edited =====
--- 1.9/mm/mlock.c	2004-08-23 01:15:25 -07:00
+++ edited/mm/mlock.c	2004-09-24 16:07:16 -07:00
@@ -8,6 +8,8 @@
 #include <linux/mman.h>
 #include <linux/mm.h>
 
+#define MCL_FOREVER	4
+#define PF_MEMLOCK	0x00400000
 
 static int mlock_fixup(struct vm_area_struct * vma, 
 	unsigned long start, unsigned long end, unsigned int newflags)
@@ -150,6 +152,9 @@
 		def_flags = VM_LOCKED;
 	current->mm->def_flags = def_flags;
 
+	if (flags & MCL_FOREVER)
+		current->flags |= PF_MEMLOCK;
+
 	error = 0;
 	for (vma = current->mm->mmap; vma ; vma = vma->vm_next) {
 		unsigned int newflags;
@@ -170,7 +175,7 @@
 	int ret = -EINVAL;
 
 	down_write(&current->mm->mmap_sem);
-	if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE)))
+	if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE | MCL_FOREVER)))
 		goto out;
 
 	lock_limit = current->rlim[RLIMIT_MEMLOCK].rlim_cur;

[-- Attachment #2: mlock.c --]
[-- Type: text/x-c++src, Size: 620 bytes --]

#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>

#define MCL_FOREVER 	4

int main(int argc, char *argv[], char *envp[])
{
	int rc;
	int c, do_fork = 0;

	while ((c = getopt(argc, argv, "f")) != -1) {
		switch(c) {
		case 'f':
			do_fork = 1;
			break;
		default:
			printf("usage:\t%s [-f] program_to_exec\n", argv[0]);
		}
	}
	argv += optind;

	rc = mlockall(MCL_FUTURE|MCL_FOREVER);
	if (rc == -1)
		perror("mlockall");

	if (do_fork) {
		int pid = fork();
		if (pid > 0)
			exit(0);
		else if (pid < 0) {
			perror("fork");
			exit(1);
		}
	}
	execve(argv[0], argv, envp);
	perror("execve");
	exit(1);
}

  reply	other threads:[~2004-09-25  0:27 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-24 19:57 mlock(1) Jeff Garzik
2004-09-24 20:15 ` mlock(1) Neil Horman
2004-09-24 20:21   ` mlock(1) Neil Horman
2004-09-24 20:31   ` mlock(1) Lee Revell
2004-09-24 20:33     ` mlock(1) Jeff Garzik
2004-09-24 20:39       ` mlock(1) Lee Revell
2004-09-24 20:22 ` mlock(1) Chris Wright
2004-09-24 20:41   ` mlock(1) Chris Friesen
2004-09-24 20:46     ` mlock(1) Chris Wright
2004-09-24 20:54       ` mlock(1) Chris Friesen
2004-09-24 20:59         ` mlock(1) Chris Wright
2004-09-24 22:48     ` mlock(1) Ryan Cumming
2004-09-24 21:07   ` mlock(1) Alan Cox
2004-09-24 22:19     ` mlock(1) Chris Wright
2004-09-24 22:30       ` mlock(1) Jeff Garzik
2004-09-24 23:08         ` mlock(1) Chris Wright
2004-09-24 22:59     ` mlock(1) Andrea Arcangeli
2004-09-24 23:46       ` mlock(1) Nigel Cunningham
2004-09-25  1:07         ` mlock(1) Andrea Arcangeli
2004-09-25  1:21           ` mlock(1) David Lang
2004-09-25  1:30             ` mlock(1) Andrea Arcangeli
2004-09-25  1:46               ` mlock(1) Valdis.Kletnieks
2004-09-25  2:15                 ` mlock(1) Andrea Arcangeli
2004-09-25  2:46                   ` mlock(1) Valdis.Kletnieks
2004-09-25  2:58                     ` mlock(1) Andrea Arcangeli
2004-09-25  3:29                       ` mlock(1) Valdis.Kletnieks
2004-09-25  4:07                         ` mlock(1) Andrea Arcangeli
2004-09-25  4:52                           ` mlock(1) Valdis.Kletnieks
2004-09-25 17:15                         ` mlock(1) Andy Lutomirski
2004-09-25  2:33                 ` mlock(1) Bernd Eckenfels
2004-09-25  1:27           ` mlock(1) Andrea Arcangeli
2004-09-28 22:03             ` mlock(1) Robert White
2004-09-28 22:15               ` mlock(1) Andrea Arcangeli
2004-09-28 23:26                 ` mlock(1) Robert White
2004-09-29  1:16                   ` mlock(1) Jon Masters
2004-09-29  1:23                     ` mlock(1) Alan Cox
2004-09-29  3:46                     ` mlock(1) Robert White
2004-09-29 12:34                       ` mlock(1) Jon Masters
2004-09-29 15:57                       ` mlock(1) Lee Revell
2004-09-29 22:56                         ` mlock(1) Paul Jackson
2004-09-25 12:21           ` mlock(1) Nigel Cunningham
2004-09-25 14:53             ` mlock(1) Andrea Arcangeli
2004-09-28  8:48               ` mlock(1) Pavel Machek
2004-09-30 17:42                 ` mlock(1) Andrea Arcangeli
2004-09-30 18:54                   ` mlock(1) Pavel Machek
2004-09-30 19:17                     ` mlock(1) Andrea Arcangeli
2004-09-30 19:52                       ` mlock(1) Pavel Machek
2004-10-04 12:21                   ` mlock(1) Jack Lloyd
2004-09-24 23:59       ` mlock(1) Bernd Eckenfels
2004-09-25  0:25         ` mlock(1) Nigel Cunningham
2004-09-25  1:18           ` mlock(1) Andrea Arcangeli
2004-09-27  6:16             ` mlock(1) Stefan Seyfried
2004-09-27 10:32               ` mlock(1) Nigel Cunningham
2004-09-27 14:29                 ` mlock(1) Andrea Arcangeli
2004-09-27 20:32                   ` mlock(1) Wolfgang Walter
2004-09-27 14:16               ` mlock(1) Andrea Arcangeli
2004-09-27 13:31                 ` mlock(1) Alan Cox
2004-09-29  1:48                   ` mlock(1) Andrea Arcangeli
2004-09-27 14:34                 ` mlock(1) Stefan Seyfried
2004-09-27 15:07                   ` mlock(1) Andrea Arcangeli
2004-09-27 15:25                     ` mlock(1) Stefan Seyfried
2004-09-27 15:38                       ` mlock(1) Andrea Arcangeli
2004-09-30 13:04                     ` mlock(1) Pavel Machek
2004-09-27 22:22                 ` mlock(1) Nigel Cunningham
2004-09-27 22:43                   ` mlock(1) Andrea Arcangeli
2004-09-28 22:03                     ` mlock(1) Nigel Cunningham
2004-09-24 20:24 ` mlock(1) Chris Friesen
2004-09-24 21:17 ` mlock(1) Andrew Morton
2004-09-25  0:26   ` Chris Wright [this message]
2004-09-25  1:28     ` mlock(1) Andrew Morton
2004-09-25  1:33       ` mlock(1) Chris Wright

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=20040924172611.Y1973@build.pdx.osdl.net \
    --to=chrisw@osdl.org \
    --cc=akpm@osdl.org \
    --cc=jgarzik@pobox.com \
    --cc=linux-kernel@vger.kernel.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

all inboxes | Powered by JetHome®