From patchwork Sun Jul 29 20:58:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Colascione X-Patchwork-Id: 950678 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="HLSfDvdY"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 41dw6L2Wtyz9s1R for ; Mon, 30 Jul 2018 06:58:50 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731494AbeG2Waf (ORCPT ); Sun, 29 Jul 2018 18:30:35 -0400 Received: from mail-it0-f73.google.com ([209.85.214.73]:50788 "EHLO mail-it0-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731146AbeG2Waf (ORCPT ); Sun, 29 Jul 2018 18:30:35 -0400 Received: by mail-it0-f73.google.com with SMTP id n194-v6so6540705itn.0 for ; Sun, 29 Jul 2018 13:58:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=AokGEKCgwJ737GNoCwSaZTwt/QvtFfmxL6P86g1wy5w=; b=HLSfDvdY9AbuDbTZYWPhcZkUhQegd7dQVwl/J+Y1zsvQhfy5P2HQZxQU6fOK8vh9Zb Sv7dlfEEzLSaiK7S8eCPnDP+K0staviqRlm8zismaM/B53HtoAQ+a5ogcEwlibC0G4Lc UWiFB5p5ZWNl75ftk3FuT9EPT6ahpf6OcppjfQ9HvvhhUbO1L8lVPafT3ivcWHocgldr z5q0QFkJ0o388m5ukFcLY2/ewM3yhS5YTYAy36gKYW+724BZuiYdo1GXjYC9//mwRAuR GX2Yv7AvSpVE+YbEk5duPEmzAEv4VwN3iQA99X/9T0Wz0DcFI/YWOQuEaOpD43Zh0vfO xN0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=AokGEKCgwJ737GNoCwSaZTwt/QvtFfmxL6P86g1wy5w=; b=NTK9Bt4zdL4Zo/GLZ/fMze/JljmGYfPub0X2bEZBG5XBCL4ro4Ia7xl7Wf0BQhmlUh 00fGvGi8SC/PjjEKUy7zAzhQ7INfmcrz6sV7OTzTgE7RpYnw2oL1jO/Ub+ER/n0bVdiS zDA53xTw1z7B+hArJ/occO/LFCA/n6ql/5twlQX2znT3kVjhqqnHs7O2RtScDLpBsxXO 7yGB0mSkBXTSZoI2qtVqefkHTXp81pdZnjXWP+C2UBIjK8nwJN9QUjwPQkdJCkVktAZZ x6YEsfiTmuqjMvlmZ6iJADYfWOH5UDHVP7kHRlBInfLHAGDphszobmE21FEtVfFZrY56 bpxg== X-Gm-Message-State: AOUpUlFmqkN4wOlONXk77bm0vbPIAg8iOSRjcZh8nMuCm7vf1jx5+x20 P5+8iY34MFxrJuxCgw5Z7KgUYUQIz6w= X-Google-Smtp-Source: AAOMgpdGbmfwNLTiELcs/i5Vnv387G0yY8MWtQy81BajVVPSBOIxXxNYz9myLIIA0ddT8ur5R5+K6wYfP6w= X-Received: by 2002:a24:47ca:: with SMTP id t193-v6mr6874648itb.24.1532897927126; Sun, 29 Jul 2018 13:58:47 -0700 (PDT) Date: Sun, 29 Jul 2018 13:58:35 -0700 In-Reply-To: Message-Id: <20180729205835.34850-1-dancol@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.18.0.345.g5c9ce644c3-goog Subject: [PATCH v3] Add BPF_SYNCHRONIZE_MAP_TO_MAP_REFERENCES bpf(2) command From: Daniel Colascione To: joelaf@google.com Cc: linux-kernel@vger.kernel.org, timmurray@google.com, netdev@vger.kernel.org, Alexei Starovoitov , Lorenzo Colitti , Chenbo Feng , Mathieu Desnoyers , Alexei Starovoitov , Daniel Borkmann , Daniel Colascione Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org BPF_SYNCHRONIZE_MAP_TO_MAP_REFERENCES waits for the release of all references to maps active at the instant the BPF_SYNCHRONIZE_MAP_TO_MAP_REFERENCES is issued. BPF_SYNCHRONIZE_MAP_TO_MAP_REFERENCES waits only for the expiration of map references obtained by BPF programs from other maps. The purpose of this command is to provide a means for userspace to replace a BPF map with another, newer version, then ensure that no component is still using the "old" map before manipulating the "old" map in some way. Signed-off-by: Daniel Colascione Signed-off-by: Daniel Borkmann --- include/uapi/linux/bpf.h | 14 ++++++++++++++ kernel/bpf/syscall.c | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index b7db3261c62d..ca3cfca76edc 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -75,6 +75,19 @@ struct bpf_lpm_trie_key { __u8 data[0]; /* Arbitrary size */ }; +/* BPF_SYNCHRONIZE_MAP_TO_MAP_REFERENCES waits for the release of all + * references to maps active at the instant the + * BPF_SYNCHRONIZE_MAP_TO_MAP_REFERENCES is + * issued. BPF_SYNCHRONIZE_MAP_TO_MAP_REFERENCES waits only for the + * expiration of map references obtained by BPF programs from + * other maps. + * + * The purpose of this command is to provide a means for userspace to + * replace a BPF map with another, newer version, then ensure that no + * component is still using the "old" map before manipulating the + * "old" map in some way. + */ + /* BPF syscall commands, see bpf(2) man-page for details. */ enum bpf_cmd { BPF_MAP_CREATE, @@ -98,6 +111,7 @@ enum bpf_cmd { BPF_BTF_LOAD, BPF_BTF_GET_FD_BY_ID, BPF_TASK_FD_QUERY, + BPF_SYNCHRONIZE_MAP_TO_MAP_REFERENCES, }; enum bpf_map_type { diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index a31a1ba0f8ea..bc9a0713f47d 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2274,6 +2274,19 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) return -EPERM; + if (cmd == BPF_SYNCHRONIZE_MAP_TO_MAP_REFERENCES) { + if (uattr != NULL || size != 0) + return -EINVAL; + err = security_bpf(cmd, NULL, 0); + if (err < 0) + return err; + /* BPF programs always enter a critical section while + * they have a map reference outstanding. + */ + synchronize_rcu(); + return 0; + } + err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); if (err) return err;