linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* memcg versus clone(CLONE_VM without CLONE_THREAD)
@ 2018-10-23 13:05 Tetsuo Handa
  2018-10-23 16:55 ` Michal Hocko
  0 siblings, 1 reply; 2+ messages in thread
From: Tetsuo Handa @ 2018-10-23 13:05 UTC (permalink / raw)
  To: linux-mm

I noticed that memcg OOM event does not trigger as expected when a thread
group ID assigned by clone(CLONE_VM without CLONE_THREAD) is specified.
A bit of surprise because what the "tasks" file says is not what the
limitation is applied to...

----------
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sched.h>
#include <unistd.h>

static int memory_eater(void *unused) {
	FILE *fp;
	const unsigned long size = 1048576 * 200;
	char *buf = malloc(size);
	mkdir("/sys/fs/cgroup/memory/test1", 0755);
	fp = fopen("/sys/fs/cgroup/memory/test1/memory.limit_in_bytes", "w");
	fprintf(fp, "%lu\n", size / 2);
	fclose(fp);
	fp = fopen("/sys/fs/cgroup/memory/test1/tasks", "w");
	fprintf(fp, "%u\n", getpid());
	fclose(fp);
	fp = fopen("/dev/zero", "r");
	fread(buf, 1, size, fp);
	fclose(fp);
	return 0;
}

int main(int argc, char *argv[])
{
	if (clone(memory_eater, malloc(8192) + 8192,
		  /*CLONE_SIGHAND | CLONE_THREAD | */CLONE_VM, NULL) == -1)
		return 1;
	while (1)
		pause();
	return 0;
}
----------

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

* Re: memcg versus clone(CLONE_VM without CLONE_THREAD)
  2018-10-23 13:05 memcg versus clone(CLONE_VM without CLONE_THREAD) Tetsuo Handa
@ 2018-10-23 16:55 ` Michal Hocko
  0 siblings, 0 replies; 2+ messages in thread
From: Michal Hocko @ 2018-10-23 16:55 UTC (permalink / raw)
  To: Tetsuo Handa; +Cc: linux-mm

On Tue 23-10-18 22:05:12, Tetsuo Handa wrote:
> I noticed that memcg OOM event does not trigger as expected when a thread
> group ID assigned by clone(CLONE_VM without CLONE_THREAD) is specified.
> A bit of surprise because what the "tasks" file says is not what the
> limitation is applied to...

Well, the issue is that the memcg is tracked by mm_struct while cgroups
organize by task_structs. So we have a concept of mm owner which
determines the memcg a task belongs to. In your case the owner is the
main process and that one doesn't run in the limited cgroup.

This is btw. a source of pain - e.g. have a look at
mm_update_next_owner.

> 
> ----------
> #define _GNU_SOURCE
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <fcntl.h>
> #include <sched.h>
> #include <unistd.h>
> 
> static int memory_eater(void *unused) {
> 	FILE *fp;
> 	const unsigned long size = 1048576 * 200;
> 	char *buf = malloc(size);
> 	mkdir("/sys/fs/cgroup/memory/test1", 0755);
> 	fp = fopen("/sys/fs/cgroup/memory/test1/memory.limit_in_bytes", "w");
> 	fprintf(fp, "%lu\n", size / 2);
> 	fclose(fp);
> 	fp = fopen("/sys/fs/cgroup/memory/test1/tasks", "w");
> 	fprintf(fp, "%u\n", getpid());
> 	fclose(fp);
> 	fp = fopen("/dev/zero", "r");
> 	fread(buf, 1, size, fp);
> 	fclose(fp);
> 	return 0;
> }
> 
> int main(int argc, char *argv[])
> {
> 	if (clone(memory_eater, malloc(8192) + 8192,
> 		  /*CLONE_SIGHAND | CLONE_THREAD | */CLONE_VM, NULL) == -1)
> 		return 1;
> 	while (1)
> 		pause();
> 	return 0;
> }
> ----------

-- 
Michal Hocko
SUSE Labs

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

end of thread, other threads:[~2018-10-23 16:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-23 13:05 memcg versus clone(CLONE_VM without CLONE_THREAD) Tetsuo Handa
2018-10-23 16:55 ` Michal Hocko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox