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 X-Spam-Level: X-Spam-Status: No, score=-14.4 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1FBDC3A59E for ; Wed, 21 Aug 2019 16:57:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 67CF622DD3 for ; Wed, 21 Aug 2019 16:57:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LZ29zPgY" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 67CF622DD3 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E7AA46B0316; Wed, 21 Aug 2019 12:57:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E04356B0322; Wed, 21 Aug 2019 12:57:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA4776B0323; Wed, 21 Aug 2019 12:57:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0116.hostedemail.com [216.40.44.116]) by kanga.kvack.org (Postfix) with ESMTP id 9F91D6B0316 for ; Wed, 21 Aug 2019 12:57:13 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with SMTP id 506B08248ABC for ; Wed, 21 Aug 2019 16:57:13 +0000 (UTC) X-FDA: 75847040346.06.key48_5c0fff61ff400 X-HE-Tag: key48_5c0fff61ff400 X-Filterd-Recvd-Size: 10360 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Wed, 21 Aug 2019 16:57:12 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id c81so1795780pfc.11 for ; Wed, 21 Aug 2019 09:57:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=VW3KnN8Acc9B0iEM/8SMTYoiqB1C69NJCO/L2fy9T0E=; b=LZ29zPgY1JCJZosPEQMWMF8sGqW2T0iQX3VHzbcY14iAPwWBFycPp+Ut1pIp60N+6M 63yBq7G0RdWy2aANHbwOBTZ+I6GIiz4Xu49tOYEDigKaPpLtizTGQEZRk2iNnATEvJ17 rn+wmv3o9QmCU40S2WKtKxFJtNrzZxRGvLmkLlhjf3DBDlOI0UBkt+DgXO3e+QQFm5Zb Y6xww4gWli2XoEyQfeJfhkDgKBP0nueMLWXzyCl4Pj/8S2K/9i5RjTU0hiY6jRz3yyL+ wlS2QqQ0eGRZ4KUFDxcOvqO2hyFB4XnME8oveHgJhobYEyApjwYfUlO7YxGHndquQH+w 3IUg== 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=VW3KnN8Acc9B0iEM/8SMTYoiqB1C69NJCO/L2fy9T0E=; b=tPi5A3bgiUcvcHBvGQE0NK+XQ7ieBzjTxTjgLXRhWVDiwufdL39n8RuquJNboCym5e pXM4S5rItacgKqHcQfn8r7XyJnQEwASUOd/0ckCwOrzJRt5kb8N4/oHy1gH+AH3Vzigo xC8SEkTMh5Uyl2zD7c/8xL5OK1yytBpxI/7npD2nyGlIybptQgLG8X6zTB9IbmCAZUEY MWcxD7826mLggSRtntsgC7Iq35MNfp9ji7NfOMbzKdaJgLuF7VPDJLeV8l83jEuDpjUQ MNLyEQPnnqqdJoNhl/Mnn7TPaIOIiwRqT0TlqOM10RGytPMY2nLu39xg81anqCWFtPlU 99Jw== X-Gm-Message-State: APjAAAVMsUBtXyhIPg6drbf9n7MeYLuCeur0Yg5ipLHviXPZLRoP3cyy 40mFe1qqzwtLgVzUvacUbyIwi0R2lDwR8xBEdyuOUQ== X-Google-Smtp-Source: APXvYqwnj4AfSHo4zaKShEx5x/xfpdyGgXeEebNiuYFmoZO8TrjatmHOB/N9SjERHBTajCv/IJuhtVvRitJpvc0lQQ8= X-Received: by 2002:a63:3006:: with SMTP id w6mr30236162pgw.440.1566406630977; Wed, 21 Aug 2019 09:57:10 -0700 (PDT) MIME-Version: 1.0 References: <20190821164730.47450-1-catalin.marinas@arm.com> <20190821164730.47450-3-catalin.marinas@arm.com> In-Reply-To: <20190821164730.47450-3-catalin.marinas@arm.com> From: Andrey Konovalov Date: Wed, 21 Aug 2019 18:57:00 +0200 Message-ID: Subject: Re: [PATCH v9 2/3] arm64: Define Documentation/arm64/tagged-address-abi.rst To: Catalin Marinas Cc: Linux ARM , Linux Memory Management List , Andrew Morton , Vincenzo Frascino , Will Deacon , Szabolcs Nagy , Kevin Brodsky , Dave P Martin , Dave Hansen , "open list:DOCUMENTATION" , linux-arch , Will Deacon Content-Type: text/plain; charset="UTF-8" X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Aug 21, 2019 at 6:47 PM Catalin Marinas wrote: > > From: Vincenzo Frascino > > On AArch64 the TCR_EL1.TBI0 bit is set by default, allowing userspace > (EL0) to perform memory accesses through 64-bit pointers with a non-zero > top byte. Introduce the document describing the relaxation of the > syscall ABI that allows userspace to pass certain tagged pointers to > kernel syscalls. > > Cc: Will Deacon > Cc: Andrey Konovalov > Cc: Szabolcs Nagy > Cc: Kevin Brodsky > Signed-off-by: Vincenzo Frascino > Co-developed-by: Catalin Marinas > Signed-off-by: Catalin Marinas Acked-by: Andrey Konovalov > --- > Documentation/arm64/tagged-address-abi.rst | 156 +++++++++++++++++++++ > 1 file changed, 156 insertions(+) > create mode 100644 Documentation/arm64/tagged-address-abi.rst > > diff --git a/Documentation/arm64/tagged-address-abi.rst b/Documentation/arm64/tagged-address-abi.rst > new file mode 100644 > index 000000000000..d4a85d535bf9 > --- /dev/null > +++ b/Documentation/arm64/tagged-address-abi.rst > @@ -0,0 +1,156 @@ > +========================== > +AArch64 TAGGED ADDRESS ABI > +========================== > + > +Authors: Vincenzo Frascino > + Catalin Marinas > + > +Date: 21 August 2019 > + > +This document describes the usage and semantics of the Tagged Address > +ABI on AArch64 Linux. > + > +1. Introduction > +--------------- > + > +On AArch64 the ``TCR_EL1.TBI0`` bit is set by default, allowing > +userspace (EL0) to perform memory accesses through 64-bit pointers with > +a non-zero top byte. This document describes the relaxation of the > +syscall ABI that allows userspace to pass certain tagged pointers to > +kernel syscalls. > + > +2. AArch64 Tagged Address ABI > +----------------------------- > + > +From the kernel syscall interface perspective and for the purposes of > +this document, a "valid tagged pointer" is a pointer with a potentially > +non-zero top-byte that references an address in the user process address > +space obtained in one of the following ways: > + > +- ``mmap()`` syscall where either: > + > + - flags have the ``MAP_ANONYMOUS`` bit set or > + - the file descriptor refers to a regular file (including those > + returned by ``memfd_create()``) or ``/dev/zero`` > + > +- ``brk()`` syscall (i.e. the heap area between the initial location of > + the program break at process creation and its current location). > + > +- any memory mapped by the kernel in the address space of the process > + during creation and with the same restrictions as for ``mmap()`` above > + (e.g. data, bss, stack). > + > +The AArch64 Tagged Address ABI has two stages of relaxation depending > +how the user addresses are used by the kernel: > + > +1. User addresses not accessed by the kernel but used for address space > + management (e.g. ``mmap()``, ``mprotect()``, ``madvise()``). The use > + of valid tagged pointers in this context is always allowed. > + > +2. User addresses accessed by the kernel (e.g. ``write()``). This ABI > + relaxation is disabled by default and the application thread needs to > + explicitly enable it via ``prctl()`` as follows: > + > + - ``PR_SET_TAGGED_ADDR_CTRL``: enable or disable the AArch64 Tagged > + Address ABI for the calling thread. > + > + The ``(unsigned int) arg2`` argument is a bit mask describing the > + control mode used: > + > + - ``PR_TAGGED_ADDR_ENABLE``: enable AArch64 Tagged Address ABI. > + Default status is disabled. > + > + Arguments ``arg3``, ``arg4``, and ``arg5`` must be 0. > + > + - ``PR_GET_TAGGED_ADDR_CTRL``: get the status of the AArch64 Tagged > + Address ABI for the calling thread. > + > + Arguments ``arg2``, ``arg3``, ``arg4``, and ``arg5`` must be 0. > + > + The ABI properties described above are thread-scoped, inherited on > + clone() and fork() and cleared on exec(). > + > + Calling ``prctl(PR_SET_TAGGED_ADDR_CTRL, PR_TAGGED_ADDR_ENABLE, 0, 0, 0)`` > + returns ``-EINVAL`` if the AArch64 Tagged Address ABI is globally > + disabled by ``sysctl abi.tagged_addr_disabled=1``. The default > + ``sysctl abi.tagged_addr_disabled`` configuration is 0. > + > +When the AArch64 Tagged Address ABI is enabled for a thread, the > +following behaviours are guaranteed: > + > +- All syscalls except the cases mentioned in section 3 can accept any > + valid tagged pointer. > + > +- The syscall behaviour is undefined for invalid tagged pointers: it may > + result in an error code being returned, a (fatal) signal being raised, > + or other modes of failure. > + > +- The syscall behaviour for a valid tagged pointer is the same as for > + the corresponding untagged pointer. > + > + > +A definition of the meaning of tagged pointers on AArch64 can be found > +in Documentation/arm64/tagged-pointers.rst. > + > +3. AArch64 Tagged Address ABI Exceptions > +----------------------------------------- > + > +The following system call parameters must be untagged regardless of the > +ABI relaxation: > + > +- ``prctl()`` other than pointers to user data either passed directly or > + indirectly as arguments to be accessed by the kernel. > + > +- ``ioctl()`` other than pointers to user data either passed directly or > + indirectly as arguments to be accessed by the kernel. > + > +- ``shmat()`` and ``shmdt()``. > + > +Any attempt to use non-zero tagged pointers may result in an error code > +being returned, a (fatal) signal being raised, or other modes of > +failure. > + > +4. Example of correct usage > +--------------------------- > +.. code-block:: c > + > + #include > + #include > + #include > + #include > + #include > + > + #define PR_SET_TAGGED_ADDR_CTRL 55 > + #define PR_TAGGED_ADDR_ENABLE (1UL << 0) > + > + #define TAG_SHIFT 56 > + > + int main(void) > + { > + int tbi_enabled = 0; > + unsigned long tag = 0; > + char *ptr; > + > + /* check/enable the tagged address ABI */ > + if (!prctl(PR_SET_TAGGED_ADDR_CTRL, PR_TAGGED_ADDR_ENABLE, 0, 0, 0)) > + tbi_enabled = 1; > + > + /* memory allocation */ > + ptr = mmap(NULL, sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE, > + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); > + if (ptr == MAP_FAILED) > + return 1; > + > + /* set a non-zero tag if the ABI is available */ > + if (tbi_enabled) > + tag = rand() & 0xff; > + ptr = (char *)((unsigned long)ptr | (tag << TAG_SHIFT)); > + > + /* memory access to a tagged address */ > + strcpy(ptr, "tagged pointer\n"); > + > + /* syscall with a tagged pointer */ > + write(1, ptr, strlen(ptr)); > + > + return 0; > + }