Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1996873pxb; Wed, 30 Mar 2022 14:15:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwAISanRDg/5+O0MzowM9SMjGJBdyY24JSP55I1JNiAIOPhNUn6CK4EZfWvZZCt1frp0Ttw X-Received: by 2002:a05:6830:3150:b0:5cd:a820:3966 with SMTP id c16-20020a056830315000b005cda8203966mr4396511ots.223.1648674929090; Wed, 30 Mar 2022 14:15:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648674929; cv=none; d=google.com; s=arc-20160816; b=lKIL4pEkCz/fX6Ixwj8HOsBWfhllMSh4UjXVWnUuL7WiFpOdtMWZYAdMqFmvFe2qxJ J5QCVLcVnKxdIHEFDnKbu/lwXdheWk8FQjh1xYKbkGEYEvbNfnLEscfnQ0hyCPzTDgH6 AVNfo/WVg5VX0zhrEAc7SaVGGSxZIb98HzajkmNxRmQXf5eSt2VRlCi4/ZmT3KKbN7qs FWGy+f5gPlI1+YfqIsuosiS1gGp9wgAF/PjrHOU3DDuggEeL1BbEer40YKpMnUx1rpIe awZU4T+34hkdvnHp6UjqjeK9uPcuOKEW7Geq9MjB5fgU86nCUMk8aSpFnJfTwbzw1cVW 1vMg== 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=2noiAB4JhD//sQbtMNMDRU7L/9/z/AP4LRXU3Z5+JvY=; b=RsMofr5S/hcOagbgoLNBQ6rYRQuuQViliVJHgML4mwgqNhV4uekC7XZEZSsOPxgBVu mt7/aO3L42SUUNOa/C63TgO2xdJg4bl4E0obzWXKYEp4w6t2dXDLZq7UyLuCNEahGqdF ojPHu30oZjyPlByW90VbBXJhvY3dBUoHfnpb/iklqv4VzOJUkLgvzU402mMNHq4Xw+q/ yqyTR0JO2WT+LPMaiKRmrfJ4tHAieYH7y1Arsd/EixJw5gNADFOJAeM4VyYVeaxLI/OP ki2hY4Uncxcy1bfo7s6NsSG/M4m1Do8F+KVBdnAOkODjwQANz4YDTJzKOWLVJU4za1AI vnMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=otNRkeUJ; 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 w16-20020a9d77d0000000b005cb2fc13879si19107169otl.245.2022.03.30.14.15.15; Wed, 30 Mar 2022 14:15:29 -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=otNRkeUJ; 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 S240319AbiC2Rpn (ORCPT + 99 others); Tue, 29 Mar 2022 13:45:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240321AbiC2Rph (ORCPT ); Tue, 29 Mar 2022 13:45:37 -0400 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9940D1F046C for ; Tue, 29 Mar 2022 10:43:54 -0700 (PDT) Received: by mail-qv1-xf29.google.com with SMTP id hu11so14953289qvb.7 for ; Tue, 29 Mar 2022 10:43:54 -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=2noiAB4JhD//sQbtMNMDRU7L/9/z/AP4LRXU3Z5+JvY=; b=otNRkeUJnJtKzUMsBnrSWYanbhWeN1Nr/I/fVv2Rq8w/YmJEhFqXr6Rdld/RrqKqq/ Q+QQ8gzlwSLGScMxlQeoDphNNODvqG7q/KBtSzPMBramlM33AO68I36uzWljETzdzbWY YsnqA+fY31Sm+w7+q5jq4FGCjr6DS7m2Wlnf1UQ2NgeTlzm8JSaRD+c8FvhlhqMCTFU6 WDDWdB+FuCgV80VJHGOOxuoA3iQQa3f+x8z1Q4d4nKPsoKB8P7IkUeG7O41qZTpKYZkC Xa4ZWwSgDht52/RJdVxsEs9kRTRtDL6yjBmtPGOfqbNNYY6pasj9PcjTjlNc+lfWIWNb nKMA== 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=2noiAB4JhD//sQbtMNMDRU7L/9/z/AP4LRXU3Z5+JvY=; b=2KvvTFppWs6C0Ak9BfPM4tx0ZucOe0Tw2XmC2hX+lfsilA2SvBakJLlxX1cAjrBqIr wSv07M2odG94S9P+NPh6sTsAe8+SKJRxhzzufvZIMqtFHc2Yxk3Fk9TMLxPYfM2F+zFg lZUellf79YZBY+2GUe1v/LxWaOkYzX458lAlY8satn42xDxbbfknFomtK21vnlRPYcm7 MaJh+BBpB0EIyiVbfpixStmAf5QwiXJfeQvUJcTMQYdvVyYc01HZkJbdMwO9QkNJjr3u F8CSY63GNvFAoz3MNt92WRlJSwo0Gid7twwQSlJMhgToEElbYnSW1Lv3siPviZGCJZ2a hM7g== X-Gm-Message-State: AOAM5317vPGx5O0RshqqGeplx9BPBbM4Jfz2xOVO3he0+ddcDIunNgcS KV9rY2xw7xoTBjc6/Iyo/mQQKbeDoXCT4pE4K48h2Q== X-Received: by 2002:a05:6214:d8d:b0:441:686e:f2c6 with SMTP id e13-20020a0562140d8d00b00441686ef2c6mr27348549qve.44.1648575833484; Tue, 29 Mar 2022 10:43:53 -0700 (PDT) MIME-Version: 1.0 References: <20220324234123.1608337-1-haoluo@google.com> <9cdf860d-8370-95b5-1688-af03265cc874@fb.com> <20220329093753.26wc3noelqrwlrcj@apollo.legion> In-Reply-To: <20220329093753.26wc3noelqrwlrcj@apollo.legion> From: Hao Luo Date: Tue, 29 Mar 2022 10:43:42 -0700 Message-ID: Subject: Re: [PATCH RFC bpf-next 0/2] Mmapable task local storage. To: Kumar Kartikeya Dwivedi Cc: Yonghong Song , Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , KP Singh , Martin KaFai Lau , Song Liu , bpf@vger.kernel.org, linux-kernel@vger.kernel.org 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=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, Mar 29, 2022 at 2:37 AM Kumar Kartikeya Dwivedi wrote: > > On Mon, Mar 28, 2022 at 11:16:15PM IST, Hao Luo wrote: > > On Mon, Mar 28, 2022 at 10:39 AM Hao Luo wrote: > > > > > > Hi Yonghong, > > > > > > On Fri, Mar 25, 2022 at 12:16 PM Yonghong Song wrote: > > > > > > > > On 3/24/22 4:41 PM, Hao Luo wrote: > > > > > Some map types support mmap operation, which allows userspace to > > > > > communicate with BPF programs directly. Currently only arraymap > > > > > and ringbuf have mmap implemented. > > > > > > > > > > However, in some use cases, when multiple program instances can > > > > > run concurrently, global mmapable memory can cause race. In that > > > > > case, userspace needs to provide necessary synchronizations to > > > > > coordinate the usage of mapped global data. This can be a source > > > > > of bottleneck. > > > > > > > > I can see your use case here. Each calling process can get the > > > > corresponding bpf program task local storage data through > > > > mmap interface. As you mentioned, there is a tradeoff > > > > between more memory vs. non-global synchronization. > > > > > > > > I am thinking that another bpf_iter approach can retrieve > > > > the similar result. We could implement a bpf_iter > > > > for task local storage map, optionally it can provide > > > > a tid to retrieve the data for that particular tid. > > > > This way, user space needs an explicit syscall, but > > > > does not need to allocate more memory than necessary. > > > > > > > > WDYT? > > > > > > > > > > Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap: > > > > > > - mmap prevents the calling task from reading other task's value. > > > Using bpf_iter, one can pass other task's tid to get their values. I > > > assume there are two potential ways of passing tid to bpf_iter: one is > > > to use global data in bpf prog, the other is adding tid parameterized > > > iter_link. For the first, it's not easy for unpriv tasks to use. For > > > the second, we need to create one iter_link object for each interested > > > tid. It may not be easy to use either. > > > > > > - Regarding adding an explicit syscall. I thought about adding > > > write/read syscalls for task local storage maps, just like reading > > > values from iter_link. Writing or reading task local storage map > > > updates/reads the current task's value. I think this could achieve the > > > same effect as mmap. > > > > > > > Actually, my use case of using mmap on task local storage is to allow > > userspace to pass FDs into bpf prog. Some of the helpers I want to add > > need to take an FD as parameter and the bpf progs can run > > concurrently, thus using global data is racy. Mmapable task local > > storage is the best solution I can find for this purpose. > > > > Song also mentioned to me offline, that mmapable task local storage > > may be useful for his use case. > > > > I am actually open to other proposals. > > > > You could also use a syscall prog, and use bpf_prog_test_run to update local > storage for current. Data can be passed for that specific prog invocation using > ctx. You might have to enable bpf_task_storage helpers in it though, since they > are not allowed to be called right now. > The loading process needs CAP_BPF to load bpf_prog_test_run. I'm thinking of allowing any thread including unpriv ones to be able to pass data to the prog and update their own storage. > > > > > > > > > > It would be great to have a mmapable local storage in that case. > > > > > This patch adds that. > > > > > > > > > > Mmap isn't BPF syscall, so unpriv users can also use it to > > > > > interact with maps. > > > > > > > > > > Currently the only way of allocating mmapable map area is using > > > > > vmalloc() and it's only used at map allocation time. Vmalloc() > > > > > may sleep, therefore it's not suitable for maps that may allocate > > > > > memory in an atomic context such as local storage. Local storage > > > > > uses kmalloc() with GFP_ATOMIC, which doesn't sleep. This patch > > > > > uses kmalloc() with GFP_ATOMIC as well for mmapable map area. > > > > > > > > > > Allocating mmapable memory has requirment on page alignment. So we > > > > > have to deliberately allocate more memory than necessary to obtain > > > > > an address that has sdata->data aligned at page boundary. The > > > > > calculations for mmapable allocation size, and the actual > > > > > allocation/deallocation are packaged in three functions: > > > > > > > > > > - bpf_map_mmapable_alloc_size() > > > > > - bpf_map_mmapable_kzalloc() > > > > > - bpf_map_mmapable_kfree() > > > > > > > > > > BPF local storage uses them to provide generic mmap API: > > > > > > > > > > - bpf_local_storage_mmap() > > > > > > > > > > And task local storage adds the mmap callback: > > > > > > > > > > - task_storage_map_mmap() > > > > > > > > > > When application calls mmap on a task local storage, it gets its > > > > > own local storage. > > > > > > > > > > Overall, mmapable local storage trades off memory with flexibility > > > > > and efficiency. It brings memory fragmentation but can make programs > > > > > stateless. Therefore useful in some cases. > > > > > > > > > > Hao Luo (2): > > > > > bpf: Mmapable local storage. > > > > > selftests/bpf: Test mmapable task local storage. > > > > > > > > > > include/linux/bpf.h | 4 + > > > > > include/linux/bpf_local_storage.h | 5 +- > > > > > kernel/bpf/bpf_local_storage.c | 73 +++++++++++++++++-- > > > > > kernel/bpf/bpf_task_storage.c | 40 ++++++++++ > > > > > kernel/bpf/syscall.c | 67 +++++++++++++++++ > > > > > .../bpf/prog_tests/task_local_storage.c | 38 ++++++++++ > > > > > .../bpf/progs/task_local_storage_mmapable.c | 38 ++++++++++ > > > > > 7 files changed, 257 insertions(+), 8 deletions(-) > > > > > create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c > > > > > > > -- > Kartikeya