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 80B02C25B77 for ; Wed, 22 May 2024 04:59:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E11F66B0085; Wed, 22 May 2024 00:59:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DC1F36B0088; Wed, 22 May 2024 00:59:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C88F86B0089; Wed, 22 May 2024 00:59:19 -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 A56346B0085 for ; Wed, 22 May 2024 00:59:19 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 593F3C1291 for ; Wed, 22 May 2024 04:59:19 +0000 (UTC) X-FDA: 82144828038.06.F798C20 Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) by imf24.hostedemail.com (Postfix) with ESMTP id 7ED2718000F for ; Wed, 22 May 2024 04:59:17 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RqN6vPHU; spf=pass (imf24.hostedemail.com: domain of mjguzik@gmail.com designates 209.85.208.51 as permitted sender) smtp.mailfrom=mjguzik@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716353957; a=rsa-sha256; cv=none; b=6POMyL7By1dLcNAkjbGNZ097LfkQk6fJz6AojtCH/ZSOuQvaPnZ+PVsGqx/VvdGHvdI7Yy yLD/roO99W2iTbBDpXuN6Q6TDFpDtSj0bjuSAdT1rCvxmAag9rOC/xhuPXffsIBbvFq0Pu D8FciXDx6iApR8+0UQM5VZhFs6xf7D0= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RqN6vPHU; spf=pass (imf24.hostedemail.com: domain of mjguzik@gmail.com designates 209.85.208.51 as permitted sender) smtp.mailfrom=mjguzik@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716353957; 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=39bxPhPVcA85UbzZ3VjWUXERVKdx/CAHlqDcF/RAs0Q=; b=gy/fumWFRnWDGaxVcu69c9SvUg+uUg2XKm0BIz3HihizszlV5alPpzrWCWI6AY8dbKJMNP 0WqXTiH05luixDYPl1tbO4Y2T5VgvyB9MeI11XKuRXxa4qW0XK45ZzsO2MBrH/gqI/LsXh SSaqODYcTcBoW9YjS+L48R8MdT5NF84= Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-572c65cea55so836511a12.0 for ; Tue, 21 May 2024 21:59:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716353956; x=1716958756; darn=kvack.org; 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=39bxPhPVcA85UbzZ3VjWUXERVKdx/CAHlqDcF/RAs0Q=; b=RqN6vPHU2B7tELR2xM8wSOnvYi0v2hu5qbIiQt1WWmLepfYlV7nslUipX4NhJlz6gw +zsLkPSGEarhAEapDxpqhaLhJA4JOiWhKoH6fgBSaRfY66jDTvgQInKfnsLuTsnxnMao k9lxWeivUk5csSBUTWfCVLCb/Bt+LlblSmmpOdiW4QLaHijcs564wAp80CL1q2yikGRU JBOnS5+++WYyT6uXoDH9lUondJYcWOdRhTco1VDRgNfVVqVk1VzCq95qtyBpYjn7G9O+ C9VyiQ+W19k1eyLUnKURCO1LjND5qi6Eqfuk5EVEbVPHW76oz1qb1MIizp9woRTB1jIx bcLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716353956; x=1716958756; 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=39bxPhPVcA85UbzZ3VjWUXERVKdx/CAHlqDcF/RAs0Q=; b=ZnssmXS0IuJ7Ojau9aClq7Zue3FhONzXO7hm1HOUkYbnEon4ZZsAXpcdyR2/wYts39 tlB595gaPVl9k3xYOxhdg7/OIqvlePWuxpXTel3IY0OevCxh+8IEpajrvwjasTC+bntd ulC+qir4VS0MGKl4VN9NHTBbm2OQrVP43uoULbXbDtaDO4Lwgmu9psTbbh1AhcMZTiVj fWKiVl7W0t+IjWThDrC1EoqvX7ku5A7hQ22nlDbpvmErjbWW/e3fHMmU9QmcUAwGvNKM 1v9i4XKhKt0Kq03RyudUjB3mbw5RfHfmvgjyE4LASszhUX7cdEhC2CPHhGRFNbtfpSK6 UbTw== X-Forwarded-Encrypted: i=1; AJvYcCV0kRLnb5Rx6qWnlMtt2LfiWa0G7V6Tju6jtnNyYasTxRErHFc1IHkkaRfemh2QNiOkdtebMVMMMCT+JJGM16l4IK4= X-Gm-Message-State: AOJu0YyQFD4NPmWWwvlQkG32fNEwtwaECPBne/XrQkvGx2kJLTzOdSEB 05Fsi2YMHwwczFIqdUK9ZCq4BY+vLbz32oahHl8efIx2LtjArnrCzU1QhfMNr53FAq5JpUZCbg7 8eHeGKOuE7+FT7bCJSv2/M7fs91M= X-Google-Smtp-Source: AGHT+IGa+13CEFwmy5GAjmwiCyxL7oM9UfZgNMhWLRQ0BlkBOC5alvxMemPCt+VCIkYC9kd+4eTQUKoxh5bxNUxKbRE= X-Received: by 2002:a50:c044:0:b0:572:7014:230a with SMTP id 4fb4d7f45d1cf-5752b4c8d66mr9496526a12.14.1716353955662; Tue, 21 May 2024 21:59:15 -0700 (PDT) MIME-Version: 1.0 References: <20240521233100.358002-1-mjguzik@gmail.com> In-Reply-To: From: Mateusz Guzik Date: Wed, 22 May 2024 06:59:02 +0200 Message-ID: Subject: Re: [PATCH v3] percpu_counter: add a cmpxchg-based _add_batch variant To: Dennis Zhou Cc: tj@kernel.org, hughd@google.com, akpm@linux-foundation.org, vbabka@suse.cz, linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 7ED2718000F X-Rspam-User: X-Rspamd-Server: rspam12 X-Stat-Signature: cqtdkf6h3bxr71y6d5n8yj6a7woomjfm X-HE-Tag: 1716353957-412527 X-HE-Meta: U2FsdGVkX19o5bkquNgQChaeI1sGo+Hy22B4yH4U/529WjOWSvNAs8D8asb6YyzyYIWVanKj6phNhHMikSz9VamJeYCrn6mS6s332P8x05Lpea7hjQ+kZZinTvott0al9X331QZVKuih6GZH0d29WgbFi2YDd4UebTNEiT+/9+xLLPmdLdd2SMmjYal3xH0+BDP5zy2m8Tl2Q28QJgle7vVu0JXDkhkLPF/XK8l6c9cq4EuFZ0DCTb3uE99j7D8M+GyWqeLFFu84PcFd3A9iRYcnwYzcHXW4LOdkkJKmz2d95hiJ5nB+wT82/pCp9NIsHKn6ulXYenAHFJ92nh++q48SKFYhM8ECFYTl2HkiukJ59X0cc3vAQilbyU3HE0QS5WLUZVXHvetddju6wflGj1VGJBZHhcoMUdxpA3TCV4XHmjv0H0Cjwzclx9CLezr9BA+ookXMG8WBywraZ/NlyxlMOb+TngEdvaSve0sycgXxP++DYAdeOs8gMa46j1guoTNyV1OjzFNfNOdMm8K5wmDG2TSoAp68GTFoU4AWPDzQuN6MjxTXpeQMKHShvyiXcl8VIMQL7Er8YRz6BzjE0hu6+HnD/ZqXYkl5h0XgWeEZ2L4R3Ig2FrKuvNsM5aOEYgR0jAh3VT/h+IpMOq3yfprWoCvKnkJoXZoexEfoqiZP+ETm54ggtZt2rVh/ogC3OOHWI9G48ZYQiX61eX+RZl3I31MWYcrWMklPH8o9xICW0JXdyw70hHu4wyt7AnLGnh72cn7Wk7LdXgvuP0Bv9CcVFHu/K07xtSRTb4DyRM83UemvEMQhoohR+QrVtiDLgofUjLv2sdP+IGZT7OiNzA6Uth2RN6L4Oq28Rje8mATxz8/0u82Hn5QfNB9j1YuEO0LMboC28pvLzE4ssbmTGpIB67ByyGO44KPMfmfNstWsa6X4VLfbZM33R0u6dwB1nHmEk5qWczjbL5YaZyX eFeQ9Vm/ eQt++GHkKBWU79uAffyXIEhanbPlJmccTU2UJyxr4xDBcwg6LRePuPOLXhnjIk2g90L9ZUTI6g9AqQEswLfw5K0iXZsEHBGhfwrRyYEtYe+XT0R3nEinSxXdmdgXI7nhrMWdRY2APQieUtrT5H4HaVPhvcY658DrRELmgE6tLVGfIckwVGMdrRa803u2Buj818hbxgV369EAqjxrwZh1NZd/30I+VPOPDoFME0IMMK5Jwf4OkTUooVEkEFJD9dMYHivUJv025LHeagKgu8KgH+xjRUCMjVjtAcj/x12Xf/ywDvoenFTVipDVJIdXDBPht8rVRBk1/Nk2GAl15lLrU21TbkGGryhrTqXzhOCIguZOb03ZVMpZxK8fJ6r6xiV5qHGMotZAHy+6BbmZsXpzXIB0lUeHFnXDhDpeCzo67lwBcLUksFct6k8GpNcQB9cXAw+c94EIhwj1veYX8tSTCFNg+mUVN/HNao71w X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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 Wed, May 22, 2024 at 3:17=E2=80=AFAM Dennis Zhou wro= te: > > Hi Mateusz, > > On Wed, May 22, 2024 at 01:31:00AM +0200, Mateusz Guzik wrote: > > Interrupt disable/enable trips are quite expensive on x86-64 compared t= o > > 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 =3D open("/tmp/nonexistent", O_RDONLY); > > assert(fd =3D=3D -1); > > > > (*iterations)++; > > } > > } > > > > The win would be higher if it was not for other slowdowns, but one has > > to start somewhere. > > This is cool! > > > > > Signed-off-by: Mateusz Guzik > > Acked-by: Vlastimil Babka > > --- > > > > v3: > > - add a missing word to the new comment > > > > v2: > > - dodge preemption > > - use this_cpu_try_cmpxchg > > - keep the old variant depending on CONFIG_HAVE_CMPXCHG_LOCAL > > > > 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..c3140276bb36 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 atom= ic, too. > > + * Add to a counter while respecting batch size. > > + * > > + * There are 2 implementations, both dealing with the following proble= m: > > + * > > + * The decision slow path/fast path and the actual update must be atom= ic. > > * 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 b= efore > > * the this_cpu_add(), and the interrupt updates this_cpu(*fbc->counte= rs), > > * then the this_cpu_add() that is executed after the interrupt has co= mpleted > > * 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 =3D this_cpu_read(*fbc->counters); > > Should this_cpu_read() be inside the do {} while in case the extreme > case that we get preempted after the read and before the cmpxchg AND > count + amount < batch on both the previous and next cpu? > this_cpu_try_cmpxchg updates the local value on failure (hence &), so from semantic pov this is equivalent to having this_cpu_read in the loop. I'm using it the same way as mod_zone_state. > > + do { > > + if (unlikely(abs(count + amount)) >=3D batch) { > > + raw_spin_lock_irqsave(&fbc->lock, flags); > > + /* > > + * Note: by now we might have migrated to another= CPU > > + * or the value might have changed. > > + */ > > + count =3D __this_cpu_read(*fbc->counters); > > + fbc->count +=3D 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 + am= ount)); > > +} > > +#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); > > > > /* > > -- > > 2.39.2 > > > > Thanks, > Dennis --=20 Mateusz Guzik