Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp170539rdb; Thu, 5 Oct 2023 22:42:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHCVNjeCSaSeZ0wd1x3ihXDyiGMwCgDq5NkbY2HSGDY3+cnGNse9VIiCY2g3NKeYRDnZSab X-Received: by 2002:aca:2808:0:b0:3a8:472b:febf with SMTP id 8-20020aca2808000000b003a8472bfebfmr7007752oix.21.1696570956520; Thu, 05 Oct 2023 22:42:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696570956; cv=none; d=google.com; s=arc-20160816; b=WHMc8AvhCAX4PYhMlTftUZN/G8E7E/cijBmIIX16f/jajtiBXYWBOBAaO1NtkEzc3C vOv4KjjRbqyTAyqnfsoxinIpUY3jEeQn+G/j6yw8HrFaxdCNuN89z7R+URO1N4XsLvjh lM9v1RHhdA2XkBPocjBBMwhoE9sBejmx/VbQH7NswVUZj7DgXcgQzVbV044/jPNCtHXo w85dfPxJAzCUYO1EKZpPcHxQvDp1ocd2D4cMBOuqIuTYtmWRLIDPK9lha/AZUxqpMNJv zqXW7w+E8yji3Y1hixw3dUhZj1/rd+n11DWNnh6OEs2f9GYwzoZDvYQNXRJoQnQ9lg7x 3QoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:message-id:in-reply-to :subject:cc:to:from:date:dkim-signature; bh=iMNU/NcReGRTQoFhUIKFEhWrRToHaZS/r6S0IyfUYQA=; fh=DhT642X75EbUG7bhi8n/poFlJu4GiIILyNsapsJYp7k=; b=hIr3Ul7SxgWOZ7brI1wRu4vgdGK+BTjfiWuLZ2Yy/Exo7QM8uDPmAqRZQe1ic1ppUa JiccweXFbWxa1xbzTxoshoAHVu9mP271TrB0skyqkcaIFep0TtdptD/EtEVQMkzLQZho cqmAfRojnbc4EA+I4INeIRJg7SuafmF3NlZKxq8Z4Swzon09pIc+tfjcvH2qY3AyjmFg be/r4cBi5Hrii8LenzdG0g3rjlDppdmR0Z6J5tFAbVPssvu28o+Oh/XQhPkaemE/K5UO gLrpIi5Q2X9VvydVwvK7eP1Zo5HjJTV+is5RLFsgNuv08ts2lvS5FXZXmC90UtZqn6CA Lj1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SA+o1acD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id c5-20020a6566c5000000b00589879deb57si1150431pgw.92.2023.10.05.22.42.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 22:42:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SA+o1acD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id CB30A8068E1E; Thu, 5 Oct 2023 22:42:33 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230124AbjJFFmX (ORCPT + 99 others); Fri, 6 Oct 2023 01:42:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230017AbjJFFmW (ORCPT ); Fri, 6 Oct 2023 01:42:22 -0400 Received: from mail-yw1-x1134.google.com (mail-yw1-x1134.google.com [IPv6:2607:f8b0:4864:20::1134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 305E6BF for ; Thu, 5 Oct 2023 22:42:21 -0700 (PDT) Received: by mail-yw1-x1134.google.com with SMTP id 00721157ae682-59c215f2f4aso20830387b3.1 for ; Thu, 05 Oct 2023 22:42:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696570940; x=1697175740; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=iMNU/NcReGRTQoFhUIKFEhWrRToHaZS/r6S0IyfUYQA=; b=SA+o1acD+FtLsYNEUhzawIOUUFxuMD4SCozuTCTQtEIf8xsGjfnehkKT3gPXtqEXyM iZompe2XYlOamDl5cMHVTLMroUFR1xlXUvBNnasTZV1/UAO33aCIpNhN1hHqJKdr9JJ/ Dl8DF13lYIPkB7uTTmhWBcsQ0y8Pv07xSlorbFnXkzApo1q1huULdlZYF/Ykuo4N40fo /7KjrlIqLTXIQaCR+NmSpqgIR4QP2QcD0TIYEhWm1O1ILY0nxnuf0IdTeFgJ5WJGQj9v Px2hWNqHqcc/sGZ4aiUVGS1l7Y5YdijhE9831ZyqvJ66+jXw8PtmmOWk9bQak3QdhBQ/ WmLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696570940; x=1697175740; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=iMNU/NcReGRTQoFhUIKFEhWrRToHaZS/r6S0IyfUYQA=; b=ARr/26wW8IlHyC3uXR/Z6NFmAcSjcsq2AuTc0NF+5jS46hRRGSYtGjJWhTxUePMqgP n2ytPVUkUM8xLg9YWRTTFnYoqmgrf5kLH7CC6r6jKAZPYVBNXt4T/gXluifh6zusDiFG /QuHdvXrZ323hAm+klYh+BAUqbTuu1Rk2OJM/l7efdfXq5oO/R9zwg4FREVqO1ryTH9V WIZBZinzfqNjK8z1Ah1XYuuFt2RLmVoAzmiMF337Xg44CBHANAM2EiwIQAv1PMB+fR+t sVBa8B26Xlyf6W9aAUytz6KJOe73NPabmfoBTgdJMpwE6RHFrxenbch20Z3T6bfKDXwW 9XYg== X-Gm-Message-State: AOJu0YzbY4Bivzm1LbA7OcE1G5Nj76s3vD1pDImmi2fmeYUWnSzx3rTz GbAMKUuzItuUX3kqRwpYVLBohA== X-Received: by 2002:a81:4810:0:b0:5a1:d0fe:e44b with SMTP id v16-20020a814810000000b005a1d0fee44bmr7584991ywa.11.1696570940293; Thu, 05 Oct 2023 22:42:20 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id w1-20020a81a201000000b005837633d9cbsm1033602ywg.64.2023.10.05.22.42.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 22:42:19 -0700 (PDT) Date: Thu, 5 Oct 2023 22:42:17 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: "Chen, Tim C" cc: Hugh Dickins , Andrew Morton , Dave Chinner , "Darrick J. Wong" , Christian Brauner , Carlos Maiolino , Chuck Lever , Jan Kara , Matthew Wilcox , Johannes Weiner , Axel Rasmussen , "linux-fsdevel@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-mm@kvack.org" Subject: RE: [PATCH 8/8] shmem,percpu_counter: add _limited_add(fbc, limit, amount) In-Reply-To: Message-ID: <17877ef1-8aac-378b-94-af5afa2793ae@google.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 05 Oct 2023 22:42:33 -0700 (PDT) On Thu, 5 Oct 2023, Chen, Tim C wrote: > >--- a/lib/percpu_counter.c > >+++ b/lib/percpu_counter.c > >@@ -278,6 +278,59 @@ int __percpu_counter_compare(struct > >percpu_counter *fbc, s64 rhs, s32 batch) } > >EXPORT_SYMBOL(__percpu_counter_compare); > > > >+/* > >+ * Compare counter, and add amount if the total is within limit. > >+ * Return true if amount was added, false if it would exceed limit. > >+ */ > >+bool __percpu_counter_limited_add(struct percpu_counter *fbc, > >+ s64 limit, s64 amount, s32 batch) { > >+ s64 count; > >+ s64 unknown; > >+ unsigned long flags; > >+ bool good; > >+ > >+ if (amount > limit) > >+ return false; > >+ > >+ local_irq_save(flags); > >+ unknown = batch * num_online_cpus(); > >+ count = __this_cpu_read(*fbc->counters); > >+ > >+ /* Skip taking the lock when safe */ > >+ if (abs(count + amount) <= batch && > >+ fbc->count + unknown <= limit) { > >+ this_cpu_add(*fbc->counters, amount); > >+ local_irq_restore(flags); > >+ return true; > >+ } > >+ > >+ raw_spin_lock(&fbc->lock); > >+ count = fbc->count + amount; > >+ > > Perhaps we can fast path the case where for sure > we will exceed limit? > > if (fbc->count + amount - unknown > limit) > return false; Thanks, that sounds reasonable: I'll try to add something like that - but haven't thought about it carefully enough yet (too easy for me to overlook some negative case which messes everything up). Hugh