bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Roman Gushchin <guro@fb.com>
To: <bpf@vger.kernel.org>
Cc: <netdev@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>, <kernel-team@fb.com>,
	<linux-kernel@vger.kernel.org>, Roman Gushchin <guro@fb.com>
Subject: [PATCH bpf-next v2 28/35] bpf: eliminate rlimit-based memory accounting for bpf progs
Date: Mon, 27 Jul 2020 11:44:59 -0700	[thread overview]
Message-ID: <20200727184506.2279656-29-guro@fb.com> (raw)
In-Reply-To: <20200727184506.2279656-1-guro@fb.com>

Do not use rlimit-based memory accounting for bpf progs. It has been
replaced with memcg-based memory accounting.

Signed-off-by: Roman Gushchin <guro@fb.com>
---
 include/linux/bpf.h  | 11 ------
 kernel/bpf/core.c    | 12 ++-----
 kernel/bpf/syscall.c | 86 ++++++--------------------------------------
 3 files changed, 12 insertions(+), 97 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 055c693d9928..0c443468200e 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1095,8 +1095,6 @@ void bpf_prog_sub(struct bpf_prog *prog, int i);
 void bpf_prog_inc(struct bpf_prog *prog);
 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
 void bpf_prog_put(struct bpf_prog *prog);
-int __bpf_prog_charge(struct user_struct *user, u32 pages);
-void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
 			  struct bpf_map **used_maps, u32 len);
 
@@ -1380,15 +1378,6 @@ bpf_prog_inc_not_zero(struct bpf_prog *prog)
 	return ERR_PTR(-EOPNOTSUPP);
 }
 
-static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
-{
-	return 0;
-}
-
-static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
-{
-}
-
 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
 {
 	return -EOPNOTSUPP;
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index daab8dcafbd4..23b8ff109ac8 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -219,23 +219,15 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
 {
 	gfp_t gfp_flags = GFP_KERNEL_ACCOUNT | __GFP_ZERO | gfp_extra_flags;
 	struct bpf_prog *fp;
-	u32 pages, delta;
-	int ret;
+	u32 pages;
 
 	size = round_up(size, PAGE_SIZE);
 	pages = size / PAGE_SIZE;
 	if (pages <= fp_old->pages)
 		return fp_old;
 
-	delta = pages - fp_old->pages;
-	ret = __bpf_prog_charge(fp_old->aux->user, delta);
-	if (ret)
-		return NULL;
-
 	fp = __vmalloc(size, gfp_flags);
-	if (fp == NULL) {
-		__bpf_prog_uncharge(fp_old->aux->user, delta);
-	} else {
+	if (fp) {
 		memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
 		fp->pages = pages;
 		fp->aux->prog = fp;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ae51e2363cc1..7f0bf60f5218 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -337,23 +337,6 @@ void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
 	map->numa_node = bpf_map_attr_numa_node(attr);
 }
 
-static int bpf_charge_memlock(struct user_struct *user, u32 pages)
-{
-	unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
-
-	if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
-		atomic_long_sub(pages, &user->locked_vm);
-		return -EPERM;
-	}
-	return 0;
-}
-
-static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
-{
-	if (user)
-		atomic_long_sub(pages, &user->locked_vm);
-}
-
 static int bpf_map_alloc_id(struct bpf_map *map)
 {
 	int id;
@@ -1563,51 +1546,6 @@ static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
 	audit_log_end(ab);
 }
 
-int __bpf_prog_charge(struct user_struct *user, u32 pages)
-{
-	unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
-	unsigned long user_bufs;
-
-	if (user) {
-		user_bufs = atomic_long_add_return(pages, &user->locked_vm);
-		if (user_bufs > memlock_limit) {
-			atomic_long_sub(pages, &user->locked_vm);
-			return -EPERM;
-		}
-	}
-
-	return 0;
-}
-
-void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
-{
-	if (user)
-		atomic_long_sub(pages, &user->locked_vm);
-}
-
-static int bpf_prog_charge_memlock(struct bpf_prog *prog)
-{
-	struct user_struct *user = get_current_user();
-	int ret;
-
-	ret = __bpf_prog_charge(user, prog->pages);
-	if (ret) {
-		free_uid(user);
-		return ret;
-	}
-
-	prog->aux->user = user;
-	return 0;
-}
-
-static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
-{
-	struct user_struct *user = prog->aux->user;
-
-	__bpf_prog_uncharge(user, prog->pages);
-	free_uid(user);
-}
-
 static int bpf_prog_alloc_id(struct bpf_prog *prog)
 {
 	int id;
@@ -1657,7 +1595,7 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)
 
 	kvfree(aux->func_info);
 	kfree(aux->func_info_aux);
-	bpf_prog_uncharge_memlock(aux->prog);
+	free_uid(aux->user);
 	security_bpf_prog_free(aux);
 	bpf_prog_free(aux->prog);
 }
@@ -2090,7 +2028,7 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
 		tgt_prog = bpf_prog_get(attr->attach_prog_fd);
 		if (IS_ERR(tgt_prog)) {
 			err = PTR_ERR(tgt_prog);
-			goto free_prog_nouncharge;
+			goto free_prog;
 		}
 		prog->aux->linked_prog = tgt_prog;
 	}
