From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) (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 617FD248166 for ; Thu, 12 Jun 2025 14:37:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.167.242.64 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749739051; cv=none; b=B6ovMGke72yIKlYrGhcuncdA/QTyQ2/xeffzlS7qLjBaQtWwyskAlICWR0uFK0TbtRDVdI40b4PMq35Ki5wZ0Xa7K4EFU6bgWZ7Nn9e7IppEMyGStyr+PgpZ2SrT8rp0g5EbXf2D7pqir0Duo1r7lFZQbR5ssH7noh5iowZZMrU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749739051; c=relaxed/simple; bh=3ON5Gv3OWdxiRwybeyLaTwXdsFOiWoMBl3YdA40kGwA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=dqi4CAjcroS1p3YGbyzrDnT6eXhzbF29xsb8tkivrQfuykn16SJpbApCJkYXU+ApJnChJoTrZhkPP0vd5aQOPNAAIdoUyQlHzvoTODkTOfayssOxfFxfMiLMAzGRaDUabs6VHsZIDPW7DAmLA4jEq0JWua8aKhyiws2MBYiJ7+U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ideasonboard.com; spf=pass smtp.mailfrom=ideasonboard.com; dkim=pass (1024-bit key) header.d=ideasonboard.com header.i=@ideasonboard.com header.b=kkYWnBX2; arc=none smtp.client-ip=213.167.242.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ideasonboard.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ideasonboard.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="kkYWnBX2" Received: from [192.168.88.20] (91-158-153-178.elisa-laajakaista.fi [91.158.153.178]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 74FE87E4; Thu, 12 Jun 2025 16:37:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1749739032; bh=3ON5Gv3OWdxiRwybeyLaTwXdsFOiWoMBl3YdA40kGwA=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=kkYWnBX2ITn3E2WKghndy5Qd38beY9eWSEg0ks8FBlFROl/mRxCGJP+VtirWYarou db9b54L1Uz3PEpCFxUMZocOLXno3eRuLFC4rDiGDQwHXxi56aqnF3BI275F7RXrtzm WssiUe0GobKjq2WpxScToXHlyPIC0xYP8q4+mOuA= Message-ID: Date: Thu, 12 Jun 2025 17:37:17 +0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] drm: omapdrm: reduce clang stack usage To: Arnd Bergmann , Arnd Bergmann Cc: Nick Desaulniers , Bill Wendling , Justin Stitt , linux , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Dave Airlie , Simona Vetter , Nathan Chancellor References: <20250610092737.2641862-1-arnd@kernel.org> <4a81fc44-7c93-49d1-891b-dae11cd5e74a@app.fastmail.com> Content-Language: en-US From: Tomi Valkeinen Autocrypt: addr=tomi.valkeinen@ideasonboard.com; keydata= xsFNBE6ms0cBEACyizowecZqXfMZtnBniOieTuFdErHAUyxVgtmr0f5ZfIi9Z4l+uUN4Zdw2 wCEZjx3o0Z34diXBaMRJ3rAk9yB90UJAnLtb8A97Oq64DskLF81GCYB2P1i0qrG7UjpAS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qazRwIbDAAKCRD6PaqMvJYe 9fA7EACS6exUedsBKmt4pT7nqXBcRsqm6YzT6DeCM8PWMTeaVGHiR4TnNFiT3otD5UpYQI7S suYxoTdHrrrBzdlKe5rUWpzoZkVK6p0s9OIvGzLT0lrb0HC9iNDWT3JgpYDnk4Z2mFi6tTbq xKMtpVFRA6FjviGDRsfkfoURZI51nf2RSAk/A8BEDDZ7lgJHskYoklSpwyrXhkp9FHGMaYII m9EKuUTX9JPDG2FTthCBrdsgWYPdJQvM+zscq09vFMQ9Fykbx5N8z/oFEUy3ACyPqW2oyfvU CH5WDpWBG0s5BALp1gBJPytIAd/pY/5ZdNoi0Cx3+Z7jaBFEyYJdWy1hGddpkgnMjyOfLI7B CFrdecTZbR5upjNSDvQ7RG85SnpYJTIin+SAUazAeA2nS6gTZzumgtdw8XmVXZwdBfF+ICof 92UkbYcYNbzWO/GHgsNT1WnM4sa9lwCSWH8Fw1o/3bX1VVPEsnESOfxkNdu+gAF5S6+I6n3a ueeIlwJl5CpT5l8RpoZXEOVtXYn8zzOJ7oGZYINRV9Pf8qKGLf3Dft7zKBP832I3PQjeok7F yjt+9S+KgSFSHP3Pa4E7lsSdWhSlHYNdG/czhoUkSCN09C0rEK93wxACx3vtxPLjXu6RptBw 3dRq7n+mQChEB1am0BueV1JZaBboIL0AGlSJkm23kw== In-Reply-To: <4a81fc44-7c93-49d1-891b-dae11cd5e74a@app.fastmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi, On 12/06/2025 15:40, Arnd Bergmann wrote: > On Thu, Jun 12, 2025, at 09:58, Tomi Valkeinen wrote: >> On 10/06/2025 12:27, Arnd Bergmann wrote: >>> >>> -static void dispc_restore_context(struct dispc_device *dispc) >>> +static noinline_for_stack void dispc_restore_context(struct dispc_device *dispc) >>> { >>> int i, j; >>> >> >> While I don't think this causes any harm, but... What's going on here? >> If I compile with gcc (x86 or arm), I see stack usage in few hundreds of >> bytes. If I compile with LLVM=1, the stack usage jumps to over a thousand. >> >> Is clang just broken? I don't see anything special with >> dispc_restore_context() or dispc_runtime_resume(), so is this same thing >> happening all around the kernel, and we need to sprinkle noinlines >> everywhere? >> >> Or do we get some extra debugging feature enabled only on clang with >> allmodconfig, and that is eating the stack? > > There is no general answer here, but a combination of multiple > effects going on at the same time throughout the kernel, which lead > to clang observing excessive stack usage in some files when gcc > does not: > > - both compilers have a number of corner cases where they run off > and do something crazy for unusual input (usually crypto code), > but since gcc has more users, most files that trigger only gcc > already have workarounds in place, while the ones that trigger > with clang are still missing them > > - The inlining algorithm works the opposite way on clang vs gcc, > while gcc always starts inlining leaf functions into their callers > and does this recursively, clang starts with global functions > and inlines its direct callees first. If you have deeply nested > static functions that could all be inlined, both stop at some > point, but the resulting object code looks completely different, > and the stack usage is a symptom of this. I've added 'noinline' > for some of the cases like this where I know both result in > the same (harmless) stack usage through the call chain, but > only clang warns about it. > > - clang has previously had bugs where it tracks the lifetime of > stack variables incorrectly, so multiple variables that > should share the same stack slot won't. Some of these are > fixed now, others are a result of the different inlining, and > some others are likely still bugs we should fix in clang > > - CONFIG_KMSAN disables some optimizations that are required > for reducing stack usage, and at the moment this is only > implemented in clang but not gcc. > > - CONFIG_KASAN has some similar issues as KMSAN but is not > quite as bad here. > > - CONFIG_KASAN_STACK tends to use more stack with clang than gcc > because of implementation choices around how hard it should > try to detect array overflows. This could be changed by having > clang make similar decisions to gcc here, but for now we just > require using CONFIG_EXPERT=y to enable KASAN_STACK on clang. > > I have managed to produce a testcase for this file that shows > how clang produces huge stack usage when gcc does not, > in this case it seems to be triggered by -fsanitize=kernel-address > > https://godbolt.org/z/TT88zPYf6 Interesting! And clang does fine if I change the DISPC_OVL_BASE() to static u16 DISPC_OVL_BASE(enum omap_plane_id plane) { static const u16 bases[] = {0x0080, 0x00BC, 0x014C, 0x0300, 0x0500}; return bases[plane]; } In any case, I'll apply this with a small comment. Tomi