Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp9132rdb; Wed, 1 Nov 2023 15:04:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF8J69cMlsDn5Ie8sFgfbxkx5lluc6Qsh+E5wGOnfB7wtDGjEPqhpKSCN3mFzXwEiwAuP4e X-Received: by 2002:a05:6a20:2590:b0:16b:977d:f7cf with SMTP id k16-20020a056a20259000b0016b977df7cfmr20853936pzd.36.1698876297769; Wed, 01 Nov 2023 15:04:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698876297; cv=none; d=google.com; s=arc-20160816; b=MTHTrOZjPwqbR/icLtd6Jg3AKkLw9KjsIJ/+kdz9ZeWv2USnT3CcFO8TYuOUsIW292 67oHUxoypBVcMtadQHYNDuj5KejKteuT85ENuSx2K4abU9HSlPk+eImUnDu143CeJTsd f4hD84vNOezqtfQY/KcfKN9s2qKFvg4D+PYcrKbNPZRvfXDjS0LQV7JrdDHYu28U8s5M 37HmB6oZqB/q00g4lHiqEJBBU0FN/AP+Ku0nMdTXyyQf156cG5RIv38GKlGJrSKwZobz vCIw2b+V8Jvx86e10g2RmyHt8IjFodRUhbUQ58zRX/A/Qeqb0yYRf5eqYxurzjGJq5Ls Ou5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=aqSv+dngOizenCCZDEe3c0pCWMK4IpYs575U6VfGGQA=; fh=yo9k1oLGaSYccmaLgBzNsHY7VKt+226CJNwoSDLImVM=; b=YzDy9d6H7Fnc873UevZUn05bMQA77VPv5Cco6/POAgmGk3c91lwVS71B5MSFpdvi4I XFsB+08wwsPuFyzPfeYMJMICXkxGkgmZn3nKetUZI95EvGpkndTl6uq9axTsKFCEsKfp Q3uuMBFuqCaqXLzy8prgsnzEhx1M+TaEirTj9eSJVwTMTRVotiYTkPrIj/L4oiKnxZe0 bxxatapU5m4FMwWvvFGNcvmxQ19g+Cm/xJtB0S0rnZXT9qyyRNVU5uk/sCFiEDE3+AZk tKAurz8t6RX6n9t0m5v4Q7fICV3IoXb+pRleq0+5A1TzFO2p2zZOHRMbAfQDWHrHg+Xi kjvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PUxofYgd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id p13-20020a056a000b4d00b006b1fb740866si2551500pfo.189.2023.11.01.15.04.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 15:04:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PUxofYgd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 99C7C819DFFD; Wed, 1 Nov 2023 15:04:50 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233861AbjKAWEj (ORCPT + 99 others); Wed, 1 Nov 2023 18:04:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232007AbjKAWEi (ORCPT ); Wed, 1 Nov 2023 18:04:38 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F50A110 for ; Wed, 1 Nov 2023 15:03:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698876231; h=from:from: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; bh=aqSv+dngOizenCCZDEe3c0pCWMK4IpYs575U6VfGGQA=; b=PUxofYgdCEtvRJwmy7vjQeUNP4HfKOA9ynaz2i1CFeqxcSyZ2YJfAhhbM45np0FJbFMI71 M65NFDfsoU0gstiAJRO5DElC96ILl8Kl9mwc+DL7T4sKJRPiBVD5VXddZtHw+M98JrCB8z k/w6F+bqCATqLKjoO/47Qg6gECGRjNY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-581-zXPTfJAmM-K_1ExMANLdkw-1; Wed, 01 Nov 2023 18:03:47 -0400 X-MC-Unique: zXPTfJAmM-K_1ExMANLdkw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4939A811E7E; Wed, 1 Nov 2023 22:03:47 +0000 (UTC) Received: from [10.22.33.245] (unknown [10.22.33.245]) by smtp.corp.redhat.com (Postfix) with ESMTP id 87A6A492BFA; Wed, 1 Nov 2023 22:03:46 +0000 (UTC) Message-ID: <5df1fa23-1a17-d2e8-7a3a-0a44478bc1de@redhat.com> Date: Wed, 1 Nov 2023 18:03:46 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: [PATCH] cgroup/rstat: Reduce cpu_lock hold time in cgroup_rstat_flush_locked() Content-Language: en-US To: Yosry Ahmed Cc: Tejun Heo , Zefan Li , Johannes Weiner , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Joe Mario , Sebastian Jug References: <20231101160911.394526-1-longman@redhat.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Spam-Status: No, score=-6.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_NONE,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 01 Nov 2023 15:04:50 -0700 (PDT) On 11/1/23 15:11, Yosry Ahmed wrote: > On Wed, Nov 1, 2023 at 9:09 AM Waiman Long wrote: >> When cgroup_rstat_updated() isn't being called concurrently with >> cgroup_rstat_flush_locked(), its run time is pretty short. When >> both are called concurrently, the cgroup_rstat_updated() run time >> can spike to a pretty high value due to high cpu_lock hold time in >> cgroup_rstat_flush_locked(). This can be problematic if the task calling >> cgroup_rstat_updated() is a realtime task running on an isolated CPU >> with a strict latency requirement. The cgroup_rstat_updated() call can >> happens when there is a page fault even though the task is running in >> user space most of the time. >> >> The percpu cpu_lock is used to protect the update tree - >> updated_next and updated_children. This protection is only needed >> when cgroup_rstat_cpu_pop_updated() is being called. The subsequent >> flushing operation which can take a much longer time does not need >> that protection. >> >> To reduce the cpu_lock hold time, we need to perform all the >> cgroup_rstat_cpu_pop_updated() calls up front with the lock >> released afterward before doing any flushing. This patch adds a new >> cgroup_rstat_flush_list() function to do just that and return a singly >> linked list of cgroup_rstat_cpu structures to be flushed. >> >> By adding some instrumentation code to measure the maximum elapsed times >> of the new cgroup_rstat_flush_list() function and each cpu iteration >> of cgroup_rstat_flush_locked() around the old cpu_lock lock/unlock pair >> on a 2-socket x86-64 server running parallel kernel build, the maximum >> elapsed times are 31us and 118us respectively. The maximum cpu_lock >> hold time is now reduced to about 1/4 of the original. > This sounds promising. It's smart to empty the whole tree while > holding the lock, then do the flush only under cgroup_rstat_lock. > Thanks for doing this. > >> Signed-off-by: Waiman Long >> --- >> include/linux/cgroup-defs.h | 7 +++++ >> kernel/cgroup/rstat.c | 57 +++++++++++++++++++++++++++---------- >> 2 files changed, 49 insertions(+), 15 deletions(-) >> >> diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h >> index 265da00a1a8b..22adb94ebb74 100644 >> --- a/include/linux/cgroup-defs.h >> +++ b/include/linux/cgroup-defs.h >> @@ -368,6 +368,13 @@ struct cgroup_rstat_cpu { >> */ >> struct cgroup *updated_children; /* terminated by self cgroup */ >> struct cgroup *updated_next; /* NULL iff not on the list */ >> + >> + /* >> + * A singly-linked list of cgroup_rstat_cpu structures to be flushed. >> + * Protected by cgroup_rstat_lock. >> + */ >> + struct cgroup_rstat_cpu *flush_next; >> + struct cgroup *cgroup; /* Cgroup back pointer */ > Why are we linking struct cgroup_rstat_cpu instead of directly linking > struct cgroup? AFAICT we only ever use the cgroup back pointer during > flushing anyway, right? You are right. > Given that only one cpu can be flushed at a time, I think it should be > okay to run the list directly through struct cgroup, and have all cpus > reuse it. That pointer would essentially be scratch space for the > flushing code to use. This should also save a bit of memory: > O(cgroups) vs O(cgroups * cpus). It's not a lot either way though. > > I think this may also simplify the code a bit. Moving the flush_next pointer to struct cgroup does save a bit of memory. Thanks for the suggestion. I will do that in the next version. > >> }; >> >> struct cgroup_freezer_state { >> diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c >> index d80d7a608141..93ef2795a68d 100644 >> --- a/kernel/cgroup/rstat.c >> +++ b/kernel/cgroup/rstat.c >> @@ -145,6 +145,42 @@ static struct cgroup *cgroup_rstat_cpu_pop_updated(struct cgroup *pos, >> return pos; >> } >> >> +/* >> + * Return a list of cgroup_rstat_cpu structures to be flushed >> + */ >> +static struct cgroup_rstat_cpu *cgroup_rstat_flush_list(struct cgroup *root, > nit: the name sounds like the function will flush a list, rather than > return a list of cgroups to be flushed. What about > cgroup_rstat_updated_list? I am not good at naming. cgroup_rstat_updated_list looks good to me. Cheers, Longman