Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp82625yba; Wed, 17 Apr 2019 20:09:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqwQG8+ubw8l2WrjvVOXqbg9eVB2tdQhMLipSf7ZoDzP1sXysphPa19kT8OrSms/efpJuXYn X-Received: by 2002:a65:494b:: with SMTP id q11mr3284659pgs.444.1555556948893; Wed, 17 Apr 2019 20:09:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555556948; cv=none; d=google.com; s=arc-20160816; b=rbf7I+x6tmQbeKrq5cIVw/q72S9eYVSJhLfP7yZYdrGxr9YWheHyeUvFj3jtVZBLcq /h6JTZof8X1bLzWMOkCSZ6VaBdu3/NBiSshzJbuPY3ngaBa6OAVYhc79GLc8IUbRtm5s bZrpAkSrfEhbnJPnlOLOtdtJyhyfPGzb4rQBxINVWrBNPtbLMBxtrS6Ycgp0yKCj9Maq x+zS2FLVq1oHYhVqWY0s4EDDmmA+hupiWPh62Xy38tB00Js3HSSJ43CuRU6z/7L2Lm/B LjclV9ZhgdbstaVD51p0X9hnl3jWirxUDPdraJmFjJAZQKKZrmCCV8VVubNkq24SHG3X BG4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :content-id:content-language:accept-language:in-reply-to:references :message-id:date:thread-index:thread-topic:subject:cc:to:from :dkim-signature:dkim-signature; bh=Z2dJelOQ0yl/7TG6RhC7wYBCcKk0cxBJ3I7/DHMqBN4=; b=IUFKRmdNwPX7SHuONrqM2ALU5FW9MslInNcbIFhZzXtFjG8tblprC9RTtJWwOWH5oV 7SFsAHY0tgQI2Qp3PbYWNoVfNgVHiqwXAueWV6/vN+GQ5/yyNR7Dtq+XFaIMqejKin3J NC7MlaXbIRkxVPtJ/1rV9tbJbDV6/t8ElajGUssLM6O2rMGpI3pmrOy0fiHnaviBPY6G 8GIMs0xSmWQKramhD77HAJsVZ2Xi1KZam47FiMDz42qlBRHjIDzDt9jtvnIkqSMEsN0y bn444Hf2pHmFpPddj001QUrw2/fBBXQ6T+F31wAtT7JJ4AD+lZ62aUD/Q83wNy+QNO50 HzwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b="D8ZKOD/I"; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=XkNACIXA; 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=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z8si660258pgf.593.2019.04.17.20.08.53; Wed, 17 Apr 2019 20:09:08 -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; dkim=pass header.i=@fb.com header.s=facebook header.b="D8ZKOD/I"; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=XkNACIXA; 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=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387891AbfDRDID (ORCPT + 99 others); Wed, 17 Apr 2019 23:08:03 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:40054 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1731985AbfDRDID (ORCPT ); Wed, 17 Apr 2019 23:08:03 -0400 Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.16.0.27/8.16.0.27) with SMTP id x3I37M9K003913; Wed, 17 Apr 2019 20:07:42 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-id : content-transfer-encoding : mime-version; s=facebook; bh=Z2dJelOQ0yl/7TG6RhC7wYBCcKk0cxBJ3I7/DHMqBN4=; b=D8ZKOD/Ihzyye6iV/EbZGj495tOCgRifPz41G+rx2tGqLvZ0zSAzFx5sMGmX6bqLXE2z SfFsGZWNSRelafkUPT+1rZeox0HmK9F9vCF3xxnlPspLknalz9GjFo2MunEcBXOkoj32 z/Qy5usbs1SqLZP02CA97sEdJWJ5pDKCiOw= Received: from mail.thefacebook.com (mailout.thefacebook.com [199.201.64.23]) by m0001303.ppops.net with ESMTP id 2rwm8mdmgv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Wed, 17 Apr 2019 20:07:42 -0700 Received: from prn-mbx06.TheFacebook.com (2620:10d:c081:6::20) by prn-hub06.TheFacebook.com (2620:10d:c081:35::130) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.1.1713.5; Wed, 17 Apr 2019 20:07:41 -0700 Received: from prn-hub05.TheFacebook.com (2620:10d:c081:35::129) by prn-mbx06.TheFacebook.com (2620:10d:c081:6::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.1.1713.5; Wed, 17 Apr 2019 20:07:40 -0700 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (192.168.54.28) by o365-in.thefacebook.com (192.168.16.29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.1.1713.5 via Frontend Transport; Wed, 17 Apr 2019 20:07:40 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com; s=selector1-fb-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Z2dJelOQ0yl/7TG6RhC7wYBCcKk0cxBJ3I7/DHMqBN4=; b=XkNACIXA9ktp4uZ/hi3GBQcWnc49gLlB5iH9DRdfGGvhW9Tb1MfrfIEQPw5WB7ZLZC0HUpOh/eU5vCwL69jrN9NHAmpmCQAKA99Ny8UD0H++Rg4GT/1HCKKNlEsa2R8BqdmU5Gi05no4GqWz5M4ATkukbjcwcp7/EanUMCQ6RkE= Received: from BYAPR15MB2631.namprd15.prod.outlook.com (20.179.156.24) by BYAPR15MB3477.namprd15.prod.outlook.com (20.179.60.17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1813.12; Thu, 18 Apr 2019 03:07:37 +0000 Received: from BYAPR15MB2631.namprd15.prod.outlook.com ([fe80::d1a1:d74:852:a21e]) by BYAPR15MB2631.namprd15.prod.outlook.com ([fe80::d1a1:d74:852:a21e%5]) with mapi id 15.20.1792.021; Thu, 18 Apr 2019 03:07:37 +0000 From: Roman Gushchin To: Shakeel Butt CC: Roman Gushchin , Andrew Morton , Linux MM , LKML , Kernel Team , "Johannes Weiner" , Michal Hocko , Rik van Riel , "david@fromorbit.com" , "Christoph Lameter" , Pekka Enberg , Vladimir Davydov , Cgroups Subject: Re: [PATCH 4/5] mm: rework non-root kmem_cache lifecycle management Thread-Topic: [PATCH 4/5] mm: rework non-root kmem_cache lifecycle management Thread-Index: AQHU9WhFLwAo90XwHUSqs2s91sh5hqZBAzaA//+a0QCAAIqtAIAAFDUA Date: Thu, 18 Apr 2019 03:07:37 +0000 Message-ID: <20190418030729.GA5038@castle> References: <20190417215434.25897-1-guro@fb.com> <20190417215434.25897-5-guro@fb.com> <20190418003850.GA13977@tower.DHCP.thefacebook.com> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MWHPR2201CA0040.namprd22.prod.outlook.com (2603:10b6:301:16::14) To BYAPR15MB2631.namprd15.prod.outlook.com (2603:10b6:a03:152::24) x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [2620:10d:c090:180::3f5] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 87abc140-bcc3-48df-d2c1-08d6c3ab02a1 x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(5600141)(711020)(4605104)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7193020);SRVR:BYAPR15MB3477; x-ms-traffictypediagnostic: BYAPR15MB3477: x-microsoft-antispam-prvs: x-forefront-prvs: 0011612A55 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(7916004)(376002)(346002)(136003)(366004)(39860400002)(396003)(55674003)(54094003)(189003)(199004)(33656002)(5660300002)(486006)(52116002)(476003)(76176011)(68736007)(8936002)(316002)(81166006)(99286004)(186003)(54906003)(11346002)(8676002)(53546011)(386003)(6916009)(46003)(6116002)(446003)(7736002)(6506007)(14444005)(71190400001)(71200400001)(305945005)(256004)(97736004)(6486002)(478600001)(7416002)(86362001)(102836004)(25786009)(81156014)(33716001)(1076003)(4326008)(53936002)(93886005)(9686003)(6246003)(229853002)(2906002)(6436002)(6512007)(14454004);DIR:OUT;SFP:1102;SCL:1;SRVR:BYAPR15MB3477;H:BYAPR15MB2631.namprd15.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; received-spf: None (protection.outlook.com: fb.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: OOvb08rZP7Ag0/s7hw+nAGUHEjK+WKpRCONorlIFbJvVjVX4Ra26585ye/81+fU/anYn24VszrAfWzKwPA9KTuYyAVf8IjRdy3lvPXuPGt2kkGkLuOh70lHnsJCVMvgXVoShi2m//9UF9YV/Zk5Q4tzf3VulxSZ0cFba0XLZVQpMkyDQQVAoyfNKfntVjwfC5d6804zMS9fS/lP4vi6JZxxH05eW+7e3yJd3/cdQ3vIZYKovrkb4Q6rcZ9Mey5JjBk46hhtR20EF5x/DMS715/AKkMeXhcwwthdaAF0B1BmqiI/JHBrDMnhgYdHieJlsrgZW119e2AfyCfPIKMaECFtXFa+xoXvt2dJZX4otH+Dxz1Mp1NBYCZmhyUHeMZKu6W3nM2OrY6PSFY4FGO0h2BoLCECX2Z98g0yZtpTiU6g= Content-Type: text/plain; charset="us-ascii" Content-ID: <247B59BC911AF840B54250803B8E442F@namprd15.prod.outlook.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 87abc140-bcc3-48df-d2c1-08d6c3ab02a1 X-MS-Exchange-CrossTenant-originalarrivaltime: 18 Apr 2019 03:07:37.4820 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR15MB3477 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-04-18_02:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 17, 2019 at 06:55:12PM -0700, Shakeel Butt wrote: > On Wed, Apr 17, 2019 at 5:39 PM Roman Gushchin wrote: > > > > On Wed, Apr 17, 2019 at 04:41:01PM -0700, Shakeel Butt wrote: > > > On Wed, Apr 17, 2019 at 2:55 PM Roman Gushchin wro= te: > > > > > > > > This commit makes several important changes in the lifecycle > > > > of a non-root kmem_cache, which also affect the lifecycle > > > > of a memory cgroup. > > > > > > > > Currently each charged slab page has a page->mem_cgroup pointer > > > > to the memory cgroup and holds a reference to it. > > > > Kmem_caches are held by the cgroup. On offlining empty kmem_caches > > > > are freed, all other are freed on cgroup release. > > > > > > No, they are not freed (i.e. destroyed) on offlining, only > > > deactivated. All memcg kmem_caches are freed/destroyed on memcg's > > > css_free. > > > > You're right, my bad. I was thinking about the corresponding sysfs entr= y > > when was writing it. We try to free it from the deactivation path too. > > > > > > > > > > > > > So the current scheme can be illustrated as: > > > > page->mem_cgroup->kmem_cache. > > > > > > > > To implement the slab memory reparenting we need to invert the sche= me > > > > into: page->kmem_cache->mem_cgroup. > > > > > > > > Let's make every page to hold a reference to the kmem_cache (we > > > > already have a stable pointer), and make kmem_caches to hold a sing= le > > > > reference to the memory cgroup. > > > > > > What about memcg_kmem_get_cache()? That function assumes that by > > > taking reference on memcg, it's kmem_caches will stay. I think you > > > need to get reference on the kmem_cache in memcg_kmem_get_cache() > > > within the rcu lock where you get the memcg through css_tryget_online= . > > > > Yeah, a very good question. > > > > I believe it's safe because css_tryget_online() guarantees that > > the cgroup is online and won't go offline before css_free() in > > slab_post_alloc_hook(). I do initialize kmem_cache's refcount to 1 > > and drop it on offlining, so it protects the online kmem_cache. > > >=20 > Let's suppose a thread doing a remote charging calls > memcg_kmem_get_cache() and gets an empty kmem_cache of the remote > memcg having refcnt equal to 1. That thread got a reference on the > remote memcg but no reference on the kmem_cache. Let's suppose that > thread got stuck in the reclaim and scheduled away. In the meantime > that remote memcg got offlined and decremented the refcnt of all of > its kmem_caches. The empty kmem_cache which the thread stuck in > reclaim have pointer to can get deleted and may be using an already > destroyed kmem_cache after coming back from reclaim. >=20 > I think the above situation is possible unless the thread gets the > reference on the kmem_cache in memcg_kmem_get_cache(). Yes, you're right and I'm writing a nonsense: css_tryget_online() can't prevent the cgroup from being offlined. So, the problem with getting a reference in memcg_kmem_get_cache() is that it's an atomic operation on the hot path, something I'd like to avoid. I can make the refcounter percpu, but it'll add some complexity and size to the kmem_cache object. Still an option, of course. I wonder if we can use rcu_read_lock() instead, and bump the refcounter only if we're going into reclaim. What do you think? Thanks!