Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp198396rdh; Wed, 7 Feb 2024 01:57:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IG1xqR1veymhbVmZu0XYrjvC4Or0jRu8GbZpjVQxsxVMqBMHqbi1Wtjg1pBy+y8sPUyrpVB X-Received: by 2002:a17:907:7793:b0:a38:29e5:e6e6 with SMTP id ky19-20020a170907779300b00a3829e5e6e6mr3020889ejc.8.1707299853739; Wed, 07 Feb 2024 01:57:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707299853; cv=pass; d=google.com; s=arc-20160816; b=odL2TE0ohEaFOlmLYtZ/N2n9PMzwx+X5/e9IMwHf7SqZ2RSvrtdVebOaykYBc4meJC Rcup+9qHznKPLX5pE3HRwLQ2cYmgauFVIx3xhd6GtjBXzZpvx1iyBJSwsRRLUNZ8kjBR 90QnmC+s9A1h0j2K5bkdwKb/8uVRE+KrzI4JzGytI5BE9X1JLrhIttZy2XOVRb1OhYHL Pj4vy+Nerk/gnhwn6QaHojCN2wleFj4NDGcylJqzu9QVcfTlcM2NMsU+Y5mXQ3976PIj wgYFzeg67k7rtvAOQolf48l9Z5rmi/YtCVxZDpWqKVCbUnnl2BDn9vV3Y4zukodznW/1 xr5g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date:dkim-signature; bh=XBRINMVN7gGvuV2gPWLOj+4bMKi6LNNeT7KGZJCI6SQ=; fh=ZY6/uVWKCve2t93NAMq5L0ob1KOIbMd9kz7cLZTcIIE=; b=dzVJqvN89R5XK5zwAGkg5y1Fhh/hht9Vn+c7R4h8eRQ77gvletOYy5JlWxWKwuUnzh 1kARkIU6OsvveQSfYV7IrHho8dJ2rY4QOCPfI5oqryYlotNnGi8fL1tHDVB6VD7Qtp6x Pe52W91+rd8IOZnANvakCbHr9DTND54Z4WGTYS+mkRIJEvAeDS3CNge97LkiAOdQsACE 7QHrx03nBPXS5KuxlH62U0y9tWOraBTEqTv+WRPqMLPco9e/ximyLQF3XX0az6WHeZpX jRk2QxbNQJYK8pUOBexSGn9v8EQ1m9Y3EyXQTD+B7G7p6/2azk0mmBCUuzli688bps1Y ttqQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="SVrZzz/Z"; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56265-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56265-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXz1SIaJNJrE5xcpabdcgCLdkNCJAJXT3seixErSgM7UTIzmoS2facxDlkrpm9EapbFUdMARRLd6Atfe9DTB8gdQfDb0Zkp4EjEU19/mg== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id uz12-20020a170907118c00b00a379802b583si699642ejb.901.2024.02.07.01.57.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 01:57:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56265-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="SVrZzz/Z"; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56265-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56265-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 175A51F22E14 for ; Wed, 7 Feb 2024 09:56:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E6B3323777; Wed, 7 Feb 2024 09:56:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SVrZzz/Z" Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E68822374A for ; Wed, 7 Feb 2024 09:56:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707299793; cv=none; b=PV45y9kP6qgGWRAfGVDoN2rTSyNdRDJbEQDfCz2nvWev1lrsoH2qPBmLjRCE+2aWKTWHfesbrzHZINr+P2B7M1ni1TL7miQqGWvTNXxGkOq9mmmIPMufOIORLySDn/2HGt1gzlRsZXZZTJpkvLVmgJoSge4m8dNDmqJqQjS3BRs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707299793; c=relaxed/simple; bh=Ffw/WkqVLoBbaHS/K/csRcmb3UzmOEbVg81Hra2QAAI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=XdZHfCqFO0NHnfC+dLDiDQr1iNb4fBWMwnIHfXSY9zQxnnwJJyTc6yJM9+N8lW0EQUvXPCTzF7UkNlzANmJ70uEBvsg6TFAOIbRVlzyNb4soSSLnr2OKV3JEnblt3fOubg4Mmpb5GWWBS9c77auKF+xdBPakdp4HT2+NQOX/dzM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SVrZzz/Z; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-40fe3244bc6so3628875e9.1 for ; Wed, 07 Feb 2024 01:56:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707299790; x=1707904590; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=XBRINMVN7gGvuV2gPWLOj+4bMKi6LNNeT7KGZJCI6SQ=; b=SVrZzz/ZDYryvQyahStA9U/4axrnfI94BjogIoOVHju99mpXjWjuBThvr9NTf6lwat s8sEGhiojS/bSPEiXbvJUt3WIpHLHaKayiXp48jSfV3t7ZvkfGA+Pq8PDvgm6WrVTZf8 cxALDR5YhUwgeB8kH0rlB/NQlbTGjRs41R4iSevwD6d169Fm4vejivdRaopUYrWsM5MO +V4xXiK6+6jgABEEghIru5JwY8+AjYskqm+HBxa/dLNt5UyEILA59PsZXsBH231nCO2/ Idf8+eIMhT3iYOGprn9J0JMrYEseMIRIKmKHDElT9ZQQCb5TMtoSrvqIULjt5wEbNPUF xB4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707299790; x=1707904590; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XBRINMVN7gGvuV2gPWLOj+4bMKi6LNNeT7KGZJCI6SQ=; b=rfAI0g8QlndBYCOZlOrzyxQFoJG9VvdSqGr+JG4C619nCMD8mK44k4u4kBteRbqY2y 2MENeLnGCBmy66pdhTbIcwtaquWoj/PT1L0vtloDP+RZzlxPTTQzasCz/5UcEVXWOWxQ 9+6diQgYos+nYCmh8RaeO2RlO+QxKLg5EisXsqX1rQLf1n25qvSdL2u1fhPk2CyL6SB4 sOgc8FIkZjuUewHdSlODDBSw7Bkyw6rkH8BHwN9VwLDYZjcHbC1d+f+RHaSyJ1qsaT/h qaAO10ZSv1u5zA/zmiA4dkGH0xcsNVgB7TzHDDKlwpqkkpyi3SfvQhksb1md2lxCRd2m PFow== X-Gm-Message-State: AOJu0YzAZjLGNwsWAKSxdCTkmQRIjInyTes0P4qrDdJKWOpyWR+uEAeU X9e0hhhkVq8jRZxQj1f5A+hZ6Dy8r9gXHKOjv//1eQ1BxvQBo4cg2gm6ul6fLw== X-Received: by 2002:a05:600c:3d8e:b0:40f:e930:9eb8 with SMTP id bi14-20020a05600c3d8e00b0040fe9309eb8mr2810266wmb.0.1707299790036; Wed, 07 Feb 2024 01:56:30 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVqjfLsSMmbCH6BsS59K6SJoox9aCWhi8IF36lRKAreLYEX5BcNuvF7bnmTfJ92oKEpCfNlZ6fvcvjcjdWqWeaU1tjs3n1xXTxIS3QJJIKp8k3yHT6GYOGa16iDWF7BG2i1loc56bHAYphnkGBqC8mnRW1+7ZCBmtRttWB/3AoagDq4XpR4v3PPlm3Lc9zRiLGuzbuhuOChPB0+BkvcLAWtlJcO4rk1EgNJNmoJhja3gHGT6FAS/6qhzUWalGA/02JHNUUw+EzDP5FogM/I78WOx1oPcmvhU15ccozNPNbXknvQfughjhW16tWbkqCSY5yAbFgdnoImLbwEsaZ0OgPUlSXmu4T+VRBD1tXkTse9SZNWG1UaexmiluYYzfis6UinmCXLTT1vTkrkPqYnLTGN5/i9wmb+tLaKII+Rm6HNJbup1DjMCanPjObkK20HXzo07uRDWNMXGM7A Received: from elver.google.com ([2a00:79e0:9c:201:4d69:c225:7956:ca4]) by smtp.gmail.com with ESMTPSA id v9-20020a05600c470900b0040fe2d3aec4sm4704383wmo.19.2024.02.07.01.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 01:56:29 -0800 (PST) Date: Wed, 7 Feb 2024 10:56:23 +0100 From: Marco Elver To: Martin KaFai Lau Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH] bpf: Separate bpf_local_storage_lookup() fast and slow paths Message-ID: References: <20240131141858.1149719-1-elver@google.com> <5a08032b-ed4d-4429-b0a9-2736689d8c33@linux.dev> <9908bdfb-1030-4a9f-8405-3696c5d03981@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9908bdfb-1030-4a9f-8405-3696c5d03981@linux.dev> User-Agent: Mutt/2.2.12 (2023-09-09) On Tue, Feb 06, 2024 at 05:22PM -0800, Martin KaFai Lau wrote: > On 2/6/24 9:04 AM, Marco Elver wrote: > > On Mon, Feb 05, 2024 at 03:24PM -0800, Martin KaFai Lau wrote: > > [...] > > > > Or can you suggest different functions to hook to for the recursion test? > > > > > > I don't prefer to add another tracepoint for the selftest. > > > > Ok - I also checked, even though it should be a no-op, it wasn't > > (compiler generated worse code). > > I am interested to how the tracepoint generates worse code. Can you share > some details ? My guess is that it produces enough code that some inlinable functions are no longer being inlined. Specifically __bpf_task_storage_get(). > > > > > The test in "SEC("fentry/bpf_local_storage_lookup")" is testing that the > > > initial bpf_local_storage_lookup() should work and the immediate recurred > > > bpf_task_storage_delete() will fail. > > > > > > Depends on how the new slow path function will look like in v2. The test can > > > probably be made to go through the slow path, e.g. by creating a lot of task > > > storage maps before triggering the lookup. [...] > > Could you suggest how we can fix up the tests? I'm a little stuck > > because there's not much we can hook to left. > > I don't see a solution either if only the cache insertion code path is in a > traceable function. > > The prog->active counter has already been covered in another test. This test > is mostly only covering the lookup => delete recur case and the code path is > contained within the bpf storage logic. The future code review should be > able to cover. I would make an exception here and remove this test case > considering anything (e.g. tracepoint) we do here is likely to make it > worse. (more on the test removal below). > > > > > Thanks, > > -- Marco > > > > ------ >8 ------ > > > > From: Marco Elver > > Date: Tue, 30 Jan 2024 17:57:45 +0100 > > Subject: [PATCH v2] bpf: Allow compiler to inline most of > > bpf_local_storage_lookup() > > > > In various performance profiles of kernels with BPF programs attached, > > bpf_local_storage_lookup() appears as a significant portion of CPU > > cycles spent. To enable the compiler generate more optimal code, turn > > bpf_local_storage_lookup() into a static inline function, where only the > > cache insertion code path is outlined (call instruction can be elided > > entirely if cacheit_lockit is a constant expression). > > Can you share more why only putting the cache insertion code to a function > improves the larger number of maps case. In the benchmark, cacheit_lockit > should always be true and __bpf_local_storage_insert_cache() should always > be called. Keeping bpf_local_storage_lookup() smaller (even if just outlining the cache insertion) makes a difference as it allows the compiler generate more optimal code, specifically we avoid duplicating setting up calls to _raw_spin_lock/unlock. E.g. __bpf_task_storage_get is not being inlined anymore if bpf_local_storage_lookup() becomes too large (i.e. everything is up for inlining incl. cache insertion). Also, on x86 preempt builds, spin_lock/unlock aren't inlinable, so we have to pay the price of 2 calls regardless: previously for calls to _raw_spin_lock_irqsave and to _raw_spin_unlock_irqsave. However, with the version of __bpf_local_storage_insert_cache in my patch, the call to _raw_spin_unlock_irqsave is tail called, which allows the compiler to perform TCO, i.e. we still only pay the price of 2 calls: one to __bpf_local_storage_insert_cache and to _raw_spin_lock_irqsave (but no call to _raw_spin_unlock_irqsave, which can just be jumped to): <__bpf_local_storage_insert_cache>: endbr64 nopl 0x0(%rax,%rax,1) push %r15 push %r14 push %r12 push %rbx mov %rdx,%rbx mov %rsi,%r12 mov %rdi,%r15 lea 0xa8(%rdi),%r14 mov %r14,%rdi call ffffffff82323650 <_raw_spin_lock_irqsave> cmpq $0x0,0x18(%rbx) je ffffffff8127ea80 <__bpf_local_storage_insert_cache+0x40> add $0x40,%rbx movzwl 0x10e(%r12),%ecx mov %rbx,(%r15,%rcx,8) mov %r14,%rdi mov %rax,%rsi pop %rbx pop %r12 pop %r14 pop %r15 jmp ffffffff823237d0 <_raw_spin_unlock_irqrestore> <--- TCO I also compared a version where _everything_ is inlined vs. the one with __bpf_local_storage_insert_cache outlined: the one where everything is inlined nullifies any performance improvements and is significantly worse than the one with __bpf_local_storage_insert_cache outlined. [...] > > -SEC("fentry/bpf_local_storage_lookup") > > +SEC("fentry/??????????????????????????") > int BPF_PROG(on_lookup) > > Remove this BPF_PROG. > > > { > > struct task_struct *task = bpf_get_current_task_btf(); > > diff --git a/tools/testing/selftests/bpf/progs/task_ls_recursion.c b/tools/testing/selftests/bpf/progs/task_ls_recursion.c > > index 4542dc683b44..d73b33a4c153 100644 > > --- a/tools/testing/selftests/bpf/progs/task_ls_recursion.c > > +++ b/tools/testing/selftests/bpf/progs/task_ls_recursion.c > > @@ -27,7 +27,7 @@ struct { > > __type(value, long); > > } map_b SEC(".maps"); > > -SEC("fentry/bpf_local_storage_lookup") > > +SEC("fentry/??????????????????????????") > > Same here. The checks related to on_lookup in > prog_tests/task_local_storage.c need to be removed also. > > > int BPF_PROG(on_lookup) > > { > > struct task_struct *task = bpf_get_current_task_btf(); > Thanks, -- Marco