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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49BA1ECAAA1 for ; Fri, 9 Sep 2022 16:37:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B4EBC6B0071; Fri, 9 Sep 2022 12:37:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AFEA28D0002; Fri, 9 Sep 2022 12:37:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 99FF68D0001; Fri, 9 Sep 2022 12:37:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8BC4A6B0071 for ; Fri, 9 Sep 2022 12:37:38 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5C3A7A0A9B for ; Fri, 9 Sep 2022 16:37:38 +0000 (UTC) X-FDA: 79893102996.30.CD7FDF3 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf14.hostedemail.com (Postfix) with ESMTP id 71A26100089 for ; Fri, 9 Sep 2022 16:37:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662741457; x=1694277457; h=message-id:subject:from:to:cc:date:in-reply-to: references:mime-version:content-transfer-encoding; bh=axQDD1e0+9YXYZpyuAeHcsK5y6w1wt57SYTlNV2Ts68=; b=ivIatemEmI0KT3Zy0eFNst+YgMP4Meg1VTX9wvANrcryM0Kir3tjKTad W60Hm8611IwOLS2bKq0fUnXMR/XOMuzz/nbLg9Qqzy6enwzVd7qE4aJLQ jw58QhYBqphK3fdWgetSDFQ+9TYQEPh4dMMThEtkh4S+IYCUMwod57eGH gX21UQecekOhfmnw765HvkYJd1feYwGKp4oa1Hg4mchMUPAzO5EcKayfl bahRhKOMvyuzPCgBqgEYuDKuKSW7WrAUCuge/Az6Z+2FLx9AeBmk3xtOu WdT3za0H3tTJlrafPl2iDRymYiGtZJ2lhdgh4rFiTH2Jr5et5bEWLYgfP g==; X-IronPort-AV: E=McAfee;i="6500,9779,10465"; a="383812566" X-IronPort-AV: E=Sophos;i="5.93,303,1654585200"; d="scan'208";a="383812566" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Sep 2022 09:37:11 -0700 X-IronPort-AV: E=Sophos;i="5.93,303,1654585200"; d="scan'208";a="943835618" Received: from schen9-mobl.amr.corp.intel.com ([10.212.177.99]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Sep 2022 09:37:10 -0700 Message-ID: <346eb3cc79a657f3e45d3fa2bea2fdffa741af3d.camel@linux.intel.com> Subject: Re: [PATCH v5 1/2] percpu: Add percpu_counter_add_local and percpu_counter_sub_local From: Tim Chen To: Jiebin Sun , akpm@linux-foundation.org, vasily.averin@linux.dev, shakeelb@google.com, dennis@kernel.org, tj@kernel.org, cl@linux.com, ebiederm@xmission.com, legion@kernel.org, manfred@colorfullife.com, alexander.mikhalitsyn@virtuozzo.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: tim.c.chen@intel.com, feng.tang@intel.com, ying.huang@intel.com, tianyou.li@intel.com, wangyang.guo@intel.com Date: Fri, 09 Sep 2022 09:37:09 -0700 In-Reply-To: <20220909203636.2652466-2-jiebin.sun@intel.com> References: <20220902152243.479592-1-jiebin.sun@intel.com> <20220909203636.2652466-1-jiebin.sun@intel.com> <20220909203636.2652466-2-jiebin.sun@intel.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.34.4 (3.34.4-1.fc31) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=ivIatemE; dmarc=fail reason="No valid SPF" header.from=intel.com (policy=none); spf=none (imf14.hostedemail.com: domain of tim.c.chen@linux.intel.com has no SPF policy when checking 192.55.52.43) smtp.mailfrom=tim.c.chen@linux.intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662741457; a=rsa-sha256; cv=none; b=u0u24ePCAAqEQPMV3pbnLltk8bzINp4p7kZppqrUkgqKZgSnROaOdDcCWw4O9ffUx9inC/ z38fjJIIy5ZjroOhMgR2JMP0xr2OwbwUUFgHOmG/74cDHCqULBrO5jZaJId1UjvZz6lITZ n6bJScMbz6JrKBnpf5mSNl2BTR+oDDQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662741457; h=from:from:sender: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:dkim-signature; bh=6PUDH2Y+dxyu5Xp8hO58hMrxuqvZPOn2RbLaJZgY3Cc=; b=QEbApJJuepHXvB17ES/DJK9BTqEWLzRkr1IHMsrN2wNFBZPtwyWQfaqWGqlFd8CdljZ8gJ mvUzCWrFk8QyXZtPdx7jFoQ4/PfSOZLYgn/dSdpm2OGIykd4UDdfKAvSc5/c+XfT0UAg2I mJEY+wca8VhB4oZfFcXw2OXuErt3jF8= Authentication-Results: imf14.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=ivIatemE; dmarc=fail reason="No valid SPF" header.from=intel.com (policy=none); spf=none (imf14.hostedemail.com: domain of tim.c.chen@linux.intel.com has no SPF policy when checking 192.55.52.43) smtp.mailfrom=tim.c.chen@linux.intel.com X-Stat-Signature: 3umr71wewfrhuk4a6fna4og1tcs3af9p X-Rspamd-Queue-Id: 71A26100089 X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1662741457-883680 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 Sat, 2022-09-10 at 04:36 +0800, Jiebin Sun wrote: > The batch size in percpu_counter_add_batch should be very large > in heavy writing and rare reading case. Add the "_local" version, > and mostly it will do local adding, reduce the global updating > and mitigate lock contention in writing. > > Signed-off-by: Jiebin Sun > --- > include/linux/percpu_counter.h | 38 ++++++++++++++++++++++++++++++++++ > 1 file changed, 38 insertions(+) > > diff --git a/include/linux/percpu_counter.h b/include/linux/percpu_counter.h > index 01861eebed79..6dd7eaba8527 100644 > --- a/include/linux/percpu_counter.h > +++ b/include/linux/percpu_counter.h > @@ -15,6 +15,9 @@ > #include > #include > > +/* percpu_counter batch for local add or sub */ > +#define PERCPU_COUNTER_LOCAL_BATCH INT_MAX > + > #ifdef CONFIG_SMP > > struct percpu_counter { > @@ -56,6 +59,27 @@ static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount) > percpu_counter_add_batch(fbc, amount, percpu_counter_batch); > } > > +/* > + * Use this function in heavy writing but rare reading case. The large > + * batch size will reduce the global updating. Suggest revising the comment, so it is clear we need to use percpu_counter_sum() to access the counter: With percpu_counter_add_local() and percpu_counter_sub_local(), counts are accumulated in local per cpu counter and not in fbc->count until local count overflows PERCPU_COUNTER_LOCAL_BATCH. This makes counter write efficient. But percpu_counter_sum(), instead of percpu_counter_read(), needs to be used to add up the counts from each CPU to account for all the local counts. So percpu_counter_add_local() and percpu_counter_sub_local() should be used when a counter is updated frequently and read rarely. > + */ > +static inline void > +percpu_counter_add_local(struct percpu_counter *fbc, s64 amount) > +{ > + percpu_counter_add_batch(fbc, amount, PERCPU_COUNTER_LOCAL_BATCH); > +} > + > +/* > + * Similar with percpu_counter_add_local, use it in heavy writing but > + * rare reading case. The large batch size will reduce the global > + * updating. > + */ > +static inline void > +percpu_counter_sub_local(struct percpu_counter *fbc, s64 amount) > +{ > + percpu_counter_add_batch(fbc, -amount, PERCPU_COUNTER_LOCAL_BATCH); > +} > + > static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc) > { > s64 ret = __percpu_counter_sum(fbc); > @@ -138,6 +162,20 @@ percpu_counter_add(struct percpu_counter *fbc, s64 amount) > preempt_enable(); > } > > +/* no smp percpu_counter_add_local is the same with percpu_counter_add */ > +static inline void > +percpu_counter_add_local(struct percpu_counter *fbc, s64 amount) > +{ > + percpu_counter_add(fbc, amount); > +} > + > +/* no smp percpu_counter_sub_local is the same with percpu_counter_sub */ > +static inline void > +percpu_counter_sub_local(struct percpu_counter *fbc, s64 amount) > +{ > + percpu_counter_sub(fbc, amount); > +} > + > static inline void > percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch) > {