From mboxrd@z Thu Jan 1 00:00:00 1970 From: Szabolcs Nagy Subject: Re: [PATCH v17 15/15] selftests, arm64: add a selftest for passing tagged pointers to kernel Date: Wed, 12 Jun 2019 12:30:36 +0000 Message-ID: <7cd942c0-d4c1-0cf4-623a-bce6ef14d992@arm.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: Content-Language: en-US Content-ID: <2FA45C6A3FFA994DA2213028275DFA44@eurprd08.prod.outlook.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=m.gmane.org@lists.infradead.org To: Andrey Konovalov , "linux-arm-kernel@lists.infradead.org" , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , "amd-gfx@lists.freedesktop.org" , "dri-devel@lists.freedesktop.org" , "linux-rdma@vger.kernel.org" , "linux-media@vger.kernel.org" , "kvm@vger.kernel.org" , "linux-kselftest@vger.kernel.org" Cc: Mark Rutland , Catalin Marinas , Will Deacon , Kostya Serebryany , Khalid Aziz , Felix Kuehling , Vincenzo Frascino nd , Jacob Bramley , Leon Romanovsky , Christoph Hellwig , Jason Gunthorpe , Dave P Martin , Evgeniy Stepanov , Kevin Brodsky , Kees Cook , Ruben Ayrapetyan , Ramana Radhakrishnan , Alex Williamson , Mauro Carvalho Chehab , Dmitry Vyukov , Greg Kroah-Hartman , Yishai Hadas List-Id: linux-rdma@vger.kernel.org On 12/06/2019 12:43, Andrey Konovalov wrote: > --- /dev/null > +++ b/tools/testing/selftests/arm64/tags_lib.c > @@ -0,0 +1,62 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +#include > +#include > + > +#define TAG_SHIFT (56) > +#define TAG_MASK (0xffUL << TAG_SHIFT) > + > +#define PR_SET_TAGGED_ADDR_CTRL 55 > +#define PR_GET_TAGGED_ADDR_CTRL 56 > +#define PR_TAGGED_ADDR_ENABLE (1UL << 0) > + > +void *__libc_malloc(size_t size); > +void __libc_free(void *ptr); > +void *__libc_realloc(void *ptr, size_t size); > +void *__libc_calloc(size_t nmemb, size_t size); this does not work on at least musl. the most robust solution would be to implement the malloc apis with mmap/munmap/mremap, if that's too cumbersome then use dlsym RTLD_NEXT (although that has the slight wart that in glibc it may call calloc so wrapping calloc that way is tricky). in simple linux tests i'd just use static or stack allocations or mmap. if a generic preloadable lib solution is needed then do it properly with pthread_once to avoid races etc. > + > +static void *tag_ptr(void *ptr) > +{ > + static int tagged_addr_err = 1; > + unsigned long tag = 0; > + > + /* > + * Note that this code is racy. We only use it as a part of a single > + * threaded test application. Beware of using in multithreaded ones. > + */ > + if (tagged_addr_err == 1) > + tagged_addr_err = prctl(PR_SET_TAGGED_ADDR_CTRL, > + PR_TAGGED_ADDR_ENABLE, 0, 0, 0); > + > + if (!ptr) > + return ptr; > + if (!tagged_addr_err) > + tag = rand() & 0xff; > + > + return (void *)((unsigned long)ptr | (tag << TAG_SHIFT)); > +} > + > +static void *untag_ptr(void *ptr) > +{ > + return (void *)((unsigned long)ptr & ~TAG_MASK); > +} > + > +void *malloc(size_t size) > +{ > + return tag_ptr(__libc_malloc(size)); > +} ...