Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp86101imm; Thu, 2 Aug 2018 14:29:24 -0700 (PDT) X-Google-Smtp-Source: AAOMgpf+WqrNgR5wMMiL9b1tw+/BlyZqnOkXs2W+oEHCh3U5fjl23zUawum3kSF3YYi56ZOKt4x2 X-Received: by 2002:aa7:87d0:: with SMTP id i16-v6mr1246410pfo.82.1533245364767; Thu, 02 Aug 2018 14:29:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533245364; cv=none; d=google.com; s=arc-20160816; b=Jl3qNLNk5k0QTcbLqazr2o7mFE0X1FklpCufPv0dYi65RpPniUhJicEs2LuSmDgnp2 cRvdl1szXJT7mi1jbvLqCrjptR8FM+7dcNEkENLTJIO3UC6tvevAlUem5TU2rSy/QEhQ /QsMdxSKdrhLFKxozx+kQV7VobnOdwgzmOxHfnEYhMTo21SuGHWqAAHQiSCAooFgQ7fb GFEVhJr7sXGhsFY9O2olfZDmvi3Rj4vu3/cNbeRj6wsF5FAkK9z+tUnllToLV/4CgpXr mXOjSOCc28QoDE36BsCcj1Br1kH7wuO+aMarc3KCUKqqEh6GfzRaV1MZlDlhKNiA2Pia eIIg== 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=SS3hL9y/Kz8AKahUGvBHEw2nIAlhiKXXnDeKs9koJN0=; b=vFaOigRzMC2GTZ+z0iMN+t4kk++yLWZmor8Qq+IMqVmHkWMh8hHlLsq69v94kfU/IB JnVlkEE3AsGwkUzJz+i0yC+npOJFpiPIALKOYx3Octb+0GfkQ+SCesvv/3qiKK3mzVkz j+yCGU1tu1Slo35HLdT8d06hQfjhg3/pngJkrT/OYw/BdKv+/NUoPmOu360JV/6O9Abv I3k+Ad6kB6Tb/X+G1X/4envN9JUpfuY0BGe8GWR5rSmLE5oCUPqea0x7jJit4r/EV6dk ta6MDZJZl+VrGAIi0i2sEoWfs3hd/oRHhjYCNAhgnHlRghBDUECJbi5BZmC7CHgy4yas 2uew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=bHRzuTpT; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=eo+l3ywR; 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 q8-v6si3295827pgh.675.2018.08.02.14.29.09; Thu, 02 Aug 2018 14:29:24 -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=bHRzuTpT; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=eo+l3ywR; 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 S1731600AbeHBXVP (ORCPT + 99 others); Thu, 2 Aug 2018 19:21:15 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:59102 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727260AbeHBXVP (ORCPT ); Thu, 2 Aug 2018 19:21:15 -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 w72LROUY012185; Thu, 2 Aug 2018 14:28:04 -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=SS3hL9y/Kz8AKahUGvBHEw2nIAlhiKXXnDeKs9koJN0=; b=bHRzuTpTn4aPV3eE6FNIFTBu8+YPprg5vLk8Vgt3V5LS2mTpAPTjhxbt1fvpzf+vtlc/ UuGPFVOFsIexnrpvg0nVvdhSJOJLWir+mbeJQos4sf6OUhOTAg0/+KInsA6BLx8frLZL w04xm9lERWaMoX+qABY5FxYo/TEv9C9gMQE= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0b-00082601.pphosted.com with ESMTP id 2km6jy8nt5-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Thu, 02 Aug 2018 14:28:04 -0700 Received: from NAM05-CO1-obe.outbound.protection.outlook.com (192.168.54.28) by o365-in.thefacebook.com (192.168.16.20) with Microsoft SMTP Server (TLS) id 14.3.361.1; Thu, 2 Aug 2018 14:28:02 -0700 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=SS3hL9y/Kz8AKahUGvBHEw2nIAlhiKXXnDeKs9koJN0=; b=eo+l3ywRi+JusC4W9RWXxq0yUmFO58CuRS2pU5Gk7SUUytYrzFFgjcj4IsloPVyyZasULjFAKl2TYkXiogB8NHQ3sIcgwpQvazBsjQV5V3IA2BCur29U6Kfz0p7KUKTpVaiOeUHQ+cFu0fywkx52zgLj3LxUuC3x5DsgjK5HtZ0= Received: from castle.thefacebook.com (2620:10d:c090:200::4:6653) by BY2PR15MB0167.namprd15.prod.outlook.com (2a01:111:e400:58e0::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.995.19; Thu, 2 Aug 2018 21:27:52 +0000 From: Roman Gushchin To: CC: , , Roman Gushchin , Alexei Starovoitov , Daniel Borkmann Subject: [PATCH v7 bpf-next 01/14] bpf: add ability to charge bpf maps memory dynamically Date: Thu, 2 Aug 2018 14:27:17 -0700 Message-ID: <20180802212730.18579-2-guro@fb.com> X-Mailer: git-send-email 2.14.4 In-Reply-To: <20180802212730.18579-1-guro@fb.com> References: <20180802212730.18579-1-guro@fb.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [2620:10d:c090:200::4:6653] X-ClientProxiedBy: MWHPR19CA0058.namprd19.prod.outlook.com (2603:10b6:300:94::20) To BY2PR15MB0167.namprd15.prod.outlook.com (2a01:111:e400:58e0::13) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f0952631-e815-4d9d-e66e-08d5f8bece89 X-Microsoft-Antispam: BCL:0;PCL:0;RULEID:(7020095)(4652040)(8989117)(5600074)(711020)(4534165)(4627221)(201703031133081)(201702281549075)(8990107)(2017052603328)(7153060)(7193020);SRVR:BY2PR15MB0167; X-Microsoft-Exchange-Diagnostics: 1;BY2PR15MB0167;3:8pwBfkHZIsemPN9xdHMS5zPb0I9Fh9R8uh4vsI5LsfXAYwd4Cj0aFE0KWJFC0tHRn31TPLFfa3ru9qhikTp/bMFO5Z89OGtgVoLBfTrCwHE2n+p1BR06Q7yW93nMEvB9tRBjQPJH5HhjsNtrTircSzwYliIURkb5ZIURlBVlwYMXuv6yxf+MEla/WSdBAUrjA+Z1dETYiRpW7h7WmTjUiLzZZ1Wc/KyV9UBdrGwrMJOIEH/XLFYIw921CrfsSLk6;25:hAUC+KAL/gNB/YJ/fWx0FymD8iq+Y5qP72M/vla0NiIMjRhrhTUcpGk8n9Da3V2jFIJn8qsUMe7Fb3QEvLFX10xgppo6NX0AlY8uMFxSP8fvsBT9su2RBn2Pf6WJwOUqCG7ZrAz1Jx0FHFXcUOXb4eXy/IHBotwfARCFf3xHmYl4sXPHKlb7nCe5Tww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Lp+N/fXWswCwXzdcdwDZCDuq5AtsF4dDUiomAIRka9OKVLbbSpN9IQvWJZPErCQVIaha36P4rtQgawbgMqeTKvOsjCgf1nKaOWwXMecFt0WCiyC6wP8lyoLnFTALTZeNjyy3Kn9gokj27Kq3jgNm7E2IxeqPr5U8z57jwYg2sc9kFX3TaNd2Q8rtiOmB/wzBc9gxY7Cztw8hJt/u13MweA6AmwF5ucw2h3UJxRY9A9onxVwYJdH9uu+855ocLzLkPA1NUZ76s= 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)(3231311)(11241501184)(944501410)(52105095)(93006095)(93001095)(10201501046)(3002001)(149027)(150027)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(20161123562045)(20161123558120)(20161123560045)(6072148)(201708071742011)(7699016);SRVR:BY2PR15MB0167;BCL:0;PCL:0;RULEID:;SRVR:BY2PR15MB0167; X-Forefront-PRVS: 07521929C1 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(396003)(136003)(346002)(376002)(366004)(39860400002)(199004)(189003)(446003)(48376002)(2906002)(97736004)(86362001)(1076002)(305945005)(8676002)(186003)(8936002)(11346002)(6916009)(6666003)(7736002)(486006)(6116002)(2351001)(476003)(14444005)(106356001)(81156014)(2616005)(2361001)(105586002)(16526019)(478600001)(81166006)(575784001)(69596002)(47776003)(46003)(53416004)(36756003)(316002)(53936002)(51416003)(4326008)(52116002)(50226002)(16586007)(68736007)(54906003)(50466002)(52396003)(386003)(6506007)(5660300001)(76176011)(6486002)(25786009)(6512007)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:BY2PR15MB0167;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;BY2PR15MB0167;23:Ce3ZiYJFtSb+zwBfVrF4RPyXNXQGqQA0nFmcYarZy?= =?us-ascii?Q?ChbJ+3yHT+V9lzN1/OYLrBkENUu0Fp2IObC/LuV5wNw2iJo3ecGQlCC8kEYu?= =?us-ascii?Q?XGXnnO8Nyxhhl9HMyebEXfClMxf7FSHKiGWRfjLuxHHWwj4TcP8clyFwIT4z?= =?us-ascii?Q?nCYOe0Lm8z+DNh5He8WI5hnyoq5VU/4CzT+hijRdeMWMfiZJD1GHlDGOCwue?= =?us-ascii?Q?9/y1ZQJWxK47iiMFDG8SID0i1ThoKHZCRhoW1j07m/+mmNpupN+T3fFAsfw9?= =?us-ascii?Q?/9HTdQlH9irFXhim9d7mWryb/SeBNwn3hvSkTeOOJ9SlSDtM3u3mT+Tis8Si?= =?us-ascii?Q?+NT8rhNT3Psk7OhrTOKiuaHpL0BosDB1mg9f+6r0xxFjQBq6Pf1quAZ2vVLE?= =?us-ascii?Q?4J0bCdHbqglbvA0RND1UNb69+nJGcVUeVEe0I6cK9O+e/gyp9IaK44XPVJ83?= =?us-ascii?Q?UkkH6UFhLuvEFl4ULJOrWU4qj8xetAtIPNHPg+khn7iGDdK6zqhSrazrbZpU?= =?us-ascii?Q?djk5acu4Iy7DTPxfDGRSgNdQh3y3AqLUP/820vG0Zan1pwyhack9cD937+gP?= =?us-ascii?Q?vzhUBJGYZwVU2XH9xryHLYBaRKLhpGN2KwWZp3zc4Tg2/bHb0lc5kZdcVOYV?= =?us-ascii?Q?2Cy5G+0muGhx53REjqZA+H0XNZrjGmEZRPKwPddLU7Hbh97swGTxwBo17OAf?= =?us-ascii?Q?u/MGeYcKyX6W0cbKD3zh7LrX0LjprD31muOiT2+kv/7Ck+hs+waiCSXhSA+S?= =?us-ascii?Q?3SfRk/lT3bJwiGGg+7Bt+BLZf+oYi2ey6KyoB+PS+sXwlCRZexcJ0Lj2df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uydSXq5+ZFvfN//qOLIh1MmZCEOo2iOd4lslUa8Dh4LL6QcTwCv1z6VnE= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Aug 2018 21:27:52.8695 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f0952631-e815-4d9d-e66e-08d5f8bece89 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR15MB0167 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-08-02_05:,, 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..7958252a4d29 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); + bpf_uncharge_memlock(user, map->pages); 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