Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp5698807iob; Tue, 10 May 2022 01:17:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyeGuxTVq07LaMl1z1LbEbaW3XTP8yCUfJN49qJ9JTvXTPOYQvvuSKI8jLQm291lIMG0PuE X-Received: by 2002:a17:906:7e50:b0:6f4:3767:b70c with SMTP id z16-20020a1709067e5000b006f43767b70cmr18308676ejr.433.1652170673618; Tue, 10 May 2022 01:17:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652170673; cv=none; d=google.com; s=arc-20160816; b=MXZ7ZzN+cnYonFlXq15bEe0guQSkAzuAufx0gKceVRE/pcx9V3NKXM+BbOdR2+NfU4 Go93347aPY4Wgr+Ztb0558qK8GDs5x4dv76L+0ItReM0CpelvH2Gk4LA4Ehk0SARif5i I3nBd6Og7pP23bQrXWLPTvNay0YhaF6FcR2U9XhLl0FGR/2IIdZijETmUUg7qOwGyNYQ LO0siL5nPAEE4DXitfOwUbkZ3yFvj2XFMlQw9uyx+nOSNSc9K0g/sXbwoLDSLLe3Sy20 6XQlNOuh7CkgyEZCsaG3xjxr4c80Y2+5xPvrOUKlknkoI8ykrNXq3updmkIMDU5JM0Sc PJzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=KWU2Ie7CLoGUJm4eKIboU9cuPF4R+pcVY2OmQlQkvqM=; b=t3GOysJrHM0dUEXu28Am6WBPNCZDaDoTe7vjwdi5ewJc2ddbUT7MnZ2V6zHAn9PPUS O4QUR7AnL4J8/hvTv0qW5wEppF1pRx6Ivf7fdYczSSWmrVJWdEjq6SIWtx0t2XpXIfop DAt3aVXNzGfZGm4l0PPsyuPFKRCJfJpfkqQvLLheTrF3GGcAAS+ZGekxZ9Tm/JbUO/Wn QGiTV7f3tPUlWPUxiA8L+9TD/sWIdyBTTAT2flle/zjuFyiddHkuYbR7sgqdp/qZhX2C CqFeZ51muWuO+wRVHu6z2Ew43IPXElQoPWIYzAzwxbzr8mXO5x0tIpmqaZkVTfhPqgF2 BEYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=otwm5O7b; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 14-20020a170906310e00b006f4da4e73e9si15957670ejx.490.2022.05.10.01.17.27; Tue, 10 May 2022 01:17:53 -0700 (PDT) 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=@google.com header.s=20210112 header.b=otwm5O7b; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234077AbiEJBJL (ORCPT + 99 others); Mon, 9 May 2022 21:09:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233260AbiEJBJJ (ORCPT ); Mon, 9 May 2022 21:09:09 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00BC3297405 for ; Mon, 9 May 2022 18:05:11 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id o12-20020a1c4d0c000000b00393fbe2973dso468823wmh.2 for ; Mon, 09 May 2022 18:05:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=KWU2Ie7CLoGUJm4eKIboU9cuPF4R+pcVY2OmQlQkvqM=; b=otwm5O7b+Cs+c/aJt5BGIr83OuJhOFQ+EMC0G8wlyaaPZowgDFt4HzUfoBEvfjN2aY Yv4/Jg9DgzI5XFWIElJz9Tb7HhvTQRjoc+y5rrgMW7Rn9cwKP24ucRVlHfh88sRpAFZH fKMYCJT4wx/aqhlflEQ3xcCLS5OeGO3gaVMUkhVwQDLhzrERSs2lTGN9YrRdp+Mmo2yr mdfz0IqlWT0mzXP+aoCd4SAADN0+/Jz7sk3mKoI01VjDPDte/svmeTmqEWlT5AYyc5zF 7ZKdydGd3+TR7wyMLg6FCEOlo5vZ/+/NRZYm1D0tOaODkAV90tX/5L9EAl8JwyblQd4C /JIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=KWU2Ie7CLoGUJm4eKIboU9cuPF4R+pcVY2OmQlQkvqM=; b=6qPUSnplqHZz51yz2LQlM+n8b1rBSmWu/q2VyD7hv78+4UAW9w1KFF68d1MsHH2rPi cjqYoTioW7NdzbKXC4K5wCsc6lxpkZTaFcMx09aX/zS8Ws03qD21CwfHz16s4E5rWjL5 MM6njZdD98p/eKQh/MvTSWl74Vtm9GYLFwfOGY/MKOCgmX6fISrpY+nF+b2MJgJPPWIC IL0tsS5GTl1Dua8YjTkk25psKSQu5WbA/No1G45uX1TIM4eFFTIbGC7/2bsMS2qDK5ds r+5V/+qcuRNsbCALTPQ7I/Ex/4Mxotpe4LB+4T43/EuxpOpioLgVjVkvsxMtSwub42Sc yvcw== X-Gm-Message-State: AOAM530YZoJd5R0UGaN3S7EMoavBcJuc7o7z2mQmO3nwxOj51ytQA6dv fIBxb7PbrvzznH/wDbWHDw1lmj0LwsLB/iThycQC+A== X-Received: by 2002:a05:600c:4ecc:b0:394:790d:5f69 with SMTP id g12-20020a05600c4ecc00b00394790d5f69mr19448042wmq.196.1652144710286; Mon, 09 May 2022 18:05:10 -0700 (PDT) MIME-Version: 1.0 References: <20220507024840.42662-1-zhoufeng.zf@bytedance.com> In-Reply-To: From: Yosry Ahmed Date: Mon, 9 May 2022 18:04:34 -0700 Message-ID: Subject: Re: [PATCH bpf-next] bpf: add bpf_map_lookup_percpu_elem for percpu map To: Andrii Nakryiko Cc: Feng zhou , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin Lau , Song Liu , Yonghong Song , john fastabend , KP Singh , Steven Rostedt , Ingo Molnar , Jiri Olsa , Dave Marchevsky , Joanne Koong , Geliang Tang , Networking , bpf , open list , duanxiongchun@bytedance.com, Muchun Song , Dongdong Wang , Cong Wang , zhouchengming@bytedance.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable 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 Mon, May 9, 2022 at 5:34 PM Andrii Nakryiko wrote: > > On Fri, May 6, 2022 at 7:49 PM Feng zhou wrote: > > > > From: Feng Zhou > > > > Trace some functions, such as enqueue_task_fair, need to access the > > corresponding cpu, not the current cpu, and bpf_map_lookup_elem percpu map > > cannot do it. So add bpf_map_lookup_percpu_elem to accomplish it for > > percpu_array_map, percpu_hash_map, lru_percpu_hash_map. > > > > The implementation method is relatively simple, refer to the implementation > > method of map_lookup_elem of percpu map, increase the parameters of cpu, and > > obtain it according to the specified cpu. > > > > I don't think it's safe in general to access per-cpu data from another > CPU. I'd suggest just having either a ARRAY_OF_MAPS or adding CPU ID > as part of the key, if you need such a custom access pattern. I actually just sent an RFC patch series containing a similar patch for the exact same purpose. There are instances in the kernel where per-cpu data is accessed from other cpus (e.g. mem_cgroup_css_rstat_flush()). I believe, like any other variable, percpu data can be safe or not safe to access, based on the access pattern. It is up to the user to coordinate accesses to the variable. For example, in my use case, one of the accessors only reads percpu values of different cpus, so it should be safe. If a user accesses percpu data of another cpu without guaranteeing safety, they corrupt their own data. I understand that the main purpose of percpu data is lockless (and therefore fast) access, but in some use cases the user may be able to safely (and locklessly) access the data concurrently. > > > Signed-off-by: Feng Zhou > > --- > > include/linux/bpf.h | 2 ++ > > include/uapi/linux/bpf.h | 9 +++++++++ > > kernel/bpf/arraymap.c | 15 +++++++++++++++ > > kernel/bpf/core.c | 1 + > > kernel/bpf/hashtab.c | 32 ++++++++++++++++++++++++++++++++ > > kernel/bpf/helpers.c | 18 ++++++++++++++++++ > > kernel/bpf/verifier.c | 17 +++++++++++++++-- > > kernel/trace/bpf_trace.c | 2 ++ > > tools/include/uapi/linux/bpf.h | 9 +++++++++ > > 9 files changed, 103 insertions(+), 2 deletions(-) > > > > [...]