Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp2466780imd; Fri, 2 Nov 2018 11:54:55 -0700 (PDT) X-Google-Smtp-Source: AJdET5dM2CAVDcHdvAtcy9XjXkKfcHnujpUTBMl42vLqio645LLcr0CSNfT3X3JzNgbzDoC6uzVJ X-Received: by 2002:a17:902:b688:: with SMTP id c8-v6mr13018735pls.306.1541184895470; Fri, 02 Nov 2018 11:54:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541184895; cv=none; d=google.com; s=arc-20160816; b=mnuH69uDF/p9gi7IJUOOjiagp9Bn0jCKLqUlaGOZN6WbkEbkDRH4QRKuCziuFeMzKj Wg3Kfz4mK0VE9/SRGhgjol7AxbLv5zaYWmdo/iJC6h877UMoyc5iwAZOLJtACFQNEcxu al84TBUqfnBw7dPJxT8si5+PeeMrFwF5DFQUKJ+atmQ5aqMEPM6KycDlz0vBo+PUQXvt 8cj4ZDxFF2gyu0hVXybnzhwBs4AwI/UVNWyyYA5EpBdLc4UAKHCmVtGv+TU53/6sfARY FU+1nb5uZinAaOSZAzYcq2PUUNev2ImZhztzdMsI3htXbZwUriOfEw06L9Zg9oCC1SBZ EKqw== 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=8fxRo6ps437ChPhNJxk+bnoeX6CtL/XgbKY5KmMcdto=; b=NMsDzy6QfI2MXNejXRvVyA5ldCPXD0+nD1N7iTZNZyfz2uOppNItYgbSEglU3nFc1P G2byftzowMkQ41sGCgmOAIfeqHwGDjcQWImWGrthB+o47iH7z0QHfu0aDt5DrzQD5p5d P6gx1CdQslGaTMja3KtnB3VIvgRZMe0cLJqTvCU/97EFb9zuzFAH29eKpN2XzkpAPeBR cwAj71IAwgCQZGzg15nH5XufwNVTPrTZ73v/cu95IMxN4ovQQpKCeyRnXGzIlOGjvO24 ck++4gYkWd32TmyItCzfVLZ7hBOCtNrXWU90/csrdDvz2idlyjwrbS8SE84oUbBm3BD/ z1+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JjciwfXt; 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 m123-v6si20365600pfd.112.2018.11.02.11.54.40; Fri, 02 Nov 2018 11:54:55 -0700 (PDT) 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=JjciwfXt; 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 S1731176AbeKCECa (ORCPT + 99 others); Sat, 3 Nov 2018 00:02:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:58290 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729463AbeKCEC3 (ORCPT ); Sat, 3 Nov 2018 00:02:29 -0400 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 33DE72084C; Fri, 2 Nov 2018 18:54:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541184852; bh=15Zw9RL54Jx8FB/2+4D1qmEc+gVjMuOLHGDB3mehgW4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JjciwfXt50NdbR25Nrli4/5PhuTpuhZt1h4BoMAuAF/34qWMZTfIAPHpSGzMUpUVV 2t5+GSckTPXhuqdHGFfMRMJUvFqSZ+rDmIsNWjOqQwldymrVfO6YuhNFYCqEykc5b5 zAnmnGwlbg1+Qfe0lIk782jXV+h+Y/DcZMTXBzCw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, John Fastabend , Daniel Borkmann , Sasha Levin Subject: [PATCH 4.14 076/143] bpf: sockmap, map_release does not hold refcnt for pinned maps Date: Fri, 2 Nov 2018 19:34:21 +0100 Message-Id: <20181102182903.541228511@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181102182857.064326086@linuxfoundation.org> References: <20181102182857.064326086@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. ------------------ [ Upstream commit ba6b8de423f8d0dee48d6030288ed81c03ddf9f0 ] Relying on map_release hook to decrement the reference counts when a map is removed only works if the map is not being pinned. In the pinned case the ref is decremented immediately and the BPF programs released. After this BPF programs may not be in-use which is not what the user would expect. This patch moves the release logic into bpf_map_put_uref() and brings sockmap in-line with how a similar case is handled in prog array maps. Fixes: 3d9e952697de ("bpf: sockmap, fix leaking maps with attached but not detached progs") Signed-off-by: John Fastabend Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin --- include/linux/bpf.h | 2 +- kernel/bpf/arraymap.c | 3 ++- kernel/bpf/sockmap.c | 4 ++-- kernel/bpf/syscall.c | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 5c5be80ce802..c9d2a1a3ef11 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -27,6 +27,7 @@ struct bpf_map_ops { void (*map_release)(struct bpf_map *map, struct file *map_file); void (*map_free)(struct bpf_map *map); int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key); + void (*map_release_uref)(struct bpf_map *map); /* funcs callable from userspace and from eBPF programs */ void *(*map_lookup_elem)(struct bpf_map *map, void *key); @@ -300,7 +301,6 @@ int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value); int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file, void *key, void *value, u64 map_flags); int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value); -void bpf_fd_array_map_clear(struct bpf_map *map); int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file, void *key, void *value, u64 map_flags); int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value); diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index f57d0bdf3c9e..a8f55ea4146b 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -467,7 +467,7 @@ static u32 prog_fd_array_sys_lookup_elem(void *ptr) } /* decrement refcnt of all bpf_progs that are stored in this map */ -void bpf_fd_array_map_clear(struct bpf_map *map) +static void bpf_fd_array_map_clear(struct bpf_map *map) { struct bpf_array *array = container_of(map, struct bpf_array, map); int i; @@ -485,6 +485,7 @@ const struct bpf_map_ops prog_array_map_ops = { .map_fd_get_ptr = prog_fd_array_get_ptr, .map_fd_put_ptr = prog_fd_array_put_ptr, .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem, + .map_release_uref = bpf_fd_array_map_clear, }; static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file, diff --git a/kernel/bpf/sockmap.c b/kernel/bpf/sockmap.c index 20eaddfa691c..22991e19c01c 100644 --- a/kernel/bpf/sockmap.c +++ b/kernel/bpf/sockmap.c @@ -875,7 +875,7 @@ static int sock_map_update_elem(struct bpf_map *map, return err; } -static void sock_map_release(struct bpf_map *map, struct file *map_file) +static void sock_map_release(struct bpf_map *map) { struct bpf_stab *stab = container_of(map, struct bpf_stab, map); struct bpf_prog *orig; @@ -895,7 +895,7 @@ const struct bpf_map_ops sock_map_ops = { .map_get_next_key = sock_map_get_next_key, .map_update_elem = sock_map_update_elem, .map_delete_elem = sock_map_delete_elem, - .map_release = sock_map_release, + .map_release_uref = sock_map_release, }; BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, bpf_sock, diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 4e933219fec6..ea22d0b6a9f0 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -214,8 +214,8 @@ static void bpf_map_free_deferred(struct work_struct *work) static void bpf_map_put_uref(struct bpf_map *map) { if (atomic_dec_and_test(&map->usercnt)) { - if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) - bpf_fd_array_map_clear(map); + if (map->ops->map_release_uref) + map->ops->map_release_uref(map); } } -- 2.17.1