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 60B19EB64DD for ; Wed, 9 Aug 2023 13:17:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 021126B0072; Wed, 9 Aug 2023 09:17:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EEB498E0001; Wed, 9 Aug 2023 09:17:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB2EF6B0075; Wed, 9 Aug 2023 09:17:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CE2ED6B0072 for ; Wed, 9 Aug 2023 09:17:57 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 96A4A80E81 for ; Wed, 9 Aug 2023 13:17:57 +0000 (UTC) X-FDA: 81104618994.04.E6CC12E Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) by imf29.hostedemail.com (Postfix) with ESMTP id C73D712001F for ; Wed, 9 Aug 2023 13:17:55 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=j2xSiU48; spf=pass (imf29.hostedemail.com: domain of yosryahmed@google.com designates 209.85.208.52 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=1691587075; a=rsa-sha256; cv=none; b=y+dodKKHUu5KDoEUKg1McB8usijh9hZC0RPWWEvr/7EEJza/zCsBg5/Lb/vfLS98iwDVOg oJ1YCMwc6PUj9Co8i6BZq/k/jCr5MdHOfxpz+rNfr0sevZEdpqZVK+qhAefGrwuMYNACnj GrIo3o91b1tlIH019q8lOdAu+hBkZt4= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=j2xSiU48; spf=pass (imf29.hostedemail.com: domain of yosryahmed@google.com designates 209.85.208.52 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=1691587075; 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=r7GQah3LR29pNwUlAFsHmzB3/MgA9amRpVJYquX5jX0=; b=mc6mDtEDOByLMcxXnpQxAho80Sg+O4sgRoHkJ8TQCsARmFoc6fkyo3H5qYe/nIZ+dr6cS9 Wiu8ZH3wSiCgqSSDmLo4dPN2gn8wbJwCaD5vBSe/H5farkcM2k32wEBw7gIKC4zlqUchL9 iIW8vdxcym7J8QE1LI5UfMEB6Yz8WuU= Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-5231410ab27so7877184a12.0 for ; Wed, 09 Aug 2023 06:17:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691587074; x=1692191874; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=r7GQah3LR29pNwUlAFsHmzB3/MgA9amRpVJYquX5jX0=; b=j2xSiU48lAhLr6GSJ/DFJP5FmUuUSjaBQhgGtPkSEPls0F47OUNh8TIbrSr5UMunMZ uM2ZUT0AV6Yl9Qlwub03wbHBRZjf4OaV9YYJm/YEXekUy4vO6Sjeybnabl5D10LLVSjn swU57o/MSbPRGeE+ljLde+9aziPyf0+7ZdSzgtsDh6skzxrYO/yjzQ+Pj0y2dU1pdUdr 2y6VTAnLM4T3A1v5je6mTuQx7nD+wKUg0/0kJJk3EZnC32WVXkdj0Q0Sk5PC/EHZ4xgU 8lISujnCVovjWP5Jh+CduFXCDAJVx2O+ylOXE2UnEchucJ2qS+wwVmDkpuWz7YVTBaqW /Pxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691587074; x=1692191874; 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=r7GQah3LR29pNwUlAFsHmzB3/MgA9amRpVJYquX5jX0=; b=jupQT4QI5L/ER629JUaeNt5mpUoBrjr196oRR0SZ/MZ5lR4r1WV182majCnuF3GzMc lNUQ77ENLC+bXSvEw8NwkmqXagbzxTVRtU4xGGPvHs4zohusalNdXNmxUHtnehZbhf29 lOSHDqk7SW2WPEb1cKW5R7mXZBDmaLlnHxr/h8hcEkYCdfgztreiV/6FYlYALkqtyl2/ 9xkRRbeWe0rqCZBUZ/9mCwBQFMGlUiltNTB6P70Qf/XFrImRseb8uxLO832jVHDl7ZTS fN5WmGN8XXxAaba1uIqLq5xTUqSUNc/nJxF2q6JDWEVY3VsFjp0b5c+/epbEibPyBpmH lyyw== X-Gm-Message-State: AOJu0Yy+oAN5tX/ONUE1W5PnM2m7f+IFXfb5JHCJP5haYg2/QDZdXuQx lE3uEaTdZ6W+alFq1aR3siJVUDkxczTar9elgLg2QA== X-Google-Smtp-Source: AGHT+IGIzc/TMK7NNoqOzdlb2frVhrYUSFPRNGS6tLA/DRsJVToruJ1YAinbznO7/6EkrahVfqFaeMRaWmgFTbKkw9M= X-Received: by 2002:a17:906:220e:b0:99b:f3d1:7735 with SMTP id s14-20020a170906220e00b0099bf3d17735mr2206402ejs.29.1691587074277; Wed, 09 Aug 2023 06:17:54 -0700 (PDT) MIME-Version: 1.0 References: <20230809045810.1659356-1-yosryahmed@google.com> In-Reply-To: From: Yosry Ahmed Date: Wed, 9 Aug 2023 06:17:18 -0700 Message-ID: Subject: Re: [PATCH] mm: memcg: provide accurate stats for userspace reads To: Michal Hocko Cc: Johannes Weiner , Roman Gushchin , Shakeel Butt , Andrew Morton , Muchun Song , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: C73D712001F X-Stat-Signature: 38k36fjnofza3ffaqmd8dmmqgn1tqtu7 X-Rspam-User: X-HE-Tag: 1691587075-164230 X-HE-Meta: U2FsdGVkX19+yrA/o7wwQaxu3rojRGGtleSqfviwcZn54nSVz6DUU2lqVBnUjC0iq4Rm5h/EG4Zr3Xq6Urr0zwlHGmDT6m16jODLLsA6+dzqZMfLv1uLoKqoc9m5D0N1GWH1AIiDU8QbpMJ+LI4LY3d2ATQzzsu234hp6wjGNgNjH5RT3zzhi+54KDa73Mxj7obLQXd2a+yW2VazCxc+SjvtnTBb/ck7gpzEQdbhd0qYx9NwGX+EwoyKAhDCxQSrBbtas+cZRA+FQvlcIwjBrY7IqW9ek+dYI/Z6mTukvtkrmVAU+d+0UgnXcjsa/T6/tXEOVJZodN37m13Saz3g+eCwfQNzOVrNBEiDx25qCTEclrjKZSG4zs/j8a2RUaRj1X5V7q0dRDC/Q4yHTK/UvEjv9pG0Nq9g8jG/Vzxf2cHuDJb7uyVJdgD0L9NHPbV8Hn1CVNo5GSnFUfrXPEGhqIt+PD56BGgbr1tG4znnJ7+7Hmvx/0taOW9hRokaSq+tzjjy+8XBbMhlFJ0GQjl6juyfFhX21SHhidJz2peVojX+AatQkApUxozUUCAZcLcITLeiiFE8XsrJvFkVe0p694x+Id2PKphIDwZ7XDUbaU8eiE6I2lBTYL/jdQW+JOTmT6zKeaY2gJtFY4fOvt48V3Nsb0YeCcGmFv/i0wPFEUsLUxX4ThYxL6DPLRCMjRgH8pyJGFoCh6CxMGtYVg3qScjxmz6N4IrmgzYFD9tE7I6i3aF9E76NAQjA/KG5yCNGw9MFvYnwmLIZRho9M04c1l+N7GMgYIz1dzwYekAqxgFyWOe7n/YdwIomGWLi4MPLcwAJgQFehrqAiTqzQf1Ty/xJUaUfTocdTolLs8EnM+IEW8u2Od0QXN9hYFxYfiS9FUsPPRphASoKRSBcS0g6Iqvgk3JKJQ7lpwIpBtK3R7rh+5tiZODfNAnIpV9kMxKJm0VOlvmSn0gOy91yuSX kCVjbpkv T98QDs6/H8xV7azSOHCTyw/bEPlonGhtpQNIHuZv9YmTTsJ5MqqRzAFpiCxDwwyDd/83JujaovbF8BcNt/AWdFRj+EjOVep6gili9A5Xsj1yS/Aeyyymo6DvbLr2dfcfzcLaSWqnq+LaKY65e4pmh+p8KBkL00AKkwHCkzvJwp/61/yFr42iSmBUx3eZGCWczxzWqeOKHR9GL8A5/MFdf59r3wYrgILgWH4F5 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: > > > [...] > > > > @@ -639,17 +639,24 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) > > > > } > > > > } > > > > > > > > -static void do_flush_stats(void) > > > > +static void do_flush_stats(bool full) > > > > { > > > > + if (!atomic_read(&stats_flush_ongoing) && > > > > + !atomic_xchg(&stats_flush_ongoing, 1)) > > > > + goto flush; > > > > + > > > > /* > > > > - * We always flush the entire tree, so concurrent flushers can just > > > > - * skip. This avoids a thundering herd problem on the rstat global lock > > > > - * from memcg flushers (e.g. reclaim, refault, etc). > > > > + * We always flush the entire tree, so concurrent flushers can choose to > > > > + * skip if accuracy is not critical. Otherwise, wait for the ongoing > > > > + * flush to complete. This avoids a thundering herd problem on the rstat > > > > + * global lock from memcg flushers (e.g. reclaim, refault, etc). > > > > */ > > > > - if (atomic_read(&stats_flush_ongoing) || > > > > - atomic_xchg(&stats_flush_ongoing, 1)) > > > > - return; > > > > - > > > > + while (full && atomic_read(&stats_flush_ongoing) == 1) { > > > > + if (!cond_resched()) > > > > + cpu_relax(); > > > > > > You are reinveting a mutex with spinning waiter. Why don't you simply > > > make stats_flush_ongoing a real mutex and make use try_lock for !full > > > flush and normal lock otherwise? > > > > So that was actually a spinlock at one point, when we used to skip if > > try_lock failed. > > AFAICS cgroup_rstat_flush is allowed to sleep so spinlocks are not > really possible. Sorry I hit the send button too early, didn't get to this part. We were able to use a spinlock because we used to disable sleeping when flushing the stats then, which opened another can of worms :) > > > We opted for an atomic because the lock was only used > > in a try_lock fashion. The problem here is that the atomic is used to > > ensure that only one thread actually attempts to flush at a time (and > > others skip/wait), to avoid a thundering herd problem on > > cgroup_rstat_lock. > > > > Here, what I am trying to do is essentially equivalent to "wait until > > the lock is available but don't grab it". If we make > > stats_flush_ongoing a mutex, I am afraid the thundering herd problem > > will be reintroduced for stats_flush_ongoing this time. > > You will have potentially many spinners for something that might take > quite a lot of time (sleep) if there is nothing else to schedule. I do > not think this is a proper behavior. Really, you shouldn't be busy > waiting for a sleeper. > > > I am not sure if there's a cleaner way of doing this, but I am > > certainly open for suggestions. I also don't like how the spinning > > loop looks as of now. > > mutex_try_lock for non-critical flushers and mutex_lock of syncing ones. > We can talk a custom locking scheme if that proves insufficient or > problematic. I have no problem with this. I can send a v2 following this scheme, once we agree on the importance of this patch :) > -- > Michal Hocko > SUSE Labs