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 X-Spam-Level: X-Spam-Status: No, score=-4.2 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DECAAC282C3 for ; Thu, 24 Jan 2019 04:14:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A87E621872 for ; Thu, 24 Jan 2019 04:14:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548303264; bh=TZ7eSqSKkpNhMw5rrr6AY7Rox1/vLfAJ+XAZKkG3Pjc=; h=From:To:CC:Subject:Date:List-ID:From; b=Xk8ZAfCLHGQ5S1+vx94Hx2N8mwJMKS+EsrpLg+t/etdwTJJJkB86s9TnviWdBH1gH uftY39Ugc0sPAxXAiod0WEWzNexZInLaOrZLQIAfDD2KNcW5aH9cnDCgEHfSXe8RXP 6OjabmW9Ezh3+Jr06mdy/0VviX1E0nANUxmJvRXI= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727323AbfAXEOV (ORCPT ); Wed, 23 Jan 2019 23:14:21 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:35434 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727259AbfAXEON (ORCPT ); Wed, 23 Jan 2019 23:14:13 -0500 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x0O4CeHe008851 for ; Wed, 23 Jan 2019 20:14:12 -0800 Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 2q755bg4d6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 23 Jan 2019 20:14:12 -0800 Received: from mx-out.facebook.com (2620:10d:c081:10::13) by mail.thefacebook.com (2620:10d:c081:35::130) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.1.1531.3; Wed, 23 Jan 2019 20:14:10 -0800 Received: by devbig007.ftw2.facebook.com (Postfix, from userid 572438) id 8C5FE760ADE; Wed, 23 Jan 2019 20:14:03 -0800 (PST) Smtp-Origin-Hostprefix: devbig From: Alexei Starovoitov Smtp-Origin-Hostname: devbig007.ftw2.facebook.com To: CC: , , , Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH v4 bpf-next 0/9] introduce bpf_spin_lock Date: Wed, 23 Jan 2019 20:13:54 -0800 Message-ID: <20190124041403.2100609-1-ast@kernel.org> X-Mailer: git-send-email 2.17.1 X-FB-Internal: Safe MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-01-24_02:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Many algorithms need to read and modify several variables atomically. Until now it was hard to impossible to implement such algorithms in BPF. Hence introduce support for bpf_spin_lock. The api consists of 'struct bpf_spin_lock' that should be placed inside hash/array/cgroup_local_storage element and bpf_spin_lock/unlock() helper function. Example: struct hash_elem { int cnt; struct bpf_spin_lock lock; }; struct hash_elem * val = bpf_map_lookup_elem(&hash_map, &key); if (val) { bpf_spin_lock(&val->lock); val->cnt++; bpf_spin_unlock(&val->lock); } and BPF_F_LOCK flag for lookup/update bpf syscall commands that allows user space to read/write map elements under lock. Together these primitives allow race free access to map elements from bpf programs and from user space. Key restriction: root only. Key requirement: maps must be annotated with BTF. This concept was discussed at Linux Plumbers Conference 2018. Thank you everyone who participated and helped to iron out details of api and implementation. Patch 1: bpf_spin_lock support in the verifier, BTF, hash, array. Patch 2: bpf_spin_lock in cgroup local storage. Patches 3,4,5: tests Patch 6: BPF_F_LOCK flag to lookup/update Patches 7,8,9: tests v3->v4: - fix BPF_EXIST | BPF_NOEXIST check patch 6. Spotted by Jakub. Thanks! - rebase v2->v3: - fixed build on ia64 and archs where qspinlock is not supported - fixed missing lock init during lookup w/o BPF_F_LOCK. Spotted by Martin v1->v2: - addressed several issues spotted by Daniel and Martin in patch 1 - added test11 to patch 4 as suggested by Daniel Alexei Starovoitov (9): bpf: introduce bpf_spin_lock bpf: add support for bpf_spin_lock to cgroup local storage tools/bpf: sync include/uapi/linux/bpf.h selftests/bpf: add bpf_spin_lock tests selftests/bpf: add bpf_spin_lock C test bpf: introduce BPF_F_LOCK flag tools/bpf: sync uapi/bpf.h libbpf: introduce bpf_map_lookup_elem_flags() selftests/bpf: test for BPF_F_LOCK include/linux/bpf.h | 39 +- include/linux/bpf_verifier.h | 1 + include/linux/btf.h | 1 + include/uapi/linux/bpf.h | 8 +- kernel/bpf/arraymap.c | 23 +- kernel/bpf/btf.c | 42 ++ kernel/bpf/core.c | 2 + kernel/bpf/hashtab.c | 48 +- kernel/bpf/helpers.c | 71 +++ kernel/bpf/local_storage.c | 16 +- kernel/bpf/map_in_map.c | 5 + kernel/bpf/syscall.c | 45 +- kernel/bpf/verifier.c | 151 +++++- net/core/filter.c | 16 +- tools/include/uapi/linux/bpf.h | 8 +- tools/lib/bpf/bpf.c | 13 + tools/lib/bpf/bpf.h | 2 + tools/lib/bpf/libbpf.map | 1 + tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/bpf_helpers.h | 4 + tools/testing/selftests/bpf/test_map_lock.c | 66 +++ tools/testing/selftests/bpf/test_progs.c | 117 ++++- tools/testing/selftests/bpf/test_spin_lock.c | 108 +++++ tools/testing/selftests/bpf/test_verifier.c | 459 ++++++++++++++++++- 24 files changed, 1219 insertions(+), 29 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_map_lock.c create mode 100644 tools/testing/selftests/bpf/test_spin_lock.c -- 2.17.1