Kernel Newbies archive on lore.kernel.org
 help / color / Atom feed
* ULIMIT: limiting user virtual address space (stack inluded)
@ 2019-05-02 13:06 Karaoui mohamed lamine
  2019-05-02 14:24 ` Greg KH
  2019-05-02 15:50 ` Valdis Klētnieks
  0 siblings, 2 replies; 3+ messages in thread
From: Karaoui mohamed lamine @ 2019-05-02 13:06 UTC (permalink / raw)
  To: kernelnewbies

[-- Attachment #1.1: Type: text/plain, Size: 704 bytes --]

Hi all,

According to the man page of "ulimit", it is possible to limit the virtual
address space of a user process: https://ss64.com/bash/ulimit.html
To limit the virtual address space to 2 GB:
$ ulimit -SH -v 1048576

However, it seems that "stack" allocation ignores this limit (I tried with
both kernel 4.4 and 4.15).

I found this article (patch) that seems to fix the problem:
https://lwn.net/Articles/373701/

My questions are:
- Why is the stack allocation not placed within the "ulimit" limit? (is it
a bug?)
- Is there a way, in user space, to force the stack to be allocated at a
certain address?
- Will the patch above (or similar) ever be integrated into the Linux
kernel?

Regards,
Mohamed

[-- Attachment #1.2: Type: text/html, Size: 1093 bytes --]

<div dir="ltr"><div dir="ltr">Hi all,<div><br></div><div>According to the man page of &quot;ulimit&quot;, it is possible to limit the virtual address space of a user process: <a href="https://ss64.com/bash/ulimit.html">https://ss64.com/bash/ulimit.html</a></div><div>To limit the virtual address space to 2 GB:<br></div><div>$ ulimit -SH -v 1048576 </div><div><br></div><div>However, it seems that &quot;stack&quot; allocation ignores this limit (I tried with both kernel 4.4 and 4.15).</div><div><br></div><div>I found this article (patch) that seems to fix the problem:</div><div><a href="https://lwn.net/Articles/373701/">https://lwn.net/Articles/373701/</a><br></div><div><br></div><div>My questions are: </div><div>- Why is the stack allocation not placed within the &quot;ulimit&quot; limit? (is it a bug?)</div><div>- Is there a way, in user space, to force the stack to be allocated at a certain address? </div><div>- Will the patch above (or similar) ever be integrated into the Linux kernel?</div><div><br></div><div>Regards,</div><div>Mohamed</div><div><br></div></div></div>

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: ULIMIT: limiting user virtual address space (stack inluded)
  2019-05-02 13:06 ULIMIT: limiting user virtual address space (stack inluded) Karaoui mohamed lamine
@ 2019-05-02 14:24 ` Greg KH
  2019-05-02 15:50 ` Valdis Klētnieks
  1 sibling, 0 replies; 3+ messages in thread
From: Greg KH @ 2019-05-02 14:24 UTC (permalink / raw)
  To: Karaoui mohamed lamine; +Cc: kernelnewbies

On Thu, May 02, 2019 at 09:06:09AM -0400, Karaoui mohamed lamine wrote:
> Hi all,
> 
> According to the man page of "ulimit", it is possible to limit the virtual
> address space of a user process: https://ss64.com/bash/ulimit.html
> To limit the virtual address space to 2 GB:
> $ ulimit -SH -v 1048576
> 
> However, it seems that "stack" allocation ignores this limit (I tried with
> both kernel 4.4 and 4.15).
> 
> I found this article (patch) that seems to fix the problem:
> https://lwn.net/Articles/373701/
> 
> My questions are:
> - Why is the stack allocation not placed within the "ulimit" limit? (is it
> a bug?)
> - Is there a way, in user space, to force the stack to be allocated at a
> certain address?
> - Will the patch above (or similar) ever be integrated into the Linux
> kernel?

The patch above was integrated into the 2.6.33 kernel, and backported to
2.6.32.9 and fixed in 2.6.32.10.  All of those kernels were released in
2010, quite a long time ago.

Are you sure you are hitting the same "problem"?

thanks,

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: ULIMIT: limiting user virtual address space (stack inluded)
  2019-05-02 13:06 ULIMIT: limiting user virtual address space (stack inluded) Karaoui mohamed lamine
  2019-05-02 14:24 ` Greg KH
@ 2019-05-02 15:50 ` Valdis Klētnieks
  1 sibling, 0 replies; 3+ messages in thread
From: Valdis Klētnieks @ 2019-05-02 15:50 UTC (permalink / raw)
  To: Karaoui mohamed lamine; +Cc: kernelnewbies

[-- Attachment #1.1: Type: text/plain, Size: 2900 bytes --]

On Thu, 02 May 2019 09:06:09 -0400, Karaoui mohamed lamine said:

> According to the man page of "ulimit", it is possible to limit the virtual
> address space of a user process: https://ss64.com/bash/ulimit.html
> To limit the virtual address space to 2 GB:
> $ ulimit -SH -v 1048576

It's possible.  It also probably doesn't do what you think it does. In particular,
you may be looking for the -d (data) and/or -m (memory) flags.

Also, to limit it to 2G, you're going to need 'ulimit -v 2097152'

> However, it seems that "stack" allocation ignores this limit (I tried with
> both kernel 4.4 and 4.15).

I'm going to go out on a limb and say that what you think is happening
is not in fact what is happening.

> I found this article (patch) that seems to fix the problem:
> https://lwn.net/Articles/373701/

It's unclear what your actual problem is, but given that the patch
has been in the kernel for a decade, it's almost certainly not related.

> My questions are:
> - Why is the stack allocation not placed within the "ulimit" limit? (is it
> a bug?)

Rule of thumb:  Before asking why something happens, verify that it does
in fact happen.

Consider the following C program:

[~] cat > moby-stack.c
#include <unistd.h>

#define MOBYSTACK 16384
#define BIG 2048

int recurse(int level) {
double a[BIG]; /* chew up stack space */

if (level == MOBYSTACK) sleep(9999999);
recurse(level+1);
}

int main() {recurse(0); };
^D
[~] gcc moby-stack.c
[~] ulimit -v 16384
[~] ulimit -s 32768
[~] ./a.out
Segmentation fault (core dumped)

Examination of the core file shows it created 895 stack entries, for a total of
895 * 8 * 2048 or 14,663,680 or so bytes of stack. That's about what you'd
expect given a virtual limit of 16M, and a bit of space taken up by shared
libraries and so on. Re-running with a virtual limit of 32768 lets it get 1916
stack entries deep. Again, that's about where you'd expect the explosion to
happen with that virtual limit in place. So regardless of the stack limit, it
blows up when it gets to the -v limit.

Conclusion: The stack *is* included in the -v virtual limit.

> - Is there a way, in user space, to force the stack to be allocated at a
> certain address?

Sure.  mmap() yourself a chunk of anonymous pages at the address you want,
and call a little assembler stub that sets your registers.  You'll probably have
to ensure you allocate enough pages, or handle expanding onto new pages
yourself (including making sure there's free pages to grow into).  And of course,
you're basically stuck with that stack, returning to older functions on the old
stack is going to be *really* hard.  So you'll probably want to do that in main().

Google for 'linux green threads' for the gory details.  Java used them on Linux until 2017
or so.

> - Will the patch above (or similar) ever be integrated into the Linux
> kernel?

See above.  It's been in the kernel for a decade.

[-- Attachment #1.2: Type: application/pgp-signature, Size: 832 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-02 13:06 ULIMIT: limiting user virtual address space (stack inluded) Karaoui mohamed lamine
2019-05-02 14:24 ` Greg KH
2019-05-02 15:50 ` Valdis Klētnieks

Kernel Newbies archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kernelnewbies/0 kernelnewbies/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kernelnewbies kernelnewbies/ https://lore.kernel.org/kernelnewbies \
		kernelnewbies@kernelnewbies.org kernelnewbies@archiver.kernel.org
	public-inbox-index kernelnewbies


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernelnewbies.kernelnewbies


AGPL code for this site: git clone https://public-inbox.org/ public-inbox