From patchwork Mon Jan 13 15:35:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qiang Yu X-Patchwork-Id: 11330411 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1EEF81398 for ; Mon, 13 Jan 2020 15:37:36 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F05B5222C3 for ; Mon, 13 Jan 2020 15:37:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="t2Gjvvf0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F05B5222C3 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DE1676E0E3; Mon, 13 Jan 2020 15:37:34 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2044.outbound.protection.outlook.com [40.107.92.44]) by gabe.freedesktop.org (Postfix) with ESMTPS id 32C616E0E3 for ; Mon, 13 Jan 2020 15:37:34 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KWcYNqvGZruKZINchv1H4HJ/DuyQdUo44xVZZ9Xo6Bsy1aPG6AXf5JJJQH9cNt3YJAZ772rspxHXAjmockOiR1j16OAZ4hR3aMjYz4eL22uV1eF2KGWKw8stnyvuV8VR+DKYpThW7FnAPjZ3rJkiO1iVwFI68g4EkRhPW5xJOwGbfo1emvTppg2pHCL2F3gM73THNRgV/cuJ9fOXXAHMOKDQfm/l5NFvgAW+tfw6R/slna+dVfn0Lc6g1X6MGy90VLWoyHHrMFvbJXooh0x1DHmZ+RD1aXvij7ZXnRaYvXObjm4gBmQOYw6mdmK71nnk7e3zvy5qxj9gVp06b/ruuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=0IuJUdjJ4NjgBqPCIK/ycAWJdGIDmA7IOHw8Lf6aQKM=; b=FgORS+SesZjbdUhQNcfWNExm3hkmgY/1R0dPI+aG3h1yqEG1+spDw4fRyuSZFRwsKrA1zkEeCB4oMKQVg6dahx6zsAU2o++N7xUed/y+0CypJ/ZxXLmHWtKWL1IHJApqebPkqFGQo4PaQKxditw+h5odYZFNtHxTRmOdGQUdJyCOLwSO80COQtpkYBFKdzchjRw8zVjji6LPrsZVSKWTPAAMj5M2G8aazlC3EJfvTDlhqsftO9lGGsR8X9i+qNzFP/ooH91+gqVW4xcv7fj0Ed/N7gBA7OJSZ/Fd0bythYTVLPCO7m6Lx2rJ+K+mUm14CQFtAOXZZF2igdXVRwqH/w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=0IuJUdjJ4NjgBqPCIK/ycAWJdGIDmA7IOHw8Lf6aQKM=; b=t2Gjvvf02iviLhJtrDTj/JboCl4gjuAtcuHLHH316g0nMYriq/JScqBwLmwx5OL66zs+0qQiSdCXqmdbAyda0hboKXTZf/3CXLtEgrolSDaTeJIVj7wPMdL7hmNO40NUnEQTP+x2zeE+T0mckGhU1E1f2D52VjCsxrbHu0zH9m4= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Qiang.Yu@amd.com; Received: from SN6PR12MB2702.namprd12.prod.outlook.com (52.135.101.144) by SN6PR12MB2685.namprd12.prod.outlook.com (52.135.99.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2623.9; Mon, 13 Jan 2020 15:37:29 +0000 Received: from SN6PR12MB2702.namprd12.prod.outlook.com ([fe80::d46f:d4ae:d96d:ef70]) by SN6PR12MB2702.namprd12.prod.outlook.com ([fe80::d46f:d4ae:d96d:ef70%5]) with mapi id 15.20.2623.015; Mon, 13 Jan 2020 15:37:29 +0000 From: Qiang Yu To: linux-mm@kvack.org, cgroups@vger.kernel.org, dri-devel@lists.freedesktop.org Subject: [PATCH RFC 1/3] mm: memcontrol: add mem_cgroup_(un)charge_drvmem Date: Mon, 13 Jan 2020 23:35:41 +0800 Message-Id: <20200113153543.24957-2-qiang.yu@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200113153543.24957-1-qiang.yu@amd.com> References: <20200113153543.24957-1-qiang.yu@amd.com> X-ClientProxiedBy: SG2PR0302CA0024.apcprd03.prod.outlook.com (2603:1096:3:2::34) To SN6PR12MB2702.namprd12.prod.outlook.com (2603:10b6:805:6c::16) MIME-Version: 1.0 Received: from localhost.localdomain (221.239.222.115) by SG2PR0302CA0024.apcprd03.prod.outlook.com (2603:1096:3:2::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2644.10 via Frontend Transport; Mon, 13 Jan 2020 15:37:24 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [221.239.222.115] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 313786df-25c7-49d5-3357-08d7983e7f6a X-MS-TrafficTypeDiagnostic: SN6PR12MB2685:|SN6PR12MB2685: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:1247; X-Forefront-PRVS: 028166BF91 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4636009)(396003)(136003)(366004)(39860400002)(346002)(376002)(199004)(189003)(86362001)(4326008)(478600001)(1076003)(81156014)(5660300002)(81166006)(8936002)(8676002)(36756003)(6512007)(6486002)(66946007)(66476007)(66556008)(2906002)(956004)(26005)(6506007)(44832011)(6666004)(54906003)(316002)(186003)(52116002)(16526019)(69590400006)(2616005); DIR:OUT; SFP:1101; SCL:1; SRVR:SN6PR12MB2685; H:SN6PR12MB2702.namprd12.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hb61W9rXIqCx3gxq2ZEtRgaKvJQXtOFt0uOvPg4eLsMx3aZlXsoTiv8vY0RayKUuuFrJuq8eflYRnaerdoQ9P7GwARqJ5XxG6WQyfng1mqCr7/93ZNpFtF+8NvXpAT5PCZk/hcOI07xoeBwcRYHOFPR+fWxKqJKVZLsiwXrmdFPIDjo0WJoVAOpDL8mINq9JJJyux1Ys/AqNErH4tdcDSumitECCJabIFOMzyVMav6X8qQj319iI08beng88gSBNaejDaBoVC+cPuKHgJ9tsZdqbSsH3CsNq2+BXMsUiXJ0f+wv3ERNf1jVsgqoQIPHbRgXgjqHGtN9Ygrp9qpgH66/Birv05Lstc8Vhjz9ZfiAfZn/B8aZDBgCfwWuN0nlHkO0L/g9/V+/SBAOreg89d6R8k7Kjiv9PMZQhgPsOitybRmGkeI7e1Tf7el83wALmeRuQqz8aqjtb/iNz1FiUNlX+XiWFVwDl3VPk0BCwSjwaTZlh0Eyhbm4XAMo+v7eQ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 313786df-25c7-49d5-3357-08d7983e7f6a X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Jan 2020 15:37:29.2153 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: QWEXHPWyaY5lr8RGO6y/jFQNIKGDS53SMVZ7BNnfr/G42pQQTwGYZ58zC/f9ORru X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2685 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: David Airlie , Kenny Ho , Michal Hocko , Qiang Yu , Huang Rui , Johannes Weiner , Tejun Heo , Andrew Morton , Christian Koenig Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This is for driver which will allocate memory for both user application and kernel device driver usage. For example, GPU driver will allocate some GFP_USER pages and mapped to user to fill commands and data like texture and vertex, then let GPU command processor "eat" these memory. These buffers can be huge (offen several MB and may get to hundred or even thousand MB). Signed-off-by: Qiang Yu --- include/linux/memcontrol.h | 21 ++++++++++++++++ mm/memcontrol.c | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index ae703ea3ef48..d76977943265 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1363,6 +1363,27 @@ static inline void memcg_set_shrinker_bit(struct mem_cgroup *memcg, } #endif +#ifdef CONFIG_MEMCG +struct mem_cgroup *mem_cgroup_driver_get_from_current(void); +int mem_cgroup_charge_drvmem(struct mem_cgroup *memcg, gfp_t gfp, + unsigned long nr_pages); +void mem_cgroup_uncharge_drvmem(struct mem_cgroup *memcg, unsigned long nr_pages); +#else +static inline struct mem_cgroup *mem_cgroup_get_from_current(void) +{ + return NULL; +} + +static inline int mem_cgroup_charge_drvmem(struct mem_cgroup *memcg, gfp_t gfp, + unsigned long nr_pages) +{ + return 0; +} + +static inline void mem_cgroup_uncharge_drvmem(struct mem_cgroup *memcg, + unsigned long nr_pages) { } +#endif + struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep); void memcg_kmem_put_cache(struct kmem_cache *cachep); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 37592dd7ae32..28595c276e6b 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6913,6 +6913,55 @@ void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) refill_stock(memcg, nr_pages); } +/** + * mem_cgroup_driver_get_from_current - get memcg from current task for driver + * + * Return memcg from current task, NULL otherwise. + */ +struct mem_cgroup *mem_cgroup_driver_get_from_current(void) +{ + struct mem_cgroup *memcg, *ret = NULL; + + if (mem_cgroup_disabled()) + return NULL; + + rcu_read_lock(); + memcg = mem_cgroup_from_task(current); + if (memcg && memcg != root_mem_cgroup && + css_tryget_online(&memcg->css)) + ret = memcg; + rcu_read_unlock(); + + return ret; +} +EXPORT_SYMBOL(mem_cgroup_driver_get_from_current); + +/** + * mem_cgroup_charge_drvmem - charge a batch of pages for driver + * @memcg: memcg to charge + * @gfp: gfp flags for charge + * @nr_pages: number of pages to charge + * + * Return %true if success, %false otherwise. + */ +int mem_cgroup_charge_drvmem(struct mem_cgroup *memcg, gfp_t gfp, + unsigned long nr_pages) +{ + return try_charge(memcg, gfp, nr_pages); +} +EXPORT_SYMBOL(mem_cgroup_charge_drvmem); + +/** + * mem_cgroup_uncharge_drvmem - uncharge a batch of pages for driver + * @memcg: memcg to uncharge + * @nr_pages: number of pages to uncharge + */ +void mem_cgroup_uncharge_drvmem(struct mem_cgroup *memcg, unsigned long nr_pages) +{ + refill_stock(memcg, nr_pages); +} +EXPORT_SYMBOL(mem_cgroup_uncharge_drvmem); + static int __init cgroup_memory(char *s) { char *token; From patchwork Mon Jan 13 15:35:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qiang Yu X-Patchwork-Id: 11330415 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 55B531398 for ; Mon, 13 Jan 2020 15:37:44 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3307C222C4 for ; Mon, 13 Jan 2020 15:37:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="ns5+RP/u" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3307C222C4 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E05FE6E0CC; Mon, 13 Jan 2020 15:37:42 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2055.outbound.protection.outlook.com [40.107.93.55]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0FDE16E0CC for ; Mon, 13 Jan 2020 15:37:41 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mPfJuw+6t86Bzi1DvvmRS53MY7UWKZD2Fl1TtqJ9LKDLjWR4oS1aj1Gr5j7Ws3PFWuHPb0orFLnLghgiSPEHg5muSLPkj8jclx27MB4gBzwDlh7dj2+g2d5IsrebuzdoO43XzmgUSxbUZSbtCf1zyA5e7kwl7Wzobd+OPXI72quur2rC4USsl4VBluLJmRl8DAu1yHbQTMf2jUOvxYLhkNGcH0rQPTBN9fzhpfhiRAHBptTTUZfYfcK3Zoh2vRgMd6NTbQr3gpFsqOYnDItWW2RVUgrnVXl5WNXlZ2JnKfkgNelCK1027G3jWkfNsc9pPpm/4yTUez6TFVy0J4IE3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3DlwxaZntIBVeSDvPKOD4vQ85k2mF4dXbT5Z/W5r0bA=; b=XxzR2AJUyYmekCPq+UeP8964aHwwkrXtUK717bJagr5PcQn7KrDjZBDn0qH5mZ3ZMO84nDV+uOtkdlEMNwTJi3v4fKOuoiqIw3qfeZKzP8hnHaIqnZuxmkOJ7MeCR+82JDiozksmPfFZcuPV4ecxfu4JItg9QVpC0/19hZn+LO1+DbA55Yba4rt7ITEAEKXswyxatcAlq/Q+Vo49a50si0yMSoohiLmaZdpCT48ELRPvQsnqyuwERuKkOslmHufRhxMsFEVn7Bn0juBXv04IqNgC7U8IZuNqf2LU+76Eo4+2iqF9tycFH7qt+AqzHZZC2i7PEY7QENF3mrIrF4qoSg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3DlwxaZntIBVeSDvPKOD4vQ85k2mF4dXbT5Z/W5r0bA=; b=ns5+RP/uMpMArt9jZ20SH0ISwxA9/Hgtpl3dnrJxiZ+2McgZ6W7lioaMyNrCi32UvMTX3VQc1GAypcglJtHvZujGMXzxkjgMQJ1bcAq8CMJxToz/4qpego9/ZfK6x+3Px5UWQ3Ssg47OQiBPrRgVYA9B5LBrNulyxGeqACwkLTY= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Qiang.Yu@amd.com; Received: from SN6PR12MB2702.namprd12.prod.outlook.com (52.135.101.144) by SN6PR12MB2847.namprd12.prod.outlook.com (52.135.106.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2623.9; Mon, 13 Jan 2020 15:37:39 +0000 Received: from SN6PR12MB2702.namprd12.prod.outlook.com ([fe80::d46f:d4ae:d96d:ef70]) by SN6PR12MB2702.namprd12.prod.outlook.com ([fe80::d46f:d4ae:d96d:ef70%5]) with mapi id 15.20.2623.015; Mon, 13 Jan 2020 15:37:39 +0000 From: Qiang Yu To: linux-mm@kvack.org, cgroups@vger.kernel.org, dri-devel@lists.freedesktop.org Subject: [PATCH RFC 2/3] mm: memcontrol: record driver memory statistics Date: Mon, 13 Jan 2020 23:35:42 +0800 Message-Id: <20200113153543.24957-3-qiang.yu@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200113153543.24957-1-qiang.yu@amd.com> References: <20200113153543.24957-1-qiang.yu@amd.com> X-ClientProxiedBy: SG2PR0302CA0024.apcprd03.prod.outlook.com (2603:1096:3:2::34) To SN6PR12MB2702.namprd12.prod.outlook.com (2603:10b6:805:6c::16) MIME-Version: 1.0 Received: from localhost.localdomain (221.239.222.115) by SG2PR0302CA0024.apcprd03.prod.outlook.com (2603:1096:3:2::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2644.10 via Frontend Transport; Mon, 13 Jan 2020 15:37:34 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [221.239.222.115] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: d35fa7ee-415d-4b2c-5505-08d7983e855b X-MS-TrafficTypeDiagnostic: SN6PR12MB2847:|SN6PR12MB2847: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:110; X-Forefront-PRVS: 028166BF91 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4636009)(39860400002)(376002)(346002)(136003)(366004)(396003)(199004)(189003)(26005)(6506007)(956004)(44832011)(2906002)(316002)(2616005)(16526019)(69590400006)(186003)(52116002)(6666004)(54906003)(8936002)(8676002)(81166006)(5660300002)(81156014)(478600001)(4326008)(86362001)(1076003)(6486002)(66946007)(66556008)(66476007)(6512007)(36756003)(21314003); DIR:OUT; SFP:1101; SCL:1; SRVR:SN6PR12MB2847; H:SN6PR12MB2702.namprd12.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZtO30H7pxzqoU3477dOsXPxcsNy98wP42F5oUBhXn37lyhVDMmf/LRaoiHj0/KbJteMkwFIhzSdv+L8Ff+N4cliG4dIq4wMfuv5UO7+IBzJ6acfmwusoIMumN9zBPZnMaOJJ8UQYPYNIahV0ajSqGGzHUGF+SHDScJMf2Ueh7v05g8o+rOl8j0u8t1MPHC+s7SJddEnHP4pwWDnWidA+uOonIGCu3vc/RrYPaMY574tTHsCfVMw2XNGGo3aumQ5KNGEwtqoL4lXJCk3Zi2+Q3iYRhnb72CZW4nN47tp5lhAO9Azq4EN9F3Gvv4FGzTgj3krtB2W//4x4hycOtvbblwsc+UjjfneKo8IDK4j1zAiAWWnkc9VLORQ30izrMTSQg9fzxKIJQ2sTzuEXJUYStm4/xJgf4aWIMuhBV3a2ToeL5jjlBsQ4CYBCJJZ/D4foYhvwnWHCjSypQnf2DJJZBmfx6f8z7pAqH5MO3qrIZcZs/AH6VomjlgRzZoAaYIIDCRcZcQNhPrchgfReGNiLEQ== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: d35fa7ee-415d-4b2c-5505-08d7983e855b X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Jan 2020 15:37:39.4011 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ai6k1zpHvT6K8PLg4mKhPv2IpPCcmWoQB4X4CxtMDjD446Qpf8/5baaxa3H5b+31 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2847 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: David Airlie , Kenny Ho , Michal Hocko , Qiang Yu , Huang Rui , Johannes Weiner , Tejun Heo , Andrew Morton , Christian Koenig Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Signed-off-by: Qiang Yu --- include/linux/memcontrol.h | 1 + mm/memcontrol.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index d76977943265..6518b4b5ee07 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -36,6 +36,7 @@ enum memcg_stat_item { MEMCG_SOCK, /* XXX: why are these zone and not node counters? */ MEMCG_KERNEL_STACK_KB, + MEMCG_DRV, MEMCG_NR_STAT, }; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 28595c276e6b..cdd3f3401598 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1413,6 +1413,9 @@ static char *memory_stat_format(struct mem_cgroup *memcg) seq_buf_printf(&s, "sock %llu\n", (u64)memcg_page_state(memcg, MEMCG_SOCK) * PAGE_SIZE); + seq_buf_printf(&s, "driver %llu\n", + (u64)memcg_page_state(memcg, MEMCG_DRV) * + PAGE_SIZE); seq_buf_printf(&s, "shmem %llu\n", (u64)memcg_page_state(memcg, NR_SHMEM) * @@ -6947,6 +6950,9 @@ EXPORT_SYMBOL(mem_cgroup_driver_get_from_current); int mem_cgroup_charge_drvmem(struct mem_cgroup *memcg, gfp_t gfp, unsigned long nr_pages) { + if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) + mod_memcg_state(memcg, MEMCG_DRV, nr_pages); + return try_charge(memcg, gfp, nr_pages); } EXPORT_SYMBOL(mem_cgroup_charge_drvmem); @@ -6958,6 +6964,9 @@ EXPORT_SYMBOL(mem_cgroup_charge_drvmem); */ void mem_cgroup_uncharge_drvmem(struct mem_cgroup *memcg, unsigned long nr_pages) { + if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) + mod_memcg_state(memcg, MEMCG_DRV, -nr_pages); + refill_stock(memcg, nr_pages); } EXPORT_SYMBOL(mem_cgroup_uncharge_drvmem); From patchwork Mon Jan 13 15:35:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qiang Yu X-Patchwork-Id: 11330421 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 128EB92A for ; Mon, 13 Jan 2020 15:37:54 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E43D321734 for ; Mon, 13 Jan 2020 15:37:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="0LatdNrS" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E43D321734 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1D5596E0FA; Mon, 13 Jan 2020 15:37:53 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2084.outbound.protection.outlook.com [40.107.93.84]) by gabe.freedesktop.org (Postfix) with ESMTPS id 053EE6E0FA for ; Mon, 13 Jan 2020 15:37:51 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YLbka09Aauf7KWnSu5smnYSJNI53UMd2ClyTas/MmM5JWQ0988xWtIFe9dCKGLAVoXSAsqv9UCR18vx8eWmCoyGY+nqCQBYjo0obcjqYF/EFWfjvUX7XfzBPAir31n0hnYbyERPKRKpVjLCst1wQ66r20Yun6q6jd+9k3cvDjyQ/wRwsrPyiyt033bLDhuzTmiq0t8a3CANkMr5su8vif3Q+wiQQqIoM5meyYX017E+v4faOJlkAtvs6mZQGMnusO28HUSd4dRkpL7oIQ6389a5eT5xuRveq6ae9iHMATb3oKCSUovgrbgvnPr/sdi0XClH2ZoMfLZe7qTVmvEgpJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BigxloxjfB4pXERXNViNNpR/XEjnHFG+mJFGOZ+Ww40=; b=hCI+kLk3Oih6w8xG0Z0bd9ob+e6sJ5lWl2pXXryRaJRm1QirQs3E2Wm8oDHpI8hi2QZsU1DW4neV6/V7BTrBiuDGy1/W8kfVmP3o/to01q+AW9kxM+arlVGI0yb92tAgYbxBoJR75sKZaHfRzFKz0KdnyWMGbPWD7pl57gUMMBVxSyqDDSSMFy6RFF4i5IfVUV5MA3u5gu8iIxfj7Ic0QqtFb2IMURiGHyh0LM0CfOQDo5IvEE/dpqqmAhfTo7S/10xPQ0vzIob8IaKUJ7URL795O857hhqA/J6ENOFVoCY19MXksF3zJv8N6qnpeJKr+7E15awgCX6tHRT8tCIFMg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BigxloxjfB4pXERXNViNNpR/XEjnHFG+mJFGOZ+Ww40=; b=0LatdNrSO41ERY0JCw2BuVz782QU+45HuhdrkjCgk+De5Qu+ZQ8a51JB9S8LxU2+ny2mDiB2eLNt/dz0vJv1gHA6hd5+dbODIMRkegRVsxSo386XaWgkpGGlsFaEr9Oih4VUWGtiYgOKatHleDOoISVrmwS8YH73xAKTcdbF4ag= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Qiang.Yu@amd.com; Received: from SN6PR12MB2702.namprd12.prod.outlook.com (52.135.101.144) by SN6PR12MB2847.namprd12.prod.outlook.com (52.135.106.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2623.9; Mon, 13 Jan 2020 15:37:49 +0000 Received: from SN6PR12MB2702.namprd12.prod.outlook.com ([fe80::d46f:d4ae:d96d:ef70]) by SN6PR12MB2702.namprd12.prod.outlook.com ([fe80::d46f:d4ae:d96d:ef70%5]) with mapi id 15.20.2623.015; Mon, 13 Jan 2020 15:37:49 +0000 From: Qiang Yu To: linux-mm@kvack.org, cgroups@vger.kernel.org, dri-devel@lists.freedesktop.org Subject: [PATCH RFC 3/3] drm/ttm: support memcg for ttm_tt Date: Mon, 13 Jan 2020 23:35:43 +0800 Message-Id: <20200113153543.24957-4-qiang.yu@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200113153543.24957-1-qiang.yu@amd.com> References: <20200113153543.24957-1-qiang.yu@amd.com> X-ClientProxiedBy: SG2PR0302CA0024.apcprd03.prod.outlook.com (2603:1096:3:2::34) To SN6PR12MB2702.namprd12.prod.outlook.com (2603:10b6:805:6c::16) MIME-Version: 1.0 Received: from localhost.localdomain (221.239.222.115) by SG2PR0302CA0024.apcprd03.prod.outlook.com (2603:1096:3:2::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2644.10 via Frontend Transport; Mon, 13 Jan 2020 15:37:44 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [221.239.222.115] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 27c02a5d-4678-40b7-196c-08d7983e8b39 X-MS-TrafficTypeDiagnostic: SN6PR12MB2847:|SN6PR12MB2847: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4125; X-Forefront-PRVS: 028166BF91 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4636009)(39860400002)(376002)(346002)(136003)(366004)(396003)(199004)(189003)(26005)(6506007)(956004)(44832011)(2906002)(316002)(2616005)(16526019)(69590400006)(186003)(52116002)(54906003)(8936002)(8676002)(81166006)(5660300002)(81156014)(478600001)(4326008)(86362001)(1076003)(6486002)(66946007)(66556008)(66476007)(6512007)(36756003); DIR:OUT; SFP:1101; SCL:1; SRVR:SN6PR12MB2847; H:SN6PR12MB2702.namprd12.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YBPL1YcnDPOn1t9B/bsIK/WBYEsUUxgowN6XZENysWj11TBLjiJk30D4tcdAEAS7gNku5SldqiB1l+4C7+6mU32UR8XlatlvnbTZe2R2nntKI+Nzoo/ydNSkRviZAMiA6oLpSgaGF3I+EI1Gk8gdj2ZFeXBKkv/ncukPEx3RSsMlFp2jnym25xz3Qy0QGJ1AHjMPSKJznbb6gyuMZ5Y7MDzTvg0N07oXsNUalqFAUwY+QHqvzHPPVs8+eCFRbuvCWcpwLh6YW61KlZ2j/l2OMaE3EzcY3A6lAJk5Ht5NDp+rm4PWilzzgMLg4HF00CwZ4yF5mD41UnRcJLzkFEl/gzdmVm6n+tSGOTkhCfistjFNdxkoSgLYvD/NOzZ+OKHBqrNwGsEOJNZBMcilhkE4PbIb80Ws842GggOlawRa8E8b5pVlSRGcuaJnvZcaOoAbgffY3Ik0rZrCfUIXXDmFLTIOWKZVHz6Eb/1Vc4lI1IXBaUABjdBYMFnhN8LNFoDu X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 27c02a5d-4678-40b7-196c-08d7983e8b39 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Jan 2020 15:37:49.0162 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: sSXu99Sa+Br1Ejach5zf3bZHMeNFVJsVYaHJ/KV2UlLp5jlhXTX2pNOkklcz9U9C X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2847 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: David Airlie , Kenny Ho , Michal Hocko , Qiang Yu , Huang Rui , Johannes Weiner , Tejun Heo , Andrew Morton , Christian Koenig Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Charge TTM allocated system memory to memory cgroup which will limit the memory usage of a group of processes. The memory is always charged to the control group of task which create this buffer object and when it's created. For example, when a buffer is created by process A and exported to process B, then process B populate this buffer, the memory is still charged to process A's memcg; if a buffer is created by process A when in memcg B, then A is moved to memcg C and populate this buffer, it will charge memcg B. Signed-off-by: Qiang Yu --- drivers/gpu/drm/ttm/ttm_bo.c | 10 ++++++++++ drivers/gpu/drm/ttm/ttm_page_alloc.c | 18 +++++++++++++++++- drivers/gpu/drm/ttm/ttm_tt.c | 3 +++ include/drm/ttm/ttm_bo_api.h | 5 +++++ include/drm/ttm/ttm_tt.h | 4 ++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c index 8d91b0428af1..4e64846ee523 100644 --- a/drivers/gpu/drm/ttm/ttm_bo.c +++ b/drivers/gpu/drm/ttm/ttm_bo.c @@ -42,6 +42,7 @@ #include #include #include +#include static void ttm_bo_global_kobj_release(struct kobject *kobj); @@ -162,6 +163,10 @@ static void ttm_bo_release_list(struct kref *list_kref) if (!ttm_bo_uses_embedded_gem_object(bo)) dma_resv_fini(&bo->base._resv); mutex_destroy(&bo->wu_mutex); +#ifdef CONFIG_MEMCG + if (bo->memcg) + css_put(&bo->memcg->css); +#endif bo->destroy(bo); ttm_mem_global_free(&ttm_mem_glob, acc_size); } @@ -1330,6 +1335,11 @@ int ttm_bo_init_reserved(struct ttm_bo_device *bdev, } atomic_inc(&ttm_bo_glob.bo_count); +#ifdef CONFIG_MEMCG + if (bo->type == ttm_bo_type_device) + bo->memcg = mem_cgroup_driver_get_from_current(); +#endif + /* * For ttm_bo_type_device buffers, allocate * address space from the device. diff --git a/drivers/gpu/drm/ttm/ttm_page_alloc.c b/drivers/gpu/drm/ttm/ttm_page_alloc.c index b40a4678c296..ecd1831a1d38 100644 --- a/drivers/gpu/drm/ttm/ttm_page_alloc.c +++ b/drivers/gpu/drm/ttm/ttm_page_alloc.c @@ -42,7 +42,7 @@ #include /* for seq_printf */ #include #include - +#include #include #include @@ -1045,6 +1045,11 @@ ttm_pool_unpopulate_helper(struct ttm_tt *ttm, unsigned mem_count_update) ttm_put_pages(ttm->pages, ttm->num_pages, ttm->page_flags, ttm->caching_state); ttm->state = tt_unpopulated; + +#ifdef CONFIG_MEMCG + if (ttm->memcg) + mem_cgroup_uncharge_drvmem(ttm->memcg, ttm->num_pages); +#endif } int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) @@ -1059,6 +1064,17 @@ int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) if (ttm_check_under_lowerlimit(mem_glob, ttm->num_pages, ctx)) return -ENOMEM; +#ifdef CONFIG_MEMCG + if (ttm->memcg) { + gfp_t gfp_flags = GFP_USER; + if (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY) + gfp_flags |= __GFP_RETRY_MAYFAIL; + ret = mem_cgroup_charge_drvmem(ttm->memcg, gfp_flags, ttm->num_pages); + if (ret) + return ret; + } +#endif + ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags, ttm->caching_state); if (unlikely(ret != 0)) { diff --git a/drivers/gpu/drm/ttm/ttm_tt.c b/drivers/gpu/drm/ttm/ttm_tt.c index e0e9b4f69db6..1acb153084e1 100644 --- a/drivers/gpu/drm/ttm/ttm_tt.c +++ b/drivers/gpu/drm/ttm/ttm_tt.c @@ -233,6 +233,9 @@ void ttm_tt_init_fields(struct ttm_tt *ttm, struct ttm_buffer_object *bo, ttm->state = tt_unpopulated; ttm->swap_storage = NULL; ttm->sg = bo->sg; +#ifdef CONFIG_MEMCG + ttm->memcg = bo->memcg; +#endif } int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo, diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h index 65e399d280f7..95a08e81a73e 100644 --- a/include/drm/ttm/ttm_bo_api.h +++ b/include/drm/ttm/ttm_bo_api.h @@ -54,6 +54,8 @@ struct ttm_place; struct ttm_lru_bulk_move; +struct mem_cgroup; + /** * struct ttm_bus_placement * @@ -180,6 +182,9 @@ struct ttm_buffer_object { void (*destroy) (struct ttm_buffer_object *); unsigned long num_pages; size_t acc_size; +#ifdef CONFIG_MEMCG + struct mem_cgroup *memcg; +#endif /** * Members not needing protection. diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h index c0e928abf592..10fb5a557b95 100644 --- a/include/drm/ttm/ttm_tt.h +++ b/include/drm/ttm/ttm_tt.h @@ -33,6 +33,7 @@ struct ttm_tt; struct ttm_mem_reg; struct ttm_buffer_object; struct ttm_operation_ctx; +struct mem_cgroup; #define TTM_PAGE_FLAG_WRITE (1 << 3) #define TTM_PAGE_FLAG_SWAPPED (1 << 4) @@ -116,6 +117,9 @@ struct ttm_tt { tt_unbound, tt_unpopulated, } state; +#ifdef CONFIG_MEMCG + struct mem_cgroup *memcg; +#endif }; /**