Received: by 2002:a05:6358:16cd:b0:dc:6189:e246 with SMTP id r13csp1578686rwl; Fri, 4 Nov 2022 16:12:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4kY8d93Ni44v5mvxURybBMofVBMUdcTl2t9MoA5eA15lfBndW+as/4ZK7HsrMyJhlgDlJ4 X-Received: by 2002:a17:902:d891:b0:186:634e:5525 with SMTP id b17-20020a170902d89100b00186634e5525mr37391095plz.126.1667603523459; Fri, 04 Nov 2022 16:12:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667603523; cv=none; d=google.com; s=arc-20160816; b=07XAWlEJSfDK7XnzPfpepgW6tyxDlOaTj91AgzuT5iFds8E+xfY2388rBRxqKn3UdA 02vdT/odoEJjHJrF4uffk8jXvundJR3RE8SzK6i0JQTRndOfJnM08vREkXu/d4KFOTiy kTWu0wOZMoYul9gKs1nPp9yuXg2Nwrlho5jTzOJYQyev5oQO6siBKEGH3S9wtNscT4zD r01QyPgjqTR51FnP5Z+Zo3ons1grn08Dtj2SF33Y4TkIgDq+JAv6cOXhf0uinqrOWXvj ECKvgdGjDiDoLYCxsADNca67jyqMmHA0D/ENklxobf2OROAKZIKV4hFeL8H3is3j1m4N yw2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=t0NXrJz6RBGQatmAab53FWu8Q4qKORqaCEexs4v4Rog=; b=Mmp1KrhthxycU3VS76i/wRk0CyrhByC7ZmBB/vl83hhRauAMi4X0xX/HDlZdI03P3K xFqeTEDKoiuQXJyXaL6eQ4SqrJonwBFnGi8TdJnwcYuljk7XoAKbrzv2hy0nJ1zEvHsS XYJzslzJMTkzXRajGdAdUbd339ipEtSio+/Os0Xy4xM6t1IIpwb61bZ/sCb9CLgTgHWu SpR7d7MjwaoYktYEwjxNeE3ULsWvwGClzzKPs9wwuu2c2sbwDrPXBC2cW05sl4z2OlM0 QT2W2C4gDB6mHgpb+pBJjnC+THkLpyiblasvwMb0u46RLyQ0mqXHGVC3LZxFZrLAOZ+C 8T9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=MjklJDPH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g19-20020a625213000000b0056c45eb2147si597940pfb.51.2022.11.04.16.11.51; Fri, 04 Nov 2022 16:12:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=MjklJDPH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229642AbiKDXF7 (ORCPT + 97 others); Fri, 4 Nov 2022 19:05:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229591AbiKDXF4 (ORCPT ); Fri, 4 Nov 2022 19:05:56 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A9E642999 for ; Fri, 4 Nov 2022 16:05:54 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 07CB261A59 for ; Fri, 4 Nov 2022 23:05:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 45F69C433D7; Fri, 4 Nov 2022 23:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1667603153; bh=Rw/MCWcvLjCJ/UWizKXePGmZvqTCn6fbSwOgN113JjU=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=MjklJDPHV10dl3G3Mc3kX/3WBeuIFfo+ldITzNm0LKr27MJrZLTXB0dA6tQLoVdLu YiqFyit8Xt8MmuzxlgmCoREIG472Rm/9MGgnBHaR6aG+JQNAEUI3P8M0/ulLEZ1o5i jnM3C0OVq++n5h8Jk3dy5fh8BWTma5R0EiSRMUTw= Date: Fri, 4 Nov 2022 16:05:52 -0700 From: Andrew Morton To: Shakeel Butt Cc: Marek Szyprowski , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm: convert mm's rss stats into percpu_counter Message-Id: <20221104160552.c249397512c5c7f8b293869f@linux-foundation.org> In-Reply-To: <20221103171407.ydubp43x7tzahriq@google.com> References: <20221024052841.3291983-1-shakeelb@google.com> <20221103171407.ydubp43x7tzahriq@google.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham 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 On Thu, 3 Nov 2022 17:14:07 +0000 Shakeel Butt wrote: > > ... > > Thanks for the report. It seems like there is a race between > for_each_online_cpu() in __percpu_counter_sum() and > percpu_counter_cpu_dead()/cpu-offlining. Normally this race is fine for > percpu_counter users but for check_mm() is not happy with this race. Can > you please try the following patch: percpu-counters supposedly avoid such races via the hotplup notifier. So can you please fully describe the race and let's see if it can be fixed at the percpu_counter level? > > From: Shakeel Butt > Date: Thu, 3 Nov 2022 06:05:13 +0000 > Subject: [PATCH] mm: percpu_counter: use race free percpu_counter sum > interface > > percpu_counter_sum can race with cpu offlining. Add a new interface > which does not race with it and use that for check_mm(). I'll grab this version for now, as others might be seeing this issue. > --- > include/linux/percpu_counter.h | 11 +++++++++++ > kernel/fork.c | 2 +- > lib/percpu_counter.c | 24 ++++++++++++++++++------ > 3 files changed, 30 insertions(+), 7 deletions(-) > > diff --git a/include/linux/percpu_counter.h b/include/linux/percpu_counter.h > index bde6c4c1f405..3070c1043acf 100644 > --- a/include/linux/percpu_counter.h > +++ b/include/linux/percpu_counter.h > @@ -45,6 +45,7 @@ void percpu_counter_set(struct percpu_counter *fbc, s64 amount); > void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, > s32 batch); > s64 __percpu_counter_sum(struct percpu_counter *fbc); > +s64 __percpu_counter_sum_all(struct percpu_counter *fbc); > int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch); > void percpu_counter_sync(struct percpu_counter *fbc); > > @@ -85,6 +86,11 @@ static inline s64 percpu_counter_sum(struct percpu_counter *fbc) > return __percpu_counter_sum(fbc); > } > > +static inline s64 percpu_counter_sum_all(struct percpu_counter *fbc) > +{ > + return __percpu_counter_sum_all(fbc); > +} We haven't been good about documenting these interfaces. Can we please start now? ;) > > ... > > + > +/* > + * Add up all the per-cpu counts, return the result. This is a more accurate > + * but much slower version of percpu_counter_read_positive() > + */ > +s64 __percpu_counter_sum(struct percpu_counter *fbc) > +{ > + return __percpu_counter_sum_mask(fbc, cpu_online_mask); > +} > EXPORT_SYMBOL(__percpu_counter_sum); > > +s64 __percpu_counter_sum_all(struct percpu_counter *fbc) > +{ > + return __percpu_counter_sum_mask(fbc, cpu_possible_mask); > +} > +EXPORT_SYMBOL(__percpu_counter_sum_all); Probably here is a good place to document it. Is there any point in having the percpu_counter_sum_all()->__percpu_counter_sum_all() inlined wrapper? Why not name this percpu_counter_sum_all() directly? > int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp, > struct lock_class_key *key) > { > -- > 2.38.1.431.g37b22c650d-goog