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 6AD26D735E1 for ; Fri, 29 Nov 2024 22:19:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F39D6B0083; Fri, 29 Nov 2024 17:19:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A3666B0085; Fri, 29 Nov 2024 17:19:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 643F76B0088; Fri, 29 Nov 2024 17:19:58 -0500 (EST) 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 46C286B0083 for ; Fri, 29 Nov 2024 17:19:58 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3BD8FADFAB for ; Fri, 29 Nov 2024 22:19:57 +0000 (UTC) X-FDA: 82840550814.08.778A185 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf25.hostedemail.com (Postfix) with ESMTP id 38BF2A0005 for ; Fri, 29 Nov 2024 22:19:49 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=o5p0Hl5t; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of frederic@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=frederic@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732918787; a=rsa-sha256; cv=none; b=ib9bsC0mteWm+Llyzk0xaCqUQU6kaIDhNV74cZQ4tLP6+zT7tkWVizhqynXZLxpjC/EaKD rPcc+MA5Ru9l6lSC14pFNoBgaqXLtZ+pF/ONjGfp2uKI7vXoppYHksHi+YhV75BSCat+6i 8ghtFBo8kKDYQAdQwnZCgq78LXbsqcc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=o5p0Hl5t; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of frederic@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=frederic@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732918787; 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=uIbjNsL50nLvvZ5omQHDcZFXeMDtzMNLNw45bIO9zHA=; b=pQ8CMYMz0XNNSRKrwm/KYkLLBTyIFDXsf9HNnAawVeqZOaJOYPBMUdZIzMP5pgg6QV3HLg NYA7u3DYKCp3KwciDj85bSEvCi3zpZKE80dNxHDguJDtS4hduQP2DQIyPcGlhLuadNwtqn 6EQu+bf8XQszrpDiKSZitFv7Nulg37s= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id BA857A43763; Fri, 29 Nov 2024 22:18:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C894C4CECF; Fri, 29 Nov 2024 22:19:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732918794; bh=OJvU5bLoYGZXh8qACQ/T6IEV8oDjNatqYL+CeW3HsJg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=o5p0Hl5t68sOGrNrYENKMovE5NztdhOpEAFABibzPZGUoqz1AXXaCH53QUmqJu9yj 4Ymv5lTSZbEsDt+NPkbGOI0BMOSI/hhFnrmJFW+/qqVN7darYuDK67nomYfyn1WTZW BFRJMR/Fl9FeauFwTeTH3phw520rl/KLfJopadlpHQUp+IZrgAicwA5DrSA19U8kSy DpTFETeRTKi33P/1gcVkoaLTCtVby2dZtM0i0fCZSI8+UVXXGJuKWkaLG6dG9bq/8l w9ueF6Q3rudD7ZWIlpxFt0GwISNHkjcxLh8drdyWqw8+a9ohium7+Z6XshqrmZWZxx DWCgNchAqIpvQ== Date: Fri, 29 Nov 2024 23:19:51 +0100 From: Frederic Weisbecker To: Valentin Schneider Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, x86@kernel.org, rcu@vger.kernel.org, linux-kselftest@vger.kernel.org, Nicolas Saenz Julienne , Steven Rostedt , Masami Hiramatsu , Jonathan Corbet , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Paolo Bonzini , Wanpeng Li , Vitaly Kuznetsov , Andy Lutomirski , Peter Zijlstra , "Paul E. McKenney" , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Andrew Morton , Uladzislau Rezki , Christoph Hellwig , Lorenzo Stoakes , Josh Poimboeuf , Jason Baron , Kees Cook , Sami Tolvanen , Ard Biesheuvel , Nicholas Piggin , Juerg Haefliger , Nicolas Saenz Julienne , "Kirill A. Shutemov" , Nadav Amit , Dan Carpenter , Chuang Wang , Yang Jihong , Petr Mladek , "Jason A. Donenfeld" , Song Liu , Julian Pidancet , Tom Lendacky , Dionna Glaze , Thomas =?iso-8859-1?Q?Wei=DFschuh?= , Juri Lelli , Marcelo Tosatti , Yair Podemsky , Daniel Wagner , Petr Tesarik Subject: Re: [RFC PATCH v3 11/15] context-tracking: Introduce work deferral infrastructure Message-ID: References: <20241119153502.41361-1-vschneid@redhat.com> <20241119153502.41361-12-vschneid@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 38BF2A0005 X-Stat-Signature: 7yfmnzsft3wjgd8azza17w13i7jqyqs7 X-Rspam-User: X-HE-Tag: 1732918789-724291 X-HE-Meta: U2FsdGVkX1+0lGD1ZFh0Cg4Vj1xOwEJRKxEMhYpv/L7RbwNQ72dbczvgu5l9x6aWVWdrhRWux0owfXNxW/f3uyePQPvoM0C1rNGSsRC/lSAmQXKwQteVDBzI24DVd0G95leqYr5vsTKydQFHJfU6UY0FyM0lABBdtmpPu/yJqusTDQ3/WtHjO7yIYqDEmLlHjn+ME8zA78WTesgkmTyV+tHwLD2edK/7qGw2JRavQ8T7vUtdihCnapRodCHv1xxMr4f1o75NUkPWUPO0h8WoIb/oAJ+3yHcAlrouIat/UwYdkUa/fGWBPVjASO9DxqH2soerr/JgWi2XYfFxqVJiT7FjBlFXmMYIbDIi4yiOp9Xj1+wMP7jMG1EnBHPuCZQMyOq5hhtlWXMIeyg11ifGICvswR+sGJzlJgSnJovpXop7DVOyBP6BYjRhsgJiArdRzEg24We+1f8wktZueaqGz9EatdU7APUs0/q8uoe/BhL0q0NSiV/QDz0rm6PX4ifdXDn1uJUQq1Z0exe0r0tZaJBpwUcGnADRldOv2vHUPdzl/N5bHGtn3Re+MRChSHxVHLKPHp4ksR1KbaZJoSy4UBYuqtN4AWKNvMWbRH9Vh25lGe5cqJv8TFOFEVn2Hs0vOx596hxc606DiOnQ7JSpLJUPykgp8vINDIsBGDgh+HNF5/5HtrGgkbWORTr/pGSGinglI8dfoi2iN7b/NOcQAEb0kXL6PQngG7l8McPamOGR1nc1LmS++l1uXUFNSNTLYkpfShTETcVHNm014gIMgijjdS+suey+yfJHocff7mNZur3/RXFRicH38u1u5vrDvy1PwuRClare65zs+MsgSG8LRUn3z5eM8w6QUj5qCUfE1Tchj8qmxEPGNVkd7s8vcf2czAhxlFgmwcuBB+hC6leiwxTraYF5SQtqJvg8ppeVCN3cpGBeG/Hx4r4PbuAFHPkox19upGwavB049Z8 Xr7jpOv6 GTsv9KWEIbTIa9jXCiCA5PDXnz2D/3jp9ahZs8gdd9onx5SkC6pZuKQ9AkHAxhjc7D4jmRgzE0Ufi6hEUMjXDvgRWaSk6Tz1QZlcBnOFD0GRvYZrzcaXDpvLpRQucapdHQbWCu+lijpHMxiZRKy1RKtyBKMQZU1TA5mIQweB/sC3WXGuQJFK4HuIfyGgCAWZEZf29tCk8Zwdkg7CbWw5FH5yFyRgyVLrjCyVqFrgmx00Rw39uyIgLvfG2JltM0iSHWCvmvNggQCxrw868hTfqxV8ectuOotBj7TmpNah4zdFs1qCqaSnYu2K/SA== 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: List-Subscribe: List-Unsubscribe: Le Fri, Nov 29, 2024 at 05:40:29PM +0100, Valentin Schneider a écrit : > On 24/11/24 22:46, Frederic Weisbecker wrote: > > Le Fri, Nov 22, 2024 at 03:56:59PM +0100, Valentin Schneider a écrit : > >> On 20/11/24 18:30, Frederic Weisbecker wrote: > >> > Le Wed, Nov 20, 2024 at 06:10:43PM +0100, Valentin Schneider a écrit : > >> >> On 20/11/24 15:23, Frederic Weisbecker wrote: > >> >> > >> >> > Ah but there is CT_STATE_GUEST and I see the last patch also applies that to > >> >> > CT_STATE_IDLE. > >> >> > > >> >> > So that could be: > >> >> > > >> >> > bool ct_set_cpu_work(unsigned int cpu, unsigned int work) > >> >> > { > >> >> > struct context_tracking *ct = per_cpu_ptr(&context_tracking, cpu); > >> >> > unsigned int old; > >> >> > bool ret = false; > >> >> > > >> >> > preempt_disable(); > >> >> > > >> >> > old = atomic_read(&ct->state); > >> >> > > >> >> > /* CT_STATE_IDLE can be added to last patch here */ > >> >> > if (!(old & (CT_STATE_USER | CT_STATE_GUEST))) { > >> >> > old &= ~CT_STATE_MASK; > >> >> > old |= CT_STATE_USER; > >> >> > } > >> >> > >> >> Hmph, so that lets us leverage the cmpxchg for a !CT_STATE_KERNEL check, > >> >> but we get an extra loop if the target CPU exits kernelspace not to > >> >> userspace (e.g. vcpu or idle) in the meantime - not great, not terrible. > >> > > >> > The thing is, what you read with atomic_read() should be close to reality. > >> > If it already is != CT_STATE_KERNEL then you're good (minus racy changes). > >> > If it is CT_STATE_KERNEL then you still must do a failing cmpxchg() in any case, > >> > at least to make sure you didn't miss a context tracking change. So the best > >> > you can do is a bet. > >> > > >> >> > >> >> At the cost of one extra bit for the CT_STATE area, with CT_STATE_KERNEL=1 > >> >> we could do: > >> >> > >> >> old = atomic_read(&ct->state); > >> >> old &= ~CT_STATE_KERNEL; > >> > > >> > And perhaps also old |= CT_STATE_IDLE (I'm seeing the last patch now), > >> > so you at least get a chance of making it right (only ~CT_STATE_KERNEL > >> > will always fail) and CPUs usually spend most of their time idle. > >> > > >> > >> I'm thinking with: > >> > >> CT_STATE_IDLE = 0, > >> CT_STATE_USER = 1, > >> CT_STATE_GUEST = 2, > >> CT_STATE_KERNEL = 4, /* Keep that as a standalone bit */ > > > > Right! > > > >> > >> we can stick with old &= ~CT_STATE_KERNEL; and that'll let the cmpxchg > >> succeed for any of IDLE/USER/GUEST. > > > > Sure but if (old & CT_STATE_KERNEL), cmpxchg() will consistently fail. > > But you can make a bet that it has switched to CT_STATE_IDLE between > > the atomic_read() and the first atomic_cmpxchg(). This way you still have > > a tiny chance to succeed. > > > > That is: > > > > old = atomic_read(&ct->state); > > if (old & CT_STATE_KERNEl) > > old |= CT_STATE_IDLE; > > old &= ~CT_STATE_KERNEL; > > > > > > do { > > atomic_try_cmpxchg(...) > > > > Hmm? > > But it could equally be CT_STATE_{USER, GUEST}, right? That is, if we have > all of this enabled them we assume the isolated CPUs spend the least amount > of time in the kernel, if they don't we get to blame the user. Unless CONTEXT_TRACKING_WORK_IDLE=y yes. Anyway that's just a detail that can be refined in the future. I'm fine with just clearing CT_STATE_KERNEL and go with that. Thanks.