Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp471097imm; Wed, 22 Aug 2018 07:21:57 -0700 (PDT) X-Google-Smtp-Source: AA+uWPwmOE/7whiMymZGLImC0hT+q/KmJRgjs2ygbIuuyKoCT+4IdcY9+ZHwnQLcpegGvr+2wuXJ X-Received: by 2002:a17:902:904c:: with SMTP id w12-v6mr54528694plz.95.1534947717696; Wed, 22 Aug 2018 07:21:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534947717; cv=none; d=google.com; s=arc-20160816; b=0LskSp23+uHjuw22b0ffO/x96x0guYpcWr4AhhiUB/WNp5L1a+TBMcvYiV/4SfpEb8 nUFfJfZd2bs8O1H1aDDmdMNVrLCBD7Fse5lk+M0P1vlG+Pd4bftq1LrGbocQ4OE9pSmB j9tiLFMT+5a09G9W4XrPV+Al+k4RELz9gKzZ0KFNjd0Bsqo7tvf4IRC03ptC7EHkgVkZ SJ8+3P1zV6UJW8fOmWAc7JQPHG2tEZumD5Qwx8XrDJzU96QPHwbVXr5dwzUrOCLJX8ao ddpTNSpV+gB3WIjcf5AYeUI2ukHbviPVAwSPzNuEOMsRPVgYbPu4Ys9BgOg6zzl5Vqml t96A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=COfIeVRucXTiqu4OArMF7LCS3GV+lh6vpdHN2zFGwpk=; b=BsrBBCZ3RM+kzF9+m0E+nT9cTqQLbBl1T45t0JW65afwV1n6kybhyuFIi+r8dYJfu6 BhZZB0hr+C7/zbqNaH7/Tn0oYOLzv+EyvKKnLBSZ46qhDrNmDr8hztCspxrc/e+kVo3r /MBzKfhMxH+P8Uxmbbg+X0LrQmTvig3QrBQJDTQfwtQ0d3dBFhzwmO2Fi1b2hGoAXViP UxI7J3IoLxp5imYnFvpF8hOgm+LDNMKCubqdek2PVPULb4EUHfNNCyZuXSjFXsXUtaxW Y2z+H8bFFbIfeNuYEANocz7V3FdXkRh8ZQuaiN8vnu4i3uAltUxJQEPqCZaKjOZR41ZY TQJA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k3-v6si1533987pld.6.2018.08.22.07.21.42; Wed, 22 Aug 2018 07:21:57 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728929AbeHVRhU (ORCPT + 99 others); Wed, 22 Aug 2018 13:37:20 -0400 Received: from mx2.suse.de ([195.135.220.15]:39314 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728057AbeHVRhT (ORCPT ); Wed, 22 Aug 2018 13:37:19 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 37617B030; Wed, 22 Aug 2018 14:12:14 +0000 (UTC) Date: Wed, 22 Aug 2018 16:12:13 +0200 From: Michal Hocko To: Roman Gushchin Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@fb.com, Johannes Weiner , Andy Lutomirski , Konstantin Khlebnikov , Tejun Heo , Shakeel Butt Subject: Re: [PATCH v2 1/3] mm: rework memcg kernel stack accounting Message-ID: <20180822141213.GO29735@dhcp22.suse.cz> References: <20180821213559.14694-1-guro@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180821213559.14694-1-guro@fb.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue 21-08-18 14:35:57, Roman Gushchin wrote: > If CONFIG_VMAP_STACK is set, kernel stacks are allocated > using __vmalloc_node_range() with __GFP_ACCOUNT. So kernel > stack pages are charged against corresponding memory cgroups > on allocation and uncharged on releasing them. > > The problem is that we do cache kernel stacks in small > per-cpu caches and do reuse them for new tasks, which can > belong to different memory cgroups. > > Each stack page still holds a reference to the original cgroup, > so the cgroup can't be released until the vmap area is released. > > To make this happen we need more than two subsequent exits > without forks in between on the current cpu, which makes it > very unlikely to happen. As a result, I saw a significant number > of dying cgroups (in theory, up to 2 * number_of_cpu + > number_of_tasks), which can't be released even by significant > memory pressure. > > As a cgroup structure can take a significant amount of memory > (first of all, per-cpu data like memcg statistics), it leads > to a noticeable waste of memory. > > Signed-off-by: Roman Gushchin > Cc: Johannes Weiner > Cc: Michal Hocko > Cc: Andy Lutomirski > Cc: Konstantin Khlebnikov > Cc: Tejun Heo > Cc: Shakeel Butt Looks good to me. Two nits below. I am not sure stable tree backport is really needed but it would be nice to put Fixes: ac496bf48d97 ("fork: Optimize task creation by caching two thread stacks per CPU if CONFIG_VMAP_STACK=y") Acked-by: Michal Hocko > @@ -248,9 +253,20 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) > static inline void free_thread_stack(struct task_struct *tsk) > { > #ifdef CONFIG_VMAP_STACK > - if (task_stack_vm_area(tsk)) { > + struct vm_struct *vm = task_stack_vm_area(tsk); > + > + if (vm) { > int i; > > + for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { > + mod_memcg_page_state(vm->pages[i], > + MEMCG_KERNEL_STACK_KB, > + -(int)(PAGE_SIZE / 1024)); > + > + memcg_kmem_uncharge(vm->pages[i], > + compound_order(vm->pages[i])); when do we have order > 0 here? Also I was wondering how come this doesn't blow up on partially charged stacks but both mod_memcg_page_state and memcg_kmem_uncharge check for page->mem_cgroup so this is safe. Maybe a comment would save people from scratching their heads. -- Michal Hocko SUSE Labs