Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1689960ybb; Sun, 29 Mar 2020 10:58:24 -0700 (PDT) X-Google-Smtp-Source: ADFU+vs5NfasV9w6JcK2NeFb/rJxg9chJA9zxG2UJkec+J5thmggq7DFFuUAV82QyeY9NgviMxs8 X-Received: by 2002:aca:5d83:: with SMTP id r125mr5558643oib.8.1585504704885; Sun, 29 Mar 2020 10:58:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585504704; cv=none; d=google.com; s=arc-20160816; b=YXj2ShE9WGCRljYS2yCwm7QgS8nJ/Gun2rSn2rCwiIufuoR47mr10zOdA8axCvRi5x eyEEmycWghXurPT0GPA1PXkTNjG596IrO9MGMIHhOpIk4vryFGqwDbSwTB4Si5n0QPm0 pw0SJWg1/opn4gTL+clKCUwsGgO7HUuVWjL/p4gulxt3yPodIYiZWj6pVDy+EI1rcYWN lM5D0MKOo21zsjJBuidgzzZDzDg70akRBN4cmDzvLHDee8QCjPnwlGhKAP8WFn9+oneJ 9MhEvKQmh8dMzhvk7alW7tOM6C/rsGp+qOr6/Gi9tsAJU9I8yrBTZ4iyK2iKKm3jYrcy bYsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=hwt0QSmZtigbUQKCjTKiHUFhUuC8imw/4DtdMUwf2eU=; b=MzxKvLs8spzQbLHY99Ccxlcs6CO1ojVuGb993dSUmjS79YjYOGvAVPW0S7UlkTJ0N6 rLowdYJfYkVRAaraPIilBGJdp0mtABJOZuu3d3RYbMoQ3ZBXMhITr+LWvdcZIWNLB+Bk etNWCs0kjIiiDunxuBIYXpFnpfOqWKp5haloBXNlPwFcusIKzIma/LkvJ1h+qalekcbp 77G4FUVcZyIsjNez9vBPSzFq49lZlAGnbsSa9SZJYiC0XrHQydoLLbOnquODmpLYoGAX Dw5J+cE5a9V0OEJDQzCiJiWp1F/G1fy2tyXpNOO+MqDodWQ+B6ZWH7kHdd9Z8b7eZ9If HqoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=R5j1FllZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s133si2878358oig.254.2020.03.29.10.58.12; Sun, 29 Mar 2020 10:58:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=R5j1FllZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728349AbgC2R5U (ORCPT + 99 others); Sun, 29 Mar 2020 13:57:20 -0400 Received: from mail-lj1-f195.google.com ([209.85.208.195]:44363 "EHLO mail-lj1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727903AbgC2R5T (ORCPT ); Sun, 29 Mar 2020 13:57:19 -0400 Received: by mail-lj1-f195.google.com with SMTP id p14so15500209lji.11 for ; Sun, 29 Mar 2020 10:57:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=hwt0QSmZtigbUQKCjTKiHUFhUuC8imw/4DtdMUwf2eU=; b=R5j1FllZf3GoUAKeFl463vnk8JJz4Bi+OEAAg2teGJbCbPKPLSRPwIJ0B3DcMLMAJR Ma2K6bJ1CWJhVomHXzi8dTdhgrowuHdUUGBkkAG47qYrIsVig9mSH/uJLEKEZ5x8Ok9C UwA6QlekDFxL2y334ry9Ky2uMQl3kWjC8KUc4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=hwt0QSmZtigbUQKCjTKiHUFhUuC8imw/4DtdMUwf2eU=; b=frprwyD+dvFttpEwANs/Hnbh+i9i10vq9siS+lrL8Y59YoV9/GbWbMm/25Shx1PlPC 42mSV6gNFuRYHecZH9f5tbI8ZR2kJc67tA0+m0Cd4LyIFvUxvHfHK04UAGman7gb1H4w y5p5Kttnhu/EuD2cNyYTHsbjCQCf8l3xeYWfSWl4fpnc9kgYVnWikBk/ea1vZiumdwuU zitZTQFwSVET/aaqzxvF+mG147NQJSMMvxcH4lX7eKynf+i2uXdCq5PAU2Vt6B8nZjYA tzFF2un98T5uOO+UTwGHxw+Lp+xR9shGpoStm+JXNpaM3hFEmcoNglSWrI5GzVMfwbds RLcg== X-Gm-Message-State: AGi0PubCuKo8Gz1tdG4PXkGD4VZlJuedlcV0CI+aZUsO2bSlsC2i3Zo2 aMV2gHQwlFsbdJ9kxQigXc6wod30lm4= X-Received: by 2002:a05:651c:1203:: with SMTP id i3mr5090311lja.175.1585504636449; Sun, 29 Mar 2020 10:57:16 -0700 (PDT) Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com. [209.85.167.44]) by smtp.gmail.com with ESMTPSA id p18sm2930877lfc.6.2020.03.29.10.57.15 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 29 Mar 2020 10:57:15 -0700 (PDT) Received: by mail-lf1-f44.google.com with SMTP id e7so12114071lfq.1 for ; Sun, 29 Mar 2020 10:57:15 -0700 (PDT) X-Received: by 2002:ac2:4a72:: with SMTP id q18mr5884697lfp.10.1585504634779; Sun, 29 Mar 2020 10:57:14 -0700 (PDT) MIME-Version: 1.0 References: <20200323183620.GD23230@ZenIV.linux.org.uk> <20200323183819.250124-1-viro@ZenIV.linux.org.uk> <20200328104857.GA93574@gmail.com> <20200328115936.GA23230@ZenIV.linux.org.uk> <20200329092602.GB93574@gmail.com> <489c9af889954649b3453e350bab6464@AcuMS.aculab.com> In-Reply-To: <489c9af889954649b3453e350bab6464@AcuMS.aculab.com> From: Linus Torvalds Date: Sun, 29 Mar 2020 10:56:59 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC][PATCH 01/22] x86 user stack frame reads: switch to explicit __get_user() To: David Laight Cc: Andy Lutomirski , Ingo Molnar , Al Viro , Thomas Gleixner , X86 ML , LKML , Borislav Petkov Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Mar 29, 2020 at 10:41 AM David Laight wrote: > > It may be worth implementing get_user() as an inline > function that writes the result of access_ok() to a > 'by reference' parameter and then returns the value > from an 'real' __get_user() function. That's how get_user() already works. It is a polymorphic function (done using macros, sizeof() and ugly compiler tricks) that generates a call, yes. But it's not a normal C call. On x86-64, it returns the error code in %rax, and the value in %rdx So "get_user()" is already basically optimal. It's likely *faster* than __get_user(), because it has a smaller I$ footprint if you do multiple ones. But, if you have lots of performance-critical get_user() calls, just use if (user_access_begin(..)) goto efault; .. multiple "unsafe_get_user(x,ptr,efault);" .. user_access_end(); ... efault: user_access_end(); return -EFAULT; and be done with it. Yes, the above sequence looks cumbersome, but it's designed for doing multiple accesses together efficiently. It's basically the "I actually had a good reason to use __get_user(), but it sucks now, so this is the new interface" It's designed for multiple accesses, because as mentioned, if you only have one, then "get_user()" is already optimal. And yes, the interface (with that "label for error cases") is optimized for a (future) world where the compiler can do "asm goto" together with outputs. Any exception on the access doesn't actually generate a test at all, the exception will branch directly to the error label instead. That already works for "unsafe_put_user()", but for "unsafe_get_user()" you need a compiler that can do that kind of "asm goto". If you use a modern clang version (ie build clang from git), I can send you a patch for the kernel to try (and a patch for clang to fix a bug, unless it's been already merged, I didn't check). The above will generate basically _optimal_ code with my patch and that modern clang version. Linus