From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 473E7309DDF for ; Thu, 18 Jun 2026 21:36:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781818618; cv=none; b=leLuErcCJS1aBpHw9AgLS4T9qZ+DZ9WNlJuVQHwNPQwl16PC1lLGxxaMgEJmrdaL9VxAE6NQhdPS48dhGfVhqmXQZl6qRgzCKhMn20HoH2czdyVureRKYZm5jtmj4oxGG0z22OZdZUzOpunrXTjK7LWQ+8SF2SqR4/Wym1tnbAI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781818618; c=relaxed/simple; bh=EYjP31/qXaZiOklDzkuCs/LFZj34JkJlNtH9t3PgL5I=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=H/MQM3dd2P3g3BHDRMXxOY+gMI8L/XeD3sLi9GvXYR4LBx9qCepcIPGHuSplFrtd1YuQ7zLEruVxxXTrKScfUc5ITzPp49NBCK9jok/04lWSRSeEMFCWJ36o9cJS9szHQlXIgDlVGrisZ2XSFVrrwxfrtBQlZuxzSD6QXFzyHJY= 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=rhOEY0O7; arc=none smtp.client-ip=209.85.128.48 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="rhOEY0O7" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-49230a567a9so7263575e9.0 for ; Thu, 18 Jun 2026 14:36:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781818616; x=1782423416; 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=Xs7gyRJwRJkVNYolc+dgF0gvaFLP3C9o7IgJDu8x6hg=; b=rhOEY0O7aheml7HozBSg9yy6AQNFKvSAu4DFD6pIRRNk5PWvBuFMoaQWKfOH/uKsoF 7konBO08nCgY6T6bYl3x/Hxmel/iCZ5DtWpdPuITX+MY8gUWDrPsaGj6i+15SokRSnUA Z4VazPgZj8FOO1aoZE309d8P3/zbLEoJvY5D3OluzLG2RNWX14qUvmDlpHnkUcMXSIiF N76LOAb+51Za4f+pwvLqxDBZs/B5N7TM6eG2kv4IRpUQXkgeBtdwf9LixVQof/ndFLbE OXc+CIkCquv7grahBxk9UQ5pBLHa0+P8OLyRFBEb85jn4zs3prd58MpxJEmCA7kjB9Dg EIiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781818616; x=1782423416; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Xs7gyRJwRJkVNYolc+dgF0gvaFLP3C9o7IgJDu8x6hg=; b=NjoKF0ck8N4Qjfr2m69004Ph3Jt4/MJB5anbHdYfVDcFLx7kMBaVlWk8uaxAndeKun XS3ysSn40aNYt5hdasCyIpbsZLHgHHD2mZzhZfvRIUsX3OHZc0WF2NCxjy4t1cINFb6f KIA3TgBZPDWxsq/30pP28alvttIDwqda+G5cVNKzYt4+fIYIZlfzLIq6Ymozr8EFfcf5 INIVdEV7fbpix4Iq8fdaX0AjPy41opBjPTCqr2cLxK24669MRgebUYHyG0JxbNuZSRmm C6hoC6dPQTawZ4J8NSyeRHlVHna9XPpc0/n7V6F49zymv3A4IGuZhILRgMWVpHnfkSDA INXw== X-Forwarded-Encrypted: i=1; AFNElJ+ieSoRl1No3S64zV/MmC7dipZnrUy2/QIOvKRRLvlmgp8gqWs9VAdhGIpNTNm1Un/8gAMx10w+jU7eSZY=@vger.kernel.org X-Gm-Message-State: AOJu0YxrRDY4rtTkxZ8pgoeMlvkWlDnRhlc5tgpWpP7RB4T5HRKpZq/4 5maYdUQHxTKNdfU1E8L3ZxSnWzbLTs6VuuTTJ7s9G4L85CdSZcuy2GgIcgbxAH2+ X-Gm-Gg: AfdE7cleeybXiTV9r8mz8h7967ND+vQ6UXMm6N+VWOVfvjYFtfkkiRrMymisS3xRgaw mDvSe2t4Zx3KBRe57bX3TqDPYCFzPFYVnIx5Z4Lcs6sgJCEhjjRdjqSKVRhw4NoQ2FuJYJrpgCV WqkgrU11aRqIK4agE7N4PftlfcuNlfInKKyUhE1IKvMfwc0kd9+i4utBuGpxlhPQp36bYna4Vu8 eIGUULpx3MbfuAzm2gevGR2Ua6QOSU+HNWjlaqWnD0jR0Hn0PpILn2TwVOIUr/6ZgJe69vGQbak nqKTBtmylbWTdisMz7vNk+brLAJlzu+1C0ppbV59tOJmEPphRXmQldlDEcQhen0sOb+STDeLXqH ZMDWyCKp9WdgJd8+hi2O5DZKET32zhljJVQr2rIAW2767NsB1NgVTA7NJqGBLLthQJ03oR9jm2E C0c6UO0lV1l150be5YitdSLSXJ1sfJk+a9gH2dIbOnCzLaEgEsDRWAhSygPxLD X-Received: by 2002:a05:600c:d4:b0:488:ac01:72de with SMTP id 5b1f17b1804b1-49240e20de0mr9055505e9.5.1781818615679; Thu, 18 Jun 2026 14:36:55 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4650c114e22sm1701307f8f.34.2026.06.18.14.36.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jun 2026 14:36:55 -0700 (PDT) Date: Thu, 18 Jun 2026 22:36:53 +0100 From: David Laight To: Johannes Berg Cc: Andy Shevchenko , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, linux-wireless@vger.kernel.org, Kees Cook , "Gustavo A. R. Silva" Subject: Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once Message-ID: <20260618223653.01b42b38@pumpkin> In-Reply-To: <37fcf7c0b1a330a40005fc5ddbe075267b93851e.camel@sipsolutions.net> References: <20260617112250.2791461-1-andriy.shevchenko@linux.intel.com> <20260617112250.2791461-2-andriy.shevchenko@linux.intel.com> <1e656f5798a9f2f36daa00aba60d2196b2456335.camel@sipsolutions.net> <20260617223056.754bfcb8@pumpkin> <37fcf7c0b1a330a40005fc5ddbe075267b93851e.camel@sipsolutions.net> 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 Thu, 18 Jun 2026 20:53:37 +0200 Johannes Berg wrote: > (hah, just found this window open from this morning ...) > > On Thu, 2026-06-18 at 09:39 +0300, Andy Shevchenko wrote: > > On Wed, Jun 17, 2026 at 10:30:56PM +0100, David Laight wrote: > > > On Wed, 17 Jun 2026 14:56:09 +0200 > > > Johannes Berg wrote: > > > > On Wed, 2026-06-17 at 13:12 +0200, Andy Shevchenko wrote: > > > > > Convert size_add() to take variadic argument, so we can simplify users > > > > > with using a macro only once. > > > > > > > > > +#define __size_add3(addend1, addend2, addend3, addend4, ...) \ > > > > > + __size_add(__size_add2(addend1, addend2, addend3), addend4) > > > > > +#define __size_add4(addend1, addend2, addend3, addend4, addend5, ...) \ > > > > > + __size_add(__size_add3(addend1, addend2, addend3, addend4), addend5) > > > > > > > > I guess it's not going to really matter, but it would generate fewer > > > > calls to have something more like > > > > > > > > #define __size_add3(a1, a2, a3, a4) \ > > > > size_add(size_add(a1, a2), size_add(a3, a4)) > > > > #define __size_add4(a1, a2, a3, a4, a5) \ > > > > size_add(size_add(a1, a2), size_add(a3, a4, a5)) > > > > > > > > as a binary tree, rather than only cutting one off every time. Not sure > > > > that results in hugely different code though - maybe fewer overflow > > > > checks? > > > > Good question. I'm also thinking that one-by-one may expand in too much of > > preprocessor code (haven't checked myself). > > No. I was confused, and managed to confuse you too perhaps, sorry! > > We have to have the same number of operations (__size_add calls) > regardless, since you have to add it all up: 1 + 2 + 3 + 4 + 5 has a > fixed number of + signs regardless of how you parenthesise it. > > I guess actual CPU execution would have a better data dependency tree if > we balance it, Absolutely. Intel Haswell onwards and zen1-4 can execute 4 independent add/sub/and/or (etc) every clock. zen5 wins with 6 arithmetic ops or 4 cmov (and 2 alu) per clock. > but ... if our hotpath depends on size_add() we've lost already. I've no idea what the compiler generates, but a cmovc to copy in ~0 when the add sets carry stands a good chance of being pretty near the best. What you don't want is a conditional jump. The add, cmov pair will take two clocks, but the pairs are independent of each other (the carry flag isn't a limitation). The cpu should be able to execute two add and two cmov every clock. So with 4 values the 'tree' version is 4 clocks The other problem with ((a + b) + c) + d is that execution can't start until both a and b are available; with (a + b) + (c + d) it is much more likely that one of the adds can be executed early. Trying to guess the performance of modern cpu is non-trivial. David > > johannes