From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9310C433EF for ; Thu, 16 Jun 2022 09:31:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E0AAE6B0074; Thu, 16 Jun 2022 05:31:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D93E96B0078; Thu, 16 Jun 2022 05:31:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C3F546B0074; Thu, 16 Jun 2022 05:31:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id ACE3D6B0074 for ; Thu, 16 Jun 2022 05:31:16 -0400 (EDT) Received: from smtpin31.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 70E8F61633 for ; Thu, 16 Jun 2022 09:31:16 +0000 (UTC) X-FDA: 79583580552.31.256572C Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) by imf08.hostedemail.com (Postfix) with ESMTP id 70C9816008C for ; Thu, 16 Jun 2022 09:31:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=/FLWc2TEgQvR6Lestc/lCyA2NX/oAWdgoQaLyVOcOsg=; b=Mtv0e/0LUtujFXUBTyvN/ogSTh 2DwxkKZc8SnJ7MCr0ztCYUVLCD9GVP3jLiDiDZxjwviLX3Ljttry0RcUNaDwlQLO05yTG/DkaJvVF nfNYY6UFvtX6uIjmLp6mUoeezK5cRE73isYCZxMUohsm3bLZYWfl8kgnTlwyFsKZjnwDdpo58OVax 6RQO2f23hOUuscc3R14mclfwXPLsH13b88v8W6MBQoKRqndH0ha5FvJ3QebZMEhWypCLf3C19BvaB /eMeFfKf9J5AhnZG1QvK4YpBy5uqBVU/irEC5pxpESCitQdUvnxxY1xO65UhJ38+98hjbf81IS3pD 4peHHqlg==; Received: from dhcp-077-249-017-003.chello.nl ([77.249.17.3] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1lq0-008Nat-CN; Thu, 16 Jun 2022 09:30:53 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 0FEE630023F; Thu, 16 Jun 2022 11:30:50 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id EA3162014ABD6; Thu, 16 Jun 2022 11:30:49 +0200 (CEST) Date: Thu, 16 Jun 2022 11:30:49 +0200 From: Peter Zijlstra To: "Edgecombe, Rick P" Cc: "kirill.shutemov@linux.intel.com" , "Lutomirski, Andy" , "dave.hansen@linux.intel.com" , "linux-kernel@vger.kernel.org" , "hjl.tools@gmail.com" , "linux-mm@kvack.org" , "kcc@google.com" , "andreyknvl@gmail.com" , "ak@linux.intel.com" , "dvyukov@google.com" , "x86@kernel.org" , "ryabinin.a.a@gmail.com" , "glider@google.com" Subject: Re: [PATCHv3 5/8] x86/uaccess: Provide untagged_addr() and remove tags before address check Message-ID: References: <20220610143527.22974-1-kirill.shutemov@linux.intel.com> <20220610143527.22974-6-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655371876; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/FLWc2TEgQvR6Lestc/lCyA2NX/oAWdgoQaLyVOcOsg=; b=593O7Jd8tDPd9aSgt/LgNiDbXoHoxLoh8a4E0I6cPQYumnmhkH1R3fRT3OxxOGuGTh642N mNOP0WNHJDRwpxNCfyV6tMGjOrMVJ15d7Eb98GKqkMA8Sj+3u5HU5fq2DyCaI+hID4ONgu +4M6lA/ucTwkCRkhIaZqaaUAVVbFyw4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655371876; a=rsa-sha256; cv=none; b=fdnMxxDKfswdYoxGGWsJm4PxWbs/qYWjuGVNZXJwIYZqK8tRwvwEZRqjqCPyE127r75OC8 dhm68tD6JRfSz91UJfOPlBQbyqysGExXEWtIXOniw4KTlSTRo7nmsKJKJG9gM3ljqsgyNU jI2PsFrkhRobQt0FEO/wYuFcjUG3swo= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b="Mtv0e/0L"; dmarc=none; spf=none (imf08.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b="Mtv0e/0L"; dmarc=none; spf=none (imf08.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: rcj3wm9bojsju9qwd8fn5g87frjr7j6q X-Rspamd-Queue-Id: 70C9816008C X-HE-Tag: 1655371875-663420 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000004, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Jun 13, 2022 at 05:36:43PM +0000, Edgecombe, Rick P wrote: > On Fri, 2022-06-10 at 17:35 +0300, Kirill A. Shutemov wrote: > > +#ifdef CONFIG_X86_64 > > +/* > > + * Mask out tag bits from the address. > > + * > > + * Magic with the 'sign' allows to untag userspace pointer without > > any branches > > + * while leaving kernel addresses intact. > > Trying to understand the magic part here. I guess how it works is, when > the high bit is set, it does the opposite of untagging the addresses by > setting the tag bits instead of clearing them. So: The magic is really rather simple to see; there's two observations: x ^ y ^ y == x That is; xor is it's own inverse. And secondly, xor with 1 is a bit toggle. So if we mask a negative value, we destroy the sign. Therefore, if we xor with the sign-bit, we have a nop for positive numbers and a toggle for negatives (effectively making them positive, -1, 2s complement yada-yada) then we can mask, without fear of destroying the sign, and then we xor again to undo whatever we did before, effectively restoring the sign. Anyway, concequence of all this is that LAM_U48 won't work correct on 5-level kernels, because the mask will still destroy kernel pointers. As such, this patch only does LAM_U57.