Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3058246rwe; Mon, 29 Aug 2022 05:16:20 -0700 (PDT) X-Google-Smtp-Source: AA6agR5M+RZkEX6YCvsKBU97mDO8ntQkDrhqjDZ1dbJ5Vop4i+8Ua1pSfWL5w87uW0rr97TS011V X-Received: by 2002:a05:6a00:4c85:b0:538:5500:4873 with SMTP id eb5-20020a056a004c8500b0053855004873mr2402977pfb.81.1661775380325; Mon, 29 Aug 2022 05:16:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661775380; cv=none; d=google.com; s=arc-20160816; b=llTWLFJbMW+OilIlzJSxgIds2pbE+SJFqIMRaYfKNFseah6DBhjwwtwOPTLPEafb3L 0/0BeSrbXCiyT1sbw0UHiZ2tN/lSwQhW1RLnc3ge0oCzTxtYyFGeALMXRamotFB2kkbW Qr/x/SL+hJ3VOD6FmeRzslW/dFW++m34PHymCHkf4bMFopadeixabkZpwxFBJAs9b62E vhecJPt80nlBOrTNgnRvQcwbe5Y7E2RlrM/iB3z+c8BkyGgspB3FBiazPWfjfGRNzenP pmGLREV8kBNXcg8HzsOuHJ0zbf3249eXgKwaTEEzvmKRlOTc+/mfg6i0F/B33ySW9Urf moWg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zSuNDor4oO++04pIcVCz17nOiIXKuQFAMpglwW52tlk=; b=RARMggNNTt6UcPbv+12LvMvNoo6Xmb/UWXvco02viAap4xMLVu4ktAs33tLkyNJvyx nglj7zvL0PY9NZew3Tk/RRfFj7h4GRrJOreANaMsp6pgCnQP6+KLS0V98E7/Vm69ybnS 9XCMGMNR82vPuh43N5BbS2CgQldo2I7Q+HTnAH2J7mFn4ZRMQ4ZwfpYCJuPIoUiCIvEY VqIjMJaf6BtFr779FGA6bdb/HOakkAkfP5+Fo6Z8ajAU8zBk5J00SdpWdPaD+9wnKDJ/ BMfKr1EaX3f7U53/aryKrTvIQfpgb+4wS8iIwztPe4dd2bGcrWzMqDZZ8uLrAMQHXGqq iCVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=B5Bk2qgK; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j71-20020a638b4a000000b0042a08b6ae1fsi6139397pge.4.2022.08.29.05.16.09; Mon, 29 Aug 2022 05:16:20 -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=@linuxfoundation.org header.s=korg header.b=B5Bk2qgK; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233098AbiH2LqN (ORCPT + 99 others); Mon, 29 Aug 2022 07:46:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232993AbiH2Lpo (ORCPT ); Mon, 29 Aug 2022 07:45:44 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 086858605E; Mon, 29 Aug 2022 04:29:30 -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 73B206122E; Mon, 29 Aug 2022 11:19:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4C07CC433C1; Mon, 29 Aug 2022 11:19:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661771963; bh=PzW5OzWOz9p821aITep6E6NYhopT2BIaxkYgtQcogvU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B5Bk2qgKmPyq3MXrhPl0gwLdphnP3wjlEiSvdqfHcKy//lemQEtW0eYAwj8log51J IJQcHRisZfNwyHg0ea+dk1vu1y3Zd+b8KEO8fqqRMaSOfnuCqoUkxsABuO0dZz7WF2 FuS6QlDQIkVDk2pAqGi1FFSyijZJH+YMSk5y+f64= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Shakeel Butt , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , David Hildenbrand , Yosry Ahmed , Greg Thelen , Andrew Morton Subject: [PATCH 5.19 122/158] Revert "memcg: cleanup racy sum avoidance code" Date: Mon, 29 Aug 2022 12:59:32 +0200 Message-Id: <20220829105814.236082185@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829105808.828227973@linuxfoundation.org> References: <20220829105808.828227973@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Shakeel Butt commit dbb16df6443c59e8a1ef21c2272fcf387d600ddf upstream. This reverts commit 96e51ccf1af33e82f429a0d6baebba29c6448d0f. Recently we started running the kernel with rstat infrastructure on production traffic and begin to see negative memcg stats values. Particularly the 'sock' stat is the one which we observed having negative value. $ grep "sock " /mnt/memory/job/memory.stat sock 253952 total_sock 18446744073708724224 Re-run after couple of seconds $ grep "sock " /mnt/memory/job/memory.stat sock 253952 total_sock 53248 For now we are only seeing this issue on large machines (256 CPUs) and only with 'sock' stat. I think the networking stack increase the stat on one cpu and decrease it on another cpu much more often. So, this negative sock is due to rstat flusher flushing the stats on the CPU that has seen the decrement of sock but missed the CPU that has increments. A typical race condition. For easy stable backport, revert is the most simple solution. For long term solution, I am thinking of two directions. First is just reduce the race window by optimizing the rstat flusher. Second is if the reader sees a negative stat value, force flush and restart the stat collection. Basically retry but limited. Link: https://lkml.kernel.org/r/20220817172139.3141101-1-shakeelb@google.com Fixes: 96e51ccf1af33e8 ("memcg: cleanup racy sum avoidance code") Signed-off-by: Shakeel Butt Cc: "Michal Koutný" Cc: Johannes Weiner Cc: Michal Hocko Cc: Roman Gushchin Cc: Muchun Song Cc: David Hildenbrand Cc: Yosry Ahmed Cc: Greg Thelen Cc: [5.15] Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- include/linux/memcontrol.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -978,19 +978,30 @@ static inline void mod_memcg_page_state( static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) { - return READ_ONCE(memcg->vmstats.state[idx]); + long x = READ_ONCE(memcg->vmstats.state[idx]); +#ifdef CONFIG_SMP + if (x < 0) + x = 0; +#endif + return x; } static inline unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx) { struct mem_cgroup_per_node *pn; + long x; if (mem_cgroup_disabled()) return node_page_state(lruvec_pgdat(lruvec), idx); pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); - return READ_ONCE(pn->lruvec_stats.state[idx]); + x = READ_ONCE(pn->lruvec_stats.state[idx]); +#ifdef CONFIG_SMP + if (x < 0) + x = 0; +#endif + return x; } static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,