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 D8A15ECAAD3 for ; Fri, 9 Sep 2022 12:17:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54CAE8D0005; Fri, 9 Sep 2022 08:17:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D5DF8D0002; Fri, 9 Sep 2022 08:17:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 329058D0005; Fri, 9 Sep 2022 08:17:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 206368D0002 for ; Fri, 9 Sep 2022 08:17:41 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F017F1204D3 for ; Fri, 9 Sep 2022 12:17:40 +0000 (UTC) X-FDA: 79892447880.08.48C3026 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf06.hostedemail.com (Postfix) with ESMTP id 7A32C18007A for ; Fri, 9 Sep 2022 12:17:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662725859; x=1694261859; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=msZeq0/MS9e5G6s32J0p8vC6jZqtVCX7tRSfsEvQZPA=; b=QGgbBBnbzrAfXI8OsexBXmFXOrRENbdypifO8vUzRXhgKT6VpbIuOBre OegSvTPA8QnlO392eIlSxECUa5b6/6PWdvKQHh52VfhzumYHGsGC+u0Ig yTsiGbYaGEkCxvS3WYxwEQ9TCv8X0g+kCD1b6yGYoSWExTh6wnA4cwdkz +3xjvDfE6VZEAtyMokn8lQCNdv7fkKwkgqOVGrCbXP/auXgqnyPweMYwH TsxiwNdMQiIYYIR8PDDops60PDSMMEoLlXMOnSY6giie52zzZMBNJyx7R r3ppXiztZ/m2FW8EvWIO76D1ExULoy4q2geYlSPijZsIVTT6xRRmpZp81 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="359185147" X-IronPort-AV: E=Sophos;i="5.93,303,1654585200"; d="scan'208";a="359185147" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Sep 2022 05:17:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,303,1654585200"; d="scan'208";a="645516067" Received: from linux-pnp-server-13.sh.intel.com ([10.239.176.176]) by orsmga008.jf.intel.com with ESMTP; 09 Sep 2022 05:17:34 -0700 From: Jiebin Sun To: 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, jiebin.sun@intel.com Subject: [PATCH v5 1/2] percpu: Add percpu_counter_add_local and percpu_counter_sub_local Date: Sat, 10 Sep 2022 04:36:35 +0800 Message-Id: <20220909203636.2652466-2-jiebin.sun@intel.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220909203636.2652466-1-jiebin.sun@intel.com> References: <20220902152243.479592-1-jiebin.sun@intel.com> <20220909203636.2652466-1-jiebin.sun@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=QGgbBBnb; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf06.hostedemail.com: domain of jiebin.sun@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=jiebin.sun@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662725859; a=rsa-sha256; cv=none; b=5pYsoXfcklPcp6Sw0hoWKkIur4WGE/4TAHvygD1u3NveXMakITor3FBc9aVZ6MNvT83c98 8HOFRykWTjDTaWsiAhKsSmhlBcRrfnxc8C7PBTnRSY+cVjMZZwxWmDSe/BZdBulTnB0M7U A0LNxowkK9U6dwLMy/QMah/Avqa6muA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662725859; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HBMYKnRWIqz6xWNhGOq/f+ta9v8wz6cR6wpoel7895Y=; b=bW8ige1W2f7oKXslWaju/6fnRRyr7pUIyKo1ILUn0a0guY6rzM/DDf2BDzoV1teP+GemA6 ldYWj6GPTS/9yaUYfE1t6mVmFINHiMp51yvSmT9uWgH1cReI0ObNWRkKcnMEDPvJeNMtiG 4Tw8VmG9svKy8EjmwwlBuYN/HdYDTeE= Authentication-Results: imf06.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=QGgbBBnb; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf06.hostedemail.com: domain of jiebin.sun@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=jiebin.sun@intel.com X-Rspam-User: X-Rspamd-Server: rspam01 X-Stat-Signature: xbq5zdgd4wdrauekre7wcd8yzxqm57ya X-Rspamd-Queue-Id: 7A32C18007A X-HE-Tag: 1662725859-138217 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: 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. + */ +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) { -- 2.31.1