From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5BDB2C433F5 for ; Tue, 10 May 2022 05:53:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232850AbiEJF5G (ORCPT ); Tue, 10 May 2022 01:57:06 -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-Google-Smtp-Source: ABdhPJwleNG9ZC/Ct3KgCrLH5h08X2vnPz/6kODQpudSZUdjtVYZupevJjEuqDQeUvUfKRcdm7Woog== 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 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.