From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (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 224351E32DB for ; Mon, 20 Jan 2025 18:55:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737399346; cv=none; b=IM6PUO1mr/DQUCIRTXDElERSCIAw9d6cCgk3ujwj3ZPW82OVZW91fQglu13OXbSCvFHqa+8gH8z5GhSW8j6Uv/FYAmZ0XHjwTj4X/EC4vTrWA+XesKKW0PYR8rehNnGEt9BKBxox9T3CryeOSEafG2as7RJIJbWTIUseoOAcgXg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737399346; c=relaxed/simple; bh=op5oAGTrMnHrTtMQubTxdXMZzxdcxLlUbJi6A9RKoMo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=FmdQdSmyZO8Z8t1MhLjIC0LTKxZ9dLfBYoEp0p9F+8O2DOvMm8kY0wCL95yoCOnGKUmApsi8jX/Q3RC8Ov9/ySqW/BeyMOCg0TD9JjgIKxdmquzwoTkjp698Vir8UMf2ywqw60uEdtwR4xgpU3cPbWJWU8mEtjRexUbDoodtSLg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ZegXYD8d; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ZegXYD8d" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1737399345; x=1768935345; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=op5oAGTrMnHrTtMQubTxdXMZzxdcxLlUbJi6A9RKoMo=; b=ZegXYD8dxMXTGudckZe+KXpmVCi8kip2aMhl0gzWiDvg/bsBIBZmU3ZT jjgDj2uu2S3uXHCGdwbYMCKe4pnIIxy8ufe6ZoYJPkCVO5+XnAZ2okrSr sTppyLgG2QDYcK0AaR94Zo2YmFJbphGDHAZkJ/VXqUQtqZMFeu7E/XaTu 70hIGmFQ3SqDqfUUFaFjAHFaB3zt+/TAJFC8je6tiWWykclJ1SLw4DhfF AFtcSH1EJtlSmcphrSBO2FmqN5z6IUDHQ6f5Cg1zlXvhUsKUaA3o/gfaR OgFk5+y630t5brNVKGH1/kxjLL6+AUgt8HuDW5659H5cRlQq7ssvL4BZL A==; X-CSE-ConnectionGUID: 3h73THo0ROmbTLqDO5ku2w== X-CSE-MsgGUID: HkVCpquaSey1lvIE0vWYUA== X-IronPort-AV: E=McAfee;i="6700,10204,11321"; a="37718309" X-IronPort-AV: E=Sophos;i="6.13,219,1732608000"; d="scan'208";a="37718309" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jan 2025 10:55:43 -0800 X-CSE-ConnectionGUID: p5dQ9kclQL6q2hlmyNaNeg== X-CSE-MsgGUID: yU17x0vTTAasbq8m/yd+eg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.13,219,1732608000"; d="scan'208";a="107205604" Received: from smile.fi.intel.com ([10.237.72.58]) by fmviesa009.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jan 2025 10:55:39 -0800 Received: from andy by smile.fi.intel.com with local (Exim 4.98) (envelope-from ) id 1tZwvv-00000003XJZ-0XDD; Mon, 20 Jan 2025 20:55:35 +0200 Date: Mon, 20 Jan 2025 20:55:34 +0200 From: Andy Shevchenko To: David Laight Cc: Guenter Roeck , Jani Nikula , Linus Torvalds , David Laight , Arnd Bergmann , "linux-kernel@vger.kernel.org" , Jens Axboe , Matthew Wilcox , Christoph Hellwig , Andrew Morton , Dan Carpenter , "Jason A . Donenfeld" , "pedro.falcato@gmail.com" , Mateusz Guzik , "linux-mm@kvack.org" , Lorenzo Stoakes , intel-xe@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, David Airlie , Simona Vetter , Rodrigo Vivi Subject: Re: Buiild error in i915/xe Message-ID: References: <29ef57a1-e4dd-4d5d-8726-f1f79c698b66@roeck-us.net> <20250118221123.5bb65e64@pumpkin> <20250119090935.7c690f85@pumpkin> <87ed0xrcb8.fsf@intel.com> <20250120111551.435176c4@pumpkin> <878qr5ras2.fsf@intel.com> <0a65de6c-74d5-4d3e-be75-0aa9ecc82da1@roeck-us.net> <20250120184143.47615b21@pumpkin> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250120184143.47615b21@pumpkin> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo On Mon, Jan 20, 2025 at 06:41:43PM +0000, David Laight wrote: > On Mon, 20 Jan 2025 06:15:30 -0800 > Guenter Roeck wrote: > > On 1/20/25 03:21, Jani Nikula wrote: > > > On Mon, 20 Jan 2025, David Laight wrote: > > >> On Mon, 20 Jan 2025 12:48:11 +0200 > > >> Jani Nikula wrote: > > >>> On Sun, 19 Jan 2025, David Laight wrote: > > >>>> On Sat, 18 Jan 2025 14:58:48 -0800 > > >>>> Guenter Roeck wrote: > > >>>>> On 1/18/25 14:11, David Laight wrote: > > >>>>>> On Sat, 18 Jan 2025 13:21:39 -0800 > > >>>>>> Linus Torvalds wrote: > > >>>>>>> On Sat, 18 Jan 2025 at 09:49, Guenter Roeck wrote: > > >>>>>>>> > > >>>>>>>> No idea why the compiler would know that the values are invalid. > > >>>>>>> > > >>>>>>> It's not that the compiler knows tat they are invalid, but I bet what > > >>>>>>> happens is in scale() (and possibly other places that do similar > > >>>>>>> checks), which does this: > > >>>>>>> > > >>>>>>> WARN_ON(source_min > source_max); > > >>>>>>> ... > > >>>>>>> source_val = clamp(source_val, source_min, source_max); > > >>>>>>> > > >>>>>>> and the compiler notices that the ordering comparison in the first > > >>>>>>> WARN_ON() is the same as the one in clamp(), so it basically converts > > >>>>>>> the logic to > > >>>>>>> > > >>>>>>> if (source_min > source_max) { > > >>>>>>> WARN(..); > > >>>>>>> /* Do the clamp() knowing that source_min > source_max */ > > >>>>>>> source_val = clamp(source_val, source_min, source_max); > > >>>>>>> } else { > > >>>>>>> /* Do the clamp knowing that source_min <= source_max */ > > >>>>>>> source_val = clamp(source_val, source_min, source_max); > > >>>>>>> } > > >>>>>>> > > >>>>>>> (obviously I dropped the other WARN_ON in the conversion, it wasn't > > >>>>>>> relevant for this case). > > >>>>>>> > > >>>>>>> And now that first clamp() case is done with source_min > source_max, > > >>>>>>> and it triggers that build error because that's invalid. > > >>>>>>> > > >>>>>>> So the condition is not statically true in the *source* code, but in > > >>>>>>> the "I have moved code around to combine tests" case it now *is* > > >>>>>>> statically true as far as the compiler is concerned. > > >>>>>> > > >>>>>> Well spotted :-) > > >>>>>> > > >>>>>> One option would be to move the WARN_ON() below the clamp() and > > >>>>>> add an OPTIMISER_HIDE_VAR(source_max) between them. > > >>>>>> > > >>>>>> Or do something more sensible than the WARN(). > > >>>>>> Perhaps return target_min on any such errors? > > >>>>>> > > >>>>> > > >>>>> This helps: > > >>>>> > > >>>>> - WARN_ON(source_min > source_max); > > >>>>> - WARN_ON(target_min > target_max); > > >>>>> - > > >>>>> /* defensive */ > > >>>>> source_val = clamp(source_val, source_min, source_max); > > >>>>> > > >>>>> + WARN_ON(source_min > source_max); > > >>>>> + WARN_ON(target_min > target_max); > > >>>> > > >>>> That is a 'quick fix' ... > > >>>> > > >>>> Much better would be to replace the WARN() with (say): > > >>>> if (target_min >= target_max) > > >>>> return target_min; > > >>>> if (source_min >= source_max) > > >>>> return target_min + (target_max - target_min)/2; > > >>>> So that the return values are actually in range (in as much as one is defined). > > >>>> Note that the >= cpmparisons also remove a divide by zero. > > >>> > > >>> I want the loud and early warnings for clear bugs instead of > > >>> "gracefully" silencing the errors only to be found through debugging > > >>> user reports. > > >> > > >> A user isn't going to notice a WARN() - not until you tell them to look for it. > > >> In any case even if you output a message you really want to return a 'sane' > > >> value, who knows what effect a very out of range value is going to have. > > > > > > The point is, we'll catch the WARN in CI before it goes out to users. > > > > It isn't going to catch the divide by 0 error, and it obviously doesn't > > catch the build problem on parisc with gcc 13.x because the CI isn't > > testing it. > > > > How about disabling DRM_XE on architectures where it isn't supported, > > matching DRM_I915 ? > > That'll just bite back later. > As Linus spotted the compiler is just 'optimising' some code paths. > It could happen for any architecture including x64. > The repeated tests are basically slightly odd, although you might only > expect them to be present in debug builds. > > An alternative would be to replace the clamp() with: > if (source_val <= source_min) > return target_min; > if (source_val >= source_max) > return target_max; Excuse me if I am missing something, but clamp() has a warning inside it, correct? Why do wee need an additional warning on top of that? P.S. However, I agree that ideally clamp() should work independently on the caller to use WARN*() or other similar stuff. -- With Best Regards, Andy Shevchenko