Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3425948imu; Sun, 11 Nov 2018 14:49:37 -0800 (PST) X-Google-Smtp-Source: AJdET5cUqo0RBMIJ6sQJipX8TCjuYhWxsYEWKAs2qrrCLVFEa9F1ek/cM798Qf+CH79AmGX9XiVO X-Received: by 2002:a63:7044:: with SMTP id a4mr14992817pgn.359.1541976577865; Sun, 11 Nov 2018 14:49:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541976577; cv=none; d=google.com; s=arc-20160816; b=e7PzKChBUe/6F5He+RbF2tBP7PQYID7eBCqfNsPLx+/dnfgKGQr54rR2nIDGliBUHu WoepIQ6JJNbqiwvJBJGG9I1b23jO1Jd/KQLnbJhNAa7Hd8mm2UZ9DTC2Ljwj5zLscklT MHnr5kneAycoA/v7IClcH0VpvXnDHYThoVNx/OCEdZpU7co0zolDwNZA+N8fNRIaFXCi RcaxftypRLMgVrd+Qksq5UTE+zChO8GI9e2PE3Wtt14/HgFYLFdXTJKPfiunGqSpUzB9 fXciRLpNgpWQgHM1ZXAVB3AVmluyI04wT7FxjSlcACVW86uW3PANzKFYgBSLf6yMkgKD d0sA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=/K42AqKEwZAv0dapnyAn5qfEU7emRhBvUZmM8pFp6O0=; b=CyuPs2+wCx9f5CFrZV7fFJEAnTbA3apz6ib6yeXeAqCFOmNGJIOviLEEBslde3/n8H Bsv3jReOs+FqRI98so0HNL9aj3OlhuAIeJnjVqqhR1f/JavPlQCfeiPMYt2ubIQa6yD2 9APSvq2gI8QAXu39dutwOpX+b/e1Ni7g0FaLJtCZyYzTZw1K/lnpRS/n0OKHbf6Ii9+X 1/N4oy1vQHi9BkTVDLo6asQJIku7cJ335YUhZkoYTDQNnX9B0LcpJVPKhSkElzEPFEq7 1W0foS7sV3yRvNcTzUqwOkaLxTIgD5EQF1bJzhdg11yq0wphd1u5cGcW9tgB+5ULovP8 VSeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=kqMfn5h7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p11-v6si14550055pgb.563.2018.11.11.14.49.23; Sun, 11 Nov 2018 14:49:37 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=kqMfn5h7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404403AbeKLIXW (ORCPT + 99 others); Mon, 12 Nov 2018 03:23:22 -0500 Received: from mail.kernel.org ([198.145.29.99]:56754 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404353AbeKLIXV (ORCPT ); Mon, 12 Nov 2018 03:23:21 -0500 Received: from localhost (unknown [206.108.79.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AB86521707; Sun, 11 Nov 2018 22:33:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975603; bh=QlReAoMnNWSoPLWOHobPqrPbnbtIQWaNSXUWeyV7UlI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kqMfn5h77cUntAeHxbjD+pMr8r1kOP8ED+L4hegXCObsa4tqNUFVN4JW3R0eOHu/I GJ3GfHK0dU5dbyOv+iGvxr2zcFtl7xxVbCmNmZTu/u8IrUqANRALLS8UfmTp01yRHH xXWRIs/uqvNpbK5cempxcO/4wkdoCPa1Gyi2/q1k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Colascione , "Joel Fernandes (Google)" , Alexei Starovoitov , Chenbo Feng Subject: [PATCH 4.14 222/222] bpf: wait for running BPF programs when updating map-in-map Date: Sun, 11 Nov 2018 14:25:19 -0800 Message-Id: <20181111221706.032923266@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221647.665769131@linuxfoundation.org> References: <20181111221647.665769131@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Daniel Colascione commit 1ae80cf31938c8f77c37a29bbe29e7f1cd492be8 upstream. The map-in-map frequently serves as a mechanism for atomic snapshotting of state that a BPF program might record. The current implementation is dangerous to use in this way, however, since userspace has no way of knowing when all programs that might have retrieved the "old" value of the map may have completed. This change ensures that map update operations on map-in-map map types always wait for all references to the old map to drop before returning to userspace. Signed-off-by: Daniel Colascione Reviewed-by: Joel Fernandes (Google) Signed-off-by: Alexei Starovoitov [fengc@google.com: 4.14 backport: adjust context] Signed-off-by: Chenbo Feng Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/syscall.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -519,6 +519,17 @@ err_put: return err; } +static void maybe_wait_bpf_programs(struct bpf_map *map) +{ + /* Wait for any running BPF programs to complete so that + * userspace, when we return to it, knows that all programs + * that could be running use the new map value. + */ + if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || + map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) + synchronize_rcu(); +} + #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags static int map_update_elem(union bpf_attr *attr) @@ -592,6 +603,7 @@ static int map_update_elem(union bpf_att } __this_cpu_dec(bpf_prog_active); preempt_enable(); + maybe_wait_bpf_programs(map); if (!err) trace_bpf_map_update_elem(map, ufd, key, value); @@ -636,6 +648,7 @@ static int map_delete_elem(union bpf_att rcu_read_unlock(); __this_cpu_dec(bpf_prog_active); preempt_enable(); + maybe_wait_bpf_programs(map); if (!err) trace_bpf_map_delete_elem(map, ufd, key);