Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp4935739rwb; Tue, 17 Jan 2023 07:17:02 -0800 (PST) X-Google-Smtp-Source: AMrXdXuMZ0irwLkhpHK3pT3tdCMtNcLmdfEBV/988eKd1D6e11OX9mwjNK1GbrwC11vwQOkExZqu X-Received: by 2002:a17:906:9bd4:b0:7b5:73aa:9984 with SMTP id de20-20020a1709069bd400b007b573aa9984mr3164564ejc.14.1673968622311; Tue, 17 Jan 2023 07:17:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673968622; cv=none; d=google.com; s=arc-20160816; b=jz4rogXsak4nvql+bUOrnEbg5O20JSQw4JbtXpfKniXm50YuMkGuQqxPSlYs6Q8eRZ rL1ktBRQWizKW7AIVt1V9mIc034FYioyoWhDBvNKO3SGoiNxhBK0DbiRp4hb7VgNA+4q 7LuZTdFdzP+MXMsZJOpivVYairzr+H2g4LkvrU2+FZwidjIQauvErGQCdWcS9N0b7g61 cHP6wMKS+HPaeckELPBtEF6NnKc6yOcDCT2b338i08kH7OYKZFS2E13Hwl5gRnQiNLpX JS9fwV5aXyhph2bFr+C/vCT24wKYa5jzvinfJwYxT4CUVcpyxOtxSjogK/cijmNODRku 6hEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=7mcvbJBSR6u09x7A6qVwIIU9taGlku+5WOymJeSEZwk=; b=Ic2mbSoZf9fheypJg4ywEuelsrUL1egrTYCCM3sp9M9HSWkeQxMdA4og2qpjKqa3Tz iqQ51bpvZo2Uo3TcQuhtQH2YSiE8q1oXeX9Kf2vDJpoJVU1n3Hvrk0rCqFRPixFrCGuk 8YA40dNkuU/DTiEG8HFvG0mys2NF84X5pzHOnNx7hvmEs8XoLaLAVtb3hjQAW4ssVrSr dLV3mcgQ3vNM/SWekJtLWVmcux2QAtiR2qS5xf2FYxjuxhw/vd3SIgHGuI7CSK20IEyu pQrPI5fcCzIWc8zZT88U++LqNXrwy1rlO4sze1wimwogZT0zf67M2edtGxMKlQha1VIM h5UA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=Gv3i9AbR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ho13-20020a1709070e8d00b0084d44edc334si27889581ejc.204.2023.01.17.07.16.49; Tue, 17 Jan 2023 07:17:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=Gv3i9AbR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230013AbjAQPEF (ORCPT + 50 others); Tue, 17 Jan 2023 10:04:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232865AbjAQPCf (ORCPT ); Tue, 17 Jan 2023 10:02:35 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC531402DB for ; Tue, 17 Jan 2023 07:02:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; 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=7mcvbJBSR6u09x7A6qVwIIU9taGlku+5WOymJeSEZwk=; b=Gv3i9AbRIJBg3eFPdyeLQDp5/S oVHwiq33nOrCpSS9rGbhxXSuKjKcA9FjiTmrpkl19v0GU3sB10oKC9ZLJ4sT6U8rhpTlVBWyv0U+o vbYILwFMjUybHxm/CvBkuz+tONRGhX7BFrCELsn3ZsxTj4y6zA8IRpJ6GgSZXHqGABamH6xamJ7h1 2IFad2LNxBkKU771V8krXmPXSBF1UMcluuHquFANqOruYjXfs6qpQ59A2UP00an6JtjeKCc19XGga 8yglf1JR6S8WgK7K3YoNYTjrOXbjlIs5Zb+M5bofQ+JRZr9VLV5y4HljOaAArETsEKST8beYrQDGo 9yYQ5jLg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHnTi-009lfG-QP; Tue, 17 Jan 2023 15:02:24 +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) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id E90F1300137; Tue, 17 Jan 2023 16:02:06 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id C51FB20B2BF0D; Tue, 17 Jan 2023 16:02:06 +0100 (CET) Date: Tue, 17 Jan 2023 16:02:06 +0100 From: Peter Zijlstra To: "Kirill A. Shutemov" Cc: Dave Hansen , Andy Lutomirski , x86@kernel.org, Kostya Serebryany , Andrey Ryabinin , Andrey Konovalov , Alexander Potapenko , Taras Madan , Dmitry Vyukov , "H . J . Lu" , Andi Kleen , Rick Edgecombe , Bharata B Rao , Jacob Pan , Ashok Raj , Linus Torvalds , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sami Tolvanen , ndesaulniers@google.com, joao@overdrivepizza.com Subject: Re: [PATCHv14 08/17] x86/mm: Reduce untagged_addr() overhead until the first LAM user Message-ID: References: <20230111123736.20025-1-kirill.shutemov@linux.intel.com> <20230111123736.20025-9-kirill.shutemov@linux.intel.com> <20230117135703.voaumisreld7crfb@box> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230117135703.voaumisreld7crfb@box> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 17, 2023 at 04:57:03PM +0300, Kirill A. Shutemov wrote: > On Tue, Jan 17, 2023 at 02:05:22PM +0100, Peter Zijlstra wrote: > > On Wed, Jan 11, 2023 at 03:37:27PM +0300, Kirill A. Shutemov wrote: > > > > > #define __untagged_addr(untag_mask, addr) > > > u64 __addr = (__force u64)(addr); \ > > > - s64 sign = (s64)__addr >> 63; \ > > > - __addr &= untag_mask | sign; \ > > > + if (static_branch_likely(&tagged_addr_key)) { \ > > > + s64 sign = (s64)__addr >> 63; \ > > > + __addr &= untag_mask | sign; \ > > > + } \ > > > (__force __typeof__(addr))__addr; \ > > > }) > > > > > > #define untagged_addr(addr) __untagged_addr(current_untag_mask(), addr) > > > > Is the compiler clever enough to put the memop inside the branch? > > Hm. You mean current_untag_mask() inside static_branch_likely()? > > But it is preprocessor who does this, not compiler. So, yes, the memop is > inside the branch. > > Or I didn't understand your question. Nah, call it a pre-lunch dip, I overlooked the whole CPP angle -- d'0h. That said, I did just put it through a compiler to see wth it did and it is pretty gross: GCC-12.2: 0000 00000000000023b0 : 0000 23b0: 48 89 fa mov %rdi,%rdx 0003 23b3: eb 76 jmp 242b 0005 23b5: 65 48 8b 0d 00 00 00 00 mov %gs:0x0(%rip),%rcx # 23bd 23b9: R_X86_64_PC32 tlbstate_untag_mask-0x4 000d 23bd: 48 89 f8 mov %rdi,%rax 0010 23c0: 48 c1 f8 3f sar $0x3f,%rax 0014 23c4: 48 09 c8 or %rcx,%rax 0017 23c7: 48 21 f8 and %rdi,%rax 001a 23ca: 48 b9 00 f0 ff ff ff 7f 00 00 movabs $0x7ffffffff000,%rcx 0024 23d4: 48 39 f1 cmp %rsi,%rcx 0027 23d7: 72 14 jb 23ed 0029 23d9: 48 29 f1 sub %rsi,%rcx 002c 23dc: be 01 00 00 00 mov $0x1,%esi 0031 23e1: 48 39 c1 cmp %rax,%rcx 0034 23e4: 72 07 jb 23ed 0036 23e6: 89 f0 mov %esi,%eax 0038 23e8: e9 00 00 00 00 jmp 23ed 23e9: R_X86_64_PLT32 __x86_return_thunk-0x4 003d 23ed: 65 48 8b 04 25 00 00 00 00 mov %gs:0x0,%rax 23f2: R_X86_64_32S pcpu_hot 0046 23f6: 48 89 90 68 0b 00 00 mov %rdx,0xb68(%rax) 004d 23fd: be 01 00 00 00 mov $0x1,%esi 0052 2402: bf 0b 00 00 00 mov $0xb,%edi 0057 2407: 48 c7 80 78 0b 00 00 06 00 00 00 movq $0x6,0xb78(%rax) 0062 2412: 48 c7 80 70 0b 00 00 0e 00 00 00 movq $0xe,0xb70(%rax) 006d 241d: e8 00 00 00 00 call 2422 241e: R_X86_64_PLT32 force_sig_fault-0x4 0072 2422: 31 f6 xor %esi,%esi 0074 2424: 89 f0 mov %esi,%eax 0076 2426: e9 00 00 00 00 jmp 242b 2427: R_X86_64_PLT32 __x86_return_thunk-0x4 007b 242b: 48 89 f8 mov %rdi,%rax 007e 242e: eb 9a jmp 23ca Note the stupid jump to the end and back. Not all sites do this mind you, but a fair number seem to do it. Let me try llvm to see if it is any smarter. CLANG-16: 0000 0000000000002d50 : 0000 2d50: 41 57 push %r15 0002 2d52: 41 56 push %r14 0004 2d54: 41 54 push %r12 0006 2d56: 53 push %rbx 0007 2d57: 48 89 f3 mov %rsi,%rbx 000a 2d5a: 48 89 fa mov %rdi,%rdx 000d 2d5d: 49 89 fe mov %rdi,%r14 0010 2d60: eb 15 jmp 2d77 0012 2d62: 48 89 d0 mov %rdx,%rax 0015 2d65: 48 c1 f8 3f sar $0x3f,%rax 0019 2d69: 65 4c 8b 35 00 00 00 00 mov %gs:0x0(%rip),%r14 # 2d71 2d6d: R_X86_64_PC32 tlbstate_untag_mask-0x4 0021 2d71: 49 09 c6 or %rax,%r14 0024 2d74: 49 21 d6 and %rdx,%r14 0027 2d77: f3 0f 1e fa endbr64 002b 2d7b: 49 bf 00 f0 ff ff ff 7f 00 00 movabs $0x7ffffffff000,%r15 0035 2d85: 4d 89 fc mov %r15,%r12 0038 2d88: 49 29 dc sub %rbx,%r12 003b 2d8b: 72 05 jb 2d92 003d 2d8d: 4d 39 f4 cmp %r14,%r12 0040 2d90: 73 34 jae 2dc6 0042 2d92: 65 48 8b 05 00 00 00 00 mov %gs:0x0(%rip),%rax # 2d9a 2d96: R_X86_64_PC32 pcpu_hot-0x4 004a 2d9a: 48 c7 80 78 0b 00 00 06 00 00 00 movq $0x6,0xb78(%rax) 0055 2da5: 48 89 90 68 0b 00 00 mov %rdx,0xb68(%rax) 005c 2dac: 48 c7 80 70 0b 00 00 0e 00 00 00 movq $0xe,0xb70(%rax) 0067 2db7: bf 0b 00 00 00 mov $0xb,%edi 006c 2dbc: be 01 00 00 00 mov $0x1,%esi 0071 2dc1: e8 00 00 00 00 call 2dc6 2dc2: R_X86_64_PLT32 force_sig_fault-0x4 0076 2dc6: 4d 39 f4 cmp %r14,%r12 0079 2dc9: 0f 93 c1 setae %cl 007c 2dcc: 49 39 df cmp %rbx,%r15 007f 2dcf: 0f 93 c0 setae %al 0082 2dd2: 20 c8 and %cl,%al 0084 2dd4: 5b pop %rbx 0085 2dd5: 41 5c pop %r12 0087 2dd7: 41 5e pop %r14 0089 2dd9: 41 5f pop %r15 008b 2ddb: e9 00 00 00 00 jmp 2de0 <__pfx_get_gate_vma> 2ddc: R_X86_64_PLT32 __x86_return_thunk-0x4 Well, it got the untag right, but OMG.. :-( Joao, Sami, any idea why it put an ENDBR in there?