Re: mmap and MAP_STACK

2020-10-21 Thread Nick Kostirya via freebsd-stable
On Wed, 21 Oct 2020 18:28:44 +0300
Konstantin Belousov  wrote:


> > 
> > kdump with MAP_STACK.
> > 
> >  87183 polyimport CALL  
> > mmap(0,0x1000,0x3,0x1402,0x,0,0)
> >  87183 polyimport RET   mmap -1 errno 22 Invalid argument  
> So it is anything but 'insufficient memory' (I suspected ENOMEM).
> EINVAL there is because sysctl security.bsd.stack_guard_page default value
> is 1, which means that at least one page of the stack is reserved as guard.
> Kernel does not allow to map stack that would have no data pages (all pages
> are guard).
> 
> Your mapping request is for one page, and one page is due to guard, so
> you get EINVAL.  Generally MAP_STACK is magic and requires caller to know
> what it does.

Thanks!
___
freebsd-stable@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"


Re: mmap and MAP_STACK

2020-10-21 Thread Konstantin Belousov
On Wed, Oct 21, 2020 at 06:18:50PM +0300, Nick Kostirya via freebsd-stable 
wrote:
> On Wed, 21 Oct 2020 17:16:57 +0300
> Konstantin Belousov  wrote:
> 
> > On Wed, Oct 21, 2020 at 04:53:11PM +0300, Nick Kostirya via freebsd-stable 
> > wrote:
> > > Hello.
> > > I have question about mmap.
> > > 
> > > void *OSMem::AllocateDataArea(size_t &space)
> > > {
> > > // Round up to an integral number of pages.
> > > space = (space + pageSize-1) & ~(pageSize-1);
> > > int fd = -1; // This value is required by FreeBSD.  Linux doesn't care
> > > int flags = MAP_PRIVATE | MAP_ANON;
> > > #ifdef MAP_STACK
> > > if (memUsage == UsageStack) flags |= MAP_STACK; // OpenBSD seems to 
> > > require this
> > > #endif
> > > void *result = mmap(0, space, PROT_READ|PROT_WRITE, flags, fd, 0);
> > > // Convert MAP_FAILED (-1) into NULL
> > > if (result == MAP_FAILED)
> > > return 0;
> > > return result;
> > > }
> > > 
> > > 
> > > When MAP_STACK is used, "insufficient memory" error occurs.
> > > When MAP_STACK removed, it is all right.
> > > 
> > > Please tell me why.  
> > Show ktrace/kdump output of the mmap(2) without and with MAP_STACK.
> > 
> > Or provide a minimal self-contained C source that demonstrates your
> > issue.
> 
> kdump with MAP_STACK.
> 
>  87183 polyimport CALL  
> mmap(0,0x1000,0x3,0x1402,0x,0,0)
>  87183 polyimport RET   mmap -1 errno 22 Invalid argument
So it is anything but 'insufficient memory' (I suspected ENOMEM).
EINVAL there is because sysctl security.bsd.stack_guard_page default value
is 1, which means that at least one page of the stack is reserved as guard.
Kernel does not allow to map stack that would have no data pages (all pages
are guard).

Your mapping request is for one page, and one page is due to guard, so
you get EINVAL.  Generally MAP_STACK is magic and requires caller to know
what it does.

> 
> 
> kdump without MAP_STACK.
> 
>  93712 polyimport CALL  
> mmap(0,0x1000,0x3,0x1002,0x,0,0)
>  93712 polyimport RET   mmap 547053568/0x209b6000
> 
> 
> 
> ___
> freebsd-stable@freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-stable
> To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"
___
freebsd-stable@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"


Re: mmap and MAP_STACK

2020-10-21 Thread Nick Kostirya via freebsd-stable
On Wed, 21 Oct 2020 17:16:57 +0300
Konstantin Belousov  wrote:

