Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2608144rwd; Mon, 22 May 2023 01:13:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4oAxBkdZ6aaLFWuNIme08/54vNOBcK+AG6bIDLEI4DxP1pjzkiY9yu5R+W5F0z8p4/7Nn8 X-Received: by 2002:a05:6a20:9d95:b0:105:63b0:5c05 with SMTP id mu21-20020a056a209d9500b0010563b05c05mr9680421pzb.15.1684743194443; Mon, 22 May 2023 01:13:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684743194; cv=none; d=google.com; s=arc-20160816; b=Obn3oYz6Wg2TBT3PcEKQ7lXB8m+dQ8OxlZOkV2tgi8YRNWyHYOzXplOoDTfTXSmfyT VAhXJtKqaVl4nueW6+Ol+WMHSfVeveve29aNKOjL2FgQRND1ubcfry4LH3WsEcgc+S1l aV8zy8XNcDa2TqkbLcK2NZkhnUzz2pgO5w5lL49dD/yzZ24712Ppkvv0wNmls9rp3Nyp CvTXDC+DsPaQBqg69rIM5Rcnae65J2ryCC0BtaX7ljBt4KbSQ3Aa7T46Om/zrhwNaSzp 54ZLw60+8oJ1l2bNGNVe/I3l/WwZ5TVRVgoV0Y+0nnaYk+ckgvHTk+fddc9Btgs189dA JyUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=5/deT6NIs/fCmfBK6TadeTF6djYRsWDmBLQzhkm7Whg=; b=X+mA83i7ypb4W3kNuOR1yaibTovbRZxa7dKzKZCmtdH0a+2ol5oRABgkp/HJ7Lp65G eHcfxUTDVcg63m29mnpkEQnWrd3lYGPkSuIJKoMmFudxoTEcdEIQdS48jr5naVdSIxF0 MRui4qYK4GzVzZ+7wQWIsyDRVOoAr6jqUSExRcEKpbCDw+ZmgEsccHRznyrXcyUPs+1N 8U7Gi+AU8pKEjmxgguDqHhAZqCjR1WelmDs2gnqbyxnuFGwmOSc356jQWCzkXKpzUuaN 3umv/K0wyDskRtW7zVjh4TIUt5vpqIjxJOadS4vWGx8Hh/8H8bIzfG1ouCJl4Bq9lfXQ 56lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=QK9rPLNa; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z188-20020a6333c5000000b0050e60e36393si2823576pgz.776.2023.05.22.01.12.59; Mon, 22 May 2023 01:13:14 -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; dkim=pass header.i=@gmail.com header.s=20221208 header.b=QK9rPLNa; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231294AbjEVIDd (ORCPT + 99 others); Mon, 22 May 2023 04:03:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230363AbjEVIDb (ORCPT ); Mon, 22 May 2023 04:03:31 -0400 Received: from mail-ua1-x936.google.com (mail-ua1-x936.google.com [IPv6:2607:f8b0:4864:20::936]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECDE69B; Mon, 22 May 2023 01:03:30 -0700 (PDT) Received: by mail-ua1-x936.google.com with SMTP id a1e0cc1a2514c-783eef15004so1831098241.3; Mon, 22 May 2023 01:03:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684742610; x=1687334610; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=5/deT6NIs/fCmfBK6TadeTF6djYRsWDmBLQzhkm7Whg=; b=QK9rPLNa5YQIm4BrTN9G+Gp8sOyYcF3EKdXnKr8/9/PSYQ1vi0/0Uj4L/BmJbTc2we qJs3SIOqu758OmnrJ2FEhJ4p1EVRlm2S9uxj/13VWFOw7RzBrlGoMWDWap2oBM20esBP hZZywbt/uGTOo36/73mWU5yQCHlThzL7ebf0DmvA3dlBHdoqp9q1tO0k5T9U4/GH14R1 i5xQkpFtI0s54mLkHCRIpQnKsT8DxWJyP2Krgo6lhJtdRNcxYH1LgFDgr79CVKxVx1eH jGYeIUAxIqXGi33JfzyBKulQ3Neom1TyrxEnKs0zyUdhp/vLUDXpNIrRPmBEicdh2VP7 syrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684742610; x=1687334610; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5/deT6NIs/fCmfBK6TadeTF6djYRsWDmBLQzhkm7Whg=; b=Dxewe7N0mGTlE7SKzJBkHZBTgZ/sNcrWI75hZ8OYqWcA0b6ExxA28B96UrMF5qFmNa mFOcr9UMYxBiZpCVsvWUAGr0EqERCM2z9WULHkyg5NRctRZrwGGYCNTD8GLTKM1Q6M/l ZVG1c6qx3cMhFidcHlLmVVcu6VHNBAzVkRcHz43LJeHOBjR0Z+VCqb0IRdAjcPXS2vjD ccKM2qYWBtY4+Nk3S7Kpkeq8deOSWc4skuewqF/QuAcs07YJPAmvJ66BLzKtbAGjx4ky 042tmJPkhL/cJ8SezCfUVWunl4EDmm1SwDTYszIprI/Y690mKZNkKI46jbay0LgWSgRk t4pA== X-Gm-Message-State: AC+VfDygLlUa5PQ6zmNvmQq/v1SUx7WhKDz/7eOrphLCkm7cpOiPPwW2 Ryb7xyKXl5nt1RfhDn1vI1UyNhSJOIXSJ9S6jJg= X-Received: by 2002:a67:fe17:0:b0:439:e3f:9d6 with SMTP id l23-20020a67fe17000000b004390e3f09d6mr2336475vsr.17.1684742609589; Mon, 22 May 2023 01:03:29 -0700 (PDT) MIME-Version: 1.0 References: <20230508075507.1720950-1-gongruiqi1@huawei.com> <5f5a858a-7017-5424-0fa0-db3b79e5d95e@huawei.com> <19707cc6-fa5e-9835-f709-bc8568e4c9cd@huawei.com> In-Reply-To: <19707cc6-fa5e-9835-f709-bc8568e4c9cd@huawei.com> From: Hyeonggon Yoo <42.hyeyoo@gmail.com> Date: Mon, 22 May 2023 17:03:37 +0900 Message-ID: Subject: Re: [PATCH RFC v2] Randomized slab caches for kmalloc() To: Gong Ruiqi 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" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HK_RANDOM_ENVFROM, HK_RANDOM_FROM,RCVD_IN_DNSWL_NONE,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 Mon, May 22, 2023 at 4:35=E2=80=AFPM 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 kmallo= c 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;