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 X-Spam-Level: X-Spam-Status: No, score=-5.7 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A53A4C433ED for ; Fri, 16 Apr 2021 11:33:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 439B661153 for ; Fri, 16 Apr 2021 11:33:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 439B661153 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C60B26B0036; Fri, 16 Apr 2021 07:33:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C10C26B006C; Fri, 16 Apr 2021 07:33:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A8B0E6B0070; Fri, 16 Apr 2021 07:33:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0128.hostedemail.com [216.40.44.128]) by kanga.kvack.org (Postfix) with ESMTP id 822E16B0036 for ; Fri, 16 Apr 2021 07:33:16 -0400 (EDT) Received: from smtpin34.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 3BF2918000DA0 for ; Fri, 16 Apr 2021 11:33:16 +0000 (UTC) X-FDA: 78038019192.34.3C39AD3 Received: from vmicros1.altlinux.org (vmicros1.altlinux.org [194.107.17.57]) by imf15.hostedemail.com (Postfix) with ESMTP id B0C64A0003A8 for ; Fri, 16 Apr 2021 11:33:13 +0000 (UTC) Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 4CD1C72C8B5; Fri, 16 Apr 2021 14:33:14 +0300 (MSK) Received: from example.org (ip-94-113-225-162.net.upcbroadband.cz [94.113.225.162]) by imap.altlinux.org (Postfix) with ESMTPSA id F1DC84A47A1; Fri, 16 Apr 2021 14:33:12 +0300 (MSK) Date: Fri, 16 Apr 2021 13:33:10 +0200 From: Alexey Gladkov To: "Eric W. Biederman" Cc: Linus Torvalds , kernel test robot , 0day robot , LKML , lkp@lists.01.org, "Huang, Ying" , Feng Tang , zhengjun.xing@intel.com, Kernel Hardening , Linux Containers , Linux-MM , Andrew Morton , Christian Brauner , Jann Horn , Jens Axboe , Kees Cook , Oleg Nesterov Subject: Re: 08ed4efad6: stress-ng.sigsegv.ops_per_sec -41.9% regression Message-ID: <20210416113310.vbxrzorycqdxwiyc@example.org> References: <7abe5ab608c61fc2363ba458bea21cf9a4a64588.1617814298.git.gladkov.alexey@gmail.com> <20210408083026.GE1696@xsang-OptiPlex-9020> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: B0C64A0003A8 X-Stat-Signature: qcaxwyhmb8i4y3hj539wekc5uo8ntxon X-Rspamd-Server: rspam02 Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf15; identity=mailfrom; envelope-from=""; helo=vmicros1.altlinux.org; client-ip=194.107.17.57 X-HE-DKIM-Result: none/none X-HE-Tag: 1618572793-225070 Content-Transfer-Encoding: quoted-printable 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, Apr 08, 2021 at 01:44:43PM -0500, Eric W. Biederman wrote: > Linus Torvalds writes: >=20 > > On Thu, Apr 8, 2021 at 1:32 AM kernel test robot wrote: > >> > >> FYI, we noticed a -41.9% regression of stress-ng.sigsegv.ops_per_sec= due to commit > >> 08ed4efad684 ("[PATCH v10 6/9] Reimplement RLIMIT_SIGPENDING on top = of ucounts") > > > > Ouch. >=20 > We were cautiously optimistic when no test problems showed up from > the last posting that there was nothing to look at here. >=20 > Unfortunately it looks like the bots just missed the last posting.=20 >=20 > So it seems we are finally pretty much at correct code in need > of performance tuning. >=20 > > I *think* this test may be testing "send so many signals that it > > triggers the signal queue overflow case". > > > > And I *think* that the performance degradation may be due to lots of > > unnecessary allocations, because ity looks like that commit changes > > __sigqueue_alloc() to do > > > > struct sigqueue *q =3D kmem_cache_alloc(sigqueue_cachep, flag= s); > > > > *before* checking the signal limit, and then if the signal limit was > > exceeded, it will just be free'd instead. > > > > The old code would check the signal count against RLIMIT_SIGPENDING > > *first*, and if there were m ore pending signals then it wouldn't do > > anything at all (including not incrementing that expensive atomic > > count). >=20 > This is an interesting test in a lot of ways as it is testing the > synchronous signal delivery path caused by an exception. The test > is either executing *ptr =3D 0 (where ptr points to a read-only page) > or it executes an x86 instruction that is excessively long. >=20 > I have found the code but I haven't figured out how it is being > called yet. The core loop is just: > for(;;) { > sigaction(SIGSEGV, &action, NULL); > sigaction(SIGILL, &action, NULL); > sigaction(SIGBUS, &action, NULL); >=20 > ret =3D sigsetjmp(jmp_env, 1); > if (done()) > break; > if (ret) { > /* verify signal */ > } else { > *ptr =3D 0; > } > } >=20 > Code like that fundamentally can not be multi-threaded. So the only wa= y > the sigpending limit is being hit is if there are more processes runnin= g > that code simultaneously than the size of the limit. >=20 > Further it looks like stress-ng pushes RLIMIT_SIGPENDING as high as it > will go before the test starts. >=20 >=20 > > Also, the old code was very careful to only do the "get_user()" for > > the *first* signal it added to the queue, and do the "put_user()" for > > when removing the last signal. Exactly because those atomics are very > > expensive. > > > > The new code just does a lot of these atomics unconditionally. >=20 > Yes. That seems a likely culprit. >=20 > > I dunno. The profile data in there is a bit hard to read, but there's > > a lot more cachee misses, and a *lot* of node crossers: > > > >> 5961544 +190.4% 17314361 perf-stat.i.cache-miss= es > >> 22107466 +119.2% 48457656 perf-stat.i.cache-refe= rences > >> 163292 =C4=85 3% +4582.0% 7645410 perf-stat.i.node-= load-misses > >> 227388 =C4=85 2% +3708.8% 8660824 perf-stat.i.node-= loads > > > > and (probably as a result) average instruction costs have gone up eno= rmously: > > > >> 3.47 +66.8% 5.79 perf-stat.overall.cpi > >> 22849 -65.6% 7866 perf-stat.overall.cycl= es-between-cache-misses > > > > and it does seem to be at least partly about "put_ucounts()": > > > >> 0.00 +4.5 4.46 perf-profile.calltrace= .cycles-pp.put_ucounts.__sigqueue_free.get_signal.arch_do_signal_or_resta= rt.exit_to_user_mode_prepare > > > > and a lot of "get_ucounts()". > > > > But it may also be that the new "get sigpending" is just *so* much > > more expensive than it used to be. >=20 > That too is possible. >=20 > That node-load-misses number does look like something is bouncing back > and forth between the nodes a lot more. So I suspect stress-ng is > running multiple copies of the sigsegv test in different processes at > once. >=20 >=20 >=20 > That really suggests cache line ping pong from get_ucounts and > incrementing sigpending. >=20 > It surprises me that obtaining the cache lines exclusively is > the dominant cost on this code path but obtaining two cache lines > exclusively instead of one cache cache line exclusively is consistent > with a causing the exception delivery to take nearly twice as long. >=20 > For the optimization we only care about the leaf count so with a little > care we can restore the optimization. So that is probably the thing > to do here. The fewer changes to worry about the less likely to find > surprises. >=20 >=20 >=20 > That said for this specific case there is a lot of potential room for > improvement. As this is a per thread signal the code update sigpending > in commit_cred and never worry about needing to pin the struct > user_struct or struct ucounts. As this is a synchronous signal we coul= d > skip the sigpending increment, skip the signal queue entirely, and > deliver the signal to user-space immediately. The removal of all cache > ping pongs might make it worth it. >=20 > There is also Thomas Gleixner's recent optimization to cache one > sigqueue entry per task to give more predictable behavior. That > would remove the cost of the allocation. https://git.kernel.org/pub/scm/linux/kernel/git/legion/linux.git/commit/?= h=3Dpatchset/per-userspace-rlimit/v11.1&id=3D08db0c814926c6f16e08de99b2de= 34c8b5ff68ce You mean something like this ? I did it on top of Thomas Gleixner's patches. --=20 Rgrds, legion