Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp3880320rwe; Mon, 17 Apr 2023 05:06:31 -0700 (PDT) X-Google-Smtp-Source: AKy350afVkkzYkfHtISeLP+HkJ7JxpWnN2YsayoBX6nQvMFy6ySmzLrkSFZC9d8T/9cEK5kKoYyl X-Received: by 2002:a05:6808:2911:b0:389:8a25:1897 with SMTP id ev17-20020a056808291100b003898a251897mr6199288oib.12.1681733191191; Mon, 17 Apr 2023 05:06:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681733191; cv=none; d=google.com; s=arc-20160816; b=o3Ic72tQ9PSx6c8MLClvxoca392FRwmq70VZTxSKSF97kpoT98QASpqd4Vz5uBEZ7R HMBumyWikiPTgkS7pIz2la5S5iKXgB0CoA6Igz3JsqNRuF3D+CQLycpjP6uEpp1BwU/h YSdX0HgG3NaR+6pcsP2rs8tdfeNxcyarEGn2APYcsq3en/8NPCyREg3VzWmyXk9nHv9Q 8lMWQRNq3MWJb3jh5Maq8clsVGV2B6Xocezn1o0qShSA5scDBoeVzcG05volfBOdx8yg ui8cIgp6PsyUyeKlc2+XlkTX1+56ngTXd2LTaLyDMCK7XHmDUhTwPjubuHxLNj6w2z30 47ug== 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=cItKBO+T06PzHqKskJtbZMH9b9s+HGyW1napsrYk6R4=; b=YADIQOH5rjtlFaFbcr6pT9iKv6v295iKJ4eetdBL0JmNeLI9geYPHx4nPqNsI8J5uS y+93goM20ki9H6xijaaoXFr3kGPU8qPo6SdYD5GdGCeB4h6Tg4UV41aWasCJeSfn6PLY 7tKqFNW8iUUANRoawXV+DewGIbBEX5mI0qp/mcJGnlCrRJa8jqkp50Idbw1L7uyqRpZ/ +0EpEsOkgEa2sOOcRGVzNWmuBqzQycqRmAIaZ+FBgO/Qrpz6a56vy81KTTFFCARadfoh yPIcPPJa2SPQJy1DL6PhQQwDJLqcY56yqpCQH3YP4gSvAW4a9NvnNIPRJLbB3273/9Kg Obiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="W3Z/9kFr"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a14-20020a056808098e00b0038722a5da9fsi9654631oic.78.2023.04.17.05.06.18; Mon, 17 Apr 2023 05:06:31 -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=@google.com header.s=20221208 header.b="W3Z/9kFr"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230298AbjDQLzt (ORCPT + 99 others); Mon, 17 Apr 2023 07:55:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230252AbjDQLzr (ORCPT ); Mon, 17 Apr 2023 07:55:47 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F4967ED4 for ; Mon, 17 Apr 2023 04:54:42 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-504eccc8fc8so3812887a12.2 for ; Mon, 17 Apr 2023 04:54:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681732464; x=1684324464; 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=cItKBO+T06PzHqKskJtbZMH9b9s+HGyW1napsrYk6R4=; b=W3Z/9kFrMfz7ubLB3nwJYFEqJi3JzIX6K1/YcqvsQaT9VNbvVGsG/EtwdytaO8NF3j i4+tNUBAtQ0awyXQEzCMD5OXpasdzkW2wRwhnUzB9zLdK/2MO1GJczhwDaMEqNgDJfLW dB3RZGXSgQABzVEzvMzqKFZ31q/xx9ESkUzB3tEtC8fiBfwfOPqKR4uL/98kYdnmr8C3 VunkYTYoahczoIPLmbYhtNiJlJyKF/jECvd0FbXZibXwlG//2PymGGfPtBmMemmxlu3I fQSyOr7KK1PaQrqkYlRquknleQD5KrDxXSwfOy5Vsxlwz05jFcswa81GGFJiD1UPje9C FxNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681732464; x=1684324464; 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=cItKBO+T06PzHqKskJtbZMH9b9s+HGyW1napsrYk6R4=; b=SlZ+AQefjoUj1K/kSypeAjJqiENHUGdt64dJPICzf6SXlqbM/AOhPuZeefgJVLwxD2 UgPIk3q9WKs5XXNzZAZ6RDa+33FC8Mz2/UGSr1dEI4EYzntgxF8JwzHSTXnOtfH22G8V utjNCL6IkbY6KhWpjfSuSJ34rOQUlszUChpnJK3t00mtI3e/xkANWiccxkcMfdSDUXce dgWFt5ZVtHwRGVntapfgiRFB5lWc4yK38mbNZMWEJJde//Xpdrm5dwQYWEQZVVquMm0x XGgYvvcpS3Gf3lRgzavtykeFwLjSPmgCfjT1LZ0lFz5Bh7ekAn9YyRWjIYJTt3RjWPP4 BnxQ== X-Gm-Message-State: AAQBX9fW4jhvGOnyBinoboE6cAkNYS/xxOiHFdQ6YkvnGGD6WQaRGKi8 uq4BAkpvOaf4vMMaeA6lPdk4kQ60TaNoFB9sKImnDA== X-Received: by 2002:a50:9b0e:0:b0:504:7684:a23c with SMTP id o14-20020a509b0e000000b005047684a23cmr7159964edi.8.1681732463533; Mon, 17 Apr 2023 04:54:23 -0700 (PDT) MIME-Version: 1.0 References: <20230417110259.1737315-1-senozhatsky@chromium.org> In-Reply-To: <20230417110259.1737315-1-senozhatsky@chromium.org> From: Yosry Ahmed Date: Mon, 17 Apr 2023 04:53:47 -0700 Message-ID: Subject: Re: [PATCH] zsmalloc: allow only one active pool compaction context To: Sergey Senozhatsky Cc: Minchan Kim , Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Hi Sergey, On Mon, Apr 17, 2023 at 4:03=E2=80=AFAM Sergey Senozhatsky wrote: > > zsmalloc pool can be compacted concurrently by many contexts, > e.g. > > cc1 handle_mm_fault() > do_anonymous_page() > __alloc_pages_slowpath() > try_to_free_pages() > do_try_to_free_pages( > lru_gen_shrink_node() > shrink_slab() > do_shrink_slab() > zs_shrinker_scan() > zs_compact() > > This creates unnecessary contention as all those processes > compete for access to the same classes. A single compaction > process is enough. Moreover contention that is created by > multiple compaction processes impact other zsmalloc functions, > e.g. zs_malloc(), since zsmalloc uses "global" pool->lock to > synchronize access to pool. > > Introduce pool compaction mutex and permit only one compaction > context at a time. I am not sure what's the best practice here, but if the only use of the mutex is a trylock, do we need a mutex here? It seems like a simple atomic would do the trick. Perhaps something like: static atomic_t ongoing_compaction =3D ATOMIC_INIT(0); ... if (atomic_xchg(&ongoing_compaction, 1)) return; .... atomic_set(&ongoing_compaction, 0); FWIW, I am suggesting this because it was recently brought up when one of my patches was reviewed that locks should protect data not code paths [1], so I am trying to apply what I learned there :) [1]https://lore.kernel.org/lkml/20230323172732.GE739026@cmpxchg.org/ > > /proc/lock-stat after make -j$((`nproc`+1)) linux kernel for > &pool->lock#3: > > Base Patched > -------------------------------------- > con-bounces 9797655 8125860 > contentions 11131185 9242153 > waittime-min 0.09 0.10 > waittime-max 4171695.76 3926258.74 > waittime-total 506197629.16 417061026.20 > waittime-avg 45.48 45.13 > acq-bounces 13809103 11383480 > acquisitions 21145155 18049364 > holdtime-min 0.06 0.07 > holdtime-max 7379928.80 3926274.89 > holdtime-total 46273950.89 37279624.53 > holdtime-avg 2.19 2.07 > > Signed-off-by: Sergey Senozhatsky > --- > mm/zsmalloc.c | 6 ++++++ > 1 file changed, 6 insertions(+) > > diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c > index cc81dfba05a0..0e036ec56c3c 100644 > --- a/mm/zsmalloc.c > +++ b/mm/zsmalloc.c > @@ -264,6 +264,7 @@ struct zs_pool { > struct work_struct free_work; > #endif > spinlock_t lock; > + struct mutex compact_lock; > }; > > struct zspage { > @@ -2274,6 +2275,9 @@ unsigned long zs_compact(struct zs_pool *pool) > struct size_class *class; > unsigned long pages_freed =3D 0; > > + if (!mutex_trylock(&pool->compact_lock)) > + return 0; > + > for (i =3D ZS_SIZE_CLASSES - 1; i >=3D 0; i--) { > class =3D pool->size_class[i]; > if (class->index !=3D i) > @@ -2281,6 +2285,7 @@ unsigned long zs_compact(struct zs_pool *pool) > pages_freed +=3D __zs_compact(pool, class); > } > atomic_long_add(pages_freed, &pool->stats.pages_compacted); > + mutex_unlock(&pool->compact_lock); > > return pages_freed; > } > @@ -2388,6 +2393,7 @@ struct zs_pool *zs_create_pool(const char *name) > > init_deferred_free(pool); > spin_lock_init(&pool->lock); > + mutex_init(&pool->compact_lock); > > pool->name =3D kstrdup(name, GFP_KERNEL); > if (!pool->name) > -- > 2.40.0.634.g4ca3ef3211-goog >