> On Wed, Oct 21, 2020 at 04:53:11PM +0300, Nick Kostirya via freebsd-stable 
> wrote:
> > Hello.
> > I have question about mmap.
> > 
> > void *OSMem::AllocateDataArea(size_t &space)
> > {
> > // Round up to an integral number of pages.
> > space = (space + pageSize-1) & ~(pageSize-1);
> > int fd = -1; // This value is required by FreeBSD.  Linux doesn't care
> > int flags = MAP_PRIVATE | MAP_ANON;
> > #ifdef MAP_STACK
> > if (memUsage == UsageStack) flags |= MAP_STACK; // OpenBSD seems to 
> > require this
> > #endif
> > void *result = mmap(0, space, PROT_READ|PROT_WRITE, flags, fd, 0);
> > // Convert MAP_FAILED (-1) into NULL
> > if (result == MAP_FAILED)
> > return 0;
> > return result;
> > }
> > 
> > 
> > When MAP_STACK is used, "insufficient memory" error occurs.
> > When MAP_STACK removed, it is all right.
> > 
> > Please tell me why.  
> Show ktrace/kdump output of the mmap(2) without and with MAP_STACK.
> 
> Or provide a minimal self-contained C source that demonstrates your
> issue.

kdump with MAP_STACK.

 87183 polyimport CALL  
mmap(0,0x1000,0x3,0x1402,0x,0,0)
 87183 polyimport RET   mmap -1 errno 22 Invalid argument


kdump without MAP_STACK.

 93712 polyimport CALL  
mmap(0,0x1000,0x3,0x1002,0x,0,0)
 93712 polyimport RET   mmap 547053568/0x209b6000



___
freebsd-stable@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"


Re: mmap and MAP_STACK

2020-10-21 Thread Konstantin Belousov
On Wed, Oct 21, 2020 at 04:53:11PM +0300, Nick Kostirya via freebsd-stable 
wrote:
> Hello.
> I have question about mmap.
> 
> void *OSMem::AllocateDataArea(size_t &space)
> {
> // Round up to an integral number of pages.
> space = (space + pageSize-1) & ~(pageSize-1);
> int fd = -1; // This value is required by FreeBSD.  Linux doesn't care
> int flags = MAP_PRIVATE | MAP_ANON;
> #ifdef MAP_STACK
> if (memUsage == UsageStack) flags |= MAP_STACK; // OpenBSD seems to 
> require this
> #endif
> void *result = mmap(0, space, PROT_READ|PROT_WRITE, flags, fd, 0);
> // Convert MAP_FAILED (-1) into NULL
> if (result == MAP_FAILED)
> return 0;
> return result;
> }
> 
> 
> When MAP_STACK is used, "insufficient memory" error occurs.
> When MAP_STACK removed, it is all right.
> 
> Please tell me why.
Show ktrace/kdump output of the mmap(2) without and with MAP_STACK.

Or provide a minimal self-contained C source that demonstrates your
issue.
___
freebsd-stable@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"


mmap and MAP_STACK

2020-10-21 Thread Nick Kostirya via freebsd-stable
Hello.
I have question about mmap.

void *OSMem::AllocateDataArea(size_t &space)
{
// Round up to an integral number of pages.
space = (space + pageSize-1) & ~(pageSize-1);
int fd = -1; // This value is required by FreeBSD.  Linux doesn't care
int flags = MAP_PRIVATE | MAP_ANON;
#ifdef MAP_STACK
if (memUsage == UsageStack) flags |= MAP_STACK; // OpenBSD seems to require 
this
#endif
void *result = mmap(0, space, PROT_READ|PROT_WRITE, flags, fd, 0);
// Convert MAP_FAILED (-1) into NULL
if (result == MAP_FAILED)
return 0;
return result;
}


When MAP_STACK is used, "insufficient memory" error occurs.
When MAP_STACK removed, it is all right.

Please tell me why.
___
freebsd-stable@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"