Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp1054744imm; Thu, 5 Jul 2018 13:53:44 -0700 (PDT) X-Google-Smtp-Source: AAOMgpelGLhw46A5UuGmPAPv1TyAqC7by411P87W0m4/TJ9vE20ctL/pyod/SvD1PbYtUjSoygXw X-Received: by 2002:a63:b91c:: with SMTP id z28-v6mr7002702pge.22.1530824024454; Thu, 05 Jul 2018 13:53:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530824024; cv=none; d=google.com; s=arc-20160816; b=T4cGrzt6xhpz+KAEctSyfc9Q7OvEomCpTJbFcxM3qbHh4flnCkm6cgNlhjFhL1Ygxv DqJpQlpYJiJIawDzuFCIdML9K5MbPaA5dBicp8p54kRty2CacCqmDB0ObZ7+gwiyZAzu tfsp1NKC6XuZAgi9AKYoH/Mb8mP3DkFvxoh3KhEr6UhUiR1jZvgfn2aB/QzcLWV+UvVP uS4q4XcCNthZgFyLdK0oKmWHTEZoRDERVL1dggA5nNdb7710EawfLjOXXvZROIhMuBY9 LYoMEdhl5Nn7uPadNIX2itj/dQ+EjsNkTcZmVcP46z0CKSd03875mbRaVu0Qo5hbEeXP yIEQ== 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=c6UKRjwJ1siNdP2ioO76Bf1tZPBBm78eVXDcfxHho1Y=; b=DOI2zlezZfyCEIda+5IC06eZ7NBFEi3khgxWurfgod9i+B8B+pdCDreiKt8rqWUzHl ZH6b0bEfey0Tqqlj/5AHJpK4UrKPaCvLMfXtYbvnsJ+C6lN9TQ77ZfO/ZKSM4d4HXmCI 9P1PFP5NF9QJIT3o2r/2uyDVIAdTpHe0uBg3ZcHNTeiNeqUT55BUF3nOGlq0H07o6vz1 HYI4Hw3qs0OFIkj0yYFS7wAd+OJY7z3zqgMBaY7wxdxgQ3c686WmMSsBWFthuXix3o6M F4gqwoy3N5dXu2OkLIct8D9Xe0l14mrOzQkIWufzvsSvX49tYLhl+yTXdw+lRaCwUNwU VyhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b="F6/l5uVl"; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=jJaBrC55; 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 x9-v6si6520168pln.465.2018.07.05.13.53.29; Thu, 05 Jul 2018 13:53:44 -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="F6/l5uVl"; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=jJaBrC55; 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 S1754335AbeGEUwl (ORCPT + 99 others); Thu, 5 Jul 2018 16:52:41 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:44096 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754074AbeGEUwh (ORCPT ); Thu, 5 Jul 2018 16:52:37 -0400 Received: from pps.filterd (m0148461.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w65KmMvQ015385; Thu, 5 Jul 2018 13:52:14 -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=c6UKRjwJ1siNdP2ioO76Bf1tZPBBm78eVXDcfxHho1Y=; b=F6/l5uVl9a4/V5JfuPs6E5T0sZEIkXJGHvTskOvFeu2FP97TtiH/CUqpVu9Jm2xxDXrv IVgKYJjUNOQq1wdEMFSIphqZG4CRFkex8xiY7djx2/A6hAg180Wmfx7e0xldkjVM8C+A hOIEeGF/ZA/l0x7QCeq/e28i32gesAApdTk= Received: from maileast.thefacebook.com ([199.201.65.23]) by mx0a-00082601.pphosted.com with ESMTP id 2k1r3m0eue-2 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Thu, 05 Jul 2018 13:52:14 -0700 Received: from NAM02-BL2-obe.outbound.protection.outlook.com (192.168.183.28) by o365-in.thefacebook.com (192.168.177.21) with Microsoft SMTP Server (TLS) id 14.3.361.1; Thu, 5 Jul 2018 16:52:12 -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=c6UKRjwJ1siNdP2ioO76Bf1tZPBBm78eVXDcfxHho1Y=; b=jJaBrC55vgekuKkl24uViY6ZQn2/J4Z6brCGdqlcCbWIH0sbJDny8lgLCorA0jfXnCzm5tbnbLphKu+34re2M3luwbGlSgvP5k9WN2ST8nVlzF8BFCAR275rNKGUV/3FnOiEJWN9Tjwh7fGE9kfWd33UjHxaUdR7XsAesJ4wpe4= Received: from castle.thefacebook.com (2620:10d:c090:200::4:9a3d) by BY2PR15MB0166.namprd15.prod.outlook.com (2a01:111:e400:58e0::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.906.26; Thu, 5 Jul 2018 20:52:08 +0000 From: Roman Gushchin To: CC: , , , Roman Gushchin , Alexei Starovoitov , Daniel Borkmann Subject: [PATCH v2 bpf-next 01/14] bpf: add ability to charge bpf maps memory dynamically Date: Thu, 5 Jul 2018 13:51:26 -0700 Message-ID: <20180705205139.3462-2-guro@fb.com> X-Mailer: git-send-email 2.14.4 In-Reply-To: <20180705205139.3462-1-guro@fb.com> References: <20180705205139.3462-1-guro@fb.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [2620:10d:c090:200::4:9a3d] X-ClientProxiedBy: MWHPR13CA0025.namprd13.prod.outlook.com (2603:10b6:300:95::11) To BY2PR15MB0166.namprd15.prod.outlook.com (2a01:111:e400:58e0::12) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6c8f88b5-b807-4f5b-6f49-08d5e2b92ca6 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652040)(8989117)(5600053)(711020)(4534165)(4627221)(201703031133081)(201702281549075)(8990107)(2017052603328)(7153060)(7193020);SRVR:BY2PR15MB0166; X-Microsoft-Exchange-Diagnostics: 1;BY2PR15MB0166;3:gOWdTC8XDjYhcezstqlu91gWC1PNrchxtUdaNniNh6sMHuRs29iOsiVH1MUS+KqDFNr81YTXoKPF5W6z4LhZ8rjMbbbBJSc4+vQlJvc34TNKWQJbNLXyl43VRs45HIgp3njJZlJic7TioW6CfA2v9+/QSsovjg+o6Q3yGUcCmvGokCFVSsKUHsf3uVPOR4Lt9DLmfpZnq55q5km34IgR2ou6e+1jVNSkg08GQTPHoSA5sKVGqsEuiCS9/vRA61Mf;25:rleb+7DFEgQ/cKjINb3kOsXgp6ZcuiZrc1yEIXaHC5Dy1uX8u2HERzu5V3j5WUdRVTzMtylZ2Ec/g+2KLH0iVkon2O8w/QRTo0YpQa4yS4dyT4oQlThdQXx6SfDrqPULIvj8HLDPi8skRdpn1BwaY+Sz6ARjslu0FxqnHRXCSyD8N4/R3TbbN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3pbHOf2RTgKPNN7lrPaC/Urs8wem5j0Y+rFiJUSZOdxLG0rnL9B6+6ai5IKjQBXaOn54PWh2bhbAFtbRhhgb6jsZ1k59J0IW2Fi6jj636pIX1Bzcf/KspTQLP1hyVoKUsl4b0+PifFh/BgR2IpCGpHd/e/3EtOvR+zncDDaocQc9L23GQMYM+G9HHSOAmun2zzBpMNkmjIZ5LdU7DQIMR2swL6ZaH/6ZPP5VCDm3+b8NZLYAj9yI9h1t/1hsPNVs2r0Q= 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)(10201501046)(93006095)(93001095)(3002001)(3231254)(11241501184)(944501410)(52105095)(149027)(150027)(6041310)(20161123558120)(20161123562045)(20161123564045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011)(7699016);SRVR:BY2PR15MB0166;BCL:0;PCL:0;RULEID:;SRVR:BY2PR15MB0166; X-Forefront-PRVS: 0724FCD4CD X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(136003)(376002)(346002)(366004)(396003)(39860400002)(189003)(199004)(11346002)(446003)(5660300001)(25786009)(6512007)(4326008)(53936002)(6916009)(6666003)(6116002)(7736002)(16526019)(50466002)(8936002)(48376002)(105586002)(53416004)(486006)(316002)(54906003)(68736007)(1076002)(186003)(16586007)(47776003)(476003)(2616005)(478600001)(106356001)(81166006)(69596002)(8676002)(81156014)(305945005)(14444005)(97736004)(51416003)(86362001)(2906002)(46003)(2351001)(50226002)(6506007)(2361001)(52396003)(36756003)(6486002)(76176011)(52116002)(386003)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:BY2PR15MB0166;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;BY2PR15MB0166;23:vA46Vw0keMzxn3hTP2Iky2TJ2gWUoOLqZb9ffHvzO?= =?us-ascii?Q?F1/CrlKnPJqbVrDnzItAUI319jz8LV2LgEKWqE4r82ZQY774gcfOoaNbboHk?= =?us-ascii?Q?hq5+qGaM48wByaTFD2hcLRGFndLp66v+p2ZtqQ3CALO2+wuBPoHmqxL/OMgB?= =?us-ascii?Q?FGz3Jf9GsORHXKK7tzr8eXFvaoKH2k2Hm5xMtEYERvCkWn56wizsqNYsdPdh?= =?us-ascii?Q?2dWW3wK3vlzJLTmpOJkWHxtZE294CM30gfnCI8n4Zx7ojmkLLUyr1z0I1osM?= =?us-ascii?Q?3MqzoYiq0xEWA/2oGSmKj2tJavANfOA+llpLziS725N03BQ4EQc5PJ0YX5Iq?= =?us-ascii?Q?6nbmLsMHAkEm3/X/ahJqxd4BoLdfjwA/DXqpMw66PhtzkQOi5Ws4pemUkM/o?= =?us-ascii?Q?/RqIKtKGAzWJgmA80zcvLUunx+ZTDl/7qEaRgClykKMolOfOet3Ubs5PTCbE?= =?us-ascii?Q?EpTwTUBpoChBkZFUM5NnbcpBhR67WA/W69mOge/y/7rCtv4P2eRhQQ2Gx+c0?= =?us-ascii?Q?CT9oedzVFjm6mUNsZnU058qM1t050fcOaAqcRcFw5VGIx0mxDIdBxAAkqOqr?= =?us-ascii?Q?C1UoxgDdD6I11q3ljZZeKmdCFhVilK3hzJGdJSkt/FZGW6GYdwAQDzonr02q?= =?us-ascii?Q?UNGI7pCuprlIUXWPxBbnaqhXabl4cW8LLaCs2mA6aHt5yfzwYSxsob1tTkUc?= =?us-ascii?Q?TBTRV/o/TyYbNuOVdW9H36dEYNCNvXCfGAGey4+dcR7Bf3sPBkz5s5TGsyU2?= =?us-ascii?Q?8BjqArJovjc0KocHhoQ/GELy+xCKDh826abwLoLrgXEdSRnmzqUA9MJiZovv?= =?us-ascii?Q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eImD0Y0Ppx5mMXO37UApkQ9MVFfNm3eduprzE1qemYiIMl0moAZwSnP2pYs3zutafW5vKXCmKQXUAG2apod1vVoT1KZKE+WHPLNlUezWbXXN0u3jEf2hBllmLz5OUKQ531NtPVyv5pN8= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Jul 2018 20:52:08.3050 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6c8f88b5-b807-4f5b-6f49-08d5e2b92ca6 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR15MB0166 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-07-05_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 8827e797ff97..3d1933707374 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 d10ecd78105f..cee452a19538 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