Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2918011rdb; Wed, 4 Oct 2023 16:10:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGEGxC2rQ/znDCO+exyENzOhud2n+anCTMoV2wzkYWJ2dSXL8WLX2PhtdItdXvEdXQCx0ks X-Received: by 2002:a92:c54d:0:b0:352:8b80:4744 with SMTP id a13-20020a92c54d000000b003528b804744mr4190677ilj.4.1696461044604; Wed, 04 Oct 2023 16:10:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696461044; cv=none; d=google.com; s=arc-20160816; b=IitdtxzhjM1bm3cKu7crSHOdSkZMEBw8vCHCIWk1yvYXYHmj0uqvaAQ+X3+jnqvVFB OHBoO/6IaRScu1WxrHG9GtZLhD3MAMcbNuvIDfCwDPbOrEhlQohKWNf7cpGYds9x31S+ w9YePWFmN0URPZKJVdGg0zoxmb3KuxV53NPSECK3wAMoGE4Oaqyf1wNbr4Uhc5sE9H/Q /6ygVXmKjydDo84ZBEzlZxL+ZMMQQMYg5xHHTIIgp65kUTgKiXGahTv0F54OX/9StlQ2 sIGeFN6e2snsYbsE5pSB4bbMjVZ9pBNaFULtIERIXiSTWjdqzYlU+ZTsD6y1a7fLvcaC FX/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=pBCvMKFa+vcZnG26/JeWxeoSgocrNcnNb84l5SDtiC0=; fh=MBE0jQANScWwt2X7OdNT+yE90t8XwxYUiTs9q4k4ltc=; b=Zr52uQnN3iUxNeyuR5ei1yBjpTQkWPoSd9e/rGYsZcK0jbwkpidVGGGDWLwS50g4iW Vs5l+38ZbfY60+gq/6xfxruonfHy1eBl8nSdfptuq9bL0uwikkia4sal/ww2ZQeEGxlE 69a13Ipr+/o/6NP77EFzBx/Loe8EyH3mHMgtjJua1W+NNATnppaK27oGdf+AnIL7xwCm TiFNWaT4Lv3VCu6i6y45rXdT+wOzb2HmnU8ySunbw8m3uWxvaW/4NKdK3T5mzyIZKo8k VUUTFmTa+fqPNJOIWpHgsaWOqlnc2TfN6iPIhDn5NSStYTOuQqURT9oKHip8Bd1Qn0HJ 6TAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fromorbit-com.20230601.gappssmtp.com header.s=20230601 header.b=JYaT0TWe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=fromorbit.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id n188-20020a6340c5000000b00578f1ab2287si159658pga.354.2023.10.04.16.10.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 16:10:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@fromorbit-com.20230601.gappssmtp.com header.s=20230601 header.b=JYaT0TWe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=fromorbit.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 8C0038291585; Wed, 4 Oct 2023 16:10:20 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240849AbjJDXKR (ORCPT + 99 others); Wed, 4 Oct 2023 19:10:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240627AbjJDXKO (ORCPT ); Wed, 4 Oct 2023 19:10:14 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E3EAE4 for ; Wed, 4 Oct 2023 16:10:11 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-694ed84c981so269937b3a.3 for ; Wed, 04 Oct 2023 16:10:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fromorbit-com.20230601.gappssmtp.com; s=20230601; t=1696461010; x=1697065810; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=pBCvMKFa+vcZnG26/JeWxeoSgocrNcnNb84l5SDtiC0=; b=JYaT0TWedclRbg6rhIKg0EkHmJaXOUcskCIaj1ApK0GoaKeIQIK+OXwH2UHFzMXhRL ROskWE9YtFNENPQsoaw/RG68F06iHOBsm1R7tqxp7w5y6qbHUPf6rQ74puaK5UhaDwmj C9icbukZ++CmH/qStRLyzoz9NTxs6fVSnJKcTH+tZUrEW3DWWxUYdjmvldG4S6zG/GVC e3Fx1p+TMiniDlrGDoAvb53Qv2dHj+6pTxr1rXD25lhtt75CMTmyM6ULMBJs6tVAfaSQ dd9uxYhx3rowJOitpiDxbpp1Bqt5JaEN9+TBeR3MsYxL4RI6BM5eDh79SGldIjvGj6Nb Q7Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696461010; x=1697065810; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=pBCvMKFa+vcZnG26/JeWxeoSgocrNcnNb84l5SDtiC0=; b=SwqVzRTRa/ybP0jHW3QA1/YGJpE40wTKNUtdQJYGE0orOhTrfdtJD2GHFzL5UGJX8U yCuus3ReVj1esiyTlUKnJDI+G6/ENo6sW0+t34EhgJlFgFuYaYcslkYDOfmL5d9cnNGC UeCIAJlcoIfMkvHzknF/yk+SMBh7zwlnxxZR3iY1rULFxYfSPAJFutfKtljTehkLxvW/ mIzZ2Zbb9eoKoRNW2hGOXMw4G24uMLnvw52ALmcwABW1okJTy+7TlFAuJCxwZKRVinCO aJZuCFrS5HAr1N1WZOUSYn5m/ehRYKp+o617mFs5t5eQP+bRI7RQDP8iT6bmRe2nNVA6 vlog== X-Gm-Message-State: AOJu0YzUvtVgl9+mre01VR+H9vGAHJrc4FtdTO/0BM73wER3E7STb0W3 04HUBvbq5FfcYsQk0MrjdnAtFA== X-Received: by 2002:a05:6a20:9698:b0:15d:624c:6e43 with SMTP id hp24-20020a056a20969800b0015d624c6e43mr3049725pzc.3.1696461010435; Wed, 04 Oct 2023 16:10:10 -0700 (PDT) Received: from dread.disaster.area (pa49-180-20-59.pa.nsw.optusnet.com.au. [49.180.20.59]) by smtp.gmail.com with ESMTPSA id x3-20020a170902ea8300b001bdb8c0b578sm100725plb.192.2023.10.04.16.10.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 16:10:08 -0700 (PDT) Received: from dave by dread.disaster.area with local (Exim 4.96) (envelope-from ) id 1qoB0H-009XMo-3D; Thu, 05 Oct 2023 10:10:06 +1100 Date: Thu, 5 Oct 2023 10:10:05 +1100 From: Dave Chinner To: Hugh Dickins Cc: Andrew Morton , Tim Chen , 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) Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Wed, 04 Oct 2023 16:10:20 -0700 (PDT) On Fri, Sep 29, 2023 at 08:42:45PM -0700, Hugh Dickins wrote: > Percpu counter's compare and add are separate functions: without locking > around them (which would defeat their purpose), it has been possible to > overflow the intended limit. Imagine all the other CPUs fallocating > tmpfs huge pages to the limit, in between this CPU's compare and its add. > > I have not seen reports of that happening; but tmpfs's recent addition > of dquot_alloc_block_nodirty() in between the compare and the add makes > it even more likely, and I'd be uncomfortable to leave it unfixed. > > Introduce percpu_counter_limited_add(fbc, limit, amount) to prevent it. > > I believe this implementation is correct, and slightly more efficient > than the combination of compare and add (taking the lock once rather > than twice when nearing full - the last 128MiB of a tmpfs volume on a > machine with 128 CPUs and 4KiB pages); but it does beg for a better > design - when nearing full, there is no new batching, but the costly > percpu counter sum across CPUs still has to be done, while locked. > > Follow __percpu_counter_sum()'s example, including cpu_dying_mask as > well as cpu_online_mask: but shouldn't __percpu_counter_compare() and > __percpu_counter_limited_add() then be adding a num_dying_cpus() to > num_online_cpus(), when they calculate the maximum which could be held > across CPUs? But the times when it matters would be vanishingly rare. > > Signed-off-by: Hugh Dickins > Cc: Tim Chen > Cc: Dave Chinner > Cc: Darrick J. Wong > --- > Tim, Dave, Darrick: I didn't want to waste your time on patches 1-7, > which are just internal to shmem, and do not affect this patch (which > applies to v6.6-rc and linux-next as is): but want to run this by you. Hmmmm. IIUC, this only works for addition that approaches the limit from below? So if we are approaching the limit from above (i.e. add of a negative amount, limit is zero) then this code doesn't work the same as the open-coded compare+add operation would? Hence I think this looks like a "add if result is less than" operation, which is distinct from then "add if result is greater than" operation that we use this same pattern for in XFS and ext4. Perhaps a better name is in order? I'm also not a great fan of having two similar-but-not-quite-the-same implementations for the two comparisons, but unless we decide to convert the XFs slow path to this it doesn't matter that much at the moment.... Implementation seems OK at a quick glance, though. Cheers, Dave. -- Dave Chinner david@fromorbit.com