Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp443459lqp; Sat, 13 Apr 2024 04:11:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWB2lPaNrTahNAKDTYd5S2BeUlxY2GKvQzGg4xvxu4+2qgdpzcEWRkKR/J+ei8E70Ur/4YZAEsqYpMVuQ5CSmA07JU81oBVd2RK/rxO0Q== X-Google-Smtp-Source: AGHT+IGsJ/iKJXC+TSvi5prcQkpGosAXPJ6mI1FrpgEzfuiFuyrMt2OtQA2FMjm+UI1XEnLALvCU X-Received: by 2002:a17:906:4919:b0:a52:551f:cc80 with SMTP id b25-20020a170906491900b00a52551fcc80mr181117ejq.2.1713006694324; Sat, 13 Apr 2024 04:11:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713006694; cv=pass; d=google.com; s=arc-20160816; b=HqX2jwlPhh4/BoKtl8YRUwSLpEi6iSpsi/mtqLv3p8i7SwyUkJssyoysHBKD0R0Z14 DrCxKVtshqja5B/YmOaRuRWOE+Yv4nMU3WHCnqtCacmgkIDjNXI+RLo+btMatlk74dTx Nn9zJUHkGG8T0csI35Mc403CBIWFTDbSvsymi/mxoRw0seAGUdI0SDM2ct5nKkoLzNU2 kt/HQ+N6btRIL1G24MqH+6QnqrZZU86vD4CDDE42cMpizrYgsOP29qkKvn3nqPvxM+ik SryX30t5aLAWQfGft7PbCEIWMdkXuKtR1oRNszlNYrZT0e/C47bvF80rmtOfTg3vGt9b eh4Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:references:in-reply-to:subject:cc:to:from:message-id :date:dkim-signature; bh=Bof7waySkU+NnQrY8EBrl0gZ2TMgJVkXd1RE56RFZX0=; fh=vXnF8lgSsnhm0fjkTnFz+ynHLvwBJ7p7IEt3IHjJUl8=; b=EiMowYrbmFeWlExDA9BY4ADLWMhh1x2p7/D4C9Jm2Gv2J1fo/aRkz88XmUjsKFYuMv gPRfh944aqU0i+DjFBuU8vgNRywONh9Keo0xaZvOJBthA4p570W7M9lizY6n6ecv6TEK Zdm1zdj78VKTWgO+YW5TYUlqHKrI1uY+BrREjWbHc3OqtZwwGkq6CZgrcBrSS/ITX0nm q3kddXpFhuPBDJwTA3ypNvHJT/YxItqC2woRzKccTKcU6UbyeIib0B0zVGUHS3a0JLHb M1zpgVNMsnFUspskhwWqjtfKdgKj73Sh7Wr4i8HZI6olp7VRnCoE8HxRln6bGkomTWJB 8Uiw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KrAsfsAR; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-143704-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143704-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id d8-20020a170906304800b00a465f2ed58esi2652113ejd.837.2024.04.13.04.11.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Apr 2024 04:11:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-143704-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KrAsfsAR; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-143704-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143704-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 982BB1F21F96 for ; Sat, 13 Apr 2024 11:11:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D797D3E47E; Sat, 13 Apr 2024 11:11:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KrAsfsAR" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E01D43DB91 for ; Sat, 13 Apr 2024 11:11:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713006683; cv=none; b=i2K1JaJ2hhX+xQ/BHKt+UaKgagmzzzF/cIqJKR2dytPnNrkUGxKywUOlqknkwJ9Vse4LZMgu1awqN8ElijKVgHt/Us42YleKFtSFFaw1jvYoQ72OvNoAH0dLcXwpgjg6E8IADwd5wvIqFQoQWrMh1XfM48EN71VYmG7+lB+WzEk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713006683; c=relaxed/simple; bh=yqFTGPFpUELq92u6Jwc0LpudMXSZpeOTpg8NEgcDRJI=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=RnU0dLhRrSZ3In/c5Ny+pf2vXxYqb3TZmav6XFH1FFZZKInz+T/7tjPOPn5clrWm56xwW4XLc3DX+BwX2VJztpwLH2xCD90LmwPWyoMc5eqrcTLyiTvIDsedK4EiL9Q3tPuhcFACDZPw/HakfXAyzQCGFAV/93P46UHuY+H6zk8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KrAsfsAR; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AF35DC2BD10; Sat, 13 Apr 2024 11:11:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713006682; bh=yqFTGPFpUELq92u6Jwc0LpudMXSZpeOTpg8NEgcDRJI=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=KrAsfsARagsovovWhbrM3DzytETGRjjf1eEVd624V+MA957i7JHN7rtdGv64H5p5u YHtu9XkrauozUBYhf7grICnq/NEQ9Q0uO4k1iUzFg4JtGnElCBkAm2ODfHxscsoNQ4 y1S5hmNEn+iPZjombc6VwQTgBXH83KY+RZ5q7KOX4AKUSAHygbJ5ytWgojWTgt/6bz daOxYgfnyKwcVYXOBayDT1R6jjOXpfCXNhBYLv4JlCCfhnvIuOQtfenFILxoID7Kt5 N21DStLF6OcAnlHG8lmoYzbFV7qWz0BmiB5wM/sS/qAr7q3zyFZNSdWizL8fcP2yA1 CjWtaMGmS7ptg== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1rvbI0-0048U7-J8; Sat, 13 Apr 2024 12:11:20 +0100 Date: Sat, 13 Apr 2024 12:11:20 +0100 Message-ID: <86cyqtsejr.wl-maz@kernel.org> From: Marc Zyngier To: Dawei Li Cc: tglx@linutronix.de, yury.norov@gmail.com, akpm@linux-foundation.org, florian.fainelli@broadcom.com, chenhuacai@kernel.org, jiaxun.yang@flygoat.com, anup@brainfault.org, palmer@dabbelt.com, samuel.holland@sifive.com, linux@rasmusvillemoes.dk, linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/6] irqchip/gic-v3-its: Avoid explicit cpumask allocation on stack In-Reply-To: <08D93AF972A58F13+ZhpegNehN5/RYie5@centos8> References: <20240412105839.2896281-1-dawei.li@shingroup.cn> <20240412105839.2896281-4-dawei.li@shingroup.cn> <86il0msn4z.wl-maz@kernel.org> <08D93AF972A58F13+ZhpegNehN5/RYie5@centos8> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/29.2 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: dawei.li@shingroup.cn, tglx@linutronix.de, yury.norov@gmail.com, akpm@linux-foundation.org, florian.fainelli@broadcom.com, chenhuacai@kernel.org, jiaxun.yang@flygoat.com, anup@brainfault.org, palmer@dabbelt.com, samuel.holland@sifive.com, linux@rasmusvillemoes.dk, linux-kernel@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false On Sat, 13 Apr 2024 11:29:20 +0100, Dawei Li wrote: > > Hi Marc, > > Thanks for the review. > > On Fri, Apr 12, 2024 at 02:53:32PM +0100, Marc Zyngier wrote: > > On Fri, 12 Apr 2024 11:58:36 +0100, > > Dawei Li wrote: > > > > > > In general it's preferable to avoid placing cpumasks on the stack, as > > > for large values of NR_CPUS these can consume significant amounts of > > > stack space and make stack overflows more likely. > > > > > > Remove cpumask var on stack and use proper cpumask API to address it. > > > > Define proper. Or better, define what is "improper" about the current > > usage. > > Sorry for the confusion. > > I didn't mean current implementation is 'improper', actually both > implementations share equivalent API usages. I will remove this > misleading expression from commit message. > > > > > > > > > Signed-off-by: Dawei Li > > > --- > > > drivers/irqchip/irq-gic-v3-its.c | 9 ++++++--- > > > 1 file changed, 6 insertions(+), 3 deletions(-) > > > > > > diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c > > > index fca888b36680..a821396c4261 100644 > > > --- a/drivers/irqchip/irq-gic-v3-its.c > > > +++ b/drivers/irqchip/irq-gic-v3-its.c > > > @@ -3826,7 +3826,7 @@ static int its_vpe_set_affinity(struct irq_data *d, > > > bool force) > > > { > > > struct its_vpe *vpe = irq_data_get_irq_chip_data(d); > > > - struct cpumask common, *table_mask; > > > + struct cpumask *table_mask; > > > unsigned long flags; > > > int from, cpu; > > > > > > @@ -3850,8 +3850,11 @@ static int its_vpe_set_affinity(struct irq_data *d, > > > * If we are offered another CPU in the same GICv4.1 ITS > > > * affinity, pick this one. Otherwise, any CPU will do. > > > */ > > > - if (table_mask && cpumask_and(&common, mask_val, table_mask)) > > > - cpu = cpumask_test_cpu(from, &common) ? from : cpumask_first(&common); > > > + if (table_mask && cpumask_intersects(mask_val, table_mask)) { > > > + cpu = cpumask_test_cpu(from, mask_val) && > > > + cpumask_test_cpu(from, table_mask) ? > > > + from : cpumask_first_and(mask_val, table_mask); > > > > So we may end-up computing the AND of the two bitmaps twice (once for > > cpumask_intersects(), once for cpumask_first_and()), instead of only > > doing it once. > > Actually maybe it's possible to merge these 2 bitmap ops into one: > > diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c > index fca888b36680..7a267777bd0b 100644 > --- a/drivers/irqchip/irq-gic-v3-its.c > +++ b/drivers/irqchip/irq-gic-v3-its.c > @@ -3826,7 +3826,8 @@ static int its_vpe_set_affinity(struct irq_data *d, > bool force) > { > struct its_vpe *vpe = irq_data_get_irq_chip_data(d); > - struct cpumask common, *table_mask; > + struct cpumask *table_mask; > + unsigned int common; > unsigned long flags; > int from, cpu; > > @@ -3850,10 +3851,13 @@ static int its_vpe_set_affinity(struct irq_data *d, > * If we are offered another CPU in the same GICv4.1 ITS > * affinity, pick this one. Otherwise, any CPU will do. > */ > - if (table_mask && cpumask_and(&common, mask_val, table_mask)) > - cpu = cpumask_test_cpu(from, &common) ? from : cpumask_first(&common); > - else > + if (table_mask && (common = cpumask_first_and(mask_val, table_mask)) < nr_cpu_ids) { > + cpu = cpumask_test_cpu(from, mask_val) && > + cpumask_test_cpu(from, table_mask) ? > + from : common; > + } else { > cpu = cpumask_first(mask_val); > + } > > > > > I don't expect that to be horrible, but I also note that you don't > > even talk about the trade-offs you are choosing to make. > > With change above, I assume that the tradeoff is minor and can be ignored? Yup, this works. My preference would be something which I find slightly more readable though (avoiding assignment in the conditional): diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c index fca888b36680..299dafc7c0ea 100644 --- a/drivers/irqchip/irq-gic-v3-its.c +++ b/drivers/irqchip/irq-gic-v3-its.c @@ -3826,9 +3826,9 @@ static int its_vpe_set_affinity(struct irq_data *d, bool force) { struct its_vpe *vpe = irq_data_get_irq_chip_data(d); - struct cpumask common, *table_mask; + struct cpumask *table_mask; unsigned long flags; - int from, cpu; + int from, cpu = nr_cpu_ids; /* * Changing affinity is mega expensive, so let's be as lazy as @@ -3850,10 +3850,15 @@ static int its_vpe_set_affinity(struct irq_data *d, * If we are offered another CPU in the same GICv4.1 ITS * affinity, pick this one. Otherwise, any CPU will do. */ - if (table_mask && cpumask_and(&common, mask_val, table_mask)) - cpu = cpumask_test_cpu(from, &common) ? from : cpumask_first(&common); - else + if (table_mask) + cpu = cpumask_any_and(mask_val, table_mask); + if (cpu < nr_cpu_ids) { + if (cpumask_test_cpu(from, mask_val) && + cpumask_test_cpu(from, table_mask)) + cpu = from; + } else { cpu = cpumask_first(mask_val); + } if (from == cpu) goto out; Thanks, M. -- Without deviation from the norm, progress is not possible.