Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp69472pxu; Tue, 24 Nov 2020 19:05:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJxX2+jTCgD6bRePAiE+Ye3ExFB6pDF7rdfRV+teIpUzSIGPO/1VXO/lWIB8XCwMQkoAfbAb X-Received: by 2002:a50:9fe6:: with SMTP id c93mr1571007edf.30.1606273554833; Tue, 24 Nov 2020 19:05:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606273554; cv=none; d=google.com; s=arc-20160816; b=Mb07Db6m7m6jWzqqNSy3mSlx60/toa3w9coREUJ1Rh4M+gVEiAMiruyEHe1NtNWX9w Gnuiwk2HLl3ljc7g4L1Clz7lFWwadz7/7UzbgRMI5ynRZxNV2wSnjfr4aL5uhr1H0YXw UClyM0FwdAuHsDYqpGtzk8yU/RsINoTGHYQaEiBTt9v3koU4aDjy84HRVCL2g6/yXPvM 8xoNiUM7TAc0CvzXEm/g3GSWLKX+afpcjLPOSKcpW6esgeyCD39njuBA/JLNyZOwDW6S 0nsekj5A5aUFz1ti95eeQZKeIUO2t+b2mikS0M6bDQ9yf8jSRmvr2gZ3zej7P63S4+W5 bbNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NlO1NmdNxRmpb25hC/xULSuihfyWc3EyX/YU5MiQoR0=; b=KWJb3cnnRiBceI3V9HcG9Z4SYBAT5BtShvVHHJfkAxYpmhxXHFynGyi+wNfQVtDxHE QDCKvuA2I2cKsGAec00rhiCNLcJxq5k8ACT8907rNhE5g4ExPTG1OUHNs95urQlQUslx B7D0lbWZ1I4kp9qQzHFb0KdOdjJ42F5XBbV0E9Vfh6US/UBIa/ESTLdRx4kLRMhPnoEp yFlL9bDG/N44RnAM9xrGz+cmbvQPjLgJd6HzbXcZJph4/b3p+fPFsXEgMKF9H1k1ljNR LeaqlqE9mkDkz9aJJQ7LK5ut43yQlIkG999vodHkoTE2cyCdEnj9x8Ho8Z84UPkFGgjO 88iA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=X3Lp8Wzp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z13si494739ejf.662.2020.11.24.19.05.32; Tue, 24 Nov 2020 19:05:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=X3Lp8Wzp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727663AbgKYDBg (ORCPT + 99 others); Tue, 24 Nov 2020 22:01:36 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:63842 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727498AbgKYDBf (ORCPT ); Tue, 24 Nov 2020 22:01:35 -0500 Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.0.42/8.16.0.42) with SMTP id 0AP2sovm000549 for ; Tue, 24 Nov 2020 19:01:33 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=NlO1NmdNxRmpb25hC/xULSuihfyWc3EyX/YU5MiQoR0=; b=X3Lp8WzpDUTVA2w/bTPHYYMvqk4U+3DhHBrCsHmqcOxKf714C5+0qYLK6RqpBHn8tl9Y 73IbQyhq5XHT1MpnOeQXCouXaTwHBIJ3iwQVInDvEf8WKil9SLbXPgaOj8QOxQz2GsRM UPwWLBahU3IeVN5C6UcgrEEuxs7JbG+1QnI= Received: from maileast.thefacebook.com ([163.114.130.16]) by m0089730.ppops.net with ESMTP id 351aqe0unq-6 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 24 Nov 2020 19:01:33 -0800 Received: from intmgw001.41.prn1.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::c) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Tue, 24 Nov 2020 19:01:28 -0800 Received: by devvm3388.prn0.facebook.com (Postfix, from userid 111017) id 8365016A18C1; Tue, 24 Nov 2020 19:01:22 -0800 (PST) From: Roman Gushchin To: CC: , , , , , , , Subject: [PATCH bpf-next v8 32/34] bpf: eliminate rlimit-based memory accounting infra for bpf maps Date: Tue, 24 Nov 2020 19:01:17 -0800 Message-ID: <20201125030119.2864302-33-guro@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20201125030119.2864302-1-guro@fb.com> References: <20201125030119.2864302-1-guro@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312,18.0.737 definitions=2020-11-24_11:2020-11-24,2020-11-24 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 spamscore=0 bulkscore=0 priorityscore=1501 lowpriorityscore=0 adultscore=0 impostorscore=0 malwarescore=0 mlxscore=0 clxscore=1015 phishscore=0 mlxlogscore=999 suspectscore=38 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011250018 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Remove rlimit-based accounting infrastructure code, which is not used anymore. To provide a backward compatibility, use an approximation of the bpf map memory footprint as a "memlock" value, available to a user via map info. The approximation is based on the maximal number of elements and key and value sizes. Signed-off-by: Roman Gushchin Acked-by: Song Liu --- include/linux/bpf.h | 12 --- kernel/bpf/syscall.c | 94 +++---------------- .../selftests/bpf/progs/bpf_iter_bpf_map.c | 2 +- .../selftests/bpf/progs/map_ptr_kern.c | 7 -- 4 files changed, 16 insertions(+), 99 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index b11436cb9e3d..c9322adedd50 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -137,11 +137,6 @@ struct bpf_map_ops { const struct bpf_iter_seq_info *iter_seq_info; }; =20 -struct bpf_map_memory { - u32 pages; - struct user_struct *user; -}; - struct bpf_map { /* The first two cachelines with read-mostly members of which some * are also accessed in fast-path (e.g. ops, max_entries). @@ -162,7 +157,6 @@ struct bpf_map { u32 btf_key_type_id; u32 btf_value_type_id; struct btf *btf; - struct bpf_map_memory memory; #ifdef CONFIG_MEMCG_KMEM struct mem_cgroup *memcg; #endif @@ -1223,12 +1217,6 @@ void bpf_map_inc_with_uref(struct bpf_map *map); struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map); void bpf_map_put_with_uref(struct bpf_map *map); void bpf_map_put(struct bpf_map *map); -int bpf_map_charge_memlock(struct bpf_map *map, u32 pages); -void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages); -int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size); -void bpf_map_charge_finish(struct bpf_map_memory *mem); -void bpf_map_charge_move(struct bpf_map_memory *dst, - struct bpf_map_memory *src); void *bpf_map_area_alloc(u64 size, int numa_node); void *bpf_map_area_mmapable_alloc(u64 size, int numa_node); void bpf_map_area_free(void *base); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 4b514bb5cd70..3dadf34c3dd0 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -343,77 +343,6 @@ void bpf_map_init_from_attr(struct bpf_map *map, uni= on bpf_attr *attr) map->numa_node =3D bpf_map_attr_numa_node(attr); } =20 -static int bpf_charge_memlock(struct user_struct *user, u32 pages) -{ - unsigned long memlock_limit =3D 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); -} - -int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size) -{ - u32 pages =3D round_up(size, PAGE_SIZE) >> PAGE_SHIFT; - struct user_struct *user; - int ret; - - if (size >=3D U32_MAX - PAGE_SIZE) - return -E2BIG; - - user =3D get_current_user(); - ret =3D bpf_charge_memlock(user, pages); - if (ret) { - free_uid(user); - return ret; - } - - mem->pages =3D pages; - mem->user =3D user; - - return 0; -} - -void bpf_map_charge_finish(struct bpf_map_memory *mem) -{ - bpf_uncharge_memlock(mem->user, mem->pages); - free_uid(mem->user); -} - -void bpf_map_charge_move(struct bpf_map_memory *dst, - struct bpf_map_memory *src) -{ - *dst =3D *src; - - /* Make sure src will not be used for the redundant uncharging. */ - memset(src, 0, sizeof(struct bpf_map_memory)); -} - -int bpf_map_charge_memlock(struct bpf_map *map, u32 pages) -{ - int ret; - - ret =3D bpf_charge_memlock(map->memory.user, pages); - if (ret) - return ret; - map->memory.pages +=3D pages; - return ret; -} - -void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages) -{ - bpf_uncharge_memlock(map->memory.user, pages); - map->memory.pages -=3D pages; -} - static int bpf_map_alloc_id(struct bpf_map *map) { int id; @@ -533,14 +462,11 @@ static void bpf_map_release_memcg(struct bpf_map *m= ap) static void bpf_map_free_deferred(struct work_struct *work) { struct bpf_map *map =3D container_of(work, struct bpf_map, work); - struct bpf_map_memory mem; =20 - bpf_map_charge_move(&mem, &map->memory); security_bpf_map_free(map); bpf_map_release_memcg(map); /* implementation dependent freeing */ map->ops->map_free(map); - bpf_map_charge_finish(&mem); } =20 static void bpf_map_put_uref(struct bpf_map *map) @@ -601,6 +527,19 @@ static fmode_t map_get_sys_perms(struct bpf_map *map= , struct fd f) } =20 #ifdef CONFIG_PROC_FS +/* Provides an approximation of the map's memory footprint. + * Used only to provide a backward compatibility and display + * a reasonable "memlock" info. + */ +static unsigned long bpf_map_memory_footprint(struct bpf_map *map) +{ + unsigned long size; + + size =3D round_up(map->key_size + bpf_map_value_size(map), 8); + + return round_up(map->max_entries * size, PAGE_SIZE); +} + static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) { const struct bpf_map *map =3D filp->private_data; @@ -619,7 +558,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, s= truct file *filp) "value_size:\t%u\n" "max_entries:\t%u\n" "map_flags:\t%#x\n" - "memlock:\t%llu\n" + "memlock:\t%lu\n" "map_id:\t%u\n" "frozen:\t%u\n", map->map_type, @@ -627,7 +566,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, s= truct file *filp) map->value_size, map->max_entries, map->map_flags, - map->memory.pages * 1ULL << PAGE_SHIFT, + bpf_map_memory_footprint(map), map->id, READ_ONCE(map->frozen)); if (type) { @@ -870,7 +809,6 @@ static int map_check_btf(struct bpf_map *map, const s= truct btf *btf, static int map_create(union bpf_attr *attr) { int numa_node =3D bpf_map_attr_numa_node(attr); - struct bpf_map_memory mem; struct bpf_map *map; int f_flags; int err; @@ -969,9 +907,7 @@ static int map_create(union bpf_attr *attr) security_bpf_map_free(map); free_map: btf_put(map->btf); - bpf_map_charge_move(&mem, &map->memory); map->ops->map_free(map); - bpf_map_charge_finish(&mem); return err; } =20 diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_bpf_map.c b/tools= /testing/selftests/bpf/progs/bpf_iter_bpf_map.c index 08651b23edba..b83b5d2e17dc 100644 --- a/tools/testing/selftests/bpf/progs/bpf_iter_bpf_map.c +++ b/tools/testing/selftests/bpf/progs/bpf_iter_bpf_map.c @@ -23,6 +23,6 @@ int dump_bpf_map(struct bpf_iter__bpf_map *ctx) =20 BPF_SEQ_PRINTF(seq, "%8u %8ld %8ld %10lu\n", map->id, map->refcnt.count= er, map->usercnt.counter, - map->memory.user->locked_vm.counter); + 0LLU); return 0; } diff --git a/tools/testing/selftests/bpf/progs/map_ptr_kern.c b/tools/tes= ting/selftests/bpf/progs/map_ptr_kern.c index c325405751e2..d8850bc6a9f1 100644 --- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c +++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c @@ -26,17 +26,12 @@ __u32 g_line =3D 0; return 0; \ }) =20 -struct bpf_map_memory { - __u32 pages; -} __attribute__((preserve_access_index)); - struct bpf_map { enum bpf_map_type map_type; __u32 key_size; __u32 value_size; __u32 max_entries; __u32 id; - struct bpf_map_memory memory; } __attribute__((preserve_access_index)); =20 static inline int check_bpf_map_fields(struct bpf_map *map, __u32 key_si= ze, @@ -47,7 +42,6 @@ static inline int check_bpf_map_fields(struct bpf_map *= map, __u32 key_size, VERIFY(map->value_size =3D=3D value_size); VERIFY(map->max_entries =3D=3D max_entries); VERIFY(map->id > 0); - VERIFY(map->memory.pages > 0); =20 return 1; } @@ -60,7 +54,6 @@ static inline int check_bpf_map_ptr(struct bpf_map *ind= irect, VERIFY(indirect->value_size =3D=3D direct->value_size); VERIFY(indirect->max_entries =3D=3D direct->max_entries); VERIFY(indirect->id =3D=3D direct->id); - VERIFY(indirect->memory.pages =3D=3D direct->memory.pages); =20 return 1; } --=20 2.26.2