From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F2051B6544 for ; Mon, 20 Jan 2025 11:15:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737371758; cv=none; b=UaT7XL19NrqHUKte+tzHmWVaPfLmWoBwfJ/M9dVGC1v5qf3uvriRZTGKmmlMJhJqBGyk68iZZXRQeFslJ4i1Lqa+PQhzHyIDYUp+NaLn9Wf09zZPWYPaIU5YdEh7yBf3KqtYDZFZa0gGHfgTSkGEW5aaKWUwsHdNSdIy0/91I+o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737371758; c=relaxed/simple; bh=gHIZJFWeaV41Jh/9XhtoTrRArUmxMksNW9pKQmGnwT0=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=o/fDR8MJBlDWMAvN0+i4QmfXXNElHvTUv4bJtWR8eojgvRniKbxSLMYUWGK5YmwxlgCuksmC9olf5ggyCXJE3HqmjIy0Jzntu/awI3enWlo9irqvbaEiblmZ1dvD0fH0pVWneIpMg+XjrBv3KHhZ9/VpheCtzA4bo3BFPklQgmU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gtwtIB/p; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gtwtIB/p" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-437a92d7b96so43063595e9.2 for ; Mon, 20 Jan 2025 03:15:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737371754; x=1737976554; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=Grk9J6/b9VBuQn2GFV1dMGR5fuj1yzKJkHNAQSI0qz8=; b=gtwtIB/pvsF8NETL271NBn97ThMou4fxd5se1KZ+CymeqAyfL4QVlr+nhoFW9VxzYy EAsdbGWbUMhoZESAQZ/Tz3PtiF+qZoyZKEttE7b4G6lsuklxsoLU+U+Q+jcqhT1XRyRp KEY/WUJxWDaBbSmH/Cto9lbYksdPsLzGjRMGcxcKS2PWyTubCyQdxiGfflXog9IAruRM kTxHeIlTj/LOf0lGEffOXg75yC22XVXDfmSc+Y2nNbUDS1gplNOh/Hzaj5uwDFS+q3YJ M3LDDkXGLgDAzeJsPAw7pqrHiDeF6prBAF9Yymxl8WgMq4/WqIkHb+p948G/nrkREePB inYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737371754; x=1737976554; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Grk9J6/b9VBuQn2GFV1dMGR5fuj1yzKJkHNAQSI0qz8=; b=J7dHS32PKZe6zeigk53K1dcFVifaoFxNhCrc5rxh4h2uEx11/oneBVO/PY/QhgpcVJ NFSAB6CshvralCuGhhijgT4AHAZMPQ/TZhZJLUHyLDiVc7qiLcXvu25e8g5PErXWKlzZ 8uq40jm2A+Lz5yLwafY+OP0E2Rg/atpeLv/dauFumJr/maSM+Lb8f8ae7EqfLSb54F0C DPT+yy8ddpTMY8x7apJbVfzgTIjc8abQ/fMuykMtpF2OIicwY2MIHGFbHuodcRSNB81/ ZD75Kw2dv9mAnT9IoOE5MHDrb41G3tzBgge7t+7ZR5RRNB1ebg7Dbei+QWYagggWAHmU godQ== X-Forwarded-Encrypted: i=1; AJvYcCWXTRK27G6YyeqceI11uyroRLmNMRhSl70jFqXp4+aYm4qNxwBIMpxHYkShZ5wj8F2uyZ35XRGB/AtSzNw=@vger.kernel.org X-Gm-Message-State: AOJu0YyxMjfHdVeJ5ayOJCGFRPmT1Ekz0Ez4jMvifpPBYc/xWcgBY0qq 4h2qRS6UdDmEBUD8hTE/6KelwHLtffk0W5yxm8ezYV+SgYGXGRat X-Gm-Gg: ASbGncsqqaTG3pAbUIYOWIYV1cJvEoKtVaLhyswYmp9TNX3Mw4LHh3eHCKqJKD398pU 7fdIcSnvvToenCFWpHiP0aM+gxShOUPOaHcYpoIdmCwTPP7rKY0uQFctkY+6uPbjbDpODKfHvuV h3+yyDAduuvAYkMXqgQNhGXCl7A6mKgUuZ0Ssw9QbWmDoQKLjt+DcW8HJORFJN0TMZe29reysIn +HWQdCZTVwAgvMv50kFswwYLmgrTWpTBlzUPGOD7Z8VjqnoqscSloTsP0n28dgKrnsu3w/pRYow oh7tf7W3VUuFr96g0YJUGttHU+VC/Z5R X-Google-Smtp-Source: AGHT+IFbr5mR//NKB/G8R+Pcmr8O/z0UAE/kTlXY81rkDpzp5l4T8SB64QRh8jdEVqTvLMPJctdhdw== X-Received: by 2002:a05:600c:a45:b0:436:faeb:2a0b with SMTP id 5b1f17b1804b1-438913e32bemr123863535e9.15.1737371752518; Mon, 20 Jan 2025 03:15:52 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43890413053sm136694905e9.10.2025.01.20.03.15.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 03:15:52 -0800 (PST) Date: Mon, 20 Jan 2025 11:15:51 +0000 From: David Laight To: Jani Nikula Cc: Guenter Roeck , Linus Torvalds , David Laight , Arnd Bergmann , "linux-kernel@vger.kernel.org" , Jens Axboe , Matthew Wilcox , Christoph Hellwig , Andrew Morton , Andy Shevchenko , 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: <20250120111551.435176c4@pumpkin> In-Reply-To: <87ed0xrcb8.fsf@intel.com> References: <34d53778977747f19cce2abb287bb3e6@AcuMS.aculab.com> <20250118170959.3aa56f4d@pumpkin> <29ef57a1-e4dd-4d5d-8726-f1f79c698b66@roeck-us.net> <20250118221123.5bb65e64@pumpkin> <20250119090935.7c690f85@pumpkin> <87ed0xrcb8.fsf@intel.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) 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-Transfer-Encoding: 7bit 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. David