Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp7618204rwl; Thu, 23 Mar 2023 06:46:12 -0700 (PDT) X-Google-Smtp-Source: AK7set+Pqjh2XnIs9Y4GMmWcWjuGw+F+fmFodicXW/X0bdlENozaljxtb0fjJdXwQfVCNgAiZcOb X-Received: by 2002:a17:906:e24d:b0:932:a2ce:aac0 with SMTP id gq13-20020a170906e24d00b00932a2ceaac0mr10834138ejb.62.1679579171967; Thu, 23 Mar 2023 06:46:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679579171; cv=none; d=google.com; s=arc-20160816; b=OAyjKx06jR5NDoDpUvO0MWH10pNNxf1D+2CdMbiC3VbZnfVYTk9OuZwT3yEkeEZZl7 XTAhX/c8LFWIwMapAiGHV+er7IFHBfZH8kCBWrOnT7R/LX5qsvK2vaCTf/YH16BtWuFc qTR+RKm2Vu29Pu2j5hX6wGXpk5MmS5RxIViYMbPZ82Rhc8W6X9NAGkmq6k2z+/c2msY0 nVnBSrnNjFIpIzmdNruCbjxMsQ/aC9INWdUuuTdrnGG6RICWSQaPuJ+YkOwebp3Gq4z9 0TybvAHyt2bEAM1xLlrscg5bVx2vdAF1QBq2aZBm60EicWrXSl627OMiUaez87wdbJjq y9iA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=EuE0cJjbmK+/BhJwvJMFWgBmHM9057+RC8AkvA4HkKU=; b=N7yYskylLW+dcaDbayuXnms4fLCaTtxqCVW18eZJjk0PY/9Nka3+/D1vB73fdTxple WFTSUh1uOT5qaoyevbAFW9cIm0QW7QpI0vNs3IH5l2pXQZAZMFfO4I7r8n8QDR4FxDo5 gRkTY2QFW+zWJHcI8ry62vusqwueYg9csOGDAzthk9PxLxIbxNqyBUNYQ2VK5eYlQIm1 iEM+CSPw/0b9/sir9e/AtVuETypMswEySuviCINopQc0m8oZznC2HonAscpqk1t4YhDP MrKIimVwniMQvJ0NFBzrF1yjWqZ3AlKL9zLkBxByz1lavZeo2uUs3MjeN3MF+lqRiq54 wblg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=DcDiKscP; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pk28-20020a170906d7bc00b0092ff1b82d7esi19098085ejb.550.2023.03.23.06.45.43; Thu, 23 Mar 2023 06:46:11 -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=@google.com header.s=20210112 header.b=DcDiKscP; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231682AbjCWNg2 (ORCPT + 99 others); Thu, 23 Mar 2023 09:36:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231571AbjCWNg0 (ORCPT ); Thu, 23 Mar 2023 09:36:26 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 373C92917C for ; Thu, 23 Mar 2023 06:36:22 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id ek18so86680064edb.6 for ; Thu, 23 Mar 2023 06:36:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679578580; 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=EuE0cJjbmK+/BhJwvJMFWgBmHM9057+RC8AkvA4HkKU=; b=DcDiKscPNUA4W1/GbdqN5BhxUIYoz9Wmqq3Xmd1C+DUMMpURN79kep/XyUMFs7O6WW MdqSEoZEpJh6K1uDYGXqCYwtqPCRXnq1QLfTpNV4Eo1IgdgOLTpM3R2PdOsbI+hV2kfK QVD9wZK3LA17mTvY76ITqoPHiau96K56j+TNrZ6a3QUQ5L79xAReQuBw7n00P8VHIWWm neNib3URA3K2d5FtCKba9hlgjdkmZkjza+ySCPA7jYZFPPJYNEHu2p+u2TV4VmmjgEJS llW6op2xvHlxZkcr/ECLM2jHofYIAEetci4tarDdc6n3marMfQcOD5Xx5wNyNvIJNjGD lz0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679578580; 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=EuE0cJjbmK+/BhJwvJMFWgBmHM9057+RC8AkvA4HkKU=; b=7BH3la0WugTYxiDT6fb7BXqREfqKm2OokDRQUU90SjWJ6D9rGplixbaaycivBePNhx PaYFzU8fQR8vJx0FwZCnCNLeMB0JFLCHDqaZPVz7p8ngSxX1h3K8+jYbneGXXcBZjl6V zA/YASqThygCFpjbuiL4v807vGERrCnQIqUZm9MGYi4L4g5eARiVlZLsuDWtzZvwopLA 9jI35oeYE3Z0wsmpZz4/Q81YOQWJ46g6ZZeZilmvHtQ8duAnptGmNmh72Bs1Lih/KJXw g4g3RPvHPDH3lf+qXuqpw4j73Pr47RTG5EXf1NJjSg2sR6HQ7VkoD95xtvqeZvtVuDOP zAmA== X-Gm-Message-State: AO0yUKWCTO8svvLZ35u4Xq6IgjbDPQxNvyByzMl7w5AFAOsKaRiqGUfm btqHi03tkQ9r1uRuyQEKHLHUdZZgq2pH9wa0OQW1RQ== X-Received: by 2002:a50:d4c5:0:b0:500:5463:35de with SMTP id e5-20020a50d4c5000000b00500546335demr5382302edj.8.1679578580439; Thu, 23 Mar 2023 06:36:20 -0700 (PDT) MIME-Version: 1.0 References: <20230323040037.2389095-1-yosryahmed@google.com> <20230323040037.2389095-2-yosryahmed@google.com> In-Reply-To: From: Yosry Ahmed Date: Thu, 23 Mar 2023 06:35:44 -0700 Message-ID: Subject: Re: [RFC PATCH 1/7] cgroup: rstat: only disable interrupts for the percpu lock To: Shakeel Butt Cc: Tejun Heo , Josef Bacik , Jens Axboe , Zefan Li , Johannes Weiner , Michal Hocko , Roman Gushchin , 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-Spam-Status: No, score=-15.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,ENV_AND_HDR_SPF_MATCH, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=unavailable 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 Wed, Mar 22, 2023 at 11:33=E2=80=AFPM Shakeel Butt = wrote: > > On Wed, Mar 22, 2023 at 10:15=E2=80=AFPM Yosry Ahmed wrote: > > > [...] > > > Couple of questions: > > > > > > 1. What exactly is cgroup_rstat_lock protecting? Can we just remove i= t > > > altogether? > > > > I believe it protects the global state variables that we flush into. > > For example, for memcg, it protects mem_cgroup->vmstats. > > > > I tried removing the lock and allowing concurrent flushing on > > different cpus, by changing mem_cgroup->vmstats to use atomics > > instead, but that turned out to be a little expensive. Also, > > cgroup_rstat_lock is already contended by different flushers > > (mitigated by stats_flush_lock on the memcg side). If we remove it, > > concurrent flushers contend on every single percpu lock instead, which > > also seems to be expensive. > > We should add a comment on what it is protecting. I think block rstat > are fine but memcg and bpf would need this. I think it also protects the cpu base stats flushed by cgroup_base_stat_flu= sh(). I will add a comment in the next version. > > > > > > 2. Are we really calling rstat flush in irq context? > > > > I think it is possible through the charge/uncharge path: > > memcg_check_events()->mem_cgroup_threshold()->mem_cgroup_usage(). I > > added the protection against flushing in an interrupt context for > > future callers as well, as it may cause a deadlock if we don't disable > > interrupts when acquiring cgroup_rstat_lock. > > > > > 3. The mem_cgroup_flush_stats() call in mem_cgroup_usage() is only > > > done for root memcg. Why is mem_cgroup_threshold() interested in root > > > memcg usage? Why not ignore root memcg in mem_cgroup_threshold() ? > > > > I am not sure, but the code looks like event notifications may be set > > up on root memcg, which is why we need to check thresholds. > > This is something we should deprecate as root memcg's usage is ill define= d. Right, but I think this would be orthogonal to this patch series. > > > > > Even if mem_cgroup_threshold() does not flush memcg stats, the purpose > > of this patch is to make sure the rstat flushing code itself is not > > disabling interrupts; which it currently does for any unsleepable > > context, even if it is interruptible. > > Basically I am saying we should aim for VM_BUG_ON(!in_task()) in the > flush function rather than adding should_skip_flush() which does not > stop potential new irq flushers. I wanted to start with VM_BUG_ON(!in_task()) but I wasn't sure that all contexts that call rstat flushing are not in irq contexts. I added should_skip_flush() so that if there are existing flushers in irq context, or new flushers are added, we are protected against a deadlock. We can change should_skip_flush() to have a WARN_ON_ONCE(!in_task()) to alert in this case. If you prefer removing should_skip_flush() and just adding VM_BUG_ON(!in_task()) we can do that, but personally I was not confident enough that we have no code paths today that may attempt flushing from irq context.