@@ -2099,18 +2037,15 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
 
 	err = security_bpf_prog_alloc(prog->aux);
 	if (err)
-		goto free_prog_nouncharge;
-
-	err = bpf_prog_charge_memlock(prog);
-	if (err)
-		goto free_prog_sec;
+		goto free_prog;
 
+	prog->aux->user = get_current_user();
 	prog->len = attr->insn_cnt;
 
 	err = -EFAULT;
 	if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
 			   bpf_prog_insn_size(prog)) != 0)
-		goto free_prog;
+		goto free_prog_sec;
 
 	prog->orig_prog = NULL;
 	prog->jited = 0;
@@ -2121,19 +2056,19 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
 	if (bpf_prog_is_dev_bound(prog->aux)) {
 		err = bpf_prog_offload_init(prog, attr);
 		if (err)
-			goto free_prog;
+			goto free_prog_sec;
 	}
 
 	/* find program type: socket_filter vs tracing_filter */
 	err = find_prog_type(type, prog);
 	if (err < 0)
-		goto free_prog;
+		goto free_prog_sec;
 
 	prog->aux->load_time = ktime_get_boottime_ns();
 	err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
 			       sizeof(attr->prog_name));
 	if (err < 0)
-		goto free_prog;
+		goto free_prog_sec;
 
 	/* run eBPF verifier */
 	err = bpf_check(&prog, attr, uattr);
@@ -2178,11 +2113,10 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
 	 */
 	__bpf_prog_put_noref(prog, prog->aux->func_cnt);
 	return err;
-free_prog:
-	bpf_prog_uncharge_memlock(prog);
 free_prog_sec:
+	free_uid(prog->aux->user);
 	security_bpf_prog_free(prog->aux);
-free_prog_nouncharge:
+free_prog:
 	bpf_prog_free(prog);
 	return err;
 }
