From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E5D1C43460 for ; Wed, 5 May 2021 16:38:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CED7A6145E for ; Wed, 5 May 2021 16:38:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CED7A6145E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 58C2B6B006E; Wed, 5 May 2021 12:38:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 53D4D6B0070; Wed, 5 May 2021 12:38:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A2786B0071; Wed, 5 May 2021 12:38:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0018.hostedemail.com [216.40.44.18]) by kanga.kvack.org (Postfix) with ESMTP id 1B9CD6B006E for ; Wed, 5 May 2021 12:38:05 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id B529D180AD837 for ; Wed, 5 May 2021 16:38:04 +0000 (UTC) X-FDA: 78107734488.12.D7CE6D2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf23.hostedemail.com (Postfix) with ESMTP id 8ACC1A0049EF for ; Wed, 5 May 2021 16:37:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620232682; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=81DdwLuppEsne8dGMXcwRqVz38oxfrnLdDs7rg3uPgM=; b=W7oldti5+bbvO+63hqGpKu3BPYTkD7RAUE58C4nGH9DsObrpiAF2m6IWKUYoMNFJcfh9Z/ kco9n4WftMWbkR88taOpPzruz60Yjx50kPbj7PC80R1pl3uqpmWIGqWXp2n55UbGPXrWXD TtCwtB2VF/v8CpFAUoaKNQgS+05gbps= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-446-t_x7-V79OOK6v0RsaKzBFQ-1; Wed, 05 May 2021 12:31:13 -0400 X-MC-Unique: t_x7-V79OOK6v0RsaKzBFQ-1 Received: by mail-qv1-f71.google.com with SMTP id g26-20020a0caada0000b02901b93eb92373so2096477qvb.3 for ; Wed, 05 May 2021 09:31:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=81DdwLuppEsne8dGMXcwRqVz38oxfrnLdDs7rg3uPgM=; b=X8RcxwISgMkRoSY32QgEwRyFqf36TLf1+79HEGUysbuz4oII02tG7IoVp4O2h0Bsxb n8TbfXkI2JO4F32vbDZg1GFrStVyzkwdj//plvg5ofo/r7NE9G+x7HWZndAsjEPHH9lz Zo+J5if/70MfsV3FXliwzdq9klpdQ0rZGHY+3upIjplsQ7Ipkz+ZsiMyDkSJ8L1olTn1 DtrX7YhhSlDjwkMvyc+scujCaA7jxyca1Sg5nBGibdUr8t/tG5F6QqDLSFyRUpneuxq0 S5K5F/krJAzoSqk44sRvRFRywUOLnjrxCK76kh07YZNrhTyYv9fA2Od8+BOJUiZNNKJX o89A== X-Gm-Message-State: AOAM531eo09LdqSQ7nQ5ov7xd6oe6EeW+YL/+aI/xsUpwkPSG+Nq7U8v IQUusc7U8kVn9oEFzJXKNsBkmHYGCJcJtcA4e8R6vhFbedrjQMg/oiVjHH+91ZsR4IknP6dyHv4 s74aMvnw3bKBU2hx7rtmU8pYSYP4IXOLnvqEOMiKxOE3vn8BpR/Su1Zdii8U= X-Received: by 2002:a05:620a:15ca:: with SMTP id o10mr3168454qkm.448.1620232272405; Wed, 05 May 2021 09:31:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTBEKFUN7wXglaJUYSMmAOxWOuFIgMxOXBXNY/vFjUujrx4c1jniCEPsD1GaLRm8BV6S4gDA== X-Received: by 2002:a05:620a:15ca:: with SMTP id o10mr3168418qkm.448.1620232272090; Wed, 05 May 2021 09:31:12 -0700 (PDT) Received: from llong.remote.csb ([2601:191:8500:76c0::cdbc]) by smtp.gmail.com with ESMTPSA id c23sm5338915qtm.46.2021.05.05.09.31.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 May 2021 09:31:11 -0700 (PDT) From: Waiman Long X-Google-Original-From: Waiman Long Subject: Re: [PATCH v3 2/2] mm: memcg/slab: Create a new set of kmalloc-cg- caches To: Vlastimil Babka , Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Shakeel Butt Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org References: <20210505154613.17214-1-longman@redhat.com> <20210505154613.17214-3-longman@redhat.com> <4c1a0436-2d46-d23a-2eef-d558e37373bf@suse.cz> Message-ID: Date: Wed, 5 May 2021 12:31:09 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.9.0 MIME-Version: 1.0 In-Reply-To: <4c1a0436-2d46-d23a-2eef-d558e37373bf@suse.cz> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Rspamd-Queue-Id: 8ACC1A0049EF Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=W7oldti5; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf23.hostedemail.com: domain of llong@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=llong@redhat.com X-Rspamd-Server: rspam04 X-Stat-Signature: 7eikt7dhy1a4jc1qirosobsyozmjombn Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf23; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620232676-47608 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 5/5/21 12:06 PM, Vlastimil Babka wrote: > On 5/5/21 5:46 PM, Waiman Long wrote: >> There are currently two problems in the way the objcg pointer array >> (memcg_data) in the page structure is being allocated and freed. >> >> On its allocation, it is possible that the allocated objcg pointer >> array comes from the same slab that requires memory accounting. If this >> happens, the slab will never become empty again as there is at least >> one object left (the obj_cgroup array) in the slab. >> >> When it is freed, the objcg pointer array object may be the last one >> in its slab and hence causes kfree() to be called again. With the >> right workload, the slab cache may be set up in a way that allows the >> recursive kfree() calling loop to nest deep enough to cause a kernel >> stack overflow and panic the system. >> >> One way to solve this problem is to split the kmalloc- caches >> (KMALLOC_NORMAL) into two separate sets - a new set of kmalloc- >> (KMALLOC_NORMAL) caches for non-accounted objects only and a new set of >> kmalloc-cg- (KMALLOC_CGROUP) caches for accounted objects only. All >> the other caches can still allow a mix of accounted and non-accounted >> objects. >> >> With this change, all the objcg pointer array objects will come from >> KMALLOC_NORMAL caches which won't have their objcg pointer arrays. So >> both the recursive kfree() problem and non-freeable slab problem are >> gone. Since both the KMALLOC_NORMAL and KMALLOC_CGROUP caches no longer >> have mixed accounted and unaccounted objects, this will slightly reduce >> the number of objcg pointer arrays that need to be allocated and save >> a bit of memory. >> >> The new KMALLOC_CGROUP is added between KMALLOC_NORMAL and >> KMALLOC_RECLAIM so that the first for loop in create_kmalloc_caches() >> will include the newly added caches without change. >> >> Suggested-by: Vlastimil Babka >> Signed-off-by: Waiman Long >> --- >> include/linux/slab.h | 42 ++++++++++++++++++++++++++++++++++-------- >> mm/slab_common.c | 23 +++++++++++++++-------- >> 2 files changed, 49 insertions(+), 16 deletions(-) >> >> diff --git a/include/linux/slab.h b/include/linux/slab.h >> index 0c97d788762c..f2d9ebc34f5c 100644 >> --- a/include/linux/slab.h >> +++ b/include/linux/slab.h >> @@ -305,9 +305,16 @@ static inline void __check_heap_object(const void *ptr, unsigned long n, >> /* >> * Whenever changing this, take care of that kmalloc_type() and >> * create_kmalloc_caches() still work as intended. >> + * >> + * KMALLOC_NORMAL is for non-accounted objects only whereas KMALLOC_CGROUP >> + * is for accounted objects only. All the other kmem caches can have both >> + * accounted and non-accounted objects. >> */ >> enum kmalloc_cache_type { >> KMALLOC_NORMAL = 0, >> +#ifdef CONFIG_MEMCG_KMEM >> + KMALLOC_CGROUP, >> +#endif >> KMALLOC_RECLAIM, >> #ifdef CONFIG_ZONE_DMA >> KMALLOC_DMA, >> @@ -315,28 +322,47 @@ enum kmalloc_cache_type { >> NR_KMALLOC_TYPES >> }; >> >> +#ifndef CONFIG_MEMCG_KMEM >> +#define KMALLOC_CGROUP KMALLOC_NORMAL >> +#endif >> +#ifndef CONFIG_ZONE_DMA >> +#define KMALLOC_DMA KMALLOC_NORMAL >> +#endif > You could move this to the enum definition itself? E.g.: > > #ifdef CONFIG_MEMCG_KMEM > KMALLOC_CGROUP, > #else > KMALLOC_CGROUP = KMALLOC_NORMAL, > #endif > >> + >> #ifndef CONFIG_SLOB >> extern struct kmem_cache * >> kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1]; >> >> +/* >> + * Define gfp bits that should not be set for KMALLOC_NORMAL. >> + */ >> +#define KMALLOC_NOT_NORMAL_BITS \ >> + (__GFP_RECLAIMABLE | \ >> + (IS_ENABLED(CONFIG_ZONE_DMA) ? __GFP_DMA : 0) | \ >> + (IS_ENABLED(CONFIG_MEMCG_KMEM) ? __GFP_ACCOUNT : 0)) >> + >> static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) >> { >> -#ifdef CONFIG_ZONE_DMA >> /* >> * The most common case is KMALLOC_NORMAL, so test for it >> * with a single branch for both flags. > Not "both flags" anymore. Something like "so test with a single branch that > there are none of the flags that would select a different type" Right. I just left the comment there without taking a deeper look. My bad. Cheers, Longman