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 DDDEBC04FFE for ; Mon, 20 May 2024 17:30:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 177BC6B0096; Mon, 20 May 2024 13:30:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1285D6B0098; Mon, 20 May 2024 13:30:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F0AEB6B0099; Mon, 20 May 2024 13:30:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D3DE76B0096 for ; Mon, 20 May 2024 13:30:14 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6397AA0F0D for ; Mon, 20 May 2024 17:30:14 +0000 (UTC) X-FDA: 82139462748.01.D05D430 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf30.hostedemail.com (Postfix) with ESMTP id 0C1C080002 for ; Mon, 20 May 2024 17:30:10 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; 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=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"; dmarc=none; spf=pass (imf30.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716226211; 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=MbT9vfV4fhIRSbiw0ZC6R173lRwaCUu2Hmcj1y018H8=; b=ZvcYtj5iKR+tVfwQ3Dz8umjZR82B6hHxGSAVjW3NKWlIPtmcZjfFQO5Lp7FBdkzDnLqEAZ lucfuTQG7vpovAlLCEgbVcn9JFVGGRyG41wbZuS5FTpGApPIdz5nQaLRVGjIrCorhKKdY3 csQbgNT03Xeh/xOmr5fCx2bLCQ1KAsk= ARC-Authentication-Results: i=1; imf30.hostedemail.com; 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=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"; dmarc=none; spf=pass (imf30.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716226211; a=rsa-sha256; cv=none; b=ezxR7Geb7O32tikN50OnlTtKu4uwXpNEQXI0rbKEcKCRQjPAYF9oNGXFShZoI+Or/CAnZt 1Ijuh6FRA3Hfy2SrjPRibiNlF6KxtxRDI1RzRpgOuCrNzQu1ec3I4KRCR5LvxoMOEpbG7t e20pdy6biyNbl418dXgJMXZGOp1gx7U= 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== 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 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; keydata= xsFNBFZdmxYBEADsw/SiUSjB0dM+vSh95UkgcHjzEVBlby/Fg+g42O7LAEkCYXi/vvq31JTB KxRWDHX0R2tgpFDXHnzZcQywawu8eSq0LxzxFNYMvtB7sV1pxYwej2qx9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r+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-Rspamd-Action: no action X-Rspamd-Queue-Id: 0C1C080002 X-Stat-Signature: n5g9wpqte3a1e7sraqqip7oikuff4agd X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1716226210-383541 X-HE-Meta: U2FsdGVkX19alek3IgmF7dwAllo/NIbiHqbgDI8aCP+GgqPmz9z3kMi/bpKktGACLFkyE5BHqefnApLRaq/JWgFY7yHhDA/T/0MvlI+zKBQbY34CkI+Erne2zmj6wxxd7uWGlbA49kdxkcR4CV2jY9g17kvjO4TtmG7+bFQ2pUQYHmd/SqxyLe8Yf81E/8ZcfsYH9Ex2yjPBcezyRxKRoIF/UznEc80fJz4GR0Pdy9LrOXqQ2y0ObqwoYmnc7d5eU3KIB1f7Llxyp/HGqh0J5YTApuKaPqYw6eETngcNuZ+PS55jcoCvmBaGEvFn6yxd6kAwhlHRkhI3+Q3waPWf7232zLLg1q/aluSjl9MrAfvkSBf99YYF4AerqO5Dt0hkgJV6nOliEHjuBmE63jQKW+i1P9iBuywei4N+6HT8cU/8Z17Dgs010BGwiC0a77CMMFQWvi4jkG/6jq/Id8+QOD9omG9fXFoOWsY5f+ru49kwyhV9JJTisS5Tt9r+eE0ucFQgC5VshfUDv0fjS6YgM4UZ0+mtVnRxKN72OWyAqY7ps7MnwasGrOTS1C6b0aN2k9uOOizbjku6cMMg8lxDCP5vev33F9OyOQOJ0wx8qm1zwxahmuCrjTrScV8YCSy8EGbBRXWz2B7MTjFYabcZpERco+1Qcwp518n6iCLRfYm8wUPh6FGNmXWU1sF/xdx8lhokKgEwDV6V/CEbrI0W7JWiAMchLlq0TzgrOu1Opu+urAEw1gTbYr4QlrlvCNnjMeZsoG+PuBao01Yi77tlxWvWyTvSJPbYfl3KgC/SysZmylo5STt1CGdbVSt8n48LDNg/hKfFoL0EVRTet9Abs9L0JINfNFHxL/KjEv9uEv+3K++BW02HMC6cvHvY7wZQYTjzzkwGkMhP5IhCkDdxedQylhGNDkWEnYpq+6dz0kBBsEFrQkbOp6soEkGm+yiydJxZkpHhNYYL5gHtILf OmDH/aN8 +AZotQ2+lA2y1C+L022iQh44ikMng1zQ9jFPT3jlrygLWDz6icJ8ra4/u6P+/0+n82DNwOfKSk/QoeBQuVjDh1n0P4480z1Mc8uh9MxmuLrHb0xlPsGVBk5BYRXZWMUcRt6bRwM/2EvXIaXg9kqKtrKABZvzMLaP+Ro6JmhsghgchVM/OL/MoScvUkOe2rAMkaGAWCPZ/ZOIVb3A2G4uT1N3SvKWrOUPOMwXtUcHHdEaCWohWz/LbSlOknoCRnGAR4pPuw0ZLEdHwyWSit58XjLSo1kob+ePvl78VdKNGMOzyrZsIWZPkQFw/UXo4G0F1vFNfAi7itpUgRyg= 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: List-Subscribe: List-Unsubscribe: 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); > > /*