Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp5925007iob; Tue, 10 May 2022 06:51:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwT3tUW4obwdZKK4to3/RVSkj7ImhHZfel2eMdNpUsuk2iPytZe4XJMns+OYzp3k0Tp6oEL X-Received: by 2002:a05:6402:368c:b0:428:715f:5cf7 with SMTP id ej12-20020a056402368c00b00428715f5cf7mr19762821edb.158.1652190680546; Tue, 10 May 2022 06:51:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652190680; cv=none; d=google.com; s=arc-20160816; b=pRAmbPNi3qFjgIm+u7MCQ+J1RFD31V5kV6unhCQyHaEPSKUd9cGWSD120clhmY6Oya +yZBWQywsUyH1xBbQyJscy6raohqyPrLeCEqPGJYb+9D0j51Nt5v9+rc5if4puWej3QL 12iaCqY8KCWs8xuabeSltpLsCSfouNTZytq9hb3f0Fb/atMDNTP4abxmuzdPRBi8pxxP oEvHBvvbl917/DuxAUoygqEAJmUx3hH+uL6Kl5d+5rJlh8UXjYoxVc4xv1OgZxMPxbne FB5zzkffyl2MSEziYQPGxLQrylwWJM1XMb1VN9FQgRtodX4AYbr/wwcCADtFNkLBm/jG U6Kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id :dkim-signature; bh=hFPZlOrD/A5L9itirMqY09jIldfwhWlMFZ4kJKsxVqI=; b=yegEAzpyGuHmnbT9X2Ztdz9oya5Yy7X1UTL4Z5qHf1N2mWz+Xe6VuyV9ElZ4x0pTWO k2wL3WHMVZBkAq6RKR1rbzFmQxOsj4AXNkUkuqJi5M5oJ0hvOFSj8dQjOHBdFVmTvXIo JQuB9t5xg8HwNsbi05sPZXgLTIJLqvXQgeSZszAo48XdnMm/rWS9Nm47mCGsckKbzMh9 ol8Dy4gc8GARnVuFpw/DLPSg6V3Cev5T5Ush8rPD5FcXtE/CD3t+tUX1f4d5tgzJVb80 81N8bcgZfzM1GhfCOxlerBu9ZP6uYGC2DC5zFT8VY6i1QhspQdSgR/c+zzxvTEoGjL91 mOVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=8VkXDXho; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l28-20020a50d6dc000000b00425d43b058csi16948502edj.368.2022.05.10.06.50.56; Tue, 10 May 2022 06:51:20 -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=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=8VkXDXho; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236870AbiEJF5F (ORCPT + 99 others); Tue, 10 May 2022 01:57:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233119AbiEJF5C (ORCPT ); Tue, 10 May 2022 01:57:02 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2D6C119227 for ; Mon, 9 May 2022 22:53:04 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id j6so14056249pfe.13 for ; Mon, 09 May 2022 22:53:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:to:cc:references :from:in-reply-to:content-transfer-encoding; bh=hFPZlOrD/A5L9itirMqY09jIldfwhWlMFZ4kJKsxVqI=; b=8VkXDXhovv95HK+Lp425GS8WIyMwtuBDJ+VdAgoNHyV0gKw4uSSV+n6SQ911T2Lwjq v+kAkSwuVo/yFISElEEM4BLaVA8gahkAsfSm/Douq1zdhCnZ/HYvXaoDdbITN9phcREs FbjsH4vIX0Hzp8FsQoE3UoHsckt00UnMA+R05y/zdOXNcfKtAyKG5BLnizlzvXi0NlMu S9dx/TgBeLbJjnc9Ql6dBI7qQRvKmAjSNx0glxly1PzZer2lkouxXcOvAQVshiPCuHWa mSIJGc6B/zZFKh2RlZu/5UAxGPdFWhCbXOSYXf/L5x7AYNsLQg3BObhWT6IdMUZR2TWi ii9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :to:cc:references:from:in-reply-to:content-transfer-encoding; bh=hFPZlOrD/A5L9itirMqY09jIldfwhWlMFZ4kJKsxVqI=; b=NQVnuUohI8U+BOWDFsZS+U+pdH0Z5nA51AbDNqYCfiaqva7MfI5bzUDHW6g09E1IOo vprtxTKZ6NU469rxj+YXlhPrCjMtklh2vrc/goMZwDyNG7tBCkimrOmpKcFizbGZIUnh h7/kWADnoMAHHK3qSPKsskM80m9Bhc3a1wLowa5eGllwXvOeqB84qcdWJWK+y9tbXYnY oADhN4+M8z5eYctYEWhxlmOF/kgxcjv78yVWahcNqXswqF0lybA0PrtzQvFtWaz0TpQo D5SVAk+G4adgDZ+xjyOpAmv8/cna1AR+w+UdA9L3eEOljsFR2SeYG/uJuy57AvItOk/1 2KXw== X-Gm-Message-State: AOAM533TRbQAktMfC/A98bxbNQuOxawzc9cwapfp1LqxW79E9ufsL9q8 TXn4FIVhZhMjQA02Q6rhI2LXxA== X-Received: by 2002:a63:1a01:0:b0:3c2:1974:1686 with SMTP id a1-20020a631a01000000b003c219741686mr15736997pga.595.1652161984312; Mon, 09 May 2022 22:53:04 -0700 (PDT) Received: from [10.86.119.184] ([139.177.225.231]) by smtp.gmail.com with ESMTPSA id h27-20020a056a00001b00b0050dc76281d7sm9960882pfk.177.2022.05.09.22.52.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 May 2022 22:53:03 -0700 (PDT) Message-ID: Date: Tue, 10 May 2022 13:52:52 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.3.2 Subject: Re: [External] Re: [PATCH bpf-next] bpf: add bpf_map_lookup_percpu_elem for percpu map To: Alexei Starovoitov Cc: Yosry Ahmed , Andrii Nakryiko , 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 , Xiongchun Duan , Muchun Song , Dongdong Wang , Cong Wang , Chengming Zhou References: <20220507024840.42662-1-zhoufeng.zf@bytedance.com> From: Feng Zhou In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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 在 2022/5/10 上午11:15, Alexei Starovoitov 写道: > On Mon, May 9, 2022 at 7:41 PM Feng Zhou wrote: >> 在 2022/5/10 上午9:04, Yosry Ahmed 写道: >>> 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. >>> >> Regarding data security, I think users need to consider before using it, >> such >> as hook enqueue_task_fair, the function itself takes the rq lock of the >> corresponding cpu, there is no problem, and the kernel only provides a >> method, >> like bpf_per_cpu_ptr and bpf_this_cpu_ptr, data security needs to be >> guaranteed >> by users in different scenarios, such as using bpf_spin_lock. > Right. The new helper looks useful and is safe. > Please add a selftest and respin. Ok, will do. Thanks.