Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3535988pxb; Mon, 4 Apr 2022 20:19:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyXgCiA+AyEh1DF0ADGSPpXUiKW12Sq6wFrOKWMp6A4i/TM2THpSR7+lnX5IycyDV2hyLsA X-Received: by 2002:a17:90a:fe04:b0:1ca:c8e5:e3d with SMTP id ck4-20020a17090afe0400b001cac8e50e3dmr1643475pjb.227.1649128790501; Mon, 04 Apr 2022 20:19:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649128790; cv=none; d=google.com; s=arc-20160816; b=DtEGe8di1/zu9S0+eTGzrPX0ff5qwEd0tFV7DGCgRAGjdeXPjlQmzwpmwJopx6xBLD mRQrm4qud1IqkTFczJXr3DYPiuRs0Yd2szkIG42ZL4n9a4LCHApR9HjMhNP35KVNqxdi 4wNaZSSpwaUzP1xA8jLq8ErpRodKGkF9oDUUF+SmX+9IZNSkhLpy30aD0MDj8HUoiiOD vT7OLLXuOwwZJoPWpPAS8/W7WloqeOx0uUsebuMrkxildhvNjvhkBZV0NHMW4351KduZ rG99SThKmbHMG8a3s8OyBKExCONVKPUGP7zbyCsuJ7uwIbQMLpyQGB78TlqU1A5zH4zd EiqA== 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=ieMDTFTbMdRB0jUFSRS4LUleYN4tThrn+ku1UPvBZSE=; b=u4CdYq8HwB9InYkbKm0uCnQjrr5IPzH9NOHHf+IG0UxWCI1xB0n6anI56J96GbEle/ SgRod/DQac8rZf90D4haZOF4xQPYCzUZHBEMvzOxFsrkuY9DubkWVqhT5KS8KtU4ewkK LOIInMHYetKQCG/8lRU2Qy5hM2YMdnrQOu7iZRqGsKi1dWwiYO6MpJALx+iPSPTh6U9J 8ps6fufTYh/EFua+HK0FtvQ1n9Y174hpJV7tXTmHZvIqJIqHhvQEpUFYTQaXdQb4sYFI 7C9Arv+p3Pu6Z0wKAS8SvuR8fPMrDaFWdKJbSye9dPSHZfV3vZR4BEGXUMCG7N3fNbCD xsUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WuGpKBHD; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id fz21-20020a17090b025500b001c685d1da8csi852516pjb.25.2022.04.04.20.19.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 20:19:50 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WuGpKBHD; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 621F53FA068; Mon, 4 Apr 2022 18:19:56 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238994AbiDBAui (ORCPT + 99 others); Fri, 1 Apr 2022 20:50:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234691AbiDBAug (ORCPT ); Fri, 1 Apr 2022 20:50:36 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 303036444 for ; Fri, 1 Apr 2022 17:48:43 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 0235CB8268D for ; Sat, 2 Apr 2022 00:48:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6700BC34114 for ; Sat, 2 Apr 2022 00:48:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1648860520; bh=g2ku+IXTdPUjzpc9uGwZZDiqSIt6z79v7mm5DWhbJAE=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=WuGpKBHDoMaDzuIH+RXMsUKmT7qq8Axfs4hCXO9ODGluWd0APLpyTIL5etM6yBdSx tXz1qtcIqIbG/Yctz7bzvlrPs1oWPn0fyaNRezr+6h0+5FHXYZUso3o8sqV0H7a/uU pbaUQjUThIqEiNIApvo5NYRh0A4aea/j9gK1oFiPD0z22ETf1FLy0CwkqKSWD6wi8u ztik7pwDLlHhSwxUDRtNgmcEI3N8w455dxBOm/zM875MtOVBmGXZvz04244MXh5wlz nod2JArd5CeH0wqlBQ3lTMzgT6eSKQYajJTbu0R5c9cPrOWx9Wchy1Q2kCdRWtDhrB +9wWoHMwzf3mQ== Received: by mail-ej1-f50.google.com with SMTP id lr4so9052746ejb.11 for ; Fri, 01 Apr 2022 17:48:40 -0700 (PDT) X-Gm-Message-State: AOAM530mxOzoHcHPp/o5XuGYGFGw8Uo7JdR3m+5dvluzGc2bcPwTyU9a 3cjMZQ1Xw2LRJ0z5l72GFDTp1MC2sZ9SgG1W+RrGxg== X-Received: by 2002:a17:907:6089:b0:6db:a3d7:3fa9 with SMTP id ht9-20020a170907608900b006dba3d73fa9mr2066375ejc.593.1648860518513; Fri, 01 Apr 2022 17:48:38 -0700 (PDT) MIME-Version: 1.0 References: <20220324234123.1608337-1-haoluo@google.com> <9cdf860d-8370-95b5-1688-af03265cc874@fb.com> <20220329093753.26wc3noelqrwlrcj@apollo.legion> <20220329232956.gbsr65jdbe4lw2m6@ast-mbp> In-Reply-To: From: KP Singh Date: Sat, 2 Apr 2022 02:48:27 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH RFC bpf-next 0/2] Mmapable task local storage. To: Alexei Starovoitov Cc: Hao Luo , Jann Horn , Kumar Kartikeya Dwivedi , Yonghong Song , Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , bpf , LKML Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE 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 Fri, Apr 1, 2022 at 1:06 AM Alexei Starovoitov wrote: > > On Thu, Mar 31, 2022 at 3:32 PM KP Singh wrote: > > > > On Wed, Mar 30, 2022 at 8:26 PM Hao Luo wrote: > > > > > > On Wed, Mar 30, 2022 at 11:16 AM Alexei Starovoitov > > > wrote: > > > > > > > > On Wed, Mar 30, 2022 at 11:06 AM Hao Luo wrote: > > > > > > > > > > On Tue, Mar 29, 2022 at 4:30 PM Alexei Starovoitov > > > > > wrote: > > > > > > > > > > > > On Tue, Mar 29, 2022 at 10:43:42AM -0700, Hao Luo wrote: > > > > > > > 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. > > > > > > > > > > > > If I understand the use case correctly all of this mmap-ing is only to > > > > > > allow unpriv userspace to access a priv map via unpriv mmap() syscall. > > > > > > But the map can be accessed as unpriv already. > > > > > > Pin it with the world read creds and do map_lookup sys_bpf cmd on it. > > > > > > > > > > Right, but, if I understand correctly, with > > > > > sysctl_unprivileged_bpf_disabled, unpriv tasks are not able to make > > > > > use of __sys_bpf(). Is there anything I missed? > > > > > > > > That sysctl is a heavy hammer. Let's fix it instead. > > > > map lookup/update/delete can be allowed for unpriv for certain map types. > > > > There are permissions checks in corresponding lookup/update calls already. > > > > > > > (Adding Jann) > > > > I wonder if we can tag a map as BPF_F_UNPRIVILEGED and allow the writes to > > only maps that are explicitly marked as writable by unprivileged processes. > > I think it's overkill for existing unpriv maps like hash and array. > These maps by themself don't pose a security threat. > The sysctl was/is in the wrong place. > > > We will have task local storage in LSM programs that we > > won't like unprivileged processes to write to as well. > > > > struct { > > __uint(type, BPF_MAP_TYPE_TASK_STORAGE); > > __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_UNPRIVILEGED); > > __type(key, int); > > __type(value, struct fd_storage); > > } task_fd_storage_map SEC(".maps"); > > local storage map was not exposed to unpriv before. > This would be a different consideration. > But even in such a case the extra flag looks unnecessary. I took a look at the code and it makes sense to allow maps like hash and array which are already mmapable. These should not really need the BPF_F_UNPRIVILEGED. Hao, I think you can send a patch that removes these map operations from the scope of the sysctl. Regarding the local storages, let's do them separately since it would be a newer access surface.