-- 
2.26.2


  parent reply	other threads:[~2020-07-27 18:45 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-27 18:44 [PATCH bpf-next v2 00/35] bpf: switch to memcg-based memory accounting Roman Gushchin
2020-07-27 18:44 ` [PATCH bpf-next v2 01/35] bpf: memcg-based memory accounting for bpf progs Roman Gushchin
2020-07-27 22:11   ` Song Liu
2020-07-28  0:08     ` Roman Gushchin
2020-07-28  4:42       ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 02/35] bpf: memcg-based memory accounting for bpf maps Roman Gushchin
2020-07-27 22:12   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 03/35] bpf: refine memcg-based memory accounting for arraymap maps Roman Gushchin
2020-07-27 22:30   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 04/35] bpf: refine memcg-based memory accounting for cpumap maps Roman Gushchin
2020-07-27 22:48   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 05/35] bpf: memcg-based memory accounting for cgroup storage maps Roman Gushchin
2020-07-27 23:05   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 06/35] bpf: refine memcg-based memory accounting for devmap maps Roman Gushchin
2020-07-27 23:35   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 07/35] bpf: refine memcg-based memory accounting for hashtab maps Roman Gushchin
2020-07-27 23:36   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 08/35] bpf: memcg-based memory accounting for lpm_trie maps Roman Gushchin
2020-07-27 23:55   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 09/35] bpf: memcg-based memory accounting for bpf ringbuffer Roman Gushchin
2020-07-27 23:56   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 10/35] bpf: memcg-based memory accounting for socket storage maps Roman Gushchin
2020-07-27 23:57   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 11/35] bpf: refine memcg-based memory accounting for sockmap and sockhash maps Roman Gushchin
2020-07-27 23:58   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 12/35] bpf: refine memcg-based memory accounting for xskmap maps Roman Gushchin
2020-07-28  0:01   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 13/35] bpf: eliminate rlimit-based memory accounting for arraymap maps Roman Gushchin
2020-07-28  0:04   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 14/35] bpf: eliminate rlimit-based memory accounting for bpf_struct_ops maps Roman Gushchin
2020-07-28  5:29   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 15/35] bpf: eliminate rlimit-based memory accounting for cpumap maps Roman Gushchin
2020-07-28  5:30   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 16/35] bpf: eliminate rlimit-based memory accounting for cgroup storage maps Roman Gushchin
2020-07-28  5:31   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 17/35] bpf: eliminate rlimit-based memory accounting for devmap maps Roman Gushchin
2020-07-28  5:31   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 18/35] bpf: eliminate rlimit-based memory accounting for hashtab maps Roman Gushchin
2020-07-28  5:32   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 19/35] bpf: eliminate rlimit-based memory accounting for lpm_trie maps Roman Gushchin
2020-07-28  5:32   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 20/35] bpf: eliminate rlimit-based memory accounting for queue_stack_maps maps Roman Gushchin
2020-07-28  5:35   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 21/35] bpf: eliminate rlimit-based memory accounting for reuseport_array maps Roman Gushchin
2020-07-28  5:36   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 22/35] bpf: eliminate rlimit-based memory accounting for bpf ringbuffer Roman Gushchin
2020-07-28  5:37   ` Song Liu
2020-07-28  5:56   ` Andrii Nakryiko
2020-07-27 18:44 ` [PATCH bpf-next v2 23/35] bpf: eliminate rlimit-based memory accounting for sockmap and sockhash maps Roman Gushchin
2020-07-28  5:37   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 24/35] bpf: eliminate rlimit-based memory accounting for stackmap maps Roman Gushchin
2020-07-28  5:38   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 25/35] bpf: eliminate rlimit-based memory accounting for socket storage maps Roman Gushchin
2020-07-28  5:41   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 26/35] bpf: eliminate rlimit-based memory accounting for xskmap maps Roman Gushchin
2020-07-28  5:42   ` Song Liu
2020-07-27 18:44 ` [PATCH bpf-next v2 27/35] bpf: eliminate rlimit-based memory accounting infra for bpf maps Roman Gushchin
2020-07-28  5:47   ` Song Liu
2020-07-28  5:58     ` Andrii Nakryiko
2020-07-28  6:06       ` Song Liu
2020-07-28 19:08         ` Roman Gushchin
2020-07-28 19:16           ` Andrii Nakryiko
2020-07-27 18:44 ` Roman Gushchin [this message]
2020-07-28  5:55   ` [PATCH bpf-next v2 28/35] bpf: eliminate rlimit-based memory accounting for bpf progs Song Liu
2020-07-27 18:45 ` [PATCH bpf-next v2 29/35] bpf: libbpf: cleanup RLIMIT_MEMLOCK usage Roman Gushchin
2020-07-27 22:05   ` Andrii Nakryiko
2020-07-27 22:44     ` Song Liu
2020-07-27 23:15     ` Roman Gushchin
2020-07-28  5:59       ` Andrii Nakryiko
2020-07-30  1:38         ` Roman Gushchin
2020-07-30 19:39           ` Andrii Nakryiko
2020-07-30 20:46             ` Roman Gushchin
2020-07-27 18:45 ` [PATCH bpf-next v2 30/35] bpf: bpftool: do not touch RLIMIT_MEMLOCK Roman Gushchin
2020-07-28  6:00   ` Song Liu
2020-07-28  6:00   ` Andrii Nakryiko
2020-07-27 18:45 ` [PATCH bpf-next v2 31/35] bpf: runqslower: don't " Roman Gushchin
2020-07-28  6:03   ` Andrii Nakryiko
2020-07-27 18:45 ` [PATCH bpf-next v2 32/35] bpf: selftests: delete bpf_rlimit.h Roman Gushchin
2020-07-28  6:06   ` Andrii Nakryiko
2020-07-28  6:11     ` Song Liu
2020-07-28 18:30       ` Andrii Nakryiko
2020-07-27 18:45 ` [PATCH bpf-next v2 33/35] bpf: selftests: don't touch RLIMIT_MEMLOCK Roman Gushchin
2020-07-28  6:08   ` Andrii Nakryiko
2020-07-27 18:45 ` [PATCH bpf-next v2 34/35] bpf: samples: do not " Roman Gushchin
2020-07-28  6:14   ` Song Liu
2020-07-27 18:45 ` [PATCH bpf-next v2 35/35] perf: don't " Roman Gushchin
2020-07-28  6:09   ` Andrii Nakryiko
2020-07-28 12:13     ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200727184506.2279656-29-guro@fb.com \
    --to=guro@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).