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 5FAECC74A5B for ; Thu, 23 Mar 2023 18:08:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C1366B0075; Thu, 23 Mar 2023 14:08:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 94B566B0078; Thu, 23 Mar 2023 14:08:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C4096B007B; Thu, 23 Mar 2023 14:08:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 666A96B0075 for ; Thu, 23 Mar 2023 14:08:27 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 26AAF1C56CE for ; Thu, 23 Mar 2023 18:08:27 +0000 (UTC) X-FDA: 80600947854.25.B9F8198 Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) by imf05.hostedemail.com (Postfix) with ESMTP id 247A8100010 for ; Thu, 23 Mar 2023 18:08:24 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=iSMmzvR4; spf=pass (imf05.hostedemail.com: domain of yosryahmed@google.com designates 209.85.208.41 as permitted sender) smtp.mailfrom=yosryahmed@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=1679594905; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GVEQ3jjBhRvvdROqbmTd0ErWpAgtFM7LpI2zkeVM6nc=; b=sYl3iHsuG/FzpZH0r1a1ZAQW2SQFcjE9xImi15AH0WoymZYQhb7GmyDzii0qeJj3gU5lcy gthAhXn0dUqWFtkk6l2PW7+nfixgqyQCyTfwk5nkG3jeLEkeIP+AnWrE/m9osvzYRrTeVC luWfWHDNDZXef9YKfWtIv9Fl6lEkpLw= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=iSMmzvR4; spf=pass (imf05.hostedemail.com: domain of yosryahmed@google.com designates 209.85.208.41 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679594905; a=rsa-sha256; cv=none; b=EaJWOJL1K0q7BZBHY7/zTg9sVgm8/yBEvaNrgJIVi+DJ+XX6JZruiq3QxiiYTxk2FVh+dH eiAjTePy+rOLPm0aTER3zunvuAM9FK4KcUAyXmXt3369qOP3HEYojUqPlOHhcKW9uvQ5hQ Oqu48y4dHFgPkhTKfnhhOe7V3H4zrFY= Received: by mail-ed1-f41.google.com with SMTP id x3so90422419edb.10 for ; Thu, 23 Mar 2023 11:08:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679594903; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=GVEQ3jjBhRvvdROqbmTd0ErWpAgtFM7LpI2zkeVM6nc=; b=iSMmzvR4nUOgFdPoIuMVdAVhXTbAdbYo7pLETlxOCbMwcNRovtBt/22dPXrSySYxbp zJCQTHhPo4PFrUR62vtMimmYSgiWTsqB9tSiqzOg0nSraK9/nvwtvh334JEH2rWJQ6FS 97I4Xoiz7o/cACMu+o9beDqaksw/PavmXjFklptwSOqiOI8p/ig5E0vc2DDXoFcGspgE dju0BzbOVvGZNQp11KXGu3UcBpKdWla8vD/e/4gAeirBuUxilCcora4A4l+Yn/h2OwD4 6M/kCw/eev/Mgj2sy4Ebimfd+9ZQNukVrkyECwua5LZ1YLU4Qdfjw8d4Qq5KqMIfy9SZ h7Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679594903; h=content-transfer-encoding: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=GVEQ3jjBhRvvdROqbmTd0ErWpAgtFM7LpI2zkeVM6nc=; b=mQizINVotIVnD4h4EqkBckyXF+kYesBu+1CosJfVyKAYDDPapbz0sNmv3MUPwAWhDw jRRVz8AO/PgXmxaOUXlKYDO7NC6cfyIktwsBy0RigoIL/3v83lRkFmnYuhgONgvvH42x MpQMNhlOxMUk+6o91tgjZ61ivOpgnfSzOrAiaZVHXvuLtP9QFi7oo2n66Kl8z1vUZBGt 4q+xz5ArZ4TgoP+tsnFQhWIPAO1XJ0WFhzrvRLcuxjogCLsEWhk8yq5of4I5E9LIRF9J HmyNasQZlH9uWb2HbaHeeuZ2A3atZ4WJ+qIJ43mb/CWYxgCDC1Sizng5iy/4PJbkrWC7 xjCQ== X-Gm-Message-State: AO0yUKXWkCyeP+M/HrN377EvKP9Zj0O2wBjK2qT9sE1olRnQRlsFR/M0 Fw3VlTPzXPMmiPWE/EshWwRdNQaYAfSH0pzl3iBMuQ== X-Google-Smtp-Source: AK7set9uzu0+uLVYPeW1/TKT07Y1C8fU/op63gdiSoakbq07Xp2dmMzhQ0EX2/+iZXC1dLM/RK6moL4HGFXShEgJUAY= X-Received: by 2002:a17:906:aac9:b0:927:912:6baf with SMTP id kt9-20020a170906aac900b0092709126bafmr5401952ejb.15.1679594903374; Thu, 23 Mar 2023 11:08:23 -0700 (PDT) MIME-Version: 1.0 References: <20230323040037.2389095-1-yosryahmed@google.com> <20230323040037.2389095-5-yosryahmed@google.com> <20230323155613.GC739026@cmpxchg.org> <20230323172732.GE739026@cmpxchg.org> In-Reply-To: <20230323172732.GE739026@cmpxchg.org> From: Yosry Ahmed Date: Thu, 23 Mar 2023 11:07:47 -0700 Message-ID: Subject: Re: [RFC PATCH 4/7] memcg: sleep during flushing stats in safe contexts To: Johannes Weiner Cc: Tejun Heo , Josef Bacik , Jens Axboe , Zefan Li , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Vasily Averin , cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 247A8100010 X-Rspam-User: X-Stat-Signature: hpkw7g8hokuam9n56ks9e4hozep6jius X-HE-Tag: 1679594904-888973 X-HE-Meta: U2FsdGVkX18YYgQT4fRmxh+JRvuEooxyq5SFDNv02ZXh1YH8G2k1W6yftOawCT2FNBEE9eeqGLDUm6z+XFcMqIfhnS+DEJ6NXF7etJBa5W8StBZgmrwOJFa7sXrLIop+kp/hkL976Qa69EdQ8/FKLNcMBOD93N84Rv5zu1isCVST+b0n/QhXoFN+vrqyT55oMwB9wXF3TR2xQdE4yjaAHu/jtzCAMeS5B4Oo5osjNa94nlB+M6NhbfnVs6rEzyreHHW5P+6IF/OoGv4hcJCnekyk1oDxB7svCZMbUb94I08yh7vuMdg41gFG/7sT26HwDw2/2Mm9gu+dcDcz8IVLqAst08PIiW3nMlhkUokVa2qhQ3GH5LUfoLeXyCW1ury9kEMBumcLgFp3EVy0EuMhFUQY+Eg3zAi7Zmscwg57MS8pDCwh5H3LLAH94irJAq6KfTLfMoFF3EO++MMVEnEb81MrJzgBdaeETzyI16YGzsBO2crSweUjhD+6iV4x6xal2AIGXVxEPJ3R13ewjhCOZCRVarBs1oUIVZmNlrGo0K7UPoKSPeGS1DFjr8Hx7Cp8SXVJd2UDNJULcT5hRzSrTXeZNQpJhauIdV/gE4+XlN56pTUlBxW0tvorqovSVaN0KK2hvzYBIKu28ZQd2Y9B/PCUhV+ndgi0UxIFjUbPKVCr3HGM0HrAvMQSi2MffIE/+MVyIRbW7MLeLWPCuss36XyiRqzxbovOo6czbIkniY7QLYC/MBgCxL8iMYekuFKwnCEjVefRcsLpsnbZMtKGiDC+Y5KrKmMT4xsJSrO0bT7cheJvi2FebXYz1a30CwMMBFL/osrtHiPpjC+3tEFB+yoW4jmUfezNgFK7oE5crg2BIwUG4XAo6H3Nz2jVQhGY899yjHxA/5vCVGiSNvpKcUAITv9jB5E0dh4XffSJh554jnlGyFQMXIdn7tnIjD4x9hIKROvEU6qJk6pJpni yDaD7vqN biqO6wBsCYyrDJNoyDYjivkQUBQGFh7Xm60RPXq/AoD6+TMuOa2WwCnNsDsDKlQ4EMoqEoL1JoYpQX3VWG5WHs4tGk7dGJu/DSy6/pVcXWqglZFdMsLuu6oo8iIkDM3OHwMSWhLD27GV/8rAcTWl1pujR/QJcriMNosBHw8n4zrTwd5Dv0YT3FS1hRs9Ddby0RStQH4WORJtcxomLcJTWsI3CFFDFHQnbizQE9YpaJD9LN988Ok0ZXCHXc4fTSAPFngLQ/vBirid3JCSMsShWl1UXsQ5K7ZlcZZqniZr9EOEZwdHXk2+H02HCRQ== 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 Thu, Mar 23, 2023 at 10:27=E2=80=AFAM Johannes Weiner wrote: > > On Thu, Mar 23, 2023 at 09:01:12AM -0700, Yosry Ahmed wrote: > > On Thu, Mar 23, 2023 at 8:56=E2=80=AFAM Johannes Weiner wrote: > > > > > > On Thu, Mar 23, 2023 at 04:00:34AM +0000, Yosry Ahmed wrote: > > > > @@ -644,26 +644,26 @@ static void __mem_cgroup_flush_stats(void) > > > > return; > > > > > > > > flush_next_time =3D jiffies_64 + 2*FLUSH_TIME; > > > > - cgroup_rstat_flush(root_mem_cgroup->css.cgroup, false); > > > > + cgroup_rstat_flush(root_mem_cgroup->css.cgroup, may_sleep); > > > > > > How is it safe to call this with may_sleep=3Dtrue when it's holding t= he > > > stats_flush_lock? > > > > stats_flush_lock is always called with trylock, it is only used today > > so that we can skip flushing if another cpu is already doing a flush > > (which is not 100% correct as they may have not finished flushing yet, > > but that's orthogonal here). So I think it should be safe to sleep as > > no one can be blocked waiting for this spinlock. > > I see. It still cannot sleep while the lock is held, though, because > preemption is disabled. Make sure you have all lock debugging on while > testing this. Thanks for pointing this out, will do. > > > Perhaps it would be better semantically to replace the spinlock with > > an atomic test and set, instead of having a lock that can only be used > > with trylock? > > It could be helpful to clarify what stats_flush_lock is protecting > first. Keep in mind that locks should protect data, not code paths. > > Right now it's doing multiple things: > > 1. It protects updates to stats_flush_threshold > 2. It protects updates to flush_next_time > 3. It serializes calls to cgroup_rstat_flush() based on those ratelimits > > However, > > 1. stats_flush_threshold is already an atomic > > 2. flush_next_time is not atomic. The writer is locked, but the reader > is lockless. If the reader races with a flush, you could see this: > > if (time_after(jiffies, flush_nex= t_time)) > spin_trylock() > flush_next_time =3D now + delay > flush() > spin_unlock() > spin_trylock() > flush_next_time =3D now + delay > flush() > spin_unlock() > > which means we already can get flushes at a higher frequency than > FLUSH_TIME during races. But it isn't really a problem. > > The reader could also see garbled partial updates, so it needs at > least READ_ONCE and WRITE_ONCE protection. > > 3. Serializing cgroup_rstat_flush() calls against the ratelimit > factors is currently broken because of the race in 2. But the race > is actually harmless, all we might get is the occasional earlier > flush. If there is no delta, the flush won't do much. And if there > is, the flush is justified. > > In summary, it seems to me the lock can be ditched altogether. All the > code needs is READ_ONCE/WRITE_ONCE around flush_next_time. Thanks a lot for this analysis. I agree that the lock can be removed with proper READ_ONCE/WRITE_ONCE, but I think there is another purpose of the lock that we are missing here. I think one other purpose of the lock is avoiding a thundering herd problem on cgroup_rstat_lock, particularly from reclaim context, as mentioned by the log of commit aa48e47e3906 ("memcg: infrastructure to flush memcg stats"). While testing, I did notice that removing this lock indeed causes a thundering herd problem if we have a lot of concurrent reclaimers. The trylock makes sure we abort immediately if someone else is flushing -- which is not ideal because that flusher might have just started, and we may end up reading stale data anyway. This is why I suggested replacing the lock by an atomic, and do something like this if we want to maintain the current behavior: static void __mem_cgroup_flush_stats(void) { ... if (atomic_xchg(&ongoing_flush, 1)) return; ... atomic_set(&ongoing_flush, 0) } Alternatively, if we want to change the behavior and wait for the concurrent flusher to finish flushing, we can maybe spin until ongoing_flush goes back to 0 and then return: static void __mem_cgroup_flush_stats(void) { ... if (atomic_xchg(&ongoing_flush, 1)) { /* wait until the ongoing flusher finishes to get updated stats */ while (atomic_read(&ongoing_flush) {}; return; } /* flush the stats ourselves */ ... atomic_set(&ongoing_flush, 0) } WDYT?