mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Zhang, Yanmin" <yanmin_zhang@linux.intel.com>
To: Andrew Morton <akpm@osdl.org>
Cc: david@gibson.dropbear.id.au,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	kenneth.w.chen@intel.com,
	"yanmin.zhang@intel.com" <yanmin.zhang@intel.com>,
	davem@davemloft.net, paulus@samba.org, benh@kernel.crashing.org,
	wli@holomorphy.com, lethal@linux-sh.org, kkojima@rr.iij4u.or.jp,
	"tony.luck@intel.com" <tony.luck@intel.com>
Subject: Re: [PATCH] Enable mprotect on huge pages
Date: Tue, 28 Feb 2006 11:23:54 +0800	[thread overview]
Message-ID: <1141097034.3898.10.camel@ymzhang-perf.sh.intel.com> (raw)
In-Reply-To: <20060227173449.26c79a44.akpm@osdl.org>

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

On Tue, 2006-02-28 at 09:34, Andrew Morton wrote:
> "Zhang, Yanmin" <yanmin_zhang@linux.intel.com> wrote:
> >
> > > > > > 2.6.16-rc3 uses hugetlb on-demand paging, but it doesn_t support hugetlb
> >  > > > > mprotect. My patch against 2.6.16-rc3 enables this capability.
> > 
> >  Based on David's comments, I worked out a new patch against 2.6.16-rc4.
> >  Thank David.
> > 
> 
> Please always send an updated changelog when sending an updated patch. 
> Otherwise I have to go trolling back through the email thread to find it,
> then work out what needs to be changed.
Thanks for your kind reminder. I would do so next time.

> 
> > 
> >  I tested it on i386/x86_64/ia64. Who could help test it on other
> >  platforms, such like PPC64?
> 
> I can do that - please send me your test app?
I attach a test case. It will create directory /mnt/hugepages and delete it
after testing automatically.

To run it by user root:
#gcc -o mprotect_testcase mprotect_testcase.c
#echo "5">/proc/sys/vm/nr_hugepages
#./mprotect_testcase

You could use gdb to step it to see the changing of the process vma maps.

Thanks.



