Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp5599361pxb; Mon, 28 Mar 2022 14:47:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJweUVr2xtRWXCqAE5Sw7eMxEq+VqTLQlkFyaLxm3X84hxEnhLXk5D+nsR8oD575c4SoTxt2 X-Received: by 2002:a05:6808:15a2:b0:2d9:df7f:e6f2 with SMTP id t34-20020a05680815a200b002d9df7fe6f2mr664314oiw.94.1648504034907; Mon, 28 Mar 2022 14:47:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648504034; cv=none; d=google.com; s=arc-20160816; b=tCjrnqSKkGgyXGEW6lJm78sFgi7633v4svLs4KR/l0UYAOC2/yE7TgMk2vOVC7+5oy NpwBRVlQ3rLqUdDrj8ndVW4KKIh2ZHlqdDshjZDCPfn0hZ+q/odb0lGDXb5AIchRGq9S /vwLaJIqqTNgLMCcMc7wR/MpkEukOOVogmCGmw79UydFXUmJFo10ND69o/89bQGspNuy GXYpm9lS8b4jnDtrKDhOPetIKB7BhW6fZn756TeWHICDH3He8qDwahwaOSnriv9yDLTs qq007nTv/Hd+Ok4foaUI8YXChJOIxq0N9fhEDCINTP6GUgjKl0ZxVfKKgb0/FO4/GDW7 7C/Q== 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=u7Pw0UDHvWPbyY/38qBqqCe3PVzLbMMxLL5t1gm37DE=; b=U9A/aM1h02J7IqctZ66Jdvuz90UDI12cQ1QV1ePzRioEvuv6UCKnuckzb4XFuPTKI0 O3w6AwD265vUOFAQKCevS3bGNeoimWGXwYF8bd6mOuDz8QAkQm1LxN41dbOub+b3dPc/ Utcm1o54tOPSG7PjA9JIYOmfBIWBU4B9EO8LFLGuukfaDqnmF8vIwXrznIM9dESk58QH kiFKzN9o8xiJQo2vuV3yD4XGsBIpO4XjA3A90Vx06f0C4KD7HPrsX0NoWdknrQ3juQ5Z qVpRT5MBV/88rrBEguX81VgjBb/jnFRovR9IYlL6pp2RjRXccNGwGKJyBv5wczSYK3Ht f3Dg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Svvqjb2S; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id f12-20020a9d5f0c000000b005cdb11f24adsi10809094oti.162.2022.03.28.14.47.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 14:47:14 -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=@google.com header.s=20210112 header.b=Svvqjb2S; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 16E4ED446F; Mon, 28 Mar 2022 14:19:46 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244628AbiC1Rlx (ORCPT + 99 others); Mon, 28 Mar 2022 13:41:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244624AbiC1Rlw (ORCPT ); Mon, 28 Mar 2022 13:41:52 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43A413C71B for ; Mon, 28 Mar 2022 10:40:11 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id i4so2607938wrb.5 for ; Mon, 28 Mar 2022 10:40: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=u7Pw0UDHvWPbyY/38qBqqCe3PVzLbMMxLL5t1gm37DE=; b=Svvqjb2SkS/48q6xbiX2X1wN7mDw3pfoVg/uY/pHR9KLPPwar6u6c+81vO+UOejvKF hpg5vPM09bdyxttV9wHFyd/TPD1bp4zjl6Y2rIjrJI8ugdvcmbyNDeKXP3mbDhUqW4tV jRzfIy1Tuu8erYzn5+TuzV7AfZl4vCUW+L2gO7UvFOyP7T04KWgnungqiVW5sFMrI/2u pPsFqx5SvlO9jrklFVUXFbw84Ncv97fsugiS35TbIYpu4xaTbeu+cELv+UHbSUpRZI34 gqowc5bDLNi+zb9ZI8vzkDcZIwZDz6WJSNuFpZwncNjNiZainL83C/VRs0+OelDiphyz hNLg== 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=u7Pw0UDHvWPbyY/38qBqqCe3PVzLbMMxLL5t1gm37DE=; b=uI7Q5JmsiDltnP2cr4LlpilXyiQs+hHAZKONSirTLoA2XXMK/5MfHD3uSpkkXH2jV4 8ueX0tRUSQdmP8e98Ss++c95BjbjxF5AN4dZFkl5llL3KejJh+QntKR41xHIRU5wsQuc TAULgusOqITKykZj4OjNJ+jseZnfbVE80xN94Yln6jfOVueUNzt1ySxd/qMVcxBOfGfU itUiD4S4o1XHIWoKLEnCl8xpr3nBxLCqHoPuxX9XbGAanZlgQPph5wa/nQgzLhU5MXJz dO7UGp+e90h69jikVraDUtdnBd7lsh5sTvxISFqSzuvInN0s+CxgQXVp2kUOmsfFCcrV OUwg== X-Gm-Message-State: AOAM531wiSl0gwZONF1GJwctpTjeiL52KyDK/rr7q7HFGgAAiayQgKN8 GskPDiuvYnvAIAD35v6nRNDMhrBrUfIrU8KA2j5nUBZEcEScZ9mU X-Received: by 2002:a05:6000:188f:b0:205:4d98:607a with SMTP id a15-20020a056000188f00b002054d98607amr24978901wri.505.1648489209640; Mon, 28 Mar 2022 10:40:09 -0700 (PDT) MIME-Version: 1.0 References: <20220324234123.1608337-1-haoluo@google.com> <9cdf860d-8370-95b5-1688-af03265cc874@fb.com> In-Reply-To: <9cdf860d-8370-95b5-1688-af03265cc874@fb.com> From: Hao Luo Date: Mon, 28 Mar 2022 10:39:58 -0700 Message-ID: Subject: Re: [PATCH RFC bpf-next 0/2] Mmapable task local storage. To: Yonghong Song Cc: 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=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no 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 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. Hao > > > > 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 > >