Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1301853imm; Fri, 27 Jul 2018 14:57:56 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcJb4KNL8kVAtMJWlqypQ8uRqAIsubW+n+3mKtBvPWjLuGDwLTLqljcMA1GK/CoJb7vLJgW X-Received: by 2002:a63:2013:: with SMTP id g19-v6mr7574308pgg.68.1532728676174; Fri, 27 Jul 2018 14:57:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532728676; cv=none; d=google.com; s=arc-20160816; b=ma+ibxL9wiy4xKeL1MRi0Bh3l1vwdYsINS75uhNJbuxPZDOXWatCkvk5S2WabwCKv5 ksatqTuM7FJSQWw9C7Brx0sglWBUI1aFh7g1ZiWReemionNyM/JSjPYoNApdABWTGCHm LPmKRSsPcoMEjFi7M0jXvb2eDWTe03V8c4GPAyGuAtcB4MwJaFJW0xu0GurtYmm09A1H X+9u6QAy5uA5m+osjIxAzIYzJH5dAB2CM1rGUQd7vFWdTzPpQL2e2294beckfHl6fjhK iTw2SYGRakcpQU/NQ87TCl0+XxdqH8g52A4Qn+bwGPZiysFL3/oehRfw7vm4V5uzLCs2 oueA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature:dkim-signature :arc-authentication-results; bh=/x8N1Jk6gVN+sXKP9uWCZsYhlj0Usy2cY3fL7ddilXg=; b=GIIxXMnZfktd5j8jRBIToOlJBBSDr28buqDi7jWSOmPh6ZjZRrZ0VF6dz3LO+g1zB1 HIGsPkCLSKW1oGErTeZQMTuUnBq52sYjDuj/KhSXwDlA9EPifmK4siENwfIuZOyioi10 P0VfmIqdkbubCcHTf2L79r0Kax0PcEdEHcR0EmkCKuQO1xOlzGq59F6qEGwdXxGA1lEW 1iOJE/yNNYBRlDxIbnXPOPlOX8b2cRuzTs4gXEwQ4dvU/Z88b7KsvC/jdRkmJ/eLY9gE eL90/RIhnZpwm85gNxVTRoCZFd/+NXA+VWf3G/hTLXsKXQRhBrouIIYsB7UmBx2dKQRF 9VNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=YQGmzLIZ; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=QzgQ9Sfy; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f16-v6si5107962pfd.276.2018.07.27.14.57.41; Fri, 27 Jul 2018 14:57:56 -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=@fb.com header.s=facebook header.b=YQGmzLIZ; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=QzgQ9Sfy; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389905AbeG0XRS (ORCPT + 99 others); Fri, 27 Jul 2018 19:17:18 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:49316 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389766AbeG0XRR (ORCPT ); Fri, 27 Jul 2018 19:17:17 -0400 Received: from pps.filterd (m0044008.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w6RLiZv8014706; Fri, 27 Jul 2018 14:53:05 -0700 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-type; s=facebook; bh=/x8N1Jk6gVN+sXKP9uWCZsYhlj0Usy2cY3fL7ddilXg=; b=YQGmzLIZhlk7SEiHDsS7G8E/pHBIB0/AAeolG3ewWtilc4oEbz8ZsPTCg5WqTCVkgH3D mC1JEoZdaXK5AbtPce61tXI2c3NYdgdtvraPdXVthZp6mbNpBZb2XhMkrNuIY43k8YX3 CkfwG9FZDpua1lhMInnkHWSCqmQXHThS1tA= Received: from maileast.thefacebook.com ([199.201.65.23]) by mx0a-00082601.pphosted.com with ESMTP id 2kgb59g2n0-3 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 27 Jul 2018 14:53:05 -0700 Received: from NAM04-CO1-obe.outbound.protection.outlook.com (192.168.183.28) by o365-in.thefacebook.com (192.168.177.32) with Microsoft SMTP Server (TLS) id 14.3.361.1; Fri, 27 Jul 2018 17:53:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com; s=selector1-fb-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/x8N1Jk6gVN+sXKP9uWCZsYhlj0Usy2cY3fL7ddilXg=; b=QzgQ9SfyIBLHUFd/DszGlVFiidgc2m+DIWhWcfreGpB8vMFCsnOJMWAhAVWuj8l0Oaz15IJwPl+qEpRclJ8OD4AiIJPQVutebqQqV8IyVLSTrUkH/1l9a/eE1wbkt/0m1w3LjnGCnbibIa4KCQE3C8sSpUjXWyUbCNrH2WdY0Kw= Received: from castle.thefacebook.com (2620:10d:c090:200::5:fd07) by CY1PR15MB0171.namprd15.prod.outlook.com (2a01:111:e400:58bb::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.995.17; Fri, 27 Jul 2018 21:53:00 +0000 From: Roman Gushchin To: CC: , , Roman Gushchin , Alexei Starovoitov , Daniel Borkmann Subject: [PATCH v4 bpf-next 01/14] bpf: add ability to charge bpf maps memory dynamically Date: Fri, 27 Jul 2018 14:52:30 -0700 Message-ID: <20180727215243.3850-2-guro@fb.com> X-Mailer: git-send-email 2.14.4 In-Reply-To: <20180727215243.3850-1-guro@fb.com> References: <20180727215243.3850-1-guro@fb.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [2620:10d:c090:200::5:fd07] X-ClientProxiedBy: MWHPR11CA0020.namprd11.prod.outlook.com (2603:10b6:301:1::30) To CY1PR15MB0171.namprd15.prod.outlook.com (2a01:111:e400:58bb::17) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3b569751-14cd-4948-16c1-08d5f40b5273 X-Microsoft-Antispam: BCL:0;PCL:0;RULEID:(7020095)(4652040)(8989117)(5600074)(711020)(4534165)(4627221)(201703031133081)(201702281549075)(8990107)(2017052603328)(7153060)(7193020);SRVR:CY1PR15MB0171; X-Microsoft-Exchange-Diagnostics: 1;CY1PR15MB0171;3:oL67KgFuCX128pIuLXFvd3z+/2lNXeRAxsrNeIX1Tgun5TAeoRxt2F49B7axyso/cEEgpbAG/fDdmxT+c8B24J3Mtv0Y/6hLCLq1ZZ7lVCNfQAPQLboVAT0le53B/ufFjE7b/7wqVWP76EtAETbWP/OlmpS19INo0bRCLHFhzzHrmKGkZ4KJosZtlC77d1wE+QXIroibJhu7WUUa2ZOyeKQAZLkKwYRvVf52Wjuf7CIZLMRwYCsx1pkq1DyEg6vp;25:7r1k49TTmsuLR+D+VEcqxPhO1lHXV1838uUjRfit2Vr3Ejbm6aLcONSon60aMJ/2u0JJPdUuRGXgw1fgnwRxuJ8KhEElfeeEdiohW+R8awzB9jALnyJRtkzUOllRTZEHow8A5woreI0Jv2RRYT+hhm6qR3Tam7MTi99wiynNIxHQ7OzcIDBu7qTr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0aQXd/NHYNwB/EEVIY9OpZ4qtZ0XhCATkLIpgUfHEk+1Rjiq5Xlu0FH1eZTrPfBogXdQW10H054pFZtciUak+VC0t+7vEG6o7/rWsm+JDV4QpDo3IsM+AocNADFHpx/FDywnF8HxN2845CDyC0j72YwB7vLQZplQkqORMAy+Qnw+vN63td6zXq3zZi/EDcWlBzJcqzRm0zrbjtL/rgJmhLxzUSqjuS5VLlwt2o7x4/An3ugODRPFEBQuKYNbHWIP0= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(192374486261705)(67672495146484); X-MS-Exchange-SenderADCheck: 1 X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(3231311)(11241501184)(944501410)(52105095)(10201501046)(3002001)(93006095)(93001095)(149027)(150027)(6041310)(20161123562045)(20161123560045)(20161123564045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011)(7699016);SRVR:CY1PR15MB0171;BCL:0;PCL:0;RULEID:;SRVR:CY1PR15MB0171; X-Forefront-PRVS: 07467C4D33 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(366004)(39860400002)(136003)(376002)(346002)(396003)(199004)(189003)(478600001)(36756003)(52116002)(76176011)(106356001)(52396003)(316002)(50226002)(53416004)(51416003)(6512007)(68736007)(50466002)(48376002)(105586002)(2361001)(4326008)(53936002)(25786009)(16586007)(54906003)(2351001)(97736004)(6506007)(386003)(305945005)(7736002)(11346002)(446003)(46003)(486006)(2616005)(69596002)(6666003)(186003)(16526019)(5660300001)(47776003)(476003)(6486002)(6916009)(8936002)(86362001)(575784001)(6116002)(14444005)(1076002)(81156014)(8676002)(81166006)(2906002)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:CY1PR15MB0171;H:castle.thefacebook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; Received-SPF: None (protection.outlook.com: fb.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;CY1PR15MB0171;23:S4ks0mP40XTKxm6y6nQIVQjBD1/+S1kf1w33hDNyn?= =?us-ascii?Q?sVvjgPcdVI6of5aYDZr9TE9oh9Cod+GvnY/3pnOgbtKtgKykmyAGAjiCtPA3?= =?us-ascii?Q?dtkYa4g1juiwUHwzKe1xUNbIeDRAaXmvW1LdoTk9xEP50K9D8ZWe8ma7j+Hz?= =?us-ascii?Q?mo8+rCe54PeCm/QdSPuXqykEdIY5X36kBaWvM8XL7HLR5OqkoF4xAdKHK8Di?= =?us-ascii?Q?4c12Q0Kow9IKT8gA+cbg0rZa9lzKnjzTFSDv4bJoqTMJTx31Nf8VbEqW2d4M?= =?us-ascii?Q?EG0r47cuUOdKQ+gjl746IDwVxi4jrv0jdroOvZWIjYFsk5tgOKvlHVmFgUSi?= =?us-ascii?Q?xmMv37+xUrc+y5ZnMld7IekZR08V+CPxLMH7u9658AKOYAUeyeEIwFktB+Xm?= =?us-ascii?Q?cCrbOzHmxq/NOoCbOFWxFnkcKsZfU3ejDbS+lj3AmF29pscsV9MIB83/uFIm?= =?us-ascii?Q?VGlkYedMKAzQFMmNDUJ2s8hG2PxgRXFFhv7TGdqMnsln9JE8jl12MeN2t8Wv?= =?us-ascii?Q?KDmHd0qRSG3nN3TISrJ3cXcAMJ6gF0GgqlLj4LmRJeQ5d9ETwrZcT8R7M+Zv?= =?us-ascii?Q?gAKt3i60vmS8AQRnI6o6uzbhQ38bAfvEoX7UlYL1shi7U/IcERadK8tRm+oj?= =?us-ascii?Q?Pkbp2LiR/irjoph/vBC4kV+NghV0hX1FKX+QeZA1llTHvg5lNP4IBVS/0/4s?= =?us-ascii?Q?l+Swoa8Ba52rgMGScHV2SqTZq4TomNInsAI0wL8BQUy0gLBYNLwpKouE5vwd?= =?us-ascii?Q?XiJvBi19iA2RoOILTf2HtO94DMDyvhJKFztdXr+YmS3319bSZiHukRl7uf0f?= =?us-ascii?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ZwdRquMmSe/R4Ca+xTNC2dPNd4A0u1U+HAE= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2018 21:53:00.3401 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3b569751-14cd-4948-16c1-08d5f40b5273 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR15MB0171 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-07-27_09:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This commits extends existing bpf maps memory charging API to support dynamic charging/uncharging. This is required to account memory used by maps, if all entries are created dynamically after the map initialization. Signed-off-by: Roman Gushchin Cc: Alexei Starovoitov Cc: Daniel Borkmann Acked-by: Martin KaFai Lau --- include/linux/bpf.h | 2 ++ kernel/bpf/syscall.c | 58 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 5b5ad95cf339..5a4a256473c3 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -435,6 +435,8 @@ struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref); void bpf_map_put_with_uref(struct bpf_map *map); void bpf_map_put(struct bpf_map *map); int bpf_map_precharge_memlock(u32 pages); +int bpf_map_charge_memlock(struct bpf_map *map, u32 pages); +void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages); void *bpf_map_area_alloc(size_t size, int numa_node); void bpf_map_area_free(void *base); void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index a31a1ba0f8ea..40119aef9ddc 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -181,32 +181,60 @@ int bpf_map_precharge_memlock(u32 pages) return 0; } -static int bpf_map_charge_memlock(struct bpf_map *map) +static int bpf_charge_memlock(struct user_struct *user, u32 pages) { - struct user_struct *user = get_current_user(); - unsigned long memlock_limit; + unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - 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; +} - atomic_long_add(map->pages, &user->locked_vm); +static void bpf_uncharge_memlock(struct user_struct *user, u32 pages) +{ + atomic_long_sub(pages, &user->locked_vm); +} + +static int bpf_map_init_memlock(struct bpf_map *map) +{ + struct user_struct *user = get_current_user(); + int ret; - if (atomic_long_read(&user->locked_vm) > memlock_limit) { - atomic_long_sub(map->pages, &user->locked_vm); + ret = bpf_charge_memlock(user, map->pages); + if (ret) { free_uid(user); - return -EPERM; + return ret; } map->user = user; - return 0; + return ret; } -static void bpf_map_uncharge_memlock(struct bpf_map *map) +static void bpf_map_release_memlock(struct bpf_map *map) { struct user_struct *user = map->user; - - atomic_long_sub(map->pages, &user->locked_vm); + atomic_long_sub(map->pages, &map->user->locked_vm); free_uid(user); } +int bpf_map_charge_memlock(struct bpf_map *map, u32 pages) +{ + int ret; + + ret = bpf_charge_memlock(map->user, pages); + if (ret) + return ret; + map->pages += pages; + return ret; +} + +void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages) +{ + bpf_uncharge_memlock(map->user, pages); + map->pages -= pages; +} + static int bpf_map_alloc_id(struct bpf_map *map) { int id; @@ -256,7 +284,7 @@ static void bpf_map_free_deferred(struct work_struct *work) { struct bpf_map *map = container_of(work, struct bpf_map, work); - bpf_map_uncharge_memlock(map); + bpf_map_release_memlock(map); security_bpf_map_free(map); /* implementation dependent freeing */ map->ops->map_free(map); @@ -492,7 +520,7 @@ static int map_create(union bpf_attr *attr) if (err) goto free_map_nouncharge; - err = bpf_map_charge_memlock(map); + err = bpf_map_init_memlock(map); if (err) goto free_map_sec; @@ -515,7 +543,7 @@ static int map_create(union bpf_attr *attr) return err; free_map: - bpf_map_uncharge_memlock(map); + bpf_map_release_memlock(map); free_map_sec: security_bpf_map_free(map); free_map_nouncharge: -- 2.14.4