From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754725AbbKCCir (ORCPT ); Mon, 2 Nov 2015 21:38:47 -0500 Received: from mail-ig0-f171.google.com ([209.85.213.171]:34393 "EHLO mail-ig0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753790AbbKCCio (ORCPT ); Mon, 2 Nov 2015 21:38:44 -0500 MIME-Version: 1.0 In-Reply-To: References: <20151027.233235.1641084823622810663.davem@davemloft.net> <5637C8DF.800@kernel.org> <1446512176.17404.30.camel@kernel.crashing.org> Date: Mon, 2 Nov 2015 18:38:44 -0800 X-Google-Sender-Auth: fHFdU45Hl_vjP1V9PxmivRtrFJ8 Message-ID: Subject: Re: [GIT] Networking From: Linus Torvalds To: Andy Lutomirski Cc: Benjamin Herrenschmidt , Andy Lutomirski , David Miller , Hannes Frederic Sowa , Andrew Morton , Network Development , Linux Kernel Mailing List , Sasha Levin Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 2, 2015 at 5:58 PM, Andy Lutomirski wrote: > > Based in part on an old patch by Sasha, what if we relied on CSE: > > if (mul_would_overflow(size, n)) > return NULL; > do_something_with(size * n); I suspect we wouldn't even have to rely on CSE. Are these things in performance-critical areas? I suspect our current "use divides" is actually slower than just using two multiplies, even if one is only used for overflow checking. That said, I also think that for something like this, where we actually have a *reason* for using a special overflow helper function, we could just try to use the gcc syntax. I don't think it's wonderful syntax, but at least there's an excuse for odd/ugly code in those kinds of situations. The reason I hated the unsigned subtraction case so much was that the simple obvious code just avoids all those issues entirely, and there wasn't any real *reason* for the nasty syntax. For multiplication overflow, we'd at least have a reason. Sadly, the *nice* syntax, where we'd do something like "goto label" when the multiply overflows, does not mesh well with inline asm. Yes, gcc now has "asm goto", but you can't use it with an output value ;( But from a syntactic standpoint, the best syntax might actually be something like mul = multiply_with_overflow(size, n, error_label); do_something_with(mul); error_label: return NULL; and it would *almost* be possible to do this with inline asm if it wasn't for the annoying "no output values" case. There are many other cases where I'd have wanted to do this (ie the whole "fetch value from user space, but if an exception happens, point the exception handler at the label). Back in May, we talked to the gcc people about allowing output values that are unspecified for the "goto" case (so only a fallthrough would have them set), but I think that that doesn't match how gcc internally thinks about branching instructions.. But you could still hide it inside a macro and make it expand to something like #define multiply_with_overflow(size, n, error_label) ({ unsigned long result, error; \ .. do multiply in asm, set result and error... \ if (error) goto error_label; result; }) which would allow the above kind of odd hand-coded try/catch model in C. Which I think would be pretty understandable and not very prone to getting it wrong. Hmm? Linus