Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp7974638imm; Thu, 28 Jun 2018 12:21:43 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdT1FgclYxjVgZkpLR9z1Pr4FZSUdGplObHTgJlq4DhTY4FLy/O8/tQsEz/2VI8vir46IdY X-Received: by 2002:a62:5991:: with SMTP id k17-v6mr11441057pfj.94.1530213703314; Thu, 28 Jun 2018 12:21:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530213703; cv=none; d=google.com; s=arc-20160816; b=PDvG0n4QgSTk/mZndMXP6CbZq2vGb6OQHtQAP0Oa4Y+ViqFDLykmoaJ1AdxnE6o+TM vIlT12+SYfA274J7UuZO4+a+5WG4Fu5g/vWtHn386tq3SsUhRYw9q3YlDf0CUd2nXy3l /V3a6pgkfVglsJ5UcJ3gDPOphBOVpQsA432Zo5r+HMba467H5qRm4/lLAFzsQEuhj64E GLHwJJ7ufc0V85OUGqXDCUqz1hI6jcMIpythYfsZZ01qFr6K2gSpezEoYnAqnvpn+xMJ 2faG07JNvQrWA1FR6TU7K3soRdXE3tyEqbxrME0dK0oWv5mAcRhP1fR+EHQFRpHULTbD kQ6Q== 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=Hsn1aP61WY5mi9caUh2vv9xRR+cwVrxtn9nVVxcjotg=; b=PNxWrCFRmN1Q4cSOxBxaNc2A/bVnKhjo27gVFnEKwVr90Mt7qSmXSjWzgipxp5ijmZ lQKlu8rR5IluTTll2S29ThmQWgigBMrDqCkYeEP4OErKrAIocRI72ML4zT2nmz5wyT/X 7hGzQJBp0KANiNO3I5GQ5e8BGlSbxkcwuMd76ZHzbRl23L2rlxLVfhJ387hauiUh0KW8 4Psabe9U7p3xGaqCE9qfi0diJ5MR7jH0itZ05lrsVl4j/a3kRJHkRrZfAHN9IQUKkwoG iZ9FOvdX7enGLSUNPAMpAzx11B53D7KnstcmOLpKQNYn7G3Rg7XZeH5JtD4vgjVH/N5Q pSTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=BtuteRtq; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=gaQ4gC6q; 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 x6-v6si7293037pll.24.2018.06.28.12.21.28; Thu, 28 Jun 2018 12:21:43 -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=BtuteRtq; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=gaQ4gC6q; 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 S1030293AbeF1QvP (ORCPT + 99 others); Thu, 28 Jun 2018 12:51:15 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:56926 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S935749AbeF1Qs1 (ORCPT ); Thu, 28 Jun 2018 12:48:27 -0400 Received: from pps.filterd (m0001255.ppops.net [127.0.0.1]) by mx0b-00082601.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w5SGka5Q013501; Thu, 28 Jun 2018 09:48: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=Hsn1aP61WY5mi9caUh2vv9xRR+cwVrxtn9nVVxcjotg=; b=BtuteRtqRj+owOpmit0dh6//zk5+dcrkuYUzO7GRLZ5CqUF6fwxIuLmeqU+TqHObTHPC 9YgUsChlQJkXFCuaU1w2hr2okluK6FZZnviESEc0Qf56hN1WUiS9cjV+ToyZt6zsoF2u 8fVQAVCGgyskDEGv6jzlu+PsSoIukDujyU4= Received: from maileast.thefacebook.com ([199.201.65.23]) by mx0b-00082601.pphosted.com with ESMTP id 2jw2u0074v-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Thu, 28 Jun 2018 09:48:05 -0700 Received: from NAM02-BL2-obe.outbound.protection.outlook.com (192.168.183.28) by o365-in.thefacebook.com (192.168.177.26) with Microsoft SMTP Server (TLS) id 14.3.361.1; Thu, 28 Jun 2018 12:48:03 -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=Hsn1aP61WY5mi9caUh2vv9xRR+cwVrxtn9nVVxcjotg=; b=gaQ4gC6q/+mbzEwIWMtMG3lmkDp/pjqLzjshdn/nqIQAyZ7gc/qZ+HrSUnUF8dQ9N886ahIEJesOBOlhhkEjKuYwslfB3eeAJRXBz11uZ8I24zFWEWHpaVzjJBy7To/T82ItzL7eGNecVjHoc1DoOOeuTmmGIhPD6AE2Bg+Yc3g= Received: from castle.thefacebook.com (2620:10d:c090:200::4:4716) by DM3PR15MB1081.namprd15.prod.outlook.com (2603:10b6:0:12::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.884.24; Thu, 28 Jun 2018 16:47:55 +0000 From: Roman Gushchin To: CC: , , , Roman Gushchin , Alexei Starovoitov , Daniel Borkmann Subject: [PATCH bpf-next 01/14] bpf: add ability to charge bpf maps memory dynamically Date: Thu, 28 Jun 2018 09:47:06 -0700 Message-ID: <20180628164719.28215-2-guro@fb.com> X-Mailer: git-send-email 2.14.4 In-Reply-To: <20180628164719.28215-1-guro@fb.com> References: <20180628164719.28215-1-guro@fb.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [2620:10d:c090:200::4:4716] X-ClientProxiedBy: MW2PR16CA0046.namprd16.prod.outlook.com (2603:10b6:907:1::23) To DM3PR15MB1081.namprd15.prod.outlook.com (2603:10b6:0:12::7) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 756120de-7245-4b79-83bd-08d5dd16e63f X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652034)(8989117)(4534165)(4627221)(201703031133081)(201702281549075)(8990107)(5600026)(711020)(2017052603328)(7153060)(7193020);SRVR:DM3PR15MB1081; X-Microsoft-Exchange-Diagnostics: 1;DM3PR15MB1081;3:BgFfXkYtt4i8uhzl9chthspDJ3fdCHgSeX+nBd9EaKB7IhPIguE+iwMSoYvqgOujcOpULLYblTe6mTs9eG9ocdNiaBVOQYFDXVVpMB4rdJR1J4Ue+ARKkTYUDxtaSfFlp7OwG4ErgdgqUo2ZC2kVrpM8roJR37m6jrRrq0En4hlCEXScHVFf59U65ZtL/B1qIlfLyChk9jRwDHfEy8EAxq6X+rAtriFWXSomWZoSca74CJXbjV0tIQsKxmfi2EHw;25:GHBxv0nRraXsDKFOslEU25YDpI2spAd+RezOFwbhUYFgWcmcHMRUzORhV1wN60wv2AoKYI+G6jRI+ELaGwwsPttsoxNxdEXUXv8LsxZ81wgZWmmCZK0SPEUStYD48aWJBkh9mBdA73Dw0WncwLn3JfxDJYKeQod0/QU4Y5vgyg1/zrppQNtFaOjrwqIlvmYV5LmsmHxTH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nPdRiwpTFZyq5GGn13zxqy7TZ2/gmPhodd7pGrOGysLYYVZ32UVbadHJdEQz4PGxwCIa6MFVXLtUvMo+7Hns4BnGAcXbpk8xQRQl3cn1n/jpo2gGe74tu9McdiD8XZUf5eD6qaGQ4piKKvv3GmV748sb77dQF/2/KRgzzJKXUfPzQBzajqA8Xjf/Z4+Si3gRw2o70o7/Lp2UOU7OaAIsennuvi3hx2Cjy9/65XwBw5jDAknczg2DDjZLWGbW/YU= 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)(8121501046)(5005006)(3002001)(10201501046)(93006095)(93001095)(3231254)(11241501184)(944501410)(52105095)(149027)(150027)(6041310)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(20161123558120)(20161123562045)(6072148)(201708071742011)(7699016);SRVR:DM3PR15MB1081;BCL:0;PCL:0;RULEID:;SRVR:DM3PR15MB1081; X-Forefront-PRVS: 0717E25089 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(136003)(396003)(346002)(39860400002)(376002)(366004)(189003)(199004)(8676002)(6512007)(4326008)(81166006)(81156014)(69596002)(97736004)(52116002)(2361001)(51416003)(8936002)(76176011)(305945005)(53416004)(186003)(16526019)(6506007)(386003)(106356001)(105586002)(2906002)(25786009)(36756003)(52396003)(7736002)(68736007)(50226002)(5660300001)(6666003)(53936002)(6916009)(48376002)(50466002)(478600001)(6486002)(1076002)(47776003)(6116002)(86362001)(14444005)(16586007)(54906003)(46003)(476003)(486006)(446003)(2351001)(11346002)(2616005)(316002)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:DM3PR15MB1081;H:castle.thefacebook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; Received-SPF: None (protection.outlook.com: fb.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;DM3PR15MB1081;23:X9priZlpVy26jgXHt7/tcFzkKcyr5fjHo7cagKROR?= =?us-ascii?Q?etj9H162XM5tobZ/TepTvp4AJ24PZAzrf5ft696VctLXfAcJ1CwzR+EeVN0T?= =?us-ascii?Q?A3UTCFcnbbAVMJM2M8LldhzrbYB+46N6RjhwJZIW9X7/kbK54/H3WaEWpBJw?= =?us-ascii?Q?DJkwG5pkaGyiXfMNKSGiBZhkgcRVWrmAobFwKtDjH6AYret5AnNIFsVps3TO?= =?us-ascii?Q?GdSXBh85KxkOl3Ww++F8EsyDU+qsQ8MiV7HLuL9BychBtKtu6EOwJNlE9/ME?= =?us-ascii?Q?jPgBcoRGfT9Zs2322skPSiFQYu63/g/88lUOUvF3bYtzY5ZIm4GjmblTjv13?= =?us-ascii?Q?qE1E6ZcRpaCoiBGBTr/qxQtsNuNIWMOldTyCosmEiRiWdQZGCF0weFV1KWyj?= =?us-ascii?Q?q6FQnyOro75JRsschlu8+H7q3o9WUmh6sLTeuryhyhMI+eIOhu0ydg2oL7L9?= =?us-ascii?Q?OGlk0/Tw2NEeC1x2QveNqj30iflXbNwbga7OkO1B9XfFw7skDq65AxX+vG3N?= =?us-ascii?Q?nzIFkdlRokGrvqYNqiUxyq9l2VnErAEHWXYwjxNU1NWDEhEkKQu29IcSwyCj?= =?us-ascii?Q?gLJtJ5eYyRQqqLWAm9qrLRALBnQ3sBwIcw4kMc2GNs6+U5yvPP60MYI7+onB?= =?us-ascii?Q?ffGgj0m66j/SlbvTuLqJs6mhxp3TUZSw+84SZ9EpcriO8Oy4oaJSfFu2fQ3A?= =?us-ascii?Q?cKB/QQw4I89yKzDcw81Oe1AjtYUm1hmtNxPtRfQxWRJE/z2q6TE3iVhaCA8H?= =?us-ascii?Q?Ca+ff9+NvcezGZw/iSUSSg42wJr8W8OXp05jN65MnvSaitYuaMr4Qgwr6//5?= =?us-ascii?Q?QxXi2rf5T4vZ9cUEWKSu9R1oQTSW0Z9SKDAI09FIy9yzaDHo7Al9vYSNf9is?= =?us-ascii?Q?xrmWk0eaeIT6adRguSNIjgYo3oAkRKOiIy5YUkpLTEUgjsyB8QOhexKJiAGF?= =?us-ascii?Q?6/S2NfbzLv8AWOWkdzi5i6/xITEf/0+xSAbpOYjPfZW3bfIPx9sAeeXCZ6/e?= =?us-ascii?Q?tsKMPV5In76lsOmDkgk4OeTJyZ7rUAaYl3lVQS9iUurP5kIu/gT8YjPBByxM?= =?us-ascii?Q?Vw5zWokMCUEI48F/P0sYgBgwvp97UivmDQH9Z6tLemxruRxiHT1puO5NZtu+?= =?us-ascii?Q?29ja8gmkVE9V9VG3OLxJr6shz6XjQAAl44h7IhxFa9shMFjenj9ARHv6du8j?= =?us-ascii?Q?cI+iYPrDCisORy1ZIs9Ut2qGUYk8AAj48qZZz/K88fDyy6MMAdnB8H5yXANc?= =?us-ascii?Q?Qq/E83m88q7INIqQFEeP4ZdaTBhAa5lNDaceqJrGgXKFoWRuYY+VVvQPEe7Z?= =?us-ascii?B?QT09?= X-Microsoft-Antispam-Message-Info: YBitIlHHdO+jMfs3A+88xSXbfXkT1VtqvBHGA+nvNlNlI2xbcdu0Ca9Q02zcBj5qaNHnWh0kn/z/1eqCZVx9SdyDtOxqbKV7EJoVKMxRc65Uey5qLuAvRRZyjkEs6mjCW2IAbKMX2xiI4GerldxYhoOLqqI91mmca7HAQeHe40SQk9BcAWBH/44UZeNBl0H7vbi43v1Adi1WF2q7n4u6eP9pWot18hwlc+TBT0AAJ59nsy0lxi0q8WVOwsaXOJXJ9b/0O2iYRlcrdsosfavPENZrJum71N9pweV31QRX59/oGjg1xcG4QvHQTpNRvVbi8+piByNnpdeoRE2v+eJ1P0ArIoUPRcQ9U4HoY/6M23c= X-Microsoft-Exchange-Diagnostics: 1;DM3PR15MB1081;6:aVgHCTaWCxqTnItuwSnflKVritZ4pshWA7puu2c7zv+MwqHCJaOB3v+KBON0bh0bs1E5c53XjWt2l4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61PM6hZ+z3q4EKYzZPHgKqeAkV+JgSJmnEoyRkaOxsxmyiC7TxwazHIpMqd5AI61Xut/C61UbcudEHU10WYq+Fz7E7KZMhRIFy85mZ8G4hgs+jU= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jun 2018 16:47:55.7390 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 756120de-7245-4b79-83bd-08d5dd16e63f X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM3PR15MB1081 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-06-28_08:,, 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 | 53 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 7df32a3200f7..e4d684ce3f5e 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -434,6 +434,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 35dc466641f2..e03aeeec01e0 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -181,32 +181,55 @@ 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 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) +{ + atomic_long_sub(pages, &map->user->locked_vm); + map->pages -= pages; +} + static int bpf_map_alloc_id(struct bpf_map *map) { int id; @@ -256,7 +279,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 +515,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 +538,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