[-- Attachment #2: mprotect_testcase.c --]
[-- Type: text/x-c, Size: 3514 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <setjmp.h>

#define WORK_DIR "/mnt/hugepages"
#define MMAP_DIR "/mnt/hugepages/mmap"
#define MMAP_FILE "/mnt/hugepages/mmap/mmap_file"

char buff[2000];

int get_htlb_mem_stat(int *total_pages, int *free_pages, long *page_size)
{
        int fd=0;
        int size1=0;
        char *start=0;

        buff[0] = '\0';
        if((fd=open("/proc/meminfo", O_RDONLY)) < 0)
        {
                printf("Current kernel does not support Huge TLB!\n");
                exit(-1);
        }

        size1 = pread(fd, buff, sizeof(buff), 0);
        if(size1>=sizeof(buff))
        {
                size1 = sizeof(buff)-1;
        }
        if(size1<0)
                size1 = 0;

        buff[size1] = '\0';
	start = buff;
	while(*start) {
		if(isupper(*start))
			*start = tolower(*start);
		start ++;
	}

	start = strstr(buff, "hugepagesize:");
	if(start==NULL)
	{
                printf("Current kernel does not support Huge TLB!\n");
                exit(-1);
        }
        start += sizeof("hugepagesize:");
        if(sscanf(start,"%ld", page_size) != 1)
        {
                printf("Current kernel does not support Huge TLB!\n");
                exit(-1);
        }
        *page_size *= 1024; /*Convert to BYTE from KB*/

        close(fd);

        return 0;
}


int mmap_open_mmap(int *fd, void **addr, size_t length, int prot, int flags)
{
	*fd = open(MMAP_FILE, O_CREAT|O_RDWR, 0755);
	if (*fd < 0) {
		/*perror("Open failed");*/
		exit(errno);
	}
	*addr = mmap(NULL, length, prot, flags, *fd, 0);
	if( *addr == (void *)-1 ) {
		/*perror("mmap failed");*/
		close(*fd);
		*fd = -1;
		return -1;
	}

	return 0;
}

int mmap_unmap(void *start, size_t length)
{
	int     result=0;

	result = munmap(start, length);
	if(result == -1 ) {
		/*perror("mmap_unmap failed");*/
		return -1;
	}

	return 0;
}

int test_mmap_mprotect()
{
	int result = -1;
	void *addr=0;
	long tt001;
	int	fd=-1;

	int	total_pages;
	int	free_pages;
	long	LPAGE_SIZE;

	printf("Test mmap mprotect ...");
	get_htlb_mem_stat(&total_pages, &free_pages, &LPAGE_SIZE);
	result = mount("none", MMAP_DIR, "hugetlbfs", 0, NULL);
	if(result != 0) {
		perror("Mount fail:");
		goto out1;
	}

	result = mmap_open_mmap(&fd, &addr, LPAGE_SIZE*3, PROT_READ|PROT_WRITE, MAP_SHARED);
	if(result < 0)
		goto out2;

	munmap(addr, LPAGE_SIZE*3);
	close(fd);

	result = mmap_open_mmap(&fd, &addr, LPAGE_SIZE*3, PROT_NONE, MAP_SHARED);
	if(result < 0)
		goto out2;

	if(mprotect(addr, LPAGE_SIZE*3, PROT_READ)) {
		printf("fail!\n");
		goto out2;
	}

	tt001 = ((long *)addr)[0];

	if(mprotect(addr+LPAGE_SIZE, LPAGE_SIZE, PROT_READ|PROT_WRITE)) {
		printf("fail!\n");
		goto out2;
	}
	((char *)addr)[LPAGE_SIZE+100] = tt001;

	if(mprotect(addr+LPAGE_SIZE, LPAGE_SIZE, PROT_READ)) {
		printf("fail!\n");
		goto out2;
	}

	printf("pass!\n");

out3:
	if(result < 0)
		mmap_unmap(addr, LPAGE_SIZE*3);
	else
		result = mmap_unmap(addr, LPAGE_SIZE*3);
out2:
	close(fd);
	if(result < 0)
		umount(MMAP_DIR);
	else
		result = umount(MMAP_DIR);
out1:
	return result;
}

void init()
{
	mkdir(WORK_DIR, 0777);
	mkdir(MMAP_DIR, 0755);
}

void uninit()
{
	rmdir(MMAP_DIR);
	rmdir(WORK_DIR);
}

int main(int argc, char * argv)
{
	int	total_pages=0;
	int	free_pages=0;
	int	result=0;

	init();

	test_mmap_mprotect();

	uninit();
	return 0;
}



  reply	other threads:[~2006-02-28  3:27 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-23  3:19 Zhang, Yanmin
2006-02-24 22:28 ` Andrew Morton
2006-02-26 23:09   ` David Gibson
2006-02-27  5:36     ` Zhang, Yanmin
2006-02-27  6:33       ` Zhang, Yanmin
2006-02-28  1:34         ` Andrew Morton
2006-02-28  3:23           ` Zhang, Yanmin [this message]
2006-02-28  3:32             ` David Gibson
2006-02-28  3:37               ` Zhang, Yanmin
2006-02-28  8:24             ` David Gibson
2006-02-27  7:26       ` David Gibson
2006-02-25  8:54 ` Christoph Hellwig
2006-02-25 10:08   ` [2.4.32 - 2.6.15.4] e1000 - Fix mii interface Paul Rolland
2006-02-26 10:42     ` Willy TARREAU
2006-02-26 11:39       ` Paul Rolland
2006-02-26 12:59     ` Jesper Juhl
2006-02-26 14:55       ` Paul Rolland
2006-02-26 15:00         ` Jesper Juhl
2006-02-26 15:12           ` Paul Rolland
2006-02-27 19:26             ` Jesse Brandeburg
     [not found]     ` <4807377b0602271234v4b6cdeecpbcf8d4a6ac51cd20@mail.gmail.com>
2006-02-28  2:31       ` Jesse Brandeburg
2006-02-28 10:46         ` Paul Rolland
2006-02-27  5:09   ` [PATCH] Enable mprotect on huge pages Zhang, Yanmin

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=1141097034.3898.10.camel@ymzhang-perf.sh.intel.com \
    --to=yanmin_zhang@linux.intel.com \
    --cc=akpm@osdl.org \
    --cc=benh@kernel.crashing.org \
    --cc=davem@davemloft.net \
    --cc=david@gibson.dropbear.id.au \
    --cc=kenneth.w.chen@intel.com \
    --cc=kkojima@rr.iij4u.or.jp \
    --cc=lethal@linux-sh.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paulus@samba.org \
    --cc=tony.luck@intel.com \
    --cc=wli@holomorphy.com \
    --cc=yanmin.zhang@intel.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.
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®