Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2605730lqo; Mon, 20 May 2024 10:31:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXOnpP3+g3Ozsa3FFQzCGRE9Z9EKPuFrEZEDrvac9JOLHNGxQ6rE+5pFQqQl7UmM8LrTk1cMfKGS/XcyX4OZLtuHQ2mKsFJhwzByTkRTA== X-Google-Smtp-Source: AGHT+IHzUt+yKSYth70Qxd2nIpMcQAQpFbD+E9/Up2kwcc+p8daJCXvzRX/F4ualMhUB5pMBm13l X-Received: by 2002:a50:9505:0:b0:572:6846:b899 with SMTP id 4fb4d7f45d1cf-5734d6b407dmr19059118a12.41.1716226281063; Mon, 20 May 2024 10:31:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716226281; cv=pass; d=google.com; s=arc-20160816; b=wOgCsCqWcBJu9mu+If0PClfsyRbS5Mj29AYyDSO9ViWANa95SUXDGgC4IgCtUhqm88 eNLcwMOYbxh7JsNrVTWA782340v35sy/Iowy2iPIbywz50nJjrv0ArVh8w+Vtm6yt7QA eHclN9VYb3DMLgz42rwTwnjmw9T220fXg/NDP3u1PTrctnGOYx5fpsf5cWzxB0z8BZgT yflMXfR0NhfwJJddsme4lTa5Wh2Q4PyfNlhKvtCZIKNCTc3NzDz97BXAp+R/wh1Zto+y VWlk0xI1eH7JTlLsNFp/96SeSsyE5MJE6M25FKadktZ+rJkaOGkDUS2gMfeNtY58kdf2 iBQg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:cc :to:content-language:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id :dkim-signature:dkim-signature:dkim-signature:dkim-signature; bh=MbT9vfV4fhIRSbiw0ZC6R173lRwaCUu2Hmcj1y018H8=; fh=j8iRdBXgscGszDNWyYHrha06wSmsJ2I0SB0blpx+wFY=; b=tojCxTJMz9aYm15bxRmPq8ajJxepil7V8FpjZUf+jcpKkdiW5Zsdazzv55mOBJIB+E F2VZz+PABqbTRdm/y99xgIcL8RalAb55Ef6I638Iv1Z/HZvmmVjaKgJy3iBM8b+LzNkk aUx5sxltXRzz9fvt0MGBq+icH9fbdt1JH9zw0KOpODZzNnqW/GOdtZ8F3zm0S1so1/b8 kFm7/uGkIO4uijkYId+q1XAuQQMhvdko8iqaBzd+gw84PGKtGtbAcnn/Z7r7ubiztZX6 e4TnycnoA7pVa8HWqsQkJ1QRuxzcrhXEBlFr5AV9XVIZiRup7MQegvl52fFn7XSxUz8d btsw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=MQWERGqP; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b="KniWNG/8"; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=MQWERGqP; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.cz dkim=pass dkdomain=suse.cz dkim=pass dkdomain=suse.cz); spf=pass (google.com: domain of linux-kernel+bounces-184014-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184014-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-5733c3789c1si13356570a12.660.2024.05.20.10.31.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 10:31:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-184014-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=MQWERGqP; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b="KniWNG/8"; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=MQWERGqP; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.cz dkim=pass dkdomain=suse.cz dkim=pass dkdomain=suse.cz); spf=pass (google.com: domain of linux-kernel+bounces-184014-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184014-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 9EE691F22499 for ; Mon, 20 May 2024 17:31:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D9BB113848B; Mon, 20 May 2024 17:30:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="MQWERGqP"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="KniWNG/8"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="MQWERGqP"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="KniWNG/8" Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78E90A2D for ; Mon, 20 May 2024 17:30:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716226213; cv=none; b=JE6zpMAwjNyfjQ51bdl7T7fgEPQ8foMjheG2lYYbaBjMYzJ4toab/+zGGX6PsKBpJbWCXWX58jjRowNxZNX9nH1B7V/LGzhHjOCaUUD+N37CmsW4xJ25PUTEsn9LWinxLdkGXqaNK7XHSQlrLHbrGJm+lrBO5Dqql4GJ2FWpvRc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716226213; c=relaxed/simple; bh=cBsM2N1jHlOPN0IreD5rHEkojD3zYmil1uPSW0Oyi6M=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=uvfsSfHYXskNro8H8oa9rViOE/Kou7gDuFj9x+pe3JvF5c9cN+ZZ4f59p/HNPIBz4VtvMIEukHa3f1Pl1YRzFDOXz+iiw6AYlpqEOsY4E6PF+Cb7afExxjrM8Y78oEiJQ+aUPdMXcygWUap1INDLTrm1pTxRSDUiBXGjHcMPstw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=MQWERGqP; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=KniWNG/8; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=MQWERGqP; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=KniWNG/8; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 5F363228C0; Mon, 20 May 2024 17:30:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1716226209; h=from:from:reply-to: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:autocrypt:autocrypt; bh=MbT9vfV4fhIRSbiw0ZC6R173lRwaCUu2Hmcj1y018H8=; b=MQWERGqPK5msWZ+oGvqKi7Y0ImpsrNk+fbWyRF7r265R5BSNwlTWIB9ypDAUu+IWFo7NQe 8FyW+gWJC9iNBAxldDk9UVOmgp27E5dNYEke+/eskd7XI+Gy1TwVdtdKgRSVSA0J30A49o V76XSKKXUsZsUxMio2cS8tWGVZs3BNE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1716226209; h=from:from:reply-to: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:autocrypt:autocrypt; bh=MbT9vfV4fhIRSbiw0ZC6R173lRwaCUu2Hmcj1y018H8=; b=KniWNG/8jmqGj4Bb0B3h2Zw9b36h7Cxqp0WiyblPYFR7LaLhpYvbdrZeXnoHWVkExM7K+i z2qn/R2Q4CESHpCw== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=MQWERGqP; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b="KniWNG/8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1716226209; h=from:from:reply-to: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:autocrypt:autocrypt; bh=MbT9vfV4fhIRSbiw0ZC6R173lRwaCUu2Hmcj1y018H8=; b=MQWERGqPK5msWZ+oGvqKi7Y0ImpsrNk+fbWyRF7r265R5BSNwlTWIB9ypDAUu+IWFo7NQe 8FyW+gWJC9iNBAxldDk9UVOmgp27E5dNYEke+/eskd7XI+Gy1TwVdtdKgRSVSA0J30A49o V76XSKKXUsZsUxMio2cS8tWGVZs3BNE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1716226209; h=from:from:reply-to: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:autocrypt:autocrypt; bh=MbT9vfV4fhIRSbiw0ZC6R173lRwaCUu2Hmcj1y018H8=; b=KniWNG/8jmqGj4Bb0B3h2Zw9b36h7Cxqp0WiyblPYFR7LaLhpYvbdrZeXnoHWVkExM7K+i z2qn/R2Q4CESHpCw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 47CCB13A6B; Mon, 20 May 2024 17:30:09 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id J+vsEKGIS2a/bQAAD6G6ig (envelope-from ); Mon, 20 May 2024 17:30:09 +0000 Message-ID: Date: Mon, 20 May 2024 19:30:08 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] percpu_counter: add a cmpxchg-based _add_batch variant Content-Language: en-US To: Mateusz Guzik , dennis@kernel.org Cc: tj@kernel.org, hughd@google.com, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <20240520071407.201618-1-mjguzik@gmail.com> From: Vlastimil Babka Autocrypt: addr=vbabka@suse.cz;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fpco/UiKEsDswL1WAr tEAZaruo7254TyH+gydURl2wJuzo/aZ7Y7PpqaODbYv727Dvm5eX64HCyyAH0s6sOCyGF5/p eIhrOn24oBf67KtdAN3H9JoFNUVTYJc1VJU3R1JtVdgwEdr+NEciEfYl0O19VpLE/PZxP4wX PWnhf5WjdoNI1Xec+RcJ5p/pSel0jnvBX8L2cmniYnmI883NhtGZsEWj++wyKiS4NranDFlA HdDM3b4lUth1pTtABKQ1YuTvehj7EfoWD3bv9kuGZGPrAeFNiHPdOT7DaXKeHpW9homgtBxj 8aX/UkSvEGJKUEbFL9cVa5tzyialGkSiZJNkWgeHe+jEcfRT6pJZOJidSCdzvJpbdJmm+eED w9XOLH1IIWh7RURU7G1iOfEfmImFeC3cbbS73LQEFGe1urxvIH5K/7vX+FkNcr9ujwWuPE9b 1C2o4i/yZPLXIVy387EjA6GZMqvQUFuSTs/GeBcv0NjIQi8867H3uLjz+mQy63fAitsDwLmR EP+ylKVEKb0Q2A== In-Reply-To: <20240520071407.201618-1-mjguzik@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Level: X-Spamd-Result: default: False [-4.50 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; XM_UA_NO_VERSION(0.01)[]; MX_GOOD(-0.01)[]; FREEMAIL_TO(0.00)[gmail.com,kernel.org]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; RCPT_COUNT_SEVEN(0.00)[7]; RCVD_VIA_SMTP_AUTH(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; TO_DN_SOME(0.00)[]; RCVD_TLS_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.cz:dkim,suse.cz:email]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; DKIM_TRACE(0.00)[suse.cz:+] X-Rspamd-Action: no action X-Rspamd-Queue-Id: 5F363228C0 X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Flag: NO X-Spam-Score: -4.50 On 5/20/24 9:14 AM, Mateusz Guzik wrote: > This was "percpu_counter: reimplement _add_batch with __this_cpu_cmpxchg". > > I chatted with vbabka a little bit and he pointed me at mod_zone_state, > which does the same thing I needed except dodges preemption -- turns out > cmpxchg with a gs-prefixed argument is safe here. > > ================ cut here ================ > > Interrupt disable/enable trips are quite expensive on x86-64 compared to > a mere cmpxchg (note: no lock prefix!) and percpu counters are used > quite often. > > With this change I get a bump of 1% ops/s for negative path lookups, > plugged into will-it-scale: > > void testcase(unsigned long long *iterations, unsigned long nr) > { > while (1) { > int fd = open("/tmp/nonexistent", O_RDONLY); > assert(fd == -1); > > (*iterations)++; > } > } > > The win would be higher if it was not for other slowdowns, but one has > to start somewhere. > > v2: > - dodge preemption > - use this_cpu_try_cmpxchg > - keep the old variant depending on CONFIG_HAVE_CMPXCHG_LOCAL > > Signed-off-by: Mateusz Guzik Acked-by: Vlastimil Babka I tried a stupid microbenchmark just doing percpu_counter_inc() in a loop and this cut the time by almost 50%. As we discussed, should be also possible to make the fastpath inlined as the next step, to avoid the function calls that are stupid expensive with cpu mitigations. > --- > lib/percpu_counter.c | 44 +++++++++++++++++++++++++++++++++++++++----- > 1 file changed, 39 insertions(+), 5 deletions(-) > > diff --git a/lib/percpu_counter.c b/lib/percpu_counter.c > index 44dd133594d4..80ec2ffc981a 100644 > --- a/lib/percpu_counter.c > +++ b/lib/percpu_counter.c > @@ -73,17 +73,50 @@ void percpu_counter_set(struct percpu_counter *fbc, s64 amount) > EXPORT_SYMBOL(percpu_counter_set); > > /* > - * local_irq_save() is needed to make the function irq safe: > - * - The slow path would be ok as protected by an irq-safe spinlock. > - * - this_cpu_add would be ok as it is irq-safe by definition. > - * But: > - * The decision slow path/fast path and the actual update must be atomic, too. > + * Add to a counter while respecting batch size. > + * > + * There are 2 implementations, both dealing with the following problem: > + * > + * The decision slow path/fast path and the actual update must be atomic. > * Otherwise a call in process context could check the current values and > * decide that the fast path can be used. If now an interrupt occurs before > * the this_cpu_add(), and the interrupt updates this_cpu(*fbc->counters), > * then the this_cpu_add() that is executed after the interrupt has completed > * can produce values larger than "batch" or even overflows. > */ > +#ifdef CONFIG_HAVE_CMPXCHG_LOCAL > +/* > + * Safety against interrupts is achieved in 2 ways: > + * 1. the fast path uses local cmpxchg (note: no lock prefix) > + * 2. the slow path operates with interrupts disabled > + */ > +void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch) > +{ > + s64 count; > + unsigned long flags; > + > + count = this_cpu_read(*fbc->counters); > + do { > + if (unlikely(abs(count + amount)) >= batch) { > + raw_spin_lock_irqsave(&fbc->lock, flags); > + /* > + * Note: by now might have migrated to another CPU or > + * the value might have changed. > + */ > + count = __this_cpu_read(*fbc->counters); > + fbc->count += count + amount; > + __this_cpu_sub(*fbc->counters, count); > + raw_spin_unlock_irqrestore(&fbc->lock, flags); > + return; > + } > + } while (!this_cpu_try_cmpxchg(*fbc->counters, &count, count + amount)); > +} > +#else > +/* > + * local_irq_save() is used to make the function irq safe: > + * - The slow path would be ok as protected by an irq-safe spinlock. > + * - this_cpu_add would be ok as it is irq-safe by definition. > + */ > void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch) > { > s64 count; > @@ -101,6 +134,7 @@ void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch) > } > local_irq_restore(flags); > } > +#endif > EXPORT_SYMBOL(percpu_counter_add_batch); > > /*