From patchwork Thu Jun 27 21:18:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesper Dangaard Brouer X-Patchwork-Id: 13715054 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 2630BC2BD09 for ; Thu, 27 Jun 2024 21:18:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 93EF66B0096; Thu, 27 Jun 2024 17:18:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8EECD6B0098; Thu, 27 Jun 2024 17:18:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B66D6B0099; Thu, 27 Jun 2024 17:18:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5E3E16B0096 for ; Thu, 27 Jun 2024 17:18:45 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D333AA1509 for ; Thu, 27 Jun 2024 21:18:44 +0000 (UTC) X-FDA: 82277932968.20.B0DFB5B Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id 451E318001A for ; Thu, 27 Jun 2024 21:18:43 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=pkyRYQwe; spf=pass (imf16.hostedemail.com: domain of hawk@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=hawk@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719523115; 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: references:dkim-signature; bh=zz3Zc6teVxuyhjcUKZVJM4+/QQ/o/g+ZirA+qV+GwsI=; b=gmTHBee+ZH9fyXGBOkqUCzgJtUMhdYEdIiL2J99fUqNavzsES4jUxLtfvyVo2pnEeu1TKM k/FA7s4va36pO/G8QpHIRdedBjErIpsc6qsQ/XmIJ96CFhBo7Qnq1mAUMrwMY1xODD6Eaa Xxbz1Z7pL0JSJK0MJkFKDPsu5ITGllc= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=pkyRYQwe; spf=pass (imf16.hostedemail.com: domain of hawk@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=hawk@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719523115; a=rsa-sha256; cv=none; b=B7NamJnTbp9uBQDXoYZWp16xyULnf4CLawHuquM4LITXPGllKpljXZKd8oIrMmjOPR2MgF H67v61Pux1btZvM0bnj+LzEw+4OmEtEPH4eQIvAHynjjFZiGFqQfCV9MXmZ9quA+T6upqG o5qyWU0lTcF1qMaXPHZfEiqrROw6agw= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 364F761EE7; Thu, 27 Jun 2024 21:18:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C82EC2BBFC; Thu, 27 Jun 2024 21:18:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719523121; bh=REm3K5MgPmRQpONdIW7zODAdzetyL88KjbhiskxYUbE=; h=Subject:From:To:Cc:Date:From; b=pkyRYQweScIoKsig8Cx8MbjTacXVSRPGcIEGoeD3unqvHPRg/t60L1fhyf9i23RUa 728VwKM2V4BVQaeT4grDh9MuO+z/bkex68PhC7wocdyfeUIitbBr/i0trodAKM7XBb i021/EsBdOGXLGlO6elkx8EbmOg3951IHGWPSEMplKmfAEdqSLt/YRhfoEf5jdFIim NcmviQ7QCsRyLW78MKkfGZoXPRZrAgUFa2mOLR5lVeQFspxY+g7kLDw5W9O7cPDyNr 1ly2VzW5LS8eFEMjlgKUV4N7wmwPAouFuXEZIz7riBNAaHzkbinkRFhXw8MQU347x2 f/OegnvAdIVLg== Subject: [PATCH V4 1/2] cgroup/rstat: Helper functions for locking expose trylock From: Jesper Dangaard Brouer To: tj@kernel.org, cgroups@vger.kernel.org, yosryahmed@google.com, shakeel.butt@linux.dev Cc: Jesper Dangaard Brouer , hannes@cmpxchg.org, lizefan.x@bytedance.com, longman@redhat.com, kernel-team@cloudflare.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Date: Thu, 27 Jun 2024 23:18:37 +0200 Message-ID: <171952310959.1810550.17003659816794335660.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 451E318001A X-Stat-Signature: in9n5h5payhc3fefg7wks9xh4rrq7rs7 X-HE-Tag: 1719523123-397096 X-HE-Meta: U2FsdGVkX19BcVmPTXGNWoy4B3vXb9QwAIIl1Lz96auR0J5OM8o3cUjo4ZGCiXYCv7btooyuD1Avd+tqUEW0JiirNNYk+QLEgyk0MM+z+ONxV1WysT3xfJaAEJ0BHZzSL1MLU+kI7rJ6HdD+09KIMZ5K6qKCIyi2c3TPP6B2ZRjAGRnYFX/rpyXTro4S2FeLj7NTeAN78ZTqVcyP35VMcfEXzgr+Epi7eXtL0cX4wHCu+m8H3t1+A690HhZvFd22fdewHV3BVuCMuOdbqbJM3yBlrEbL0y3zs/lXfQc/wCGTgrvx234wI854QHucs6v9cwlxcC2p983OOYapIkFbKPBLHkWoKcpFMTFK9iwX0MmgCowYq5MSAndc2S/VYONdmf3nlVLN+RYOpJ780VEzc2e8wbRwdSr/LiehfNWuBCz1enL9jWcKYTBSZz4Wgck+FG/LsIDll7ZAiIqEJUsEZUBphP0Uek9bHJlbVkxqp0ssM490IXifeTURUEyHCDIKeSbNZHYpa3PTqi6KEcaVkBK/kxAXVD1lE1EATj76gToTKNbe0nzmo9OxeIOrMKy2DDpz4FSsxRUX4vA2eR5KPsK2LO8Q7GPhEj7cuBgNRMYskyU8iJruXv/08qAAO93pZXyO5W3uehj+XuQ0JF4MMfja0JZn9zj+P7BrQlsV/w3GVwF7zHRkZLp3arkeNjGTi8Q3fJzq0mXEPAKSZ2GJTeRaqNGa4xvtCKSGoxp9tfsJlvG/hKmuLjVbmtANTjSP0e2vyCe9XfilO+/JIm9wJydDnSDut1R7Smm6n6juERAVgT9A01No/KxCW0tj/LPA1cnIq+dgnFRzDa59iP2cnJEFGB33Y+mz9J7ebwxvbJ0YwRuKrDGRydEvhb0TlLmllhpAr4RCHJYlTA6uEk750wILpWc1O5ZO/CnJlwjn426II8G3N5Yu3La+D2Sagyy5eJvn/iBYqGOihAxY/L6 2u6bXtlw 36hqJyk+dZmNV9CqVogoGtHlWRGQyCxk3Gt90TRv0kE93rjYNp8rQyS8b2wgunf7uQNIBps0vYw+6v+8JVsDU2XtUO1l5ShD3CipqX2VTlVJPMZ1GwfOFiPrTidP907fcK/rGyfoAdGgb8X3CMlrrwWs9XFqBrGwJ1aZa8JnCATfMyGig4y2nMYiFYo+cmOwjD0fp3sY86ffZDC2PUziqN6yskUYy5kLrtmnqmKwsMPQsb5MGWsYJlRRkBQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000610, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Signed-off-by: Jesper Dangaard Brouer --- kernel/cgroup/rstat.c | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index fb8b49437573..2a42be3a9bb3 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -279,17 +279,30 @@ __bpf_hook_end(); * value -1 is used when obtaining the main lock else this is the CPU * number processed last. */ -static inline void __cgroup_rstat_lock(struct cgroup *cgrp, int cpu_in_loop) +static inline bool __cgroup_rstat_trylock(struct cgroup *cgrp, int cpu_in_loop) +{ + bool locked; + + locked = spin_trylock_irq(&cgroup_rstat_lock); + if (!locked) + trace_cgroup_rstat_lock_contended(cgrp, cpu_in_loop, !locked); + + return locked; +} + +static inline void __cgroup_rstat_lock(struct cgroup *cgrp, int cpu_in_loop, + bool check_contention) __acquires(&cgroup_rstat_lock) { - bool contended; + bool locked = false; - contended = !spin_trylock_irq(&cgroup_rstat_lock); - if (contended) { - trace_cgroup_rstat_lock_contended(cgrp, cpu_in_loop, contended); + if (check_contention) + locked = __cgroup_rstat_trylock(cgrp, cpu_in_loop); + + if (!locked) spin_lock_irq(&cgroup_rstat_lock); - } - trace_cgroup_rstat_locked(cgrp, cpu_in_loop, contended); + + trace_cgroup_rstat_locked(cgrp, cpu_in_loop, !locked); } static inline void __cgroup_rstat_unlock(struct cgroup *cgrp, int cpu_in_loop) @@ -328,7 +341,7 @@ static void cgroup_rstat_flush_locked(struct cgroup *cgrp) __cgroup_rstat_unlock(cgrp, cpu); if (!cond_resched()) cpu_relax(); - __cgroup_rstat_lock(cgrp, cpu); + __cgroup_rstat_lock(cgrp, cpu, true); } } } @@ -348,9 +361,16 @@ static void cgroup_rstat_flush_locked(struct cgroup *cgrp) */ __bpf_kfunc void cgroup_rstat_flush(struct cgroup *cgrp) { + bool locked; + might_sleep(); - __cgroup_rstat_lock(cgrp, -1); + locked = __cgroup_rstat_trylock(cgrp, -1); + if (!locked) { + /* Opportunity to ongoing flush detection */ + __cgroup_rstat_lock(cgrp, -1, false); + } + cgroup_rstat_flush_locked(cgrp); __cgroup_rstat_unlock(cgrp, -1); } @@ -368,7 +388,7 @@ void cgroup_rstat_flush_hold(struct cgroup *cgrp) __acquires(&cgroup_rstat_lock) { might_sleep(); - __cgroup_rstat_lock(cgrp, -1); + __cgroup_rstat_lock(cgrp, -1, true); cgroup_rstat_flush_locked(cgrp); } From patchwork Thu Jun 27 21:18:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesper Dangaard Brouer X-Patchwork-Id: 13715055 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 EEADBC2BD09 for ; Thu, 27 Jun 2024 21:19:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 807DB6B0099; Thu, 27 Jun 2024 17:19:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B7096B009A; Thu, 27 Jun 2024 17:19:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 662646B009C; Thu, 27 Jun 2024 17:19:08 -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 467CF6B0099 for ; Thu, 27 Jun 2024 17:19:08 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 04919141036 for ; Thu, 27 Jun 2024 21:19:07 +0000 (UTC) X-FDA: 82277933976.17.F62648C Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf11.hostedemail.com (Postfix) with ESMTP id AB9B340017 for ; Thu, 27 Jun 2024 21:19:05 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=I2mZ3wkO; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.hostedemail.com: domain of hawk@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=hawk@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719523133; a=rsa-sha256; cv=none; b=ygnq3g1Z+sxJ83r67H7JEkgrtEnlmhXugcn++Wm+Bhcb5Rr/E/tALfnUFM1fz0NHDfWY5o R4Jy8yDr6StX3p8uypF2M3acIIuEAbwJMrSyfVrvsy/K7oHTt073NO67hGfVrRByBtAao2 5+kYKY7EGQOtEQSUGXhzlHN24HdaxyQ= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=I2mZ3wkO; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.hostedemail.com: domain of hawk@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=hawk@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719523133; 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=K0DGCP+OzEIutNCCGhdX/EUG8f7aYZZnW7gCHgdGhZg=; b=MhopPEO7jvx4TDScSdDW3wPVgdaeL4SkdErZvOl6OELWW5TTuEdcwUSQcPhEQNKouZokPu VWsPNicFsqRSjJtrFeGfaFuxuH2SESu2liCCdf9WH25xSabZ0ZUqRDKJtfCzLjSOfCnijK Yq9x8ofBdQcPspmpB3xRzX4L6c98Opc= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 00384CE2F4F; Thu, 27 Jun 2024 21:19:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C0E00C2BBFC; Thu, 27 Jun 2024 21:18:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719523141; bh=XAqqHw5F4Knai6LgI9AllUzQiY/8S4EOIBHey6rrs4Y=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=I2mZ3wkO/SiSEfGND4einmRwpN8a6yMVWgd6PchaEg+uFZztN6Ov5TawdzMXVGGUf y+TsqltS1ZTBK3CMMRHk4K3o2Q0CGvJ59Qu7aZfUsMooGXKz7hgtfig0zm833KjyPQ Pw+CO6R2l3tyjRpr8S03JHe/LHvMyLqO7LfFLMMx02b1SwWuyPS95RRh0TVAbiAuBR PbnaqVZ6kbHs/g+BeiE4F7a6awrrx5jmo6ObUT1H2uHeFuZcGEQ3fGsSzWaiN+TWdL 1x3kkQMnaKqdcvoCay6mocnX6wEl0/ybFM8hF2/aIfC+se5+Y3l1hDBFNVjl0OXBe5 cnMBL2PGsG9nQ== Subject: [PATCH V4 2/2] cgroup/rstat: Avoid thundering herd problem by kswapd across NUMA nodes From: Jesper Dangaard Brouer To: tj@kernel.org, cgroups@vger.kernel.org, yosryahmed@google.com, shakeel.butt@linux.dev Cc: Jesper Dangaard Brouer , hannes@cmpxchg.org, lizefan.x@bytedance.com, longman@redhat.com, kernel-team@cloudflare.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Date: Thu, 27 Jun 2024 23:18:56 +0200 Message-ID: <171952312320.1810550.13209360603489797077.stgit@firesoul> In-Reply-To: <171952310959.1810550.17003659816794335660.stgit@firesoul> References: <171952310959.1810550.17003659816794335660.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Queue-Id: AB9B340017 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 1c776nmyyjoqjpjr9y5d3889pspsxy9t X-HE-Tag: 1719523145-155778 X-HE-Meta: U2FsdGVkX19lcm13aK7Ed1V4WfbfC90NM+TtNaTnyiHqmaUT8zepE0MD0kSuYgNQ+9Zr2UprjdRy76E28g61u7jRi4rkYI4Ss2ZfdNOQpjQl80EKxqMZcmFGsJMVqk2mKjU3yqkevXTnJiG1ZF9BtAJc1ZiNDJu1C9cH+F3IRSn/NKTiVS0j4chrCmYWjDI3B78zovcD8j8F/xvaoV4/Ol5Uf4XgRsTuLuiTSLbFGi2NvW0mDr0tzMQXFNDRDV4T9BUW/HIYAAR70gZ3MV5dDr69db6GpxcRu0FCe+yB78aAa6ybKewR5V30ZzsJ6jIYZe57yrU0gXdWJC/SdhaZ1Iu80ove5eLG72hFwx8miLWNLEwj/l+3m//lovpjfpn0I2cjTh4wAFkWV3Wx9j/ME5s8jXA1zojslVVYUc/OmHwmErOnFcM6pQYCVjsm2CNUfRrJRBJAqh26EAPxOxnORoxyyMWHyqgc4jJApOSARIE1zpzp1mOLXlevVU1K+I756KgP2VPmXdohNCZDff/zDrn83zQlrdESKGQb4EC93yhqrgmwMELsvHOcAzFX02LcGyngn0NFUegdC9hAm58l83JslrGV6crGoOQvA3CLISrwtquLckaGQ/RhwPj6Sjn/N/8AXDIcowCkbQiEwOaX5DKmPcb8k4vcNjR0H4n1SLO2+in6sHCaZ4kG6RG3kkn7la58oV++JD99MVt57NOo4FHDQn5XLW6v8bwaWXbkikFONBCa7B1VpRPEMjo69fNWBdQU6g4tXjeBmluDosd0UhcpFvYTOPXELeJ9Qwg521Rp7iQ1GQMuaNfc2Y8QBMqI2bu6H4R3H8RTK9bOoIJnThE1C5U1w1cuZ9YrgFc0Dcq9BsfIEjjKDuEZoCorBazFUJvQDZaUSVjLy2i7tvP6isS11CyminYqvbzlSfGe1EADXtd3bfPYj3agN+wcDeo4BLSXp6JBp4kuph698RS Bq7o3Sdn 7x64JYYPk8Ag0JQytLRqWSshN04tDCJM7X9PX6rT5VzrLgTiYvgQv6csWB1dfnBWE8R84t4dGy/nG6i+pKmaZOdDCXaWnGQpY/6WkwMhwoFNpGuPhaWeS/mChTTd7GKhF8Hq9urh5YVe6N2OwiCoZsx5fVl3KesfcQ/kqBwyGJB706VWShl1yms/TkjMo1wmriIyWv2mgDdwpfOWOIfNYOOcz5Gwhu11wdjB3TBtDvF4gUR3B27X+yhNF+Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Avoid lock contention on the global cgroup rstat lock caused by kswapd starting on all NUMA nodes simultaneously. At Cloudflare, we observed massive issues due to kswapd and the specific mem_cgroup_flush_stats() call inlined in shrink_node, which takes the rstat lock. On our 12 NUMA node machines, each with a kswapd kthread per NUMA node, we noted severe lock contention on the rstat lock. This contention causes 12 CPUs to waste cycles spinning every time kswapd runs. Fleet-wide stats (/proc/N/schedstat) for kthreads revealed that we are burning an average of 20,000 CPU cores fleet-wide on kswapd, primarily due to spinning on the rstat lock. To help reviewer follow code: When the Per-CPU-Pages (PCP) freelist is empty, __alloc_pages_slowpath calls wake_all_kswapds(), causing all kswapdN threads to wake up simultaneously. The kswapd thread invokes shrink_node (via balance_pgdat) triggering the cgroup rstat flush operation as part of its work. This results in kernel self-induced rstat lock contention by waking up all kswapd threads simultaneously. Leveraging this detail: balance_pgdat() have NULL value in target_mem_cgroup, this cause mem_cgroup_flush_stats() to do flush with root_mem_cgroup. To avoid this kind of thundering herd problem, kernel previously had a "stats_flush_ongoing" concept, but this was removed as part of commit 7d7ef0a4686a ("mm: memcg: restore subtree stats flushing"). This patch reintroduce and generalized the concept to apply to all users of cgroup rstat, not just memcg. If there is an ongoing rstat flush, and current cgroup is a descendant, then it is unnecessary to do the flush. For callers to still see updated stats, wait for ongoing flusher to complete before returning, but add timeout as stats are already inaccurate given updaters keeps running. Fixes: 7d7ef0a4686a ("mm: memcg: restore subtree stats flushing"). Signed-off-by: Jesper Dangaard Brouer --- V3: https://lore.kernel.org/all/171943668946.1638606.1320095353103578332.stgit@firesoul/ V2: https://lore.kernel.org/all/171923011608.1500238.3591002573732683639.stgit@firesoul/ V1: https://lore.kernel.org/all/171898037079.1222367.13467317484793748519.stgit@firesoul/ RFC: https://lore.kernel.org/all/171895533185.1084853.3033751561302228252.stgit@firesoul/ include/linux/cgroup-defs.h | 2 + kernel/cgroup/rstat.c | 64 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index b36690ca0d3f..a33b37514c29 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -548,6 +548,8 @@ struct cgroup { #ifdef CONFIG_BPF_SYSCALL struct bpf_local_storage __rcu *bpf_cgrp_storage; #endif + /* completion queue for cgrp_rstat_ongoing_flusher */ + struct completion flush_done; /* All ancestors including self */ struct cgroup *ancestors[]; diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index 2a42be3a9bb3..a98af43bdce7 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -2,6 +2,7 @@ #include "cgroup-internal.h" #include +#include #include #include @@ -11,6 +12,8 @@ static DEFINE_SPINLOCK(cgroup_rstat_lock); static DEFINE_PER_CPU(raw_spinlock_t, cgroup_rstat_cpu_lock); +static struct cgroup *cgrp_rstat_ongoing_flusher = NULL; +static DECLARE_COMPLETION(cgrp_rstat_flusher_done); static void cgroup_base_stat_flush(struct cgroup *cgrp, int cpu); @@ -312,6 +315,45 @@ static inline void __cgroup_rstat_unlock(struct cgroup *cgrp, int cpu_in_loop) spin_unlock_irq(&cgroup_rstat_lock); } +#define MAX_WAIT msecs_to_jiffies(100) +/* Trylock helper that also checks for on ongoing flusher */ +static bool cgroup_rstat_trylock_flusher(struct cgroup *cgrp) +{ + bool locked = __cgroup_rstat_trylock(cgrp, -1); + if (!locked) { + struct cgroup *cgrp_ongoing; + + /* Lock is contended, lets check if ongoing flusher is already + * taking care of this, if we are a descendant. + */ + cgrp_ongoing = READ_ONCE(cgrp_rstat_ongoing_flusher); + if (cgrp_ongoing && cgroup_is_descendant(cgrp, cgrp_ongoing)) { + wait_for_completion_interruptible_timeout( + &cgrp_ongoing->flush_done, MAX_WAIT); + + return false; + } + __cgroup_rstat_lock(cgrp, -1, false); + } + /* Obtained lock, record this cgrp as the ongoing flusher */ + if (!READ_ONCE(cgrp_rstat_ongoing_flusher)) { + reinit_completion(&cgrp->flush_done); + WRITE_ONCE(cgrp_rstat_ongoing_flusher, cgrp); + } + + return true; /* locked */ +} + +static void cgroup_rstat_unlock_flusher(struct cgroup *cgrp) +{ + /* Detect if we are the ongoing flusher */ + if (cgrp == READ_ONCE(cgrp_rstat_ongoing_flusher)) { + WRITE_ONCE(cgrp_rstat_ongoing_flusher, NULL); + complete_all(&cgrp->flush_done); + } + __cgroup_rstat_unlock(cgrp, -1); +} + /* see cgroup_rstat_flush() */ static void cgroup_rstat_flush_locked(struct cgroup *cgrp) __releases(&cgroup_rstat_lock) __acquires(&cgroup_rstat_lock) @@ -361,18 +403,13 @@ static void cgroup_rstat_flush_locked(struct cgroup *cgrp) */ __bpf_kfunc void cgroup_rstat_flush(struct cgroup *cgrp) { - bool locked; - might_sleep(); - locked = __cgroup_rstat_trylock(cgrp, -1); - if (!locked) { - /* Opportunity to ongoing flush detection */ - __cgroup_rstat_lock(cgrp, -1, false); - } + if (!cgroup_rstat_trylock_flusher(cgrp)) + return; cgroup_rstat_flush_locked(cgrp); - __cgroup_rstat_unlock(cgrp, -1); + cgroup_rstat_unlock_flusher(cgrp); } /** @@ -388,8 +425,11 @@ void cgroup_rstat_flush_hold(struct cgroup *cgrp) __acquires(&cgroup_rstat_lock) { might_sleep(); - __cgroup_rstat_lock(cgrp, -1, true); - cgroup_rstat_flush_locked(cgrp); + + if (cgroup_rstat_trylock_flusher(cgrp)) + cgroup_rstat_flush_locked(cgrp); + else + __cgroup_rstat_lock(cgrp, -1, true); } /** @@ -399,7 +439,7 @@ void cgroup_rstat_flush_hold(struct cgroup *cgrp) void cgroup_rstat_flush_release(struct cgroup *cgrp) __releases(&cgroup_rstat_lock) { - __cgroup_rstat_unlock(cgrp, -1); + cgroup_rstat_unlock_flusher(cgrp); } int cgroup_rstat_init(struct cgroup *cgrp) @@ -421,6 +461,8 @@ int cgroup_rstat_init(struct cgroup *cgrp) u64_stats_init(&rstatc->bsync); } + init_completion(&cgrp->flush_done); + return 0; }