Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2652153rwd; Mon, 22 May 2023 02:05:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5f76Fiv8hTgYQcxvLvlXdQFzjyVnouGa/RuSna5fyKSUmd68v2zNmMc59Z09/Z3t7D4wAf X-Received: by 2002:a17:90b:1d01:b0:24e:109a:94e with SMTP id on1-20020a17090b1d0100b0024e109a094emr10117905pjb.7.1684746329209; Mon, 22 May 2023 02:05:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684746329; cv=none; d=google.com; s=arc-20160816; b=0PPlvVtcop/ooY4eUHzzB8SRLIu+KZ0i/iRli5uOI9YMy37WevPhWANbPoOnyluFAu QGLfa4jk4517ZtP/gphnPmqgHWtHSGZcqCRwODZqNQduNOH5rzuPuoLCimG0YI1A20Dx qhVH/GFBfnQVivau0xqhsPTzbE5Cy8RVS9VRzZDqb1sSH++yuX8c07d6T7tFr7N3LbpQ 5WyoB9aspDgizUNACGHZaU1O1l96TEg+nh51zGnzL1KOTbhnY4+78IOEyR65nJmdzDqu WgwVTgG17TWkXvhpTY4GcIPmeNsbs4EccKUwF/EPuRcXawYyDY3TOOTUon2ZXE+gOMI/ hTBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=UQeSrfWF6EvL8Tyxm40SSqQ/6Cm3EN9lJUOCedt0x48=; b=clUCZeKRFrZYcSecBCq+RyB8u+NxWRaf5HIlG4SbEAHTQc6/cYOiLSILbFsnwsjlGJ A+mo75jDTrirI5a2I++zH5e52aak+j7vQLbuFcN9yayjkhjVOxAKUa6HoHaxIrjO2Zcp jDjC/BQNhBPVpCAIMAnOvcD5I8xHr6AEjI16ts1g0oDK5DHXwrZ0qmwaA+b1/IVkedES PPrfOfbWFmpQ6r6RqL9RGVw5233fwmXfEaYbZ09Rvyr+dgdbdKkCUUSAPv/TvW9bT5B5 +0KylJ9FexHj+cBU7ZLmst9AfFQvtgf7CAQobhu++SYuWvhDaY1000rkdhIyb6jp4UaL 7uDA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s11-20020a63924b000000b00530b1e7099dsi2759713pgn.84.2023.05.22.02.05.15; Mon, 22 May 2023 02:05:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231873AbjEVJAO (ORCPT + 99 others); Mon, 22 May 2023 05:00:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231872AbjEVI7Y (ORCPT ); Mon, 22 May 2023 04:59:24 -0400 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC7F5AF; Mon, 22 May 2023 01:58:53 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4QPrw94DMLz4f463D; Mon, 22 May 2023 16:58:49 +0800 (CST) Received: from [10.67.110.48] (unknown [10.67.110.48]) by APP1 (Coremail) with SMTP id cCh0CgCH6yWxLmtk91CHJQ--.43916S2; Mon, 22 May 2023 16:58:50 +0800 (CST) Message-ID: <1cec95d5-5cd4-fbf9-754b-e6a1229d45c3@huaweicloud.com> Date: Mon, 22 May 2023 16:58:25 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH RFC v2] Randomized slab caches for kmalloc() Content-Language: en-US To: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Alexander Lobakin , kasan-dev@googlegroups.com, Wang Weiyang , Xiu Jianfeng , Vlastimil Babka , Christoph Lameter , David Rientjes , Roman Gushchin , Joonsoo Kim , Andrew Morton , Pekka Enberg , Kees Cook , Paul Moore , James Morris , "Serge E. Hallyn" , "Gustavo A. R. Silva" , Gong Ruiqi References: <20230508075507.1720950-1-gongruiqi1@huawei.com> <5f5a858a-7017-5424-0fa0-db3b79e5d95e@huawei.com> <19707cc6-fa5e-9835-f709-bc8568e4c9cd@huawei.com> From: "GONG, Ruiqi" In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID: cCh0CgCH6yWxLmtk91CHJQ--.43916S2 X-Coremail-Antispam: 1UD129KBjvJXoW7KFWUXF1fAF47Xry3Wr47XFb_yoW8tFyUpF WIyF1UCr4xCr17Cry0ya10va92v3y7tF1Uu3s0gryUZr1kJw18XFsakr109r93ZF45GFy3 XFsYkF13WF9xt3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvIb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6r1F6r1fM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x 0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG 6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFV Cjc4AY6r1j6r4UM4x0Y48IcVAKI48JM4IIrI8v6xkF7I0E8cxan2IY04v7Mxk0xIA0c2IE e2xFo4CEbIxvr21l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxV Aqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r4a 6rW5MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6x kF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWrZr1j6s0DMIIF0xvEx4A2jsIE 14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf 9x07UZ18PUUUUU= X-CM-SenderInfo: pjrqw2pxltxq5kxd4v5lfo033gof0z/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2023/05/22 16:03, Hyeonggon Yoo wrote: > On Mon, May 22, 2023 at 4:35 PM Gong Ruiqi wrote: >> On 2023/05/17 6:35, Hyeonggon Yoo wrote: > [...] >>>>>> +#ifdef CONFIG_RANDOM_KMALLOC_CACHES >>>>>> +# define SLAB_RANDOMSLAB ((slab_flags_t __force)0x01000000U) >>>>>> +#else >>>>>> +# define SLAB_RANDOMSLAB 0 >>>>>> +#endif >>> >>> There is already the SLAB_KMALLOC flag that indicates if a cache is a >>> kmalloc cache. I think that would be enough for preventing merging >>> kmalloc caches? >> >> After digging into the code of slab merging (e.g. slab_unmergeable(), >> find_mergeable(), SLAB_NEVER_MERGE, SLAB_MERGE_SAME etc), I haven't >> found an existing mechanism that prevents normal kmalloc caches with >> SLAB_KMALLOC from being merged with other slab caches. Maybe I missed >> something? >> >> While SLAB_RANDOMSLAB, unlike SLAB_KMALLOC, is added into >> SLAB_NEVER_MERGE, which explicitly indicates the no-merge policy. > > I mean, why not make slab_unmergable()/find_mergeable() not to merge kmalloc > caches when CONFIG_RANDOM_KMALLOC_CACHES is enabled, instead of a new flag? > > Something like this: > > diff --git a/mm/slab_common.c b/mm/slab_common.c > index 607249785c07..13ac08e3e6a0 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -140,6 +140,9 @@ int slab_unmergeable(struct kmem_cache *s) > if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE)) > return 1; > > + if (IS_ENALBED(CONFIG_RANDOM_KMALLOC_CACHES) && (flags & SLAB_KMALLOC)) > + return 1; > + > if (s->ctor) > return 1; > > @@ -176,6 +179,9 @@ struct kmem_cache *find_mergeable(unsigned int > size, unsigned int align, > if (flags & SLAB_NEVER_MERGE) > return NULL; > > + if (IS_ENALBED(CONFIG_RANDOM_KMALLOC_CACHES) && (flags & SLAB_KMALLOC)) > + return NULL; > + > list_for_each_entry_reverse(s, &slab_caches, list) { > if (slab_unmergeable(s)) > continue; Ah I see. My concern is that it would affect not only normal kmalloc caches, but kmalloc_{dma,cgroup,rcl} as well: since they were all marked with SLAB_KMALLOC when being created, this code could potentially change their mergeablity. I think it's better not to influence those irrelevant caches.