From patchwork Thu Jul 26 16:51:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Colascione X-Patchwork-Id: 949820 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="WpmOdhdK"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 41bym52qc9z9ryn for ; Fri, 27 Jul 2018 02:51:17 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732279AbeGZSIu (ORCPT ); Thu, 26 Jul 2018 14:08:50 -0400 Received: from mail-vk0-f74.google.com ([209.85.213.74]:36555 "EHLO mail-vk0-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731947AbeGZSIt (ORCPT ); Thu, 26 Jul 2018 14:08:49 -0400 Received: by mail-vk0-f74.google.com with SMTP id m71-v6so890290vke.3 for ; Thu, 26 Jul 2018 09:51:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:date:in-reply-to:message-id:references:subject:from:to :cc; bh=C14wB0B6Vjxk29Z+2i5VLR9Y7UfpstNpOXC4VFZGbDs=; b=WpmOdhdKimotsnIn7qu88S/bXh50tUyRa/UrXkN1SxIBq7rhSBjyWP56conyvGP1NA 0MTLxFZcvHHT/H0wWhLo173QBJ5DQHmKW1zPJIuIXzy4JSxMB5VeDOyxugypKEXcbicn 2Q+f3kO3vCzpdLi9MVb+bcSNovg8QvKtHd/bxaj7UTJcTJw5ofxpOu1bLQR36h69Cswx eNUZRAbUXx5N13GLwhRNKGazTRB42Th18BjFqD3EAWuxLqRMI0ytN4eWZBbs4wQw9dkZ jAx/ZdDU7ohWDQk5yeeMdRnBwqPon6eXGWbDmpPgdc9OLuzLNM4tzrHgeDEEbm210+9H 5Ykg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:date:in-reply-to:message-id :references:subject:from:to:cc; bh=C14wB0B6Vjxk29Z+2i5VLR9Y7UfpstNpOXC4VFZGbDs=; b=Bf/H2nqn4l8gxoNhAl0xj7MqRPn4o8DgD9Ak8/8UH7gTyy7H2T/mkXVqv8vsAIQjTI L70OqKo1w62Kb0LDI37oFdQypq1nWPneSg/TcdOqb31qTnrPwqrpCgFa2GYgm1f4QX7P 5tqiR0FcfK4WwyYufnI1HQ+c7TtgAzR1UJT21PZTp8uS1siEti1LmEBEf/qiwNDQqfMt 4WfPzv/bExbKRfEFInzw9hQyqkZzi7W1jrv6Nme4DWyaK4qpOp+TWAx38qXdUvSY0NVy dY3hlVgJDaW3e87fEDEC/dRv/cvc8txntdRojrk553Ghena/JeMHwxwpuy+c6+7OpUMM 5JNA== X-Gm-Message-State: AOUpUlGecmEOAIkM95nU9RkN3QHLfuAc3Iw3EszofCiXZ0TmUjj6azT7 SWe4N7z2SdPXJzE6fnyDTlIZ3CmQiOw= X-Google-Smtp-Source: AAOMgpdNJdcU9Ds7gSCX0NCCncSdph3QsewHYEaacbQvQiMGTn9wALs3ccPzdz+oxANTodaHvnId0z5cwAI= MIME-Version: 1.0 X-Received: by 2002:a1f:9209:: with SMTP id u9-v6mr1245686vkd.73.1532623869478; Thu, 26 Jul 2018 09:51:09 -0700 (PDT) Date: Thu, 26 Jul 2018 09:51:05 -0700 In-Reply-To: <20180716202301.GB160902@joelaf.mtv.corp.google.com> Message-Id: <20180726165105.8634-1-dancol@google.com> References: <20180716202301.GB160902@joelaf.mtv.corp.google.com> X-Mailer: git-send-email 2.18.0.233.g985f88cf7e-goog Subject: [PATCH v2] Add BPF_SYNCHRONIZE_MAPS 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_MAPS waits for the release of any references to a BPF map made by a BPF program that is running at the time the BPF_SYNCHRONIZE_MAPS command is issued. 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 --- include/uapi/linux/bpf.h | 9 +++++++++ kernel/bpf/syscall.c | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index b7db3261c62d..5b27e9117d3e 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -75,6 +75,14 @@ struct bpf_lpm_trie_key { __u8 data[0]; /* Arbitrary size */ }; +/* BPF_SYNCHRONIZE_MAPS waits for the release of any references to a + * BPF map made by a BPF program that is running at the time the + * BPF_SYNCHRONIZE_MAPS command is issued. 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 +106,7 @@ enum bpf_cmd { BPF_BTF_LOAD, BPF_BTF_GET_FD_BY_ID, BPF_TASK_FD_QUERY, + BPF_SYNCHRONIZE_MAPS, }; enum bpf_map_type { diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index a31a1ba0f8ea..8bbd1a5d01d1 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_MAPS) { + 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;