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 EE67AC4332F for ; Mon, 7 Nov 2022 21:19:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D7418E0001; Mon, 7 Nov 2022 16:19:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5872E6B0073; Mon, 7 Nov 2022 16:19:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 476338E0001; Mon, 7 Nov 2022 16:19:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 39A546B0071 for ; Mon, 7 Nov 2022 16:19:52 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E6F4E120766 for ; Mon, 7 Nov 2022 21:19:51 +0000 (UTC) X-FDA: 80107913382.07.17A6165 Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) by imf05.hostedemail.com (Postfix) with ESMTP id 876DA10000A for ; Mon, 7 Nov 2022 21:19:50 +0000 (UTC) Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-3701a0681daso116948897b3.4 for ; Mon, 07 Nov 2022 13:19:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=H/Db9QVhek5neko0fLOabrR6exgAplpWG4R/xjzBLjc=; b=imEQtpG+c8ktMmb1IK8HOqoPy+FWxyFnTrQ5MuPDImKbZwqqKHdXZNmv87GXcXaVCs 29qRyI/Tm3otWgD/6t/foNpYuhM69MZLLH28LjAJCtbT+2TlbiLymJ/tqNtNAkAdcsQ4 9ymcb0gFFYjisR2epDcbv4UwvtyKVT67Yt2EPb2cYhOHWWOT9RmYO84MK3QJY7vQnAH9 s5vl8hiQgK6QnqgOgVvBwpjnDwXQymGjfsiJwmk+Tydx//b8TMP8zFO/oW3zMeOf8op1 NHSH+wiBsS++JJjlmdSz+DS3v2tGs3gaSNgwTKXYBJ+nEjX7KElwVIrSqyXsac4neBaK Wubg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=H/Db9QVhek5neko0fLOabrR6exgAplpWG4R/xjzBLjc=; b=4jYrejkb6h0wZ0L+q6hm8ntqAvlUM+Hn16tF2OW5rEpPse4OsKHePliygxDkQX0vx+ +EeZXtDuJVtvd5Kc+zMKScMtHisU6eV7OG0hgTPgkKJLmJG49u6CG/aytAODC+MCgNOG E+nmpr35z9VEtebd8l3ak/a8QCZFfuQfLTDRQNJJ3oQVpY+u6eF9Dm/HZ0CHx9NNZNzp MYvzgGWYHNLCt3+orgCdV2Zq9KyGTIVLro9UVhAeny2TLN1gkS9LMOiDIszrFxoYSSu3 T9Z1/9mb9iIOtEQjZLz3wsDAgD87HObNnIoJdaJ0rwQoPPHndNKElV4jFcwIzQYpGL8z j/Eg== X-Gm-Message-State: ACrzQf2OBY5bij9uWozKrDq4KL1PlYad10DPpymDHLnMBEyt6m3/rcFs /oz2VDPFax/ld1QR/F8+yohYOQser31+Uh3u2nvbCQ== X-Google-Smtp-Source: AMsMyM5fzdrKc3lZbym82tTvqEBMT9VP1gGgGUGVcjcPVwCVUkT1CvTsrfwQ8pKHkg35WOYQOpo++veb55QcVIgNkFc= X-Received: by 2002:a0d:d7c4:0:b0:368:9884:fe84 with SMTP id z187-20020a0dd7c4000000b003689884fe84mr50081753ywd.252.1667855989534; Mon, 07 Nov 2022 13:19:49 -0800 (PST) MIME-Version: 1.0 References: <20221105014013.930636-1-shakeelb@google.com> <20221107130549.db68c48afe5f711b2e99c5c0@linux-foundation.org> In-Reply-To: <20221107130549.db68c48afe5f711b2e99c5c0@linux-foundation.org> From: Shakeel Butt Date: Mon, 7 Nov 2022 13:19:38 -0800 Message-ID: Subject: Re: [PATCH] percpu_counter: add percpu_counter_sum_all interface To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Marek Szyprowski Content-Type: text/plain; charset="UTF-8" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667855990; a=rsa-sha256; cv=none; b=vt64lct9x+Fb0KiutDynzMiLXtwJAhBdsI/wmozzxrcoNEu75K0mWyICLkkHfzRcJRZSm2 Di0hN6dM29HuNKeyXwRqondRQus3lCcRrBd0UU2ADQAnLo9rubiCKseuUjgUffvmcvrmoj L9T+yX89ye6BOu9PtVsDyiMFUtJLQpg= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=imEQtpG+; spf=pass (imf05.hostedemail.com: domain of shakeelb@google.com designates 209.85.128.179 as permitted sender) smtp.mailfrom=shakeelb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667855990; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=H/Db9QVhek5neko0fLOabrR6exgAplpWG4R/xjzBLjc=; b=16BzrWZ8ZloCJFLZ8YA2oqekLXAotwNdAN7R9K0iFXdXbJwwiIC6ZTbbB1bIwqyJOJHDQ7 xjs8EhSa5S8RFK95AVIiEsw0LHBPh7fy26H1EUn36w8VyFgzxtXN1avvjf4UlENK0gOqtn 5VtJs4A7KhEkg4KJvxd73zifSAB8H1w= X-Stat-Signature: jfboo6cwfru7mnw4mwof36q9nqfd6mpr X-Rspam-User: Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=imEQtpG+; spf=pass (imf05.hostedemail.com: domain of shakeelb@google.com designates 209.85.128.179 as permitted sender) smtp.mailfrom=shakeelb@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Queue-Id: 876DA10000A X-Rspamd-Server: rspam09 X-HE-Tag: 1667855990-135591 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: On Mon, Nov 7, 2022 at 1:05 PM Andrew Morton wrote: > > On Sat, 5 Nov 2022 01:40:13 +0000 Shakeel Butt wrote: > > > The percpu_counter is used for scenarios where performance is more > > important than the accuracy. For percpu_counter users, who want more > > accurate information in their slowpath, percpu_counter_sum is provided > > which traverses all the online CPUs to accumulate the data. The reason > > it only needs to traverse online CPUs is because percpu_counter does > > implement CPU offline callback which syncs the local data of the > > offlined CPU. > > > > However there is a small race window between the online CPUs traversal > > of percpu_counter_sum and the CPU offline callback. The offline callback > > has to traverse all the percpu_counters on the system to flush the CPU > > local data which can be a lot. During that time, the CPU which is going > > offline has already been published as offline to all the readers. So, as > > the offline callback is running, percpu_counter_sum can be called for > > one counter which has some state on the CPU going offline. Since > > percpu_counter_sum only traverses online CPUs, it will skip that > > specific CPU and the offline callback might not have flushed the state > > for that specific percpu_counter on that offlined CPU. > > OK, got it, thanks. > > > Normally this is not an issue because percpu_counter users can deal with > > some inaccuracy for small time window. However a new user i.e. mm_struct > > on the cleanup path wants to check the exact state of the percpu_counter > > through check_mm(). For such users, this patch introduces > > percpu_counter_sum_all() which traverses all possible CPUs. > > And uses it in fork.c:check_mm()! > > > --- a/kernel/fork.c > > +++ b/kernel/fork.c > > @@ -756,7 +756,7 @@ static void check_mm(struct mm_struct *mm) > > "Please make sure 'struct resident_page_types[]' is updated as well"); > > > > for (i = 0; i < NR_MM_COUNTERS; i++) { > > - long x = percpu_counter_sum(&mm->rss_stat[i]); > > + long x = percpu_counter_sum_all(&mm->rss_stat[i]); > > check_mm() just became more expensive in some cases. nr_possible_cpus > * 4. I wonder if this is enough for people to start caring about. > > check_mm() is presently non-optional and I'd be reluctant to change > this, given how commonly we see the "BUG: Bad rss-counter state" > getting reported (22 million hits in a google search!). > > We could save a ton of that cost by running percpu_counter_sum() first, > then trying percpu_counter_sum_all() if percpu_counter_sum() indicated > an error. This is only worth bothering about if the new check_mm() > cost is a concern. > Yes, this makes much more sense. I had run hackbench on the original patch and didn't see any significant difference. I will update this and run some more perf benchmarks to make sure there is no regression due to this change. thanks, Shakeel