From patchwork Wed Jun 26 15:05:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho, Kenny" X-Patchwork-Id: 11018051 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3341E14C0 for ; Wed, 26 Jun 2019 15:05:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F0852288B9 for ; Wed, 26 Jun 2019 15:05:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ED7B62888A; Wed, 26 Jun 2019 15:05:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7FF142888A for ; Wed, 26 Jun 2019 15:05:43 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C91F76E45F; Wed, 26 Jun 2019 15:05:36 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM02-BL2-obe.outbound.protection.outlook.com (mail-eopbgr750085.outbound.protection.outlook.com [40.107.75.85]) by gabe.freedesktop.org (Postfix) with ESMTPS id 29CB66E456; Wed, 26 Jun 2019 15:05:32 +0000 (UTC) Received: from MWHPR12CA0058.namprd12.prod.outlook.com (2603:10b6:300:103::20) by CY4PR1201MB0021.namprd12.prod.outlook.com (2603:10b6:910:1a::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Wed, 26 Jun 2019 15:05:30 +0000 Received: from DM3NAM03FT041.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e49::209) by MWHPR12CA0058.outlook.office365.com (2603:10b6:300:103::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2008.16 via Frontend Transport; Wed, 26 Jun 2019 15:05:30 +0000 Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.17) by DM3NAM03FT041.mail.protection.outlook.com (10.152.83.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2032.15 via Frontend Transport; Wed, 26 Jun 2019 15:05:29 +0000 Received: from kho-5039A.amd.com (10.180.168.240) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.389.1; Wed, 26 Jun 2019 10:05:26 -0500 From: Kenny Ho To: , , , , , , , , , Subject: [RFC PATCH v3 01/11] cgroup: Introduce cgroup for drm subsystem Date: Wed, 26 Jun 2019 11:05:12 -0400 Message-ID: <20190626150522.11618-2-Kenny.Ho@amd.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190626150522.11618-1-Kenny.Ho@amd.com> References: <20190626150522.11618-1-Kenny.Ho@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.17; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(39860400002)(376002)(346002)(396003)(136003)(2980300002)(428003)(189003)(199004)(2616005)(77096007)(81156014)(26005)(186003)(8936002)(305945005)(2201001)(53936002)(86362001)(8676002)(81166006)(36756003)(2870700001)(50226002)(4326008)(110136005)(47776003)(316002)(5024004)(14444005)(53416004)(356004)(5660300002)(50466002)(6666004)(2906002)(48376002)(1076003)(7696005)(72206003)(426003)(76176011)(11346002)(70586007)(446003)(70206006)(478600001)(476003)(486006)(51416003)(68736007)(126002)(336012)(921003)(1121003)(83996005)(2101003); DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR1201MB0021; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; LANG:en; PTR:InfoDomainNonexistent; MX:1; A:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a14e9f9e-d050-4a0d-5060-08d6fa47ba88 X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328); SRVR:CY4PR1201MB0021; X-MS-TrafficTypeDiagnostic: CY4PR1201MB0021: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5797; X-Forefront-PRVS: 00808B16F3 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: +Qeg+w7YJ5KoXzLpoJo0Xvde4SYeofwx/Mt3lKrz/vdX4bdCJBULC1/VBCWiKJlb7wyw/MV1r6m5UFm4Bi3wOO2L6ojkjTK6zdGR6vBCdWpA79bcQhjr8NRgBBY3u6qXFPU88meIGv4Xoyzv43wWCuyZCD1Z8AmF0zNdHwS7PWiodNjeOYeTogK0cR4CYzzO9jighKNAP9maKL21AgMGxCMUV6Z1QBgzftvoxZ2yifLnqD7wUoWmhBKLe19sfimsmLmJ+ewmj4zaRPAehRxeRZ/Ud0H+OM9JGK70ZRqCWeodvTWkJT6lQhILKT1rJTEZZImWCul8MWsaJluBDYawSrUUAHgjeEp5KbtvgXGIYLuZuDIB4iqPlFrOkn88lxxReaE6Qu/SPIP6K5bGSUiEmwH1v1LkJeB5wQLuxLXHWH4= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2019 15:05:29.3147 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a14e9f9e-d050-4a0d-5060-08d6fa47ba88 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR1201MB0021 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=C8Vkkh6EI+Jvy+fTB+fueJFxoa5pBliRjRlepUuST+I=; b=mKcJYS/q+sq1YxpbmeiFzXFZ/qgWUbH/8UQ6pqPYJrtGqBG8nRqyk5MsQlFr/7OfhxFIyzv498EdF+OlAFn+PYelqyHMv6Rzd2zgL2PInLKFf6LCbU0Ht+ML7y/fMRm5LMCYXBEje6j9pesUN0UsHD0iRsGK+zaHzOUBA9Lv6+s= X-Mailman-Original-Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=permerror action=none header.from=amd.com; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kenny Ho Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Change-Id: I6830d3990f63f0c13abeba29b1d330cf28882831 Signed-off-by: Kenny Ho --- include/linux/cgroup_drm.h | 76 +++++++++++++++++++++++++++++++++++ include/linux/cgroup_subsys.h | 4 ++ init/Kconfig | 5 +++ kernel/cgroup/Makefile | 1 + kernel/cgroup/drm.c | 42 +++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 include/linux/cgroup_drm.h create mode 100644 kernel/cgroup/drm.c diff --git a/include/linux/cgroup_drm.h b/include/linux/cgroup_drm.h new file mode 100644 index 000000000000..9928e60037a5 --- /dev/null +++ b/include/linux/cgroup_drm.h @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: MIT + * Copyright 2019 Advanced Micro Devices, Inc. + */ +#ifndef _CGROUP_DRM_H +#define _CGROUP_DRM_H + +#ifdef CONFIG_CGROUP_DRM + +#include + +struct drmcgrp { + struct cgroup_subsys_state css; +}; + +static inline struct drmcgrp *css_drmcgrp(struct cgroup_subsys_state *css) +{ + return css ? container_of(css, struct drmcgrp, css) : NULL; +} + +static inline struct drmcgrp *drmcgrp_from(struct task_struct *task) +{ + return css_drmcgrp(task_get_css(task, drm_cgrp_id)); +} + +static inline struct drmcgrp *get_drmcgrp(struct task_struct *task) +{ + struct cgroup_subsys_state *css = task_get_css(task, drm_cgrp_id); + + if (css) + css_get(css); + + return css_drmcgrp(css); +} + +static inline void put_drmcgrp(struct drmcgrp *drmcgrp) +{ + if (drmcgrp) + css_put(&drmcgrp->css); +} + +static inline struct drmcgrp *parent_drmcgrp(struct drmcgrp *cg) +{ + return css_drmcgrp(cg->css.parent); +} + +#else /* CONFIG_CGROUP_DRM */ + +struct drmcgrp { +}; + +static inline struct drmcgrp *css_drmcgrp(struct cgroup_subsys_state *css) +{ + return NULL; +} + +static inline struct drmcgrp *drmcgrp_from(struct task_struct *task) +{ + return NULL; +} + +static inline struct drmcgrp *get_drmcgrp(struct task_struct *task) +{ + return NULL; +} + +static inline void put_drmcgrp(struct drmcgrp *drmcgrp) +{ +} + +static inline struct drmcgrp *parent_drmcgrp(struct drmcgrp *cg) +{ + return NULL; +} + +#endif /* CONFIG_CGROUP_DRM */ +#endif /* _CGROUP_DRM_H */ diff --git a/include/linux/cgroup_subsys.h b/include/linux/cgroup_subsys.h index acb77dcff3b4..ddedad809e8b 100644 --- a/include/linux/cgroup_subsys.h +++ b/include/linux/cgroup_subsys.h @@ -61,6 +61,10 @@ SUBSYS(pids) SUBSYS(rdma) #endif +#if IS_ENABLED(CONFIG_CGROUP_DRM) +SUBSYS(drm) +#endif + /* * The following subsystems are not supported on the default hierarchy. */ diff --git a/init/Kconfig b/init/Kconfig index d47cb77a220e..0b0f112eb23b 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -839,6 +839,11 @@ config CGROUP_RDMA Attaching processes with active RDMA resources to the cgroup hierarchy is allowed even if can cross the hierarchy's limit. +config CGROUP_DRM + bool "DRM controller (EXPERIMENTAL)" + help + Provides accounting and enforcement of resources in the DRM subsystem. + config CGROUP_FREEZER bool "Freezer controller" help diff --git a/kernel/cgroup/Makefile b/kernel/cgroup/Makefile index bfcdae896122..6af14bd93050 100644 --- a/kernel/cgroup/Makefile +++ b/kernel/cgroup/Makefile @@ -4,5 +4,6 @@ obj-y := cgroup.o rstat.o namespace.o cgroup-v1.o obj-$(CONFIG_CGROUP_FREEZER) += freezer.o obj-$(CONFIG_CGROUP_PIDS) += pids.o obj-$(CONFIG_CGROUP_RDMA) += rdma.o +obj-$(CONFIG_CGROUP_DRM) += drm.o obj-$(CONFIG_CPUSETS) += cpuset.o obj-$(CONFIG_CGROUP_DEBUG) += debug.o diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c new file mode 100644 index 000000000000..66cb1dda023d --- /dev/null +++ b/kernel/cgroup/drm.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: MIT +// Copyright 2019 Advanced Micro Devices, Inc. +#include +#include +#include + +static struct drmcgrp *root_drmcgrp __read_mostly; + +static void drmcgrp_css_free(struct cgroup_subsys_state *css) +{ + struct drmcgrp *drmcgrp = css_drmcgrp(css); + + kfree(drmcgrp); +} + +static struct cgroup_subsys_state * +drmcgrp_css_alloc(struct cgroup_subsys_state *parent_css) +{ + struct drmcgrp *parent = css_drmcgrp(parent_css); + struct drmcgrp *drmcgrp; + + drmcgrp = kzalloc(sizeof(struct drmcgrp), GFP_KERNEL); + if (!drmcgrp) + return ERR_PTR(-ENOMEM); + + if (!parent) + root_drmcgrp = drmcgrp; + + return &drmcgrp->css; +} + +struct cftype files[] = { + { } /* terminate */ +}; + +struct cgroup_subsys drm_cgrp_subsys = { + .css_alloc = drmcgrp_css_alloc, + .css_free = drmcgrp_css_free, + .early_init = false, + .legacy_cftypes = files, + .dfl_cftypes = files, +}; From patchwork Wed Jun 26 15:05:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho, Kenny" X-Patchwork-Id: 11018049 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E4DA2924 for ; Wed, 26 Jun 2019 15:05:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D53A4285FF for ; Wed, 26 Jun 2019 15:05:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D2C3E288AC; Wed, 26 Jun 2019 15:05:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5D17E285FF for ; Wed, 26 Jun 2019 15:05:41 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0B9456E45D; Wed, 26 Jun 2019 15:05:36 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM01-SN1-obe.outbound.protection.outlook.com (mail-eopbgr820073.outbound.protection.outlook.com [40.107.82.73]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3353B6E456; Wed, 26 Jun 2019 15:05:33 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=testarcselector01; d=microsoft.com; cv=none; b=HUXXqqu0kxTvH5uUzySLJGGpSb1mELi57Xgi7WiJlicZ+4hQqPGI7hwCfGS8eq1vajFkO2HjOn7l/k+Opfpb7Odkow+JFCLC8+rDGKP+R0VB03piR0Su30ZJ5M/4EZuV2drxW3g/Q8225FVuqnjRWXl0nf4Gh0uyoXiDn4J2nYE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=testarcselector01; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SU1hgST+JyNKTp1qUXKZfbLnqt+3iOdlQlRhiWUMa8A=; b=kk6EhZ36rYx/uhElp1NqMo6WjTbZG3RLt9WEHXwXtgMOn8UUX2JxuqGm83MRhLdEDUqG36/usn7vezXS24Ac57iSb6zJtd9Enw+Gf1qTRe2g7hnQYMAJjc/ht2YfeDMtDi/ysnTCjMQXtUhQaEhIpEhdIMOJ2ia6/qw3/0B43b4= ARC-Authentication-Results: i=1; mx.microsoft.com 1;spf=none (sender ip is 165.204.84.17) smtp.rcpttodomain=kernel.org smtp.mailfrom=amd.com;dmarc=permerror action=none header.from=amd.com;dkim=none (message not signed);arc=none Received: from MWHPR12CA0065.namprd12.prod.outlook.com (2603:10b6:300:103::27) by CY4PR1201MB0022.namprd12.prod.outlook.com (2603:10b6:910:1e::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Wed, 26 Jun 2019 15:05:31 +0000 Received: from DM3NAM03FT041.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e49::207) by MWHPR12CA0065.outlook.office365.com (2603:10b6:300:103::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2008.16 via Frontend Transport; Wed, 26 Jun 2019 15:05:31 +0000 Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.17) by DM3NAM03FT041.mail.protection.outlook.com (10.152.83.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2032.15 via Frontend Transport; Wed, 26 Jun 2019 15:05:30 +0000 Received: from kho-5039A.amd.com (10.180.168.240) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.389.1; Wed, 26 Jun 2019 10:05:27 -0500 From: Kenny Ho To: , , , , , , , , , Subject: [RFC PATCH v3 02/11] cgroup: Add mechanism to register DRM devices Date: Wed, 26 Jun 2019 11:05:13 -0400 Message-ID: <20190626150522.11618-3-Kenny.Ho@amd.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190626150522.11618-1-Kenny.Ho@amd.com> References: <20190626150522.11618-1-Kenny.Ho@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.17; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(376002)(39860400002)(396003)(136003)(346002)(2980300002)(428003)(189003)(199004)(2616005)(70206006)(2906002)(476003)(126002)(2870700001)(70586007)(426003)(48376002)(50466002)(81156014)(81166006)(8676002)(5660300002)(186003)(446003)(11346002)(1076003)(8936002)(14444005)(7696005)(53936002)(51416003)(76176011)(36756003)(4326008)(86362001)(336012)(2201001)(316002)(72206003)(77096007)(486006)(68736007)(50226002)(478600001)(110136005)(26005)(6666004)(305945005)(356004)(47776003)(53416004)(921003)(1121003)(83996005)(2101003); DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR1201MB0022; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; LANG:en; PTR:InfoDomainNonexistent; MX:1; A:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f08f7153-441d-475c-41ca-08d6fa47bb4a X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328); SRVR:CY4PR1201MB0022; X-MS-TrafficTypeDiagnostic: CY4PR1201MB0022: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:663; X-Forefront-PRVS: 00808B16F3 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: KGbLAE0tBLpuLQNwJ6iLFYVhQh+kYpaRWptQsCzct5wrmi/NGU2OVisfY/8wMqjKB7BYkQruSNVB1F+g5OF288CZSP/hFK5TGrr6fYXH2DNjGe46QtbkM4Xwt866EH+YV6d2vi7T5vt3RMKk8m2WbDufvMJkCKK/VodkKrYrhv34ubQEtWP19A4ksa12B1w2aY6hxXibRdpUfJksT3TJGwHJJdRb5Yq8qy0MV3RGJLi1m1vs/6hUSRBif+3AdYt2m4ydz9Av4zQeWMB2bzG7l3J3vSU8kzjOYjYvNG4/DFYq2/hOEzxC70SIfP0f+U2RtTLIBUzqEkdmo9KUQ2b/efZtXaOJzYYLzGpl4UFswdmYbzf1wrTxp4XEtUfFSkp93S3D1Sf3buaNUpCy2lHYEU/S24QIUBuy/r5N8ZGME4s= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2019 15:05:30.5677 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f08f7153-441d-475c-41ca-08d6fa47bb4a X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR1201MB0022 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SU1hgST+JyNKTp1qUXKZfbLnqt+3iOdlQlRhiWUMa8A=; b=PC9IPDIPiGWL4fv2rTuWQhtkyOuc6u/SbEi/trh6n9Ty3Cv0m4xTboC81oNrr8t7PBOOpjEgO5pXgF/72m53o/qYxRTn6OMEtS/8niSZ36PBJ8wQVOQHosskEaB3wmgIBBETRWJxwdRFtmf4eZEQM8n7eOyomQQhqaHmUGyHvOI= X-Mailman-Original-Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=permerror action=none header.from=amd.com; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kenny Ho Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Change-Id: I908ee6975ea0585e4c30eafde4599f87094d8c65 Signed-off-by: Kenny Ho --- include/drm/drm_cgroup.h | 24 ++++++++ include/linux/cgroup_drm.h | 10 ++++ kernel/cgroup/drm.c | 116 +++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 include/drm/drm_cgroup.h diff --git a/include/drm/drm_cgroup.h b/include/drm/drm_cgroup.h new file mode 100644 index 000000000000..ddb9eab64360 --- /dev/null +++ b/include/drm/drm_cgroup.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: MIT + * Copyright 2019 Advanced Micro Devices, Inc. + */ +#ifndef __DRM_CGROUP_H__ +#define __DRM_CGROUP_H__ + +#ifdef CONFIG_CGROUP_DRM + +int drmcgrp_register_device(struct drm_device *device); + +int drmcgrp_unregister_device(struct drm_device *device); + +#else +static inline int drmcgrp_register_device(struct drm_device *device) +{ + return 0; +} + +static inline int drmcgrp_unregister_device(struct drm_device *device) +{ + return 0; +} +#endif /* CONFIG_CGROUP_DRM */ +#endif /* __DRM_CGROUP_H__ */ diff --git a/include/linux/cgroup_drm.h b/include/linux/cgroup_drm.h index 9928e60037a5..27497f786c93 100644 --- a/include/linux/cgroup_drm.h +++ b/include/linux/cgroup_drm.h @@ -6,10 +6,20 @@ #ifdef CONFIG_CGROUP_DRM +#include #include +#include + +/* limit defined per the way drm_minor_alloc operates */ +#define MAX_DRM_DEV (64 * DRM_MINOR_RENDER) + +struct drmcgrp_device_resource { + /* for per device stats */ +}; struct drmcgrp { struct cgroup_subsys_state css; + struct drmcgrp_device_resource *dev_resources[MAX_DRM_DEV]; }; static inline struct drmcgrp *css_drmcgrp(struct cgroup_subsys_state *css) diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c index 66cb1dda023d..7da6e0d93991 100644 --- a/kernel/cgroup/drm.c +++ b/kernel/cgroup/drm.c @@ -1,28 +1,99 @@ // SPDX-License-Identifier: MIT // Copyright 2019 Advanced Micro Devices, Inc. +#include #include #include +#include +#include +#include #include +#include +#include +#include + +static DEFINE_MUTEX(drmcgrp_mutex); + +struct drmcgrp_device { + struct drm_device *dev; + struct mutex mutex; +}; + +/* indexed by drm_minor for access speed */ +static struct drmcgrp_device *known_drmcgrp_devs[MAX_DRM_DEV]; + +static int max_minor; + static struct drmcgrp *root_drmcgrp __read_mostly; static void drmcgrp_css_free(struct cgroup_subsys_state *css) { struct drmcgrp *drmcgrp = css_drmcgrp(css); + int i; + + for (i = 0; i <= max_minor; i++) { + if (drmcgrp->dev_resources[i] != NULL) + kfree(drmcgrp->dev_resources[i]); + } kfree(drmcgrp); } +static inline int init_drmcgrp_single(struct drmcgrp *drmcgrp, int minor) +{ + struct drmcgrp_device_resource *ddr = drmcgrp->dev_resources[minor]; + + if (ddr == NULL) { + ddr = kzalloc(sizeof(struct drmcgrp_device_resource), + GFP_KERNEL); + + if (!ddr) + return -ENOMEM; + + drmcgrp->dev_resources[minor] = ddr; + } + + /* set defaults here */ + + return 0; +} + +static inline int init_drmcgrp(struct drmcgrp *drmcgrp, struct drm_device *dev) +{ + int rc = 0; + int i; + + if (dev != NULL) { + rc = init_drmcgrp_single(drmcgrp, dev->primary->index); + return rc; + } + + for (i = 0; i <= max_minor; i++) { + rc = init_drmcgrp_single(drmcgrp, i); + if (rc) + return rc; + } + + return 0; +} + static struct cgroup_subsys_state * drmcgrp_css_alloc(struct cgroup_subsys_state *parent_css) { struct drmcgrp *parent = css_drmcgrp(parent_css); struct drmcgrp *drmcgrp; + int rc; drmcgrp = kzalloc(sizeof(struct drmcgrp), GFP_KERNEL); if (!drmcgrp) return ERR_PTR(-ENOMEM); + rc = init_drmcgrp(drmcgrp, NULL); + if (rc) { + drmcgrp_css_free(&drmcgrp->css); + return ERR_PTR(rc); + } + if (!parent) root_drmcgrp = drmcgrp; @@ -40,3 +111,48 @@ struct cgroup_subsys drm_cgrp_subsys = { .legacy_cftypes = files, .dfl_cftypes = files, }; + +int drmcgrp_register_device(struct drm_device *dev) +{ + struct drmcgrp_device *ddev; + + ddev = kzalloc(sizeof(struct drmcgrp_device), GFP_KERNEL); + if (!ddev) + return -ENOMEM; + + ddev->dev = dev; + mutex_init(&ddev->mutex); + + mutex_lock(&drmcgrp_mutex); + known_drmcgrp_devs[dev->primary->index] = ddev; + max_minor = max(max_minor, dev->primary->index); + mutex_unlock(&drmcgrp_mutex); + + /* init cgroups created before registration (i.e. root cgroup) */ + if (root_drmcgrp != NULL) { + struct cgroup_subsys_state *pos; + struct drmcgrp *child; + + rcu_read_lock(); + css_for_each_descendant_pre(pos, &root_drmcgrp->css) { + child = css_drmcgrp(pos); + init_drmcgrp(child, dev); + } + rcu_read_unlock(); + } + + return 0; +} +EXPORT_SYMBOL(drmcgrp_register_device); + +int drmcgrp_unregister_device(struct drm_device *dev) +{ + mutex_lock(&drmcgrp_mutex); + + kfree(known_drmcgrp_devs[dev->primary->index]); + known_drmcgrp_devs[dev->primary->index] = NULL; + + mutex_unlock(&drmcgrp_mutex); + return 0; +} +EXPORT_SYMBOL(drmcgrp_unregister_device); From patchwork Wed Jun 26 15:05:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho, Kenny" X-Patchwork-Id: 11018047 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 51C9714C0 for ; Wed, 26 Jun 2019 15:05:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 375ED2845C for ; Wed, 26 Jun 2019 15:05:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2B11628856; Wed, 26 Jun 2019 15:05:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D06DC287E5 for ; Wed, 26 Jun 2019 15:05:38 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0B2706E45C; Wed, 26 Jun 2019 15:05:36 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM04-BN3-obe.outbound.protection.outlook.com (mail-eopbgr680065.outbound.protection.outlook.com [40.107.68.65]) by gabe.freedesktop.org (Postfix) with ESMTPS id DACE06E45C; Wed, 26 Jun 2019 15:05:34 +0000 (UTC) Received: from MWHPR12CA0063.namprd12.prod.outlook.com (10.175.47.153) by DM5PR1201MB0028.namprd12.prod.outlook.com (10.174.109.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Wed, 26 Jun 2019 15:05:33 +0000 Received: from DM3NAM03FT041.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e49::204) by MWHPR12CA0063.outlook.office365.com (2603:10b6:300:103::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2008.16 via Frontend Transport; Wed, 26 Jun 2019 15:05:32 +0000 Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.17) by DM3NAM03FT041.mail.protection.outlook.com (10.152.83.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2032.15 via Frontend Transport; Wed, 26 Jun 2019 15:05:31 +0000 Received: from kho-5039A.amd.com (10.180.168.240) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.389.1; Wed, 26 Jun 2019 10:05:28 -0500 From: Kenny Ho To: , , , , , , , , , Subject: [RFC PATCH v3 03/11] drm/amdgpu: Register AMD devices for DRM cgroup Date: Wed, 26 Jun 2019 11:05:14 -0400 Message-ID: <20190626150522.11618-4-Kenny.Ho@amd.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190626150522.11618-1-Kenny.Ho@amd.com> References: <20190626150522.11618-1-Kenny.Ho@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.17; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(346002)(39860400002)(396003)(136003)(376002)(2980300002)(428003)(189003)(199004)(6666004)(76176011)(110136005)(72206003)(5660300002)(53936002)(51416003)(4326008)(70206006)(26005)(77096007)(1076003)(86362001)(186003)(70586007)(7696005)(316002)(336012)(356004)(2201001)(81166006)(8936002)(446003)(11346002)(8676002)(53416004)(426003)(2870700001)(2906002)(68736007)(81156014)(36756003)(478600001)(48376002)(50466002)(47776003)(305945005)(486006)(50226002)(476003)(126002)(2616005)(921003)(83996005)(1121003)(2101003); DIR:OUT; SFP:1101; SCL:1; SRVR:DM5PR1201MB0028; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; LANG:en; PTR:InfoDomainNonexistent; MX:1; A:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f7b2eeb1-c104-40a8-0e6b-08d6fa47bc0a X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328); SRVR:DM5PR1201MB0028; X-MS-TrafficTypeDiagnostic: DM5PR1201MB0028: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:346; X-Forefront-PRVS: 00808B16F3 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: TuCmM08DqKMLJyKydIc52jYl4x3WFKzX6X6mgvW8UH/27BrK31SNwH9UTXV3F+aCfiJ03cco0iVqFEmLxwgzuAROZ0KoxhfVigED+HJuBLC4a4/qKnMg8t/a+3ZjHmOeKlHrWJaS87sRobQnAEi4CmZ201fHn+xVv2i+tMKxnM12pjktVH/jsg5qm7cdk4k0DpTRL4Xn/1LiqN+qNZ+80X6Nhp9o8AdGbP2KMx6k8vsBYzZTWv1KvB8G8Y9OaKywVeIvSBuIKbuZGQPltd67cZYTTeHYVj7O28lOJ3U4WAk5vzCD087skZXAlEyyV5XZ6xGAm3M2kzjTiOExcRBjTW/mc3EykVJt81lZETv+aUi+tB/mGfkvbSpHsS+auJ5kWP7I9ixFI4gai8Wd+WxWuPrx7TPHFig6Yma4BOwUYbI= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2019 15:05:31.8847 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f7b2eeb1-c104-40a8-0e6b-08d6fa47bc0a X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1201MB0028 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=0p3P4yVdgVx46kHS0E6T83+GWZwfheVmXlIy09vAgIk=; b=BJvEOfUZzCWXKzkhai4oFHWv2Yhu22FMzt3u6I+s1v1YWSx6ycTjGv44xtk2cUHWlH2Bc7rql0aUQsU/WNzp/W+g5oflAHZ15ibobYjwas1KCpVNEQtW2ADHKDiHEfzVPbTTm5T+vG+2OP5EUMBAl2BFLHnQVdsdg2bPtbb6rSE= X-Mailman-Original-Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=permerror action=none header.from=amd.com; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kenny Ho Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Change-Id: I3750fc657b956b52750a36cb303c54fa6a265b44 Signed-off-by: Kenny Ho --- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c index da7b4fe8ade3..2568fd730161 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c @@ -28,6 +28,7 @@ #include #include "amdgpu.h" #include +#include #include "amdgpu_sched.h" #include "amdgpu_uvd.h" #include "amdgpu_vce.h" @@ -97,6 +98,7 @@ void amdgpu_driver_unload_kms(struct drm_device *dev) amdgpu_device_fini(adev); + drmcgrp_unregister_device(dev); done_free: kfree(adev); dev->dev_private = NULL; @@ -141,6 +143,8 @@ int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags) struct amdgpu_device *adev; int r, acpi_status; + drmcgrp_register_device(dev); + #ifdef CONFIG_DRM_AMDGPU_SI if (!amdgpu_si_support) { switch (flags & AMD_ASIC_MASK) { From patchwork Wed Jun 26 15:05:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho, Kenny" X-Patchwork-Id: 11018053 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9CF3C14C0 for ; Wed, 26 Jun 2019 15:05:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 834272888A for ; Wed, 26 Jun 2019 15:05:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 77A6928848; Wed, 26 Jun 2019 15:05:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7895B287A8 for ; Wed, 26 Jun 2019 15:05:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B1D406E45E; Wed, 26 Jun 2019 15:05:38 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM03-BY2-obe.outbound.protection.outlook.com (mail-eopbgr780041.outbound.protection.outlook.com [40.107.78.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id 674EE6E45E; Wed, 26 Jun 2019 15:05:36 +0000 (UTC) Received: from MWHPR12CA0055.namprd12.prod.outlook.com (2603:10b6:300:103::17) by MWHPR12MB1711.namprd12.prod.outlook.com (2603:10b6:300:10a::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.13; Wed, 26 Jun 2019 15:05:34 +0000 Received: from DM3NAM03FT041.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e49::203) by MWHPR12CA0055.outlook.office365.com (2603:10b6:300:103::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2008.16 via Frontend Transport; Wed, 26 Jun 2019 15:05:34 +0000 Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.17) by DM3NAM03FT041.mail.protection.outlook.com (10.152.83.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2032.15 via Frontend Transport; Wed, 26 Jun 2019 15:05:33 +0000 Received: from kho-5039A.amd.com (10.180.168.240) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.389.1; Wed, 26 Jun 2019 10:05:29 -0500 From: Kenny Ho To: , , , , , , , , , Subject: [RFC PATCH v3 04/11] drm, cgroup: Add total GEM buffer allocation limit Date: Wed, 26 Jun 2019 11:05:15 -0400 Message-ID: <20190626150522.11618-5-Kenny.Ho@amd.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190626150522.11618-1-Kenny.Ho@amd.com> References: <20190626150522.11618-1-Kenny.Ho@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.17; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(39860400002)(136003)(376002)(396003)(346002)(2980300002)(428003)(199004)(189003)(53936002)(336012)(50226002)(126002)(476003)(2616005)(70586007)(86362001)(426003)(486006)(2201001)(70206006)(47776003)(446003)(11346002)(81156014)(8676002)(8936002)(81166006)(478600001)(36756003)(305945005)(1076003)(68736007)(4326008)(53416004)(72206003)(186003)(77096007)(2906002)(110136005)(50466002)(26005)(48376002)(316002)(6666004)(356004)(76176011)(30864003)(5660300002)(2870700001)(7696005)(51416003)(14444005)(921003)(2101003)(1121003)(83996005); DIR:OUT; SFP:1101; SCL:1; SRVR:MWHPR12MB1711; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; LANG:en; PTR:InfoDomainNonexistent; A:1; MX:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a3c0d4b3-0a47-44b6-74a8-08d6fa47bcff X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328); SRVR:MWHPR12MB1711; X-MS-TrafficTypeDiagnostic: MWHPR12MB1711: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:792; X-Forefront-PRVS: 00808B16F3 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: n9VJU1Jp72gK+6Gu/nmG4RhU2jk8ilFigdmvrl9z6cU6ykc3J8VlhJEsgTdcWMo7nhCuVwtlwFGoN4WCL6tNijRW1yzlZU5eR7XMzKt2v7TwlXewm+iRHl2byYzF1WXfq0uiZxhSn4t4wDzHGOF6HANeekmfawhEjg+STNPSQfIaZGtgeJ0FnFJpca0xmSmlpy05gh4OZgzIgH3K21uHKUX5C/aN+wOCo5dRWq1+3Cbwo7KM1uXuiZAJa5mqN3ZgtQSlcOtaU1jJXIdadzR+T04oGcAYU0hTronboL9v3sihWCUTo04PQ7C4K9wCqwJh2APbBfRNBUP8ay82x/tGg4rrBrMD40z4MHA1wUxXdkDTW9rPGjNIfUwbR4IYgUVMNs3xGFzBDW63qo0fhq3ymkhItYdXLAbvJZL1V9scaAA= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2019 15:05:33.4850 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a3c0d4b3-0a47-44b6-74a8-08d6fa47bcff X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR12MB1711 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=RGJmMWvDHBXJZc2j0yJEgM7sgSWqL2bpZcdMgnZsWmA=; b=jdSrJKl00lATT1IA7nk4YbLG/2IANEzC52vgh+ByZJY3NKnmFUte2RLua/SduKII5Nden/zAWSTyPIjyV5cmGgp9oaOk13Tj6fdXwRw8FCSdU81Wa8F7ONsDhIIeHvJNTzvWOp0LRZLPjXoCeFKprp9oFL1/LNnI+O91B9f5qqE= X-Mailman-Original-Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=permerror action=none header.from=amd.com; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kenny Ho Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP The drm resource being measured and limited here is the GEM buffer objects. User applications allocate and free these buffers. In addition, a process can allocate a buffer and share it with another process. The consumer of a shared buffer can also outlive the allocator of the buffer. For the purpose of cgroup accounting and limiting, ownership of the buffer is deemed to be the cgroup for which the allocating process belongs to. There is one cgroup limit per drm device. In order to prevent the buffer outliving the cgroup that owns it, a process is prevented from importing buffers that are not own by the process' cgroup or the ancestors of the process' cgroup. In other words, in order for a buffer to be shared between two cgroups, the buffer must be created by the common ancestors of the cgroups. drm.buffer.stats A read-only flat-keyed file which exists on all cgroups. Each entry is keyed by the drm device's major:minor. Total GEM buffer allocation in bytes. drm.buffer.default A read-only flat-keyed file which exists on the root cgroup. Each entry is keyed by the drm device's major:minor. Default limits on the total GEM buffer allocation in bytes. drm.buffer.max A read-write flat-keyed file which exists on all cgroups. Each entry is keyed by the drm device's major:minor. Per device limits on the total GEM buffer allocation in byte. This is a hard limit. Attempts in allocating beyond the cgroup limit will result in ENOMEM. Shorthand understood by memparse (such as k, m, g) can be used. Set allocation limit for /dev/dri/card1 to 1GB echo "226:1 1g" > drm.buffer.total.max Set allocation limit for /dev/dri/card0 to 512MB echo "226:0 512m" > drm.buffer.total.max Change-Id: I4c249d06d45ec709d6481d4cbe87c5168545c5d0 Signed-off-by: Kenny Ho --- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 4 + drivers/gpu/drm/drm_gem.c | 8 + drivers/gpu/drm/drm_prime.c | 9 + include/drm/drm_cgroup.h | 34 ++- include/drm/drm_gem.h | 11 + include/linux/cgroup_drm.h | 2 + kernel/cgroup/drm.c | 321 +++++++++++++++++++++ 7 files changed, 387 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c index 93b2c5a48a71..b4c078b7ad63 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c @@ -34,6 +34,7 @@ #include #include #include +#include #include "amdgpu.h" #include "amdgpu_trace.h" #include "amdgpu_amdkfd.h" @@ -446,6 +447,9 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev, if (!amdgpu_bo_validate_size(adev, size, bp->domain)) return -ENOMEM; + if (!drmcgrp_bo_can_allocate(current, adev->ddev, size)) + return -ENOMEM; + *bo_ptr = NULL; acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size, diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c index 6a80db077dc6..e20c1034bf2b 100644 --- a/drivers/gpu/drm/drm_gem.c +++ b/drivers/gpu/drm/drm_gem.c @@ -37,10 +37,12 @@ #include #include #include +#include #include #include #include #include +#include #include "drm_internal.h" /** @file drm_gem.c @@ -154,6 +156,9 @@ void drm_gem_private_object_init(struct drm_device *dev, obj->handle_count = 0; obj->size = size; drm_vma_node_reset(&obj->vma_node); + + obj->drmcgrp = get_drmcgrp(current); + drmcgrp_chg_bo_alloc(obj->drmcgrp, dev, size); } EXPORT_SYMBOL(drm_gem_private_object_init); @@ -804,6 +809,9 @@ drm_gem_object_release(struct drm_gem_object *obj) if (obj->filp) fput(obj->filp); + drmcgrp_unchg_bo_alloc(obj->drmcgrp, obj->dev, obj->size); + put_drmcgrp(obj->drmcgrp); + drm_gem_free_mmap_offset(obj); } EXPORT_SYMBOL(drm_gem_object_release); diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c index 231e3f6d5f41..eeb612116810 100644 --- a/drivers/gpu/drm/drm_prime.c +++ b/drivers/gpu/drm/drm_prime.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "drm_internal.h" @@ -794,6 +795,7 @@ int drm_gem_prime_fd_to_handle(struct drm_device *dev, { struct dma_buf *dma_buf; struct drm_gem_object *obj; + struct drmcgrp *drmcgrp = drmcgrp_from(current); int ret; dma_buf = dma_buf_get(prime_fd); @@ -818,6 +820,13 @@ int drm_gem_prime_fd_to_handle(struct drm_device *dev, goto out_unlock; } + /* only allow bo from the same cgroup or its ancestor to be imported */ + if (drmcgrp != NULL && + !drmcgrp_is_self_or_ancestor(drmcgrp, obj->drmcgrp)) { + ret = -EACCES; + goto out_unlock; + } + if (obj->dma_buf) { WARN_ON(obj->dma_buf != dma_buf); } else { diff --git a/include/drm/drm_cgroup.h b/include/drm/drm_cgroup.h index ddb9eab64360..8711b7c5f7bf 100644 --- a/include/drm/drm_cgroup.h +++ b/include/drm/drm_cgroup.h @@ -4,12 +4,20 @@ #ifndef __DRM_CGROUP_H__ #define __DRM_CGROUP_H__ +#include + #ifdef CONFIG_CGROUP_DRM int drmcgrp_register_device(struct drm_device *device); - int drmcgrp_unregister_device(struct drm_device *device); - +bool drmcgrp_is_self_or_ancestor(struct drmcgrp *self, + struct drmcgrp *relative); +void drmcgrp_chg_bo_alloc(struct drmcgrp *drmcgrp, struct drm_device *dev, + size_t size); +void drmcgrp_unchg_bo_alloc(struct drmcgrp *drmcgrp, struct drm_device *dev, + size_t size); +bool drmcgrp_bo_can_allocate(struct task_struct *task, struct drm_device *dev, + size_t size); #else static inline int drmcgrp_register_device(struct drm_device *device) { @@ -20,5 +28,27 @@ static inline int drmcgrp_unregister_device(struct drm_device *device) { return 0; } + +static inline bool drmcgrp_is_self_or_ancestor(struct drmcgrp *self, + struct drmcgrp *relative) +{ + return false; +} + +static inline void drmcgrp_chg_bo_alloc(struct drmcgrp *drmcgrp, + struct drm_device *dev, size_t size) +{ +} + +static inline void drmcgrp_unchg_bo_alloc(struct drmcgrp *drmcgrp, + struct drm_device *dev, size_t size) +{ +} + +static inline bool drmcgrp_bo_can_allocate(struct task_struct *task, + struct drm_device *dev, size_t size) +{ + return true; +} #endif /* CONFIG_CGROUP_DRM */ #endif /* __DRM_CGROUP_H__ */ diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h index c95727425284..09d1c69a3f0c 100644 --- a/include/drm/drm_gem.h +++ b/include/drm/drm_gem.h @@ -272,6 +272,17 @@ struct drm_gem_object { * */ const struct drm_gem_object_funcs *funcs; + + /** + * @drmcgrp: + * + * DRM cgroup this GEM object belongs to. + * + * This is used to track and limit the amount of GEM objects a user + * can allocate. Since GEM objects can be shared, this is also used + * to ensure GEM objects are only shared within the same cgroup. + */ + struct drmcgrp *drmcgrp; }; /** diff --git a/include/linux/cgroup_drm.h b/include/linux/cgroup_drm.h index 27497f786c93..efa019666f1c 100644 --- a/include/linux/cgroup_drm.h +++ b/include/linux/cgroup_drm.h @@ -15,6 +15,8 @@ struct drmcgrp_device_resource { /* for per device stats */ + s64 bo_stats_total_allocated; + s64 bo_limits_total_allocated; }; struct drmcgrp { diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c index 7da6e0d93991..cfc1fe74dca3 100644 --- a/kernel/cgroup/drm.c +++ b/kernel/cgroup/drm.c @@ -9,6 +9,7 @@ #include #include #include +#include #include static DEFINE_MUTEX(drmcgrp_mutex); @@ -16,6 +17,26 @@ static DEFINE_MUTEX(drmcgrp_mutex); struct drmcgrp_device { struct drm_device *dev; struct mutex mutex; + + s64 bo_limits_total_allocated_default; +}; + +#define DRMCG_CTF_PRIV_SIZE 3 +#define DRMCG_CTF_PRIV_MASK GENMASK((DRMCG_CTF_PRIV_SIZE - 1), 0) +#define DRMCG_CTF_PRIV(res_type, f_type) ((res_type) <<\ + DRMCG_CTF_PRIV_SIZE | (f_type)) +#define DRMCG_CTF_PRIV2RESTYPE(priv) ((priv) >> DRMCG_CTF_PRIV_SIZE) +#define DRMCG_CTF_PRIV2FTYPE(priv) ((priv) & DRMCG_CTF_PRIV_MASK) + + +enum drmcgrp_res_type { + DRMCGRP_TYPE_BO_TOTAL, +}; + +enum drmcgrp_file_type { + DRMCGRP_FTYPE_STATS, + DRMCGRP_FTYPE_LIMIT, + DRMCGRP_FTYPE_DEFAULT, }; /* indexed by drm_minor for access speed */ @@ -54,6 +75,10 @@ static inline int init_drmcgrp_single(struct drmcgrp *drmcgrp, int minor) } /* set defaults here */ + if (known_drmcgrp_devs[minor] != NULL) { + ddr->bo_limits_total_allocated = + known_drmcgrp_devs[minor]->bo_limits_total_allocated_default; + } return 0; } @@ -100,7 +125,225 @@ drmcgrp_css_alloc(struct cgroup_subsys_state *parent_css) return &drmcgrp->css; } +static inline void drmcgrp_print_stats(struct drmcgrp_device_resource *ddr, + struct seq_file *sf, enum drmcgrp_res_type type) +{ + if (ddr == NULL) { + seq_puts(sf, "\n"); + return; + } + + switch (type) { + case DRMCGRP_TYPE_BO_TOTAL: + seq_printf(sf, "%lld\n", ddr->bo_stats_total_allocated); + break; + default: + seq_puts(sf, "\n"); + break; + } +} + +static inline void drmcgrp_print_limits(struct drmcgrp_device_resource *ddr, + struct seq_file *sf, enum drmcgrp_res_type type) +{ + if (ddr == NULL) { + seq_puts(sf, "\n"); + return; + } + + switch (type) { + case DRMCGRP_TYPE_BO_TOTAL: + seq_printf(sf, "%lld\n", ddr->bo_limits_total_allocated); + break; + default: + seq_puts(sf, "\n"); + break; + } +} + +static inline void drmcgrp_print_default(struct drmcgrp_device *ddev, + struct seq_file *sf, enum drmcgrp_res_type type) +{ + if (ddev == NULL) { + seq_puts(sf, "\n"); + return; + } + + switch (type) { + case DRMCGRP_TYPE_BO_TOTAL: + seq_printf(sf, "%lld\n", + ddev->bo_limits_total_allocated_default); + break; + default: + seq_puts(sf, "\n"); + break; + } +} + +int drmcgrp_bo_show(struct seq_file *sf, void *v) +{ + struct drmcgrp *drmcgrp = css_drmcgrp(seq_css(sf)); + struct drmcgrp_device_resource *ddr = NULL; + enum drmcgrp_file_type f_type = + DRMCG_CTF_PRIV2FTYPE(seq_cft(sf)->private); + enum drmcgrp_res_type type = + DRMCG_CTF_PRIV2RESTYPE(seq_cft(sf)->private); + struct drmcgrp_device *ddev; + int i; + + for (i = 0; i <= max_minor; i++) { + ddr = drmcgrp->dev_resources[i]; + ddev = known_drmcgrp_devs[i]; + + seq_printf(sf, "%d:%d ", DRM_MAJOR, i); + + switch (f_type) { + case DRMCGRP_FTYPE_STATS: + drmcgrp_print_stats(ddr, sf, type); + break; + case DRMCGRP_FTYPE_LIMIT: + drmcgrp_print_limits(ddr, sf, type); + break; + case DRMCGRP_FTYPE_DEFAULT: + drmcgrp_print_default(ddev, sf, type); + break; + default: + seq_puts(sf, "\n"); + break; + } + } + + return 0; +} + +static inline void drmcgrp_pr_cft_err(const struct drmcgrp *drmcgrp, + const char *cft_name, int minor) +{ + pr_err("drmcgrp: error parsing %s, minor %d ", + cft_name, minor); + pr_cont_cgroup_name(drmcgrp->css.cgroup); + pr_cont("\n"); +} + +static inline int drmcgrp_process_limit_val(char *sval, bool is_mem, + s64 def_val, s64 max_val, s64 *ret_val) +{ + int rc = strcmp("max", sval); + + + if (!rc) + *ret_val = max_val; + else { + rc = strcmp("default", sval); + + if (!rc) + *ret_val = def_val; + } + + if (rc) { + if (is_mem) { + *ret_val = memparse(sval, NULL); + rc = 0; + } else { + rc = kstrtoll(sval, 0, ret_val); + } + } + + if (*ret_val > max_val) + *ret_val = max_val; + + return rc; +} + +ssize_t drmcgrp_bo_limit_write(struct kernfs_open_file *of, char *buf, + size_t nbytes, loff_t off) +{ + struct drmcgrp *drmcgrp = css_drmcgrp(of_css(of)); + struct drmcgrp *parent = parent_drmcgrp(drmcgrp); + enum drmcgrp_res_type type = + DRMCG_CTF_PRIV2RESTYPE(of_cft(of)->private); + char *cft_name = of_cft(of)->name; + char *limits = strstrip(buf); + struct drmcgrp_device *ddev; + struct drmcgrp_device_resource *ddr; + char *line; + char sattr[256]; + s64 val; + s64 p_max; + int rc; + int minor; + + while (limits != NULL) { + line = strsep(&limits, "\n"); + + if (sscanf(line, + __stringify(DRM_MAJOR)":%u %255[^\t\n]", + &minor, sattr) != 2) { + pr_err("drmcgrp: error parsing %s ", cft_name); + pr_cont_cgroup_name(drmcgrp->css.cgroup); + pr_cont("\n"); + + continue; + } + + if (minor < 0 || minor > max_minor) { + pr_err("drmcgrp: invalid minor %d for %s ", + minor, cft_name); + pr_cont_cgroup_name(drmcgrp->css.cgroup); + pr_cont("\n"); + + continue; + } + + ddr = drmcgrp->dev_resources[minor]; + ddev = known_drmcgrp_devs[minor]; + switch (type) { + case DRMCGRP_TYPE_BO_TOTAL: + p_max = parent == NULL ? S64_MAX : + parent->dev_resources[minor]-> + bo_limits_total_allocated; + + rc = drmcgrp_process_limit_val(sattr, true, + ddev->bo_limits_total_allocated_default, + p_max, + &val); + + if (rc || val < 0) { + drmcgrp_pr_cft_err(drmcgrp, cft_name, minor); + continue; + } + + ddr->bo_limits_total_allocated = val; + break; + default: + break; + } + } + + return nbytes; +} + struct cftype files[] = { + { + .name = "buffer.total.stats", + .seq_show = drmcgrp_bo_show, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BO_TOTAL, + DRMCGRP_FTYPE_STATS), + }, + { + .name = "buffer.total.default", + .seq_show = drmcgrp_bo_show, + .flags = CFTYPE_ONLY_ON_ROOT, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BO_TOTAL, + DRMCGRP_FTYPE_DEFAULT), + }, + { + .name = "buffer.total.max", + .write = drmcgrp_bo_limit_write, + .seq_show = drmcgrp_bo_show, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BO_TOTAL, + DRMCGRP_FTYPE_LIMIT), + }, { } /* terminate */ }; @@ -121,6 +364,8 @@ int drmcgrp_register_device(struct drm_device *dev) return -ENOMEM; ddev->dev = dev; + ddev->bo_limits_total_allocated_default = S64_MAX; + mutex_init(&ddev->mutex); mutex_lock(&drmcgrp_mutex); @@ -156,3 +401,79 @@ int drmcgrp_unregister_device(struct drm_device *dev) return 0; } EXPORT_SYMBOL(drmcgrp_unregister_device); + +bool drmcgrp_is_self_or_ancestor(struct drmcgrp *self, struct drmcgrp *relative) +{ + for (; self != NULL; self = parent_drmcgrp(self)) + if (self == relative) + return true; + + return false; +} +EXPORT_SYMBOL(drmcgrp_is_self_or_ancestor); + +bool drmcgrp_bo_can_allocate(struct task_struct *task, struct drm_device *dev, + size_t size) +{ + struct drmcgrp *drmcgrp = drmcgrp_from(task); + struct drmcgrp_device_resource *ddr; + struct drmcgrp_device_resource *d; + int devIdx = dev->primary->index; + bool result = true; + s64 delta = 0; + + if (drmcgrp == NULL || drmcgrp == root_drmcgrp) + return true; + + ddr = drmcgrp->dev_resources[devIdx]; + mutex_lock(&known_drmcgrp_devs[devIdx]->mutex); + for ( ; drmcgrp != root_drmcgrp; drmcgrp = parent_drmcgrp(drmcgrp)) { + d = drmcgrp->dev_resources[devIdx]; + delta = d->bo_limits_total_allocated - + d->bo_stats_total_allocated; + + if (delta <= 0 || size > delta) { + result = false; + break; + } + } + mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); + + return result; +} +EXPORT_SYMBOL(drmcgrp_bo_can_allocate); + +void drmcgrp_chg_bo_alloc(struct drmcgrp *drmcgrp, struct drm_device *dev, + size_t size) +{ + struct drmcgrp_device_resource *ddr; + int devIdx = dev->primary->index; + + if (drmcgrp == NULL || known_drmcgrp_devs[devIdx] == NULL) + return; + + mutex_lock(&known_drmcgrp_devs[devIdx]->mutex); + for ( ; drmcgrp != NULL; drmcgrp = parent_drmcgrp(drmcgrp)) { + ddr = drmcgrp->dev_resources[devIdx]; + + ddr->bo_stats_total_allocated += (s64)size; + } + mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); +} +EXPORT_SYMBOL(drmcgrp_chg_bo_alloc); + +void drmcgrp_unchg_bo_alloc(struct drmcgrp *drmcgrp, struct drm_device *dev, + size_t size) +{ + int devIdx = dev->primary->index; + + if (drmcgrp == NULL || known_drmcgrp_devs[devIdx] == NULL) + return; + + mutex_lock(&known_drmcgrp_devs[devIdx]->mutex); + for ( ; drmcgrp != NULL; drmcgrp = parent_drmcgrp(drmcgrp)) + drmcgrp->dev_resources[devIdx]->bo_stats_total_allocated + -= (s64)size; + mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); +} +EXPORT_SYMBOL(drmcgrp_unchg_bo_alloc); From patchwork Wed Jun 26 15:05:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho, Kenny" X-Patchwork-Id: 11018063 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7ED0D924 for ; Wed, 26 Jun 2019 15:06:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6D15028998 for ; Wed, 26 Jun 2019 15:06:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6134A289A2; Wed, 26 Jun 2019 15:06:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BF63428998 for ; Wed, 26 Jun 2019 15:05:59 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E4C506E478; Wed, 26 Jun 2019 15:05:50 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM03-BY2-obe.outbound.protection.outlook.com (mail-eopbgr780044.outbound.protection.outlook.com [40.107.78.44]) by gabe.freedesktop.org (Postfix) with ESMTPS id 881106E45E; Wed, 26 Jun 2019 15:05:37 +0000 (UTC) Received: from MWHPR12CA0063.namprd12.prod.outlook.com (10.175.47.153) by DM5PR1201MB0028.namprd12.prod.outlook.com (10.174.109.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Wed, 26 Jun 2019 15:05:35 +0000 Received: from DM3NAM03FT041.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e49::204) by MWHPR12CA0063.outlook.office365.com (2603:10b6:300:103::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2008.16 via Frontend Transport; Wed, 26 Jun 2019 15:05:35 +0000 Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.17) by DM3NAM03FT041.mail.protection.outlook.com (10.152.83.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2032.15 via Frontend Transport; Wed, 26 Jun 2019 15:05:35 +0000 Received: from kho-5039A.amd.com (10.180.168.240) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.389.1; Wed, 26 Jun 2019 10:05:30 -0500 From: Kenny Ho To: , , , , , , , , , Subject: [RFC PATCH v3 05/11] drm, cgroup: Add peak GEM buffer allocation limit Date: Wed, 26 Jun 2019 11:05:16 -0400 Message-ID: <20190626150522.11618-6-Kenny.Ho@amd.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190626150522.11618-1-Kenny.Ho@amd.com> References: <20190626150522.11618-1-Kenny.Ho@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.17; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(346002)(39860400002)(396003)(136003)(376002)(2980300002)(428003)(189003)(199004)(6666004)(76176011)(110136005)(72206003)(5660300002)(53936002)(51416003)(4326008)(70206006)(26005)(77096007)(1076003)(86362001)(186003)(70586007)(7696005)(316002)(336012)(356004)(2201001)(81166006)(8936002)(446003)(11346002)(8676002)(53416004)(426003)(2870700001)(2906002)(68736007)(81156014)(36756003)(478600001)(14444005)(48376002)(50466002)(47776003)(305945005)(486006)(50226002)(476003)(126002)(2616005)(921003)(83996005)(1121003)(2101003); DIR:OUT; SFP:1101; SCL:1; SRVR:DM5PR1201MB0028; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; LANG:en; PTR:InfoDomainNonexistent; MX:1; A:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 40444587-89c9-42a7-2fa7-08d6fa47bddd X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328); SRVR:DM5PR1201MB0028; X-MS-TrafficTypeDiagnostic: DM5PR1201MB0028: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-Forefront-PRVS: 00808B16F3 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: hWiUcZJUbTuEKKY0rFU/eXpzpgxXOyaESh+mhgiT4jTeizTsm3/HqvpdthkFT461hqkFOe0L2jURyWHl9jews9Wk0LnVIREAU6wstSXTHonlbwPMk+h8VrBYhBNlLRDyUzw++G/Yq58wTrBVEkNEDQ3XcGcfltiB7MdDKBro51IPLb8SQxD1pnU+hcyG3C27rlzQuf2vZaWlqMe9i/T/3CrxfGGALecQHqouEQ58WjAGbbLhYLeyWTBgwr7ng0Su40Vg4VdPT3zQ2XnhCCofFoiBzGOJsXxFY0pbYA1JcD9lG7n4JHIyc0AM0dLuxu/1tDVEuk0tCKe1iUIiGis5przfTM4nRcFbrkSIOQhFgBadRN3sgBSq/BB1EvhoTAtgMU6sP+Tu8MphhmASj7vRRTC16cZuTOo6hIjvbmc2lAM= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2019 15:05:35.0422 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 40444587-89c9-42a7-2fa7-08d6fa47bddd X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1201MB0028 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=83ikolpppUs7rb6wD292y/uDlNku2ipE9H78xsGZVLY=; b=qWCIdD1En8PCvDN3XCiX8sPRI32ouDpgyBEUGh2Q9901Rjz8wM10AObUlHCs71r0+YYhW4luuyQcIN8u/tuYviLZqsj4NncjDlKXvlF3zVukwYclW9eWXH/kQjswuj2rwQivNBcOj4QWUMAXoOzOiWNrbnxNz+7x8U/pqjLVB9E= X-Mailman-Original-Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=permerror action=none header.from=amd.com; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kenny Ho Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP drm.buffer.peak.stats A read-only flat-keyed file which exists on all cgroups. Each entry is keyed by the drm device's major:minor. Largest GEM buffer allocated in bytes. drm.buffer.peak.default A read-only flat-keyed file which exists on the root cgroup. Each entry is keyed by the drm device's major:minor. Default limits on the largest GEM buffer allocation in bytes. drm.buffer.peak.max A read-write flat-keyed file which exists on all cgroups. Each entry is keyed by the drm device's major:minor. Per device limits on the largest GEM buffer allocation in bytes. This is a hard limit. Attempts in allocating beyond the cgroup limit will result in ENOMEM. Shorthand understood by memparse (such as k, m, g) can be used. Set largest allocation for /dev/dri/card1 to 4MB echo "226:1 4m" > drm.buffer.peak.max Change-Id: I0830d56775568e1cf215b56cc892d5e7945e9f25 Signed-off-by: Kenny Ho --- include/linux/cgroup_drm.h | 3 ++ kernel/cgroup/drm.c | 61 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/include/linux/cgroup_drm.h b/include/linux/cgroup_drm.h index efa019666f1c..126c156ffd70 100644 --- a/include/linux/cgroup_drm.h +++ b/include/linux/cgroup_drm.h @@ -17,6 +17,9 @@ struct drmcgrp_device_resource { /* for per device stats */ s64 bo_stats_total_allocated; s64 bo_limits_total_allocated; + + size_t bo_stats_peak_allocated; + size_t bo_limits_peak_allocated; }; struct drmcgrp { diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c index cfc1fe74dca3..265008197654 100644 --- a/kernel/cgroup/drm.c +++ b/kernel/cgroup/drm.c @@ -19,6 +19,7 @@ struct drmcgrp_device { struct mutex mutex; s64 bo_limits_total_allocated_default; + size_t bo_limits_peak_allocated_default; }; #define DRMCG_CTF_PRIV_SIZE 3 @@ -31,6 +32,7 @@ struct drmcgrp_device { enum drmcgrp_res_type { DRMCGRP_TYPE_BO_TOTAL, + DRMCGRP_TYPE_BO_PEAK, }; enum drmcgrp_file_type { @@ -78,6 +80,9 @@ static inline int init_drmcgrp_single(struct drmcgrp *drmcgrp, int minor) if (known_drmcgrp_devs[minor] != NULL) { ddr->bo_limits_total_allocated = known_drmcgrp_devs[minor]->bo_limits_total_allocated_default; + + ddr->bo_limits_peak_allocated = + known_drmcgrp_devs[minor]->bo_limits_peak_allocated_default; } return 0; @@ -137,6 +142,9 @@ static inline void drmcgrp_print_stats(struct drmcgrp_device_resource *ddr, case DRMCGRP_TYPE_BO_TOTAL: seq_printf(sf, "%lld\n", ddr->bo_stats_total_allocated); break; + case DRMCGRP_TYPE_BO_PEAK: + seq_printf(sf, "%zu\n", ddr->bo_stats_peak_allocated); + break; default: seq_puts(sf, "\n"); break; @@ -155,6 +163,9 @@ static inline void drmcgrp_print_limits(struct drmcgrp_device_resource *ddr, case DRMCGRP_TYPE_BO_TOTAL: seq_printf(sf, "%lld\n", ddr->bo_limits_total_allocated); break; + case DRMCGRP_TYPE_BO_PEAK: + seq_printf(sf, "%zu\n", ddr->bo_limits_peak_allocated); + break; default: seq_puts(sf, "\n"); break; @@ -174,6 +185,10 @@ static inline void drmcgrp_print_default(struct drmcgrp_device *ddev, seq_printf(sf, "%lld\n", ddev->bo_limits_total_allocated_default); break; + case DRMCGRP_TYPE_BO_PEAK: + seq_printf(sf, "%zu\n", + ddev->bo_limits_peak_allocated_default); + break; default: seq_puts(sf, "\n"); break; @@ -315,6 +330,23 @@ ssize_t drmcgrp_bo_limit_write(struct kernfs_open_file *of, char *buf, ddr->bo_limits_total_allocated = val; break; + case DRMCGRP_TYPE_BO_PEAK: + p_max = parent == NULL ? SIZE_MAX : + parent->dev_resources[minor]-> + bo_limits_peak_allocated; + + rc = drmcgrp_process_limit_val(sattr, true, + ddev->bo_limits_peak_allocated_default, + p_max, + &val); + + if (rc || val < 0) { + drmcgrp_pr_cft_err(drmcgrp, cft_name, minor); + continue; + } + + ddr->bo_limits_peak_allocated = val; + break; default: break; } @@ -344,6 +376,26 @@ struct cftype files[] = { .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BO_TOTAL, DRMCGRP_FTYPE_LIMIT), }, + { + .name = "buffer.peak.stats", + .seq_show = drmcgrp_bo_show, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BO_PEAK, + DRMCGRP_FTYPE_STATS), + }, + { + .name = "buffer.peak.default", + .seq_show = drmcgrp_bo_show, + .flags = CFTYPE_ONLY_ON_ROOT, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BO_PEAK, + DRMCGRP_FTYPE_DEFAULT), + }, + { + .name = "buffer.peak.max", + .write = drmcgrp_bo_limit_write, + .seq_show = drmcgrp_bo_show, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BO_PEAK, + DRMCGRP_FTYPE_LIMIT), + }, { } /* terminate */ }; @@ -365,6 +417,7 @@ int drmcgrp_register_device(struct drm_device *dev) ddev->dev = dev; ddev->bo_limits_total_allocated_default = S64_MAX; + ddev->bo_limits_peak_allocated_default = SIZE_MAX; mutex_init(&ddev->mutex); @@ -436,6 +489,11 @@ bool drmcgrp_bo_can_allocate(struct task_struct *task, struct drm_device *dev, result = false; break; } + + if (d->bo_limits_peak_allocated < size) { + result = false; + break; + } } mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); @@ -457,6 +515,9 @@ void drmcgrp_chg_bo_alloc(struct drmcgrp *drmcgrp, struct drm_device *dev, ddr = drmcgrp->dev_resources[devIdx]; ddr->bo_stats_total_allocated += (s64)size; + + if (ddr->bo_stats_peak_allocated < (size_t)size) + ddr->bo_stats_peak_allocated = (size_t)size; } mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); } From patchwork Wed Jun 26 15:05:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho, Kenny" X-Patchwork-Id: 11018057 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 738A7924 for ; Wed, 26 Jun 2019 15:05:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 46F6B27FB7 for ; Wed, 26 Jun 2019 15:05:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2D6DA287E5; Wed, 26 Jun 2019 15:05:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C967027FB7 for ; Wed, 26 Jun 2019 15:05:53 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8CB576E46B; Wed, 26 Jun 2019 15:05:49 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM02-BL2-obe.outbound.protection.outlook.com (mail-bl2nam02on062b.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe46::62b]) by gabe.freedesktop.org (Postfix) with ESMTPS id 739016E462; Wed, 26 Jun 2019 15:05:39 +0000 (UTC) Received: from MWHPR12CA0058.namprd12.prod.outlook.com (2603:10b6:300:103::20) by CY4PR12MB1751.namprd12.prod.outlook.com (2603:10b6:903:121::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Wed, 26 Jun 2019 15:05:37 +0000 Received: from DM3NAM03FT041.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e49::209) by MWHPR12CA0058.outlook.office365.com (2603:10b6:300:103::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2008.16 via Frontend Transport; Wed, 26 Jun 2019 15:05:36 +0000 Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.17) by DM3NAM03FT041.mail.protection.outlook.com (10.152.83.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2032.15 via Frontend Transport; Wed, 26 Jun 2019 15:05:36 +0000 Received: from kho-5039A.amd.com (10.180.168.240) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.389.1; Wed, 26 Jun 2019 10:05:31 -0500 From: Kenny Ho To: , , , , , , , , , Subject: [RFC PATCH v3 06/11] drm, cgroup: Add GEM buffer allocation count stats Date: Wed, 26 Jun 2019 11:05:17 -0400 Message-ID: <20190626150522.11618-7-Kenny.Ho@amd.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190626150522.11618-1-Kenny.Ho@amd.com> References: <20190626150522.11618-1-Kenny.Ho@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.17; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(136003)(39860400002)(346002)(376002)(396003)(2980300002)(428003)(199004)(189003)(53416004)(426003)(77096007)(36756003)(26005)(5660300002)(336012)(186003)(2201001)(70586007)(70206006)(86362001)(47776003)(8936002)(68736007)(4326008)(8676002)(110136005)(316002)(1076003)(81156014)(81166006)(72206003)(478600001)(486006)(50466002)(76176011)(305945005)(50226002)(2616005)(48376002)(476003)(126002)(11346002)(6666004)(446003)(2906002)(53936002)(7696005)(51416003)(14444005)(356004)(2870700001)(921003)(1121003)(2101003)(83996005); DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR12MB1751; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; LANG:en; PTR:InfoDomainNonexistent; A:1; MX:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 77ed1547-d3dc-44cd-e4b1-08d6fa47be98 X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328); SRVR:CY4PR12MB1751; X-MS-TrafficTypeDiagnostic: CY4PR12MB1751: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3513; X-Forefront-PRVS: 00808B16F3 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: +XMNoG4oqpvuqahA2k6RjT4YJDlnccDQJToDnhvOiKarrneRZXcLuDqSxQ6rshwCYlpbIw98mPW8xVYWGK09AfJI3I7dxIetzOQ9tjvnHHEq3ovbV1rr10VhzgWXBaV0WHMx2MqRA7QKbIUHu+LhJlc+LkgP1fp4JvWO7mDfBeOi+5b8lNHyEap8WUBnhV0GlBqQWwr/SwqQ1l2XYWglXN43OJ6HcAEi8g0kq2VGBoRuwrvGKVWv3mC9X4ZsQ3AnAQ/XXrRzaErKLJPOnuyJ8Zc0KASRtH+wdkNuzQnWFvq4+G/sFpXV5X8ZeAzcL/BA5AvXhVqC892SBIBJ3J3esxTEL4ZvJw0YAlu7iPqLd6kTiChJXvcQ1xs81KxJY6FYd2Y9Mt46e40pIqqZr39YTm7+QCLO2OSkvlDGbDPHylM= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2019 15:05:36.2493 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 77ed1547-d3dc-44cd-e4b1-08d6fa47be98 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1751 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=oy70+PsSIFW3ia6hp19FBTQnzI35FoEFw66qRWkUfP8=; b=uJfjBQ+Xvo2Juc0ogtNJZAPsvKsUmZFBlTjLia6ENxHTcskhU+1cdO/4RWOOW5qBnX8u4O9nN/CeT0UE33XcMPBppI2ATKWLmWXeUrdOBmuCbfHVMalarqlLNO621/W2fT+8CUJTjotND0xD2dL5kOANDiscdmJvyndzPEhtqoo= X-Mailman-Original-Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=permerror action=none header.from=amd.com; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kenny Ho Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP drm.buffer.count.stats A read-only flat-keyed file which exists on all cgroups. Each entry is keyed by the drm device's major:minor. Total number of GEM buffer allocated. Change-Id: Id3e1809d5fee8562e47a7d2b961688956d844ec6 Signed-off-by: Kenny Ho --- include/linux/cgroup_drm.h | 2 ++ kernel/cgroup/drm.c | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/include/linux/cgroup_drm.h b/include/linux/cgroup_drm.h index 126c156ffd70..e4400b21ab8e 100644 --- a/include/linux/cgroup_drm.h +++ b/include/linux/cgroup_drm.h @@ -20,6 +20,8 @@ struct drmcgrp_device_resource { size_t bo_stats_peak_allocated; size_t bo_limits_peak_allocated; + + s64 bo_stats_count_allocated; }; struct drmcgrp { diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c index 265008197654..9144f93b851f 100644 --- a/kernel/cgroup/drm.c +++ b/kernel/cgroup/drm.c @@ -33,6 +33,7 @@ struct drmcgrp_device { enum drmcgrp_res_type { DRMCGRP_TYPE_BO_TOTAL, DRMCGRP_TYPE_BO_PEAK, + DRMCGRP_TYPE_BO_COUNT, }; enum drmcgrp_file_type { @@ -145,6 +146,9 @@ static inline void drmcgrp_print_stats(struct drmcgrp_device_resource *ddr, case DRMCGRP_TYPE_BO_PEAK: seq_printf(sf, "%zu\n", ddr->bo_stats_peak_allocated); break; + case DRMCGRP_TYPE_BO_COUNT: + seq_printf(sf, "%lld\n", ddr->bo_stats_count_allocated); + break; default: seq_puts(sf, "\n"); break; @@ -396,6 +400,12 @@ struct cftype files[] = { .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BO_PEAK, DRMCGRP_FTYPE_LIMIT), }, + { + .name = "buffer.count.stats", + .seq_show = drmcgrp_bo_show, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BO_COUNT, + DRMCGRP_FTYPE_STATS), + }, { } /* terminate */ }; @@ -518,6 +528,8 @@ void drmcgrp_chg_bo_alloc(struct drmcgrp *drmcgrp, struct drm_device *dev, if (ddr->bo_stats_peak_allocated < (size_t)size) ddr->bo_stats_peak_allocated = (size_t)size; + + ddr->bo_stats_count_allocated++; } mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); } @@ -526,15 +538,20 @@ EXPORT_SYMBOL(drmcgrp_chg_bo_alloc); void drmcgrp_unchg_bo_alloc(struct drmcgrp *drmcgrp, struct drm_device *dev, size_t size) { + struct drmcgrp_device_resource *ddr; int devIdx = dev->primary->index; if (drmcgrp == NULL || known_drmcgrp_devs[devIdx] == NULL) return; mutex_lock(&known_drmcgrp_devs[devIdx]->mutex); - for ( ; drmcgrp != NULL; drmcgrp = parent_drmcgrp(drmcgrp)) - drmcgrp->dev_resources[devIdx]->bo_stats_total_allocated - -= (s64)size; + for ( ; drmcgrp != NULL; drmcgrp = parent_drmcgrp(drmcgrp)) { + ddr = drmcgrp->dev_resources[devIdx]; + + ddr->bo_stats_total_allocated -= (s64)size; + + ddr->bo_stats_count_allocated--; + } mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); } EXPORT_SYMBOL(drmcgrp_unchg_bo_alloc); From patchwork Wed Jun 26 15:05:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho, Kenny" X-Patchwork-Id: 11018059 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3397D14C0 for ; Wed, 26 Jun 2019 15:05:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 21A3D287E5 for ; Wed, 26 Jun 2019 15:05:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 15AED288AC; Wed, 26 Jun 2019 15:05:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B73F5284D1 for ; Wed, 26 Jun 2019 15:05:55 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 97E5F6E46C; Wed, 26 Jun 2019 15:05:49 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM02-CY1-obe.outbound.protection.outlook.com (mail-eopbgr760082.outbound.protection.outlook.com [40.107.76.82]) by gabe.freedesktop.org (Postfix) with ESMTPS id 409906E467; Wed, 26 Jun 2019 15:05:41 +0000 (UTC) Received: from MWHPR12CA0068.namprd12.prod.outlook.com (2603:10b6:300:103::30) by MWHPR1201MB0030.namprd12.prod.outlook.com (2603:10b6:301:53::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Wed, 26 Jun 2019 15:05:38 +0000 Received: from DM3NAM03FT041.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e49::209) by MWHPR12CA0068.outlook.office365.com (2603:10b6:300:103::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2008.16 via Frontend Transport; Wed, 26 Jun 2019 15:05:38 +0000 Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.17) by DM3NAM03FT041.mail.protection.outlook.com (10.152.83.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2032.15 via Frontend Transport; Wed, 26 Jun 2019 15:05:37 +0000 Received: from kho-5039A.amd.com (10.180.168.240) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.389.1; Wed, 26 Jun 2019 10:05:31 -0500 From: Kenny Ho To: , , , , , , , , , Subject: [RFC PATCH v3 07/11] drm, cgroup: Add TTM buffer allocation stats Date: Wed, 26 Jun 2019 11:05:18 -0400 Message-ID: <20190626150522.11618-8-Kenny.Ho@amd.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190626150522.11618-1-Kenny.Ho@amd.com> References: <20190626150522.11618-1-Kenny.Ho@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.17; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(376002)(136003)(396003)(39860400002)(346002)(2980300002)(428003)(199004)(189003)(126002)(2616005)(476003)(30864003)(68736007)(6666004)(486006)(356004)(76176011)(8936002)(2201001)(4326008)(5660300002)(426003)(50226002)(36756003)(336012)(1076003)(446003)(77096007)(11346002)(186003)(14444005)(316002)(110136005)(50466002)(48376002)(7696005)(81166006)(2906002)(47776003)(2870700001)(53416004)(53936002)(72206003)(51416003)(81156014)(305945005)(8676002)(478600001)(86362001)(70206006)(26005)(70586007)(921003)(1121003)(2101003)(83996005); DIR:OUT; SFP:1101; SCL:1; SRVR:MWHPR1201MB0030; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; LANG:en; PTR:InfoDomainNonexistent; A:1; MX:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ed8f9b89-2dbb-4798-4c5c-08d6fa47bf83 X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328); SRVR:MWHPR1201MB0030; X-MS-TrafficTypeDiagnostic: MWHPR1201MB0030: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2887; X-Forefront-PRVS: 00808B16F3 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: 7JUfSxVgabMi2euTBF1CO/QHEMDeN6T9BuCU6ECbkLjlgxMaUadAH8alhhR5KWf6eVjPmchAJm5JcoHq6mgVqX9M/awMa/oa7S9BZXUYJgvZJ3Tcv/tCIODJrQzmAGzxAcFxce2+JCN3+l2DvYR3rXQFI1hbl3BfTvY0DTwsAD+SxnokX02zkYhGoEkFivqXXYEQU/lPJXjcmqQaYWZ3S0AlLaizG1g63bxFQyfPFIQBhU/MikQhWsTUxyLyORj0N+EKXaN7kKjUyk807+VKfos4v4OkfAcAle1GMrTFi3aNpeLirIzgmmcY1NkqOUbSH3vF5cLYYCdc+6k1Pht2TV58TmZSA/Dfvfn0/IyI7nu+OXSFZQbi6ofjErR5IJumY6LHMKtGSdWmZik0SsmAQf0018BLV6ZBdGlij36tcJM= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2019 15:05:37.4593 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ed8f9b89-2dbb-4798-4c5c-08d6fa47bf83 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR1201MB0030 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2NCdNe76QYSnO5DSBCCWD+xVVU3Fpx5WL0Ju2T0zdds=; b=shldbPvohzi3h+tSsi7sY2vX//gbf4Nze4tmxdgZKxKsuLi8zd8bOYiJvlTIueiRIIz5i/Zu8I+WWol8cbHfw8SQq1fiPEfF7oKzvbXp5M5xnytJTQTiSV27Ni+0tOo6U7bY8//31AaCT3yXtpmDtWmknUfJsNwY2DVdgk2gj80= X-Mailman-Original-Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=permerror action=none header.from=amd.com; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kenny Ho Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP The drm resource being measured is the TTM (Translation Table Manager) buffers. TTM manages different types of memory that a GPU might access. These memory types include dedicated Video RAM (VRAM) and host/system memory accessible through IOMMU (GART/GTT). TTM is currently used by multiple drm drivers (amd, ast, bochs, cirrus, hisilicon, maga200, nouveau, qxl, virtio, vmwgfx.) drm.memory.stats A read-only nested-keyed file which exists on all cgroups. Each entry is keyed by the drm device's major:minor. The following nested keys are defined. ====== ============================================= system Host/system memory tt Host memory used by the drm device (GTT/GART) vram Video RAM used by the drm device priv Other drm device, vendor specific memory ====== ============================================= Reading returns the following:: 226:0 system=0 tt=0 vram=0 priv=0 226:1 system=0 tt=9035776 vram=17768448 priv=16809984 226:2 system=0 tt=9035776 vram=17768448 priv=16809984 drm.memory.evict.stats A read-only flat-keyed file which exists on all cgroups. Each entry is keyed by the drm device's major:minor. Total number of evictions. Change-Id: Ice2c4cc845051229549bebeb6aa2d7d6153bdf6a Signed-off-by: Kenny Ho --- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 3 +- drivers/gpu/drm/ttm/ttm_bo.c | 30 +++++++ drivers/gpu/drm/ttm/ttm_bo_util.c | 4 + include/drm/drm_cgroup.h | 19 ++++ include/drm/ttm/ttm_bo_api.h | 2 + include/drm/ttm/ttm_bo_driver.h | 8 ++ include/linux/cgroup_drm.h | 4 + kernel/cgroup/drm.c | 113 ++++++++++++++++++++++++ 8 files changed, 182 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c index e9ecc3953673..a8dfc78ed45f 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c @@ -1678,8 +1678,9 @@ int amdgpu_ttm_init(struct amdgpu_device *adev) mutex_init(&adev->mman.gtt_window_lock); /* No others user of address space so set it to 0 */ - r = ttm_bo_device_init(&adev->mman.bdev, + r = ttm_bo_device_init_tmp(&adev->mman.bdev, &amdgpu_bo_driver, + adev->ddev, adev->ddev->anon_inode->i_mapping, adev->need_dma32); if (r) { diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c index 2845fceb2fbd..e9f70547f0ad 100644 --- a/drivers/gpu/drm/ttm/ttm_bo.c +++ b/drivers/gpu/drm/ttm/ttm_bo.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,7 @@ #include #include #include +#include static void ttm_bo_global_kobj_release(struct kobject *kobj); @@ -151,6 +153,10 @@ static void ttm_bo_release_list(struct kref *list_kref) struct ttm_bo_device *bdev = bo->bdev; size_t acc_size = bo->acc_size; + if (bo->bdev->ddev != NULL) // TODO: remove after ddev initiazlied for all + drmcgrp_unchg_mem(bo); + put_drmcgrp(bo->drmcgrp); + BUG_ON(kref_read(&bo->list_kref)); BUG_ON(kref_read(&bo->kref)); BUG_ON(atomic_read(&bo->cpu_writers)); @@ -353,6 +359,8 @@ static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, if (bo->mem.mem_type == TTM_PL_SYSTEM) { if (bdev->driver->move_notify) bdev->driver->move_notify(bo, evict, mem); + if (bo->bdev->ddev != NULL) // TODO: remove after ddev initiazlied for all + drmcgrp_mem_track_move(bo, evict, mem); bo->mem = *mem; mem->mm_node = NULL; goto moved; @@ -361,6 +369,8 @@ static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, if (bdev->driver->move_notify) bdev->driver->move_notify(bo, evict, mem); + if (bo->bdev->ddev != NULL) // TODO: remove after ddev initiazlied for all + drmcgrp_mem_track_move(bo, evict, mem); if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) && !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) @@ -374,6 +384,8 @@ static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, if (bdev->driver->move_notify) { swap(*mem, bo->mem); bdev->driver->move_notify(bo, false, mem); + if (bo->bdev->ddev != NULL) // TODO: remove after ddev initiazlied for all + drmcgrp_mem_track_move(bo, evict, mem); swap(*mem, bo->mem); } @@ -1275,6 +1287,10 @@ int ttm_bo_init_reserved(struct ttm_bo_device *bdev, WARN_ON(!locked); } + bo->drmcgrp = get_drmcgrp(current); + if (bo->bdev->ddev != NULL) // TODO: remove after ddev initiazlied for all + drmcgrp_chg_mem(bo); + if (likely(!ret)) ret = ttm_bo_validate(bo, placement, ctx); @@ -1666,6 +1682,20 @@ int ttm_bo_device_init(struct ttm_bo_device *bdev, } EXPORT_SYMBOL(ttm_bo_device_init); +/* TODO merge with official function when implementation finalized*/ +int ttm_bo_device_init_tmp(struct ttm_bo_device *bdev, + struct ttm_bo_driver *driver, + struct drm_device *ddev, + struct address_space *mapping, + bool need_dma32) +{ + int ret = ttm_bo_device_init(bdev, driver, mapping, need_dma32); + + bdev->ddev = ddev; + return ret; +} +EXPORT_SYMBOL(ttm_bo_device_init_tmp); + /* * buffer object vm functions. */ diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c index 895d77d799e4..4ed7847c21f4 100644 --- a/drivers/gpu/drm/ttm/ttm_bo_util.c +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -522,6 +523,9 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo, ret = reservation_object_trylock(fbo->base.resv); WARN_ON(!ret); + if (bo->bdev->ddev != NULL) // TODO: remove after ddev initiazlied for all + drmcgrp_chg_mem(bo); + *new_obj = &fbo->base; return 0; } diff --git a/include/drm/drm_cgroup.h b/include/drm/drm_cgroup.h index 8711b7c5f7bf..48ab5450cf17 100644 --- a/include/drm/drm_cgroup.h +++ b/include/drm/drm_cgroup.h @@ -5,6 +5,7 @@ #define __DRM_CGROUP_H__ #include +#include #ifdef CONFIG_CGROUP_DRM @@ -18,6 +19,11 @@ void drmcgrp_unchg_bo_alloc(struct drmcgrp *drmcgrp, struct drm_device *dev, size_t size); bool drmcgrp_bo_can_allocate(struct task_struct *task, struct drm_device *dev, size_t size); +void drmcgrp_chg_mem(struct ttm_buffer_object *tbo); +void drmcgrp_unchg_mem(struct ttm_buffer_object *tbo); +void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict, + struct ttm_mem_reg *new_mem); + #else static inline int drmcgrp_register_device(struct drm_device *device) { @@ -50,5 +56,18 @@ static inline bool drmcgrp_bo_can_allocate(struct task_struct *task, { return true; } + +static inline void drmcgrp_chg_mem(struct ttm_buffer_object *tbo) +{ +} + +static inline void drmcgrp_unchg_mem(struct ttm_buffer_object *tbo) +{ +} + +static inline void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, + bool evict, struct ttm_mem_reg *new_mem) +{ +} #endif /* CONFIG_CGROUP_DRM */ #endif /* __DRM_CGROUP_H__ */ diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h index 49d9cdfc58f2..ae1bb6daec81 100644 --- a/include/drm/ttm/ttm_bo_api.h +++ b/include/drm/ttm/ttm_bo_api.h @@ -128,6 +128,7 @@ struct ttm_tt; * struct ttm_buffer_object * * @bdev: Pointer to the buffer object device structure. + * @drmcgrp: DRM cgroup this object belongs to. * @type: The bo type. * @destroy: Destruction function. If NULL, kfree is used. * @num_pages: Actual number of pages. @@ -174,6 +175,7 @@ struct ttm_buffer_object { */ struct ttm_bo_device *bdev; + struct drmcgrp *drmcgrp; enum ttm_bo_type type; void (*destroy) (struct ttm_buffer_object *); unsigned long num_pages; diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h index c008346c2401..4cbcb41e5aa9 100644 --- a/include/drm/ttm/ttm_bo_driver.h +++ b/include/drm/ttm/ttm_bo_driver.h @@ -30,6 +30,7 @@ #ifndef _TTM_BO_DRIVER_H_ #define _TTM_BO_DRIVER_H_ +#include #include #include #include @@ -442,6 +443,7 @@ extern struct ttm_bo_global { * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver. * @man: An array of mem_type_managers. * @vma_manager: Address space manager + * @ddev: Pointer to struct drm_device that this ttm_bo_device belongs to * lru_lock: Spinlock that protects the buffer+device lru lists and * ddestroy lists. * @dev_mapping: A pointer to the struct address_space representing the @@ -460,6 +462,7 @@ struct ttm_bo_device { struct ttm_bo_global *glob; struct ttm_bo_driver *driver; struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES]; + struct drm_device *ddev; /* * Protected by internal locks. @@ -598,6 +601,11 @@ int ttm_bo_device_init(struct ttm_bo_device *bdev, struct address_space *mapping, bool need_dma32); +int ttm_bo_device_init_tmp(struct ttm_bo_device *bdev, + struct ttm_bo_driver *driver, + struct drm_device *ddev, + struct address_space *mapping, + bool need_dma32); /** * ttm_bo_unmap_virtual * diff --git a/include/linux/cgroup_drm.h b/include/linux/cgroup_drm.h index e4400b21ab8e..141bea06f74c 100644 --- a/include/linux/cgroup_drm.h +++ b/include/linux/cgroup_drm.h @@ -9,6 +9,7 @@ #include #include #include +#include /* limit defined per the way drm_minor_alloc operates */ #define MAX_DRM_DEV (64 * DRM_MINOR_RENDER) @@ -22,6 +23,9 @@ struct drmcgrp_device_resource { size_t bo_limits_peak_allocated; s64 bo_stats_count_allocated; + + s64 mem_stats[TTM_PL_PRIV+1]; + s64 mem_stats_evict; }; struct drmcgrp { diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c index 9144f93b851f..5aee42a628c1 100644 --- a/kernel/cgroup/drm.c +++ b/kernel/cgroup/drm.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include #include @@ -34,6 +36,8 @@ enum drmcgrp_res_type { DRMCGRP_TYPE_BO_TOTAL, DRMCGRP_TYPE_BO_PEAK, DRMCGRP_TYPE_BO_COUNT, + DRMCGRP_TYPE_MEM, + DRMCGRP_TYPE_MEM_EVICT, }; enum drmcgrp_file_type { @@ -42,6 +46,13 @@ enum drmcgrp_file_type { DRMCGRP_FTYPE_DEFAULT, }; +static char const *ttm_placement_names[] = { + [TTM_PL_SYSTEM] = "system", + [TTM_PL_TT] = "tt", + [TTM_PL_VRAM] = "vram", + [TTM_PL_PRIV] = "priv", +}; + /* indexed by drm_minor for access speed */ static struct drmcgrp_device *known_drmcgrp_devs[MAX_DRM_DEV]; @@ -134,6 +145,7 @@ drmcgrp_css_alloc(struct cgroup_subsys_state *parent_css) static inline void drmcgrp_print_stats(struct drmcgrp_device_resource *ddr, struct seq_file *sf, enum drmcgrp_res_type type) { + int i; if (ddr == NULL) { seq_puts(sf, "\n"); return; @@ -149,6 +161,16 @@ static inline void drmcgrp_print_stats(struct drmcgrp_device_resource *ddr, case DRMCGRP_TYPE_BO_COUNT: seq_printf(sf, "%lld\n", ddr->bo_stats_count_allocated); break; + case DRMCGRP_TYPE_MEM: + for (i = 0; i <= TTM_PL_PRIV; i++) { + seq_printf(sf, "%s=%lld ", ttm_placement_names[i], + ddr->mem_stats[i]); + } + seq_puts(sf, "\n"); + break; + case DRMCGRP_TYPE_MEM_EVICT: + seq_printf(sf, "%lld\n", ddr->mem_stats_evict); + break; default: seq_puts(sf, "\n"); break; @@ -406,6 +428,18 @@ struct cftype files[] = { .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BO_COUNT, DRMCGRP_FTYPE_STATS), }, + { + .name = "memory.stats", + .seq_show = drmcgrp_bo_show, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_MEM, + DRMCGRP_FTYPE_STATS), + }, + { + .name = "memory.evict.stats", + .seq_show = drmcgrp_bo_show, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_MEM_EVICT, + DRMCGRP_FTYPE_STATS), + }, { } /* terminate */ }; @@ -555,3 +589,82 @@ void drmcgrp_unchg_bo_alloc(struct drmcgrp *drmcgrp, struct drm_device *dev, mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); } EXPORT_SYMBOL(drmcgrp_unchg_bo_alloc); + +void drmcgrp_chg_mem(struct ttm_buffer_object *tbo) +{ + struct drm_device *dev = tbo->bdev->ddev; + struct drmcgrp *drmcgrp = tbo->drmcgrp; + int devIdx = dev->primary->index; + s64 size = (s64)(tbo->mem.size); + int mem_type = tbo->mem.mem_type; + struct drmcgrp_device_resource *ddr; + + if (drmcgrp == NULL || known_drmcgrp_devs[devIdx] == NULL) + return; + + mem_type = mem_type > TTM_PL_PRIV ? TTM_PL_PRIV : mem_type; + + mutex_lock(&known_drmcgrp_devs[devIdx]->mutex); + for ( ; drmcgrp != NULL; drmcgrp = parent_drmcgrp(drmcgrp)) { + ddr = drmcgrp->dev_resources[devIdx]; + ddr->mem_stats[mem_type] += size; + } + mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); +} +EXPORT_SYMBOL(drmcgrp_chg_mem); + +void drmcgrp_unchg_mem(struct ttm_buffer_object *tbo) +{ + struct drm_device *dev = tbo->bdev->ddev; + struct drmcgrp *drmcgrp = tbo->drmcgrp; + int devIdx = dev->primary->index; + s64 size = (s64)(tbo->mem.size); + int mem_type = tbo->mem.mem_type; + struct drmcgrp_device_resource *ddr; + + if (drmcgrp == NULL || known_drmcgrp_devs[devIdx] == NULL) + return; + + mem_type = mem_type > TTM_PL_PRIV ? TTM_PL_PRIV : mem_type; + + mutex_lock(&known_drmcgrp_devs[devIdx]->mutex); + for ( ; drmcgrp != NULL; drmcgrp = parent_drmcgrp(drmcgrp)) { + ddr = drmcgrp->dev_resources[devIdx]; + ddr->mem_stats[mem_type] -= size; + } + mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); +} +EXPORT_SYMBOL(drmcgrp_unchg_mem); + +void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict, + struct ttm_mem_reg *new_mem) +{ + struct drm_device *dev = old_bo->bdev->ddev; + struct drmcgrp *drmcgrp = old_bo->drmcgrp; + s64 move_in_bytes = (s64)(old_bo->mem.size); + int devIdx = dev->primary->index; + int old_mem_type = old_bo->mem.mem_type; + int new_mem_type = new_mem->mem_type; + struct drmcgrp_device_resource *ddr; + struct drmcgrp_device *known_dev; + + known_dev = known_drmcgrp_devs[devIdx]; + + if (drmcgrp == NULL || known_dev == NULL) + return; + + old_mem_type = old_mem_type > TTM_PL_PRIV ? TTM_PL_PRIV : old_mem_type; + new_mem_type = new_mem_type > TTM_PL_PRIV ? TTM_PL_PRIV : new_mem_type; + + mutex_lock(&known_dev->mutex); + for ( ; drmcgrp != NULL; drmcgrp = parent_drmcgrp(drmcgrp)) { + ddr = drmcgrp->dev_resources[devIdx]; + ddr->mem_stats[old_mem_type] -= move_in_bytes; + ddr->mem_stats[new_mem_type] += move_in_bytes; + + if (evict) + ddr->mem_stats_evict++; + } + mutex_unlock(&known_dev->mutex); +} +EXPORT_SYMBOL(drmcgrp_mem_track_move); From patchwork Wed Jun 26 15:05:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho, Kenny" X-Patchwork-Id: 11018055 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 36ADD924 for ; Wed, 26 Jun 2019 15:05:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E7BD32859F for ; Wed, 26 Jun 2019 15:05:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D84612888A; Wed, 26 Jun 2019 15:05:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7D3D52888A for ; Wed, 26 Jun 2019 15:05:50 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9BED76E467; Wed, 26 Jun 2019 15:05:48 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM03-BY2-obe.outbound.protection.outlook.com (mail-eopbgr780051.outbound.protection.outlook.com [40.107.78.51]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0F37C6E462; Wed, 26 Jun 2019 15:05:41 +0000 (UTC) Received: from MWHPR12CA0058.namprd12.prod.outlook.com (2603:10b6:300:103::20) by CY4PR12MB1701.namprd12.prod.outlook.com (2603:10b6:903:121::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Wed, 26 Jun 2019 15:05:39 +0000 Received: from DM3NAM03FT041.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e49::209) by MWHPR12CA0058.outlook.office365.com (2603:10b6:300:103::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2008.16 via Frontend Transport; Wed, 26 Jun 2019 15:05:39 +0000 Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.17) by DM3NAM03FT041.mail.protection.outlook.com (10.152.83.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2032.15 via Frontend Transport; Wed, 26 Jun 2019 15:05:38 +0000 Received: from kho-5039A.amd.com (10.180.168.240) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.389.1; Wed, 26 Jun 2019 10:05:33 -0500 From: Kenny Ho To: , , , , , , , , , Subject: [RFC PATCH v3 08/11] drm, cgroup: Add TTM buffer peak usage stats Date: Wed, 26 Jun 2019 11:05:19 -0400 Message-ID: <20190626150522.11618-9-Kenny.Ho@amd.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190626150522.11618-1-Kenny.Ho@amd.com> References: <20190626150522.11618-1-Kenny.Ho@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.17; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(346002)(396003)(376002)(136003)(39860400002)(2980300002)(428003)(189003)(199004)(110136005)(186003)(26005)(77096007)(316002)(4326008)(70586007)(70206006)(5660300002)(51416003)(7696005)(50466002)(53936002)(47776003)(86362001)(36756003)(2201001)(48376002)(76176011)(53416004)(8936002)(81166006)(14444005)(68736007)(72206003)(305945005)(81156014)(8676002)(2870700001)(486006)(2906002)(6666004)(478600001)(1076003)(356004)(476003)(126002)(426003)(50226002)(336012)(2616005)(446003)(11346002)(921003)(83996005)(2101003)(1121003); DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR12MB1701; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; LANG:en; PTR:InfoDomainNonexistent; MX:1; A:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6cbdd77e-30bf-47f3-0f36-08d6fa47c01f X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328); SRVR:CY4PR12MB1701; X-MS-TrafficTypeDiagnostic: CY4PR12MB1701: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4714; X-Forefront-PRVS: 00808B16F3 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: wI8Jq3oRrXKJO6ZGXAfL0HdaLcEjwXUPls+9ZTrADgy4rHhIQDtrucjv4SbSpHm22pb2sUEpAWhziVlfSGU/EyL4JUsaLQ7E9pxItC+cBTaV7NpOsQsHNWy6OfuVueyjBVhXZqLLqhhEhH1CDvby3sWxmd5DWQpNKZ4dpxAIwyzVBXzISkhZ9SIUucsYZ8L2NIVfl0yvCWlzmzOyVEWWmSziK7vpoedyrOOVJoT47i8yBmTy7/wc4kP2HpUYG0+KdRcf7p0YUI0nme9nt8ftP48sxv9WXk/XLcX1BkMAEkcin/GezSnfHJatxp3qcg0t+xKBFe+Dr5kf8JhNZrHczWPy12JHmw8jJ85rly9WP9XLbgPQyaM0eFodfnDe7OG9IKlsC/KCNLPB5C92scJkHYm0P05Qb/OWt4sgXCW/44c= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2019 15:05:38.8373 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6cbdd77e-30bf-47f3-0f36-08d6fa47c01f X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1701 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1V3DSHiL6emfOa52C6ir4zWzrOls98N76v201ss0MIY=; b=jhIGRbtrGqdtPIIgBw999Ft4O1R2LDhrnaDbnRYYE0ypwIjwatEtaqKuhEjMJdoNRO9JZXWcSRxpHwneQ4IWCeUrlWsB6pkTPHWOMirklcJWVDtc1sP5FUrvzB2ycI1TV8lnz76ORgPn6alIjObaln78KiU6nTghx5bR9HWjagQ= X-Mailman-Original-Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=permerror action=none header.from=amd.com; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kenny Ho Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP drm.memory.peak.stats A read-only nested-keyed file which exists on all cgroups. Each entry is keyed by the drm device's major:minor. The following nested keys are defined. ====== ============================================== system Peak host memory used tt Peak host memory used by the device (GTT/GART) vram Peak Video RAM used by the drm device priv Other drm device specific memory peak usage ====== ============================================== Reading returns the following:: 226:0 system=0 tt=0 vram=0 priv=0 226:1 system=0 tt=9035776 vram=17768448 priv=16809984 226:2 system=0 tt=9035776 vram=17768448 priv=16809984 Change-Id: I986e44533848f66411465bdd52105e78105a709a Signed-off-by: Kenny Ho --- include/linux/cgroup_drm.h | 1 + kernel/cgroup/drm.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/linux/cgroup_drm.h b/include/linux/cgroup_drm.h index 141bea06f74c..922529641df5 100644 --- a/include/linux/cgroup_drm.h +++ b/include/linux/cgroup_drm.h @@ -25,6 +25,7 @@ struct drmcgrp_device_resource { s64 bo_stats_count_allocated; s64 mem_stats[TTM_PL_PRIV+1]; + s64 mem_peaks[TTM_PL_PRIV+1]; s64 mem_stats_evict; }; diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c index 5aee42a628c1..5f5fa6a2b068 100644 --- a/kernel/cgroup/drm.c +++ b/kernel/cgroup/drm.c @@ -38,6 +38,7 @@ enum drmcgrp_res_type { DRMCGRP_TYPE_BO_COUNT, DRMCGRP_TYPE_MEM, DRMCGRP_TYPE_MEM_EVICT, + DRMCGRP_TYPE_MEM_PEAK, }; enum drmcgrp_file_type { @@ -171,6 +172,13 @@ static inline void drmcgrp_print_stats(struct drmcgrp_device_resource *ddr, case DRMCGRP_TYPE_MEM_EVICT: seq_printf(sf, "%lld\n", ddr->mem_stats_evict); break; + case DRMCGRP_TYPE_MEM_PEAK: + for (i = 0; i <= TTM_PL_PRIV; i++) { + seq_printf(sf, "%s=%lld ", ttm_placement_names[i], + ddr->mem_peaks[i]); + } + seq_puts(sf, "\n"); + break; default: seq_puts(sf, "\n"); break; @@ -440,6 +448,12 @@ struct cftype files[] = { .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_MEM_EVICT, DRMCGRP_FTYPE_STATS), }, + { + .name = "memory.peaks.stats", + .seq_show = drmcgrp_bo_show, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_MEM_PEAK, + DRMCGRP_FTYPE_STATS), + }, { } /* terminate */ }; @@ -608,6 +622,8 @@ void drmcgrp_chg_mem(struct ttm_buffer_object *tbo) for ( ; drmcgrp != NULL; drmcgrp = parent_drmcgrp(drmcgrp)) { ddr = drmcgrp->dev_resources[devIdx]; ddr->mem_stats[mem_type] += size; + ddr->mem_peaks[mem_type] = max(ddr->mem_peaks[mem_type], + ddr->mem_stats[mem_type]); } mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); } @@ -662,6 +678,10 @@ void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict, ddr->mem_stats[old_mem_type] -= move_in_bytes; ddr->mem_stats[new_mem_type] += move_in_bytes; + ddr->mem_peaks[new_mem_type] = max( + ddr->mem_peaks[new_mem_type], + ddr->mem_stats[new_mem_type]); + if (evict) ddr->mem_stats_evict++; } From patchwork Wed Jun 26 15:05:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho, Kenny" X-Patchwork-Id: 11018067 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4983114C0 for ; Wed, 26 Jun 2019 15:06:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2F3D9289AA for ; Wed, 26 Jun 2019 15:06:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 23CA9289A2; Wed, 26 Jun 2019 15:06:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id DAAC52899C for ; Wed, 26 Jun 2019 15:06:03 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EE9296E46F; Wed, 26 Jun 2019 15:05:49 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM03-DM3-obe.outbound.protection.outlook.com (mail-eopbgr800044.outbound.protection.outlook.com [40.107.80.44]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8BB456E462; Wed, 26 Jun 2019 15:05:43 +0000 (UTC) Received: from MWHPR12CA0068.namprd12.prod.outlook.com (2603:10b6:300:103::30) by BN6PR12MB1747.namprd12.prod.outlook.com (2603:10b6:404:106::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Wed, 26 Jun 2019 15:05:40 +0000 Received: from DM3NAM03FT041.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e49::209) by MWHPR12CA0068.outlook.office365.com (2603:10b6:300:103::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2008.16 via Frontend Transport; Wed, 26 Jun 2019 15:05:40 +0000 Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.17) by DM3NAM03FT041.mail.protection.outlook.com (10.152.83.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2032.15 via Frontend Transport; Wed, 26 Jun 2019 15:05:39 +0000 Received: from kho-5039A.amd.com (10.180.168.240) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.389.1; Wed, 26 Jun 2019 10:05:33 -0500 From: Kenny Ho To: , , , , , , , , , Subject: [RFC PATCH v3 09/11] drm, cgroup: Add per cgroup bw measure and control Date: Wed, 26 Jun 2019 11:05:20 -0400 Message-ID: <20190626150522.11618-10-Kenny.Ho@amd.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190626150522.11618-1-Kenny.Ho@amd.com> References: <20190626150522.11618-1-Kenny.Ho@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.17; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(396003)(346002)(376002)(39860400002)(136003)(2980300002)(428003)(199004)(189003)(336012)(76176011)(81166006)(68736007)(316002)(110136005)(77096007)(186003)(26005)(70206006)(50226002)(81156014)(8676002)(48376002)(11346002)(5660300002)(51416003)(7696005)(2870700001)(50466002)(426003)(446003)(2616005)(476003)(47776003)(126002)(305945005)(4326008)(486006)(478600001)(356004)(6666004)(2906002)(2201001)(86362001)(53936002)(30864003)(53416004)(72206003)(70586007)(8936002)(36756003)(14444005)(1076003)(921003)(83996005)(2101003)(1121003); DIR:OUT; SFP:1101; SCL:1; SRVR:BN6PR12MB1747; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; LANG:en; PTR:InfoDomainNonexistent; A:1; MX:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f05948a7-e4a4-47e0-8690-08d6fa47c0ca X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328); SRVR:BN6PR12MB1747; X-MS-TrafficTypeDiagnostic: BN6PR12MB1747: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:1060; X-Forefront-PRVS: 00808B16F3 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: 0jJdzuji2tQICouQ+JuRNLLOgMMKGLh3VVNcnpOE66mzuKbqbkSTu/09i1Nv+TYNQ58TelUKkW0mDFBoXxd7h3KDPG5uVBxKrS5wsiEZ+o9+2FZT+8I9Wavng6+RMTh8vQCKTrgXN+SnVo5F3bcVGuhmGC5uBj/P/oJo1OThqab1MrnrfPrXGE5nM0T0jCb1ktuzqMcDp+rzXOT1tR3qH558jylnh05StyKiP/Qo7TK+06ni3/b9XYJ+ahQ8fkdEeo/jPe8gnlssBX0HMWJZZpzDxBsTlVQ6H9r8GpV+bCB4Oh8y5jHn23AGEx0WcKeYlDPz8cdsBAqR/B7CUxdNpgZsuLWcLrUjDuuI5S/OVdl5Qj8iR86V6fZohF70ypwFv7BntOc+XefFH6uVdt1ixHWabXyJl/5EiTHqOikDyII= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2019 15:05:39.9143 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f05948a7-e4a4-47e0-8690-08d6fa47c0ca X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR12MB1747 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qL16NkCpzCCNgM1RShu3+2nusaksV+Bt3h/GVWUML9Y=; b=p+uz/Pc+C6+h9f+KbTTWERsw4MGpv2x/XwYVx0RjA4zHzCn3JlTiATJ0plaz7x3xt8AsA6ZmECFjZbM589kKIxzIG1EGP6nqsdGjRspFDgKDhPnFRChN1qdz2o/jZ61MX12p8BMoDSyUAm18PmRJjz5tUK6Sh//Bu2TQPaxWaC0= X-Mailman-Original-Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=permerror action=none header.from=amd.com; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kenny Ho Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP The bandwidth is measured by keeping track of the amount of bytes moved by ttm within a time period. We defined two type of bandwidth: burst and average. Average bandwidth is calculated by dividing the total amount of bytes moved within a cgroup by the lifetime of the cgroup. Burst bandwidth is similar except that the byte and time measurement is reset after a user configurable period. The bandwidth control is best effort since it is done on a per move basis instead of per byte. The bandwidth is limited by delaying the move of a buffer. The bandwidth limit can be exceeded when the next move is larger than the remaining allowance. drm.burst_bw_period_in_us A read-write flat-keyed file which exists on the root cgroup. Each entry is keyed by the drm device's major:minor. Length of a period use to measure burst bandwidth in us. One period per device. drm.burst_bw_period_in_us.default A read-only flat-keyed file which exists on the root cgroup. Each entry is keyed by the drm device's major:minor. Default length of a period in us (one per device.) drm.bandwidth.stats A read-only nested-keyed file which exists on all cgroups. Each entry is keyed by the drm device's major:minor. The following nested keys are defined. ================= ====================================== burst_byte_per_us Burst bandwidth avg_bytes_per_us Average bandwidth moved_byte Amount of byte moved within a period accum_us Amount of time accumulated in a period total_moved_byte Byte moved within the cgroup lifetime total_accum_us Cgroup lifetime in us byte_credit Available byte credit to limit avg bw ================= ====================================== Reading returns the following:: 226:1 burst_byte_per_us=23 avg_bytes_per_us=0 moved_byte=2244608 accum_us=95575 total_moved_byte=45899776 total_accum_us=201634590 byte_credit=13214278590464 226:2 burst_byte_per_us=10 avg_bytes_per_us=219 moved_byte=430080 accum_us=39350 total_moved_byte=65518026752 total_accum_us=298337721 byte_credit=9223372036854644735 drm.bandwidth.high A read-write nested-keyed file which exists on all cgroups. Each entry is keyed by the drm device's major:minor. The following nested keys are defined. ================ ======================================= bytes_in_period Burst limit per period in byte avg_bytes_per_us Average bandwidth limit in bytes per us ================ ======================================= Reading returns the following:: 226:1 bytes_in_period=9223372036854775807 avg_bytes_per_us=65536 226:2 bytes_in_period=9223372036854775807 avg_bytes_per_us=65536 drm.bandwidth.default A read-only nested-keyed file which exists on the root cgroup. Each entry is keyed by the drm device's major:minor. The following nested keys are defined. ================ ======================================== bytes_in_period Default burst limit per period in byte avg_bytes_per_us Default average bw limit in bytes per us ================ ======================================== Reading returns the following:: 226:1 bytes_in_period=9223372036854775807 avg_bytes_per_us=65536 226:2 bytes_in_period=9223372036854775807 avg_bytes_per_us=65536 Change-Id: Ie573491325ccc16535bb943e7857f43bd0962add Signed-off-by: Kenny Ho --- drivers/gpu/drm/ttm/ttm_bo.c | 7 + include/drm/drm_cgroup.h | 13 ++ include/linux/cgroup_drm.h | 14 ++ kernel/cgroup/drm.c | 309 ++++++++++++++++++++++++++++++++++- 4 files changed, 340 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c index e9f70547f0ad..f06c2b9d8a4a 100644 --- a/drivers/gpu/drm/ttm/ttm_bo.c +++ b/drivers/gpu/drm/ttm/ttm_bo.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -1176,6 +1177,12 @@ int ttm_bo_validate(struct ttm_buffer_object *bo, * Check whether we need to move buffer. */ if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) { + unsigned int move_delay = drmcgrp_get_mem_bw_period_in_us(bo); + move_delay /= 2000; /* check every half period in ms*/ + while (bo->bdev->ddev != NULL && !drmcgrp_mem_can_move(bo)) { + msleep(move_delay); + } + ret = ttm_bo_move_buffer(bo, placement, ctx); if (ret) return ret; diff --git a/include/drm/drm_cgroup.h b/include/drm/drm_cgroup.h index 48ab5450cf17..9b1dbd6a4eca 100644 --- a/include/drm/drm_cgroup.h +++ b/include/drm/drm_cgroup.h @@ -23,6 +23,8 @@ void drmcgrp_chg_mem(struct ttm_buffer_object *tbo); void drmcgrp_unchg_mem(struct ttm_buffer_object *tbo); void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict, struct ttm_mem_reg *new_mem); +unsigned int drmcgrp_get_mem_bw_period_in_us(struct ttm_buffer_object *tbo); +bool drmcgrp_mem_can_move(struct ttm_buffer_object *tbo); #else static inline int drmcgrp_register_device(struct drm_device *device) @@ -69,5 +71,16 @@ static inline void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict, struct ttm_mem_reg *new_mem) { } + +static inline unsigned int drmcgrp_get_mem_bw_period_in_us( + struct ttm_buffer_object *tbo) +{ + return 0; +} + +static inline bool drmcgrp_mem_can_move(struct ttm_buffer_object *tbo) +{ + return true; +} #endif /* CONFIG_CGROUP_DRM */ #endif /* __DRM_CGROUP_H__ */ diff --git a/include/linux/cgroup_drm.h b/include/linux/cgroup_drm.h index 922529641df5..94828da2104a 100644 --- a/include/linux/cgroup_drm.h +++ b/include/linux/cgroup_drm.h @@ -14,6 +14,15 @@ /* limit defined per the way drm_minor_alloc operates */ #define MAX_DRM_DEV (64 * DRM_MINOR_RENDER) +enum drmcgrp_mem_bw_attr { + DRMCGRP_MEM_BW_ATTR_BYTE_MOVED, /* for calulating 'instantaneous' bw */ + DRMCGRP_MEM_BW_ATTR_ACCUM_US, /* for calulating 'instantaneous' bw */ + DRMCGRP_MEM_BW_ATTR_TOTAL_BYTE_MOVED, + DRMCGRP_MEM_BW_ATTR_TOTAL_ACCUM_US, + DRMCGRP_MEM_BW_ATTR_BYTE_CREDIT, + __DRMCGRP_MEM_BW_ATTR_LAST, +}; + struct drmcgrp_device_resource { /* for per device stats */ s64 bo_stats_total_allocated; @@ -27,6 +36,11 @@ struct drmcgrp_device_resource { s64 mem_stats[TTM_PL_PRIV+1]; s64 mem_peaks[TTM_PL_PRIV+1]; s64 mem_stats_evict; + + s64 mem_bw_stats_last_update_us; + s64 mem_bw_stats[__DRMCGRP_MEM_BW_ATTR_LAST]; + s64 mem_bw_limits_bytes_in_period; + s64 mem_bw_limits_avg_bytes_per_us; }; struct drmcgrp { diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c index 5f5fa6a2b068..bbc6612200a4 100644 --- a/kernel/cgroup/drm.c +++ b/kernel/cgroup/drm.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,12 @@ struct drmcgrp_device { s64 bo_limits_total_allocated_default; size_t bo_limits_peak_allocated_default; + + s64 mem_bw_limits_period_in_us; + s64 mem_bw_limits_period_in_us_default; + + s64 mem_bw_bytes_in_period_default; + s64 mem_bw_avg_bytes_per_us_default; }; #define DRMCG_CTF_PRIV_SIZE 3 @@ -39,6 +46,8 @@ enum drmcgrp_res_type { DRMCGRP_TYPE_MEM, DRMCGRP_TYPE_MEM_EVICT, DRMCGRP_TYPE_MEM_PEAK, + DRMCGRP_TYPE_BANDWIDTH, + DRMCGRP_TYPE_BANDWIDTH_PERIOD_BURST, }; enum drmcgrp_file_type { @@ -54,6 +63,17 @@ static char const *ttm_placement_names[] = { [TTM_PL_PRIV] = "priv", }; +static char const *mem_bw_attr_names[] = { + [DRMCGRP_MEM_BW_ATTR_BYTE_MOVED] = "moved_byte", + [DRMCGRP_MEM_BW_ATTR_ACCUM_US] = "accum_us", + [DRMCGRP_MEM_BW_ATTR_TOTAL_BYTE_MOVED] = "total_moved_byte", + [DRMCGRP_MEM_BW_ATTR_TOTAL_ACCUM_US] = "total_accum_us", + [DRMCGRP_MEM_BW_ATTR_BYTE_CREDIT] = "byte_credit", +}; + +#define MEM_BW_LIMITS_NAME_AVG "avg_bytes_per_us" +#define MEM_BW_LIMITS_NAME_BURST "bytes_in_period" + /* indexed by drm_minor for access speed */ static struct drmcgrp_device *known_drmcgrp_devs[MAX_DRM_DEV]; @@ -86,6 +106,9 @@ static inline int init_drmcgrp_single(struct drmcgrp *drmcgrp, int minor) if (!ddr) return -ENOMEM; + ddr->mem_bw_stats_last_update_us = ktime_to_us(ktime_get()); + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_ACCUM_US] = 1; + drmcgrp->dev_resources[minor] = ddr; } @@ -96,6 +119,12 @@ static inline int init_drmcgrp_single(struct drmcgrp *drmcgrp, int minor) ddr->bo_limits_peak_allocated = known_drmcgrp_devs[minor]->bo_limits_peak_allocated_default; + + ddr->mem_bw_limits_bytes_in_period = + known_drmcgrp_devs[minor]->mem_bw_bytes_in_period_default; + + ddr->mem_bw_limits_avg_bytes_per_us = + known_drmcgrp_devs[minor]->mem_bw_avg_bytes_per_us_default; } return 0; @@ -143,6 +172,26 @@ drmcgrp_css_alloc(struct cgroup_subsys_state *parent_css) return &drmcgrp->css; } +static inline void drmcgrp_mem_burst_bw_stats_reset(struct drm_device *dev) +{ + struct cgroup_subsys_state *pos; + struct drmcgrp *node; + struct drmcgrp_device_resource *ddr; + int devIdx; + + devIdx = dev->primary->index; + + rcu_read_lock(); + css_for_each_descendant_pre(pos, &root_drmcgrp->css) { + node = css_drmcgrp(pos); + ddr = node->dev_resources[devIdx]; + + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_ACCUM_US] = 1; + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_BYTE_MOVED] = 0; + } + rcu_read_unlock(); +} + static inline void drmcgrp_print_stats(struct drmcgrp_device_resource *ddr, struct seq_file *sf, enum drmcgrp_res_type type) { @@ -179,6 +228,31 @@ static inline void drmcgrp_print_stats(struct drmcgrp_device_resource *ddr, } seq_puts(sf, "\n"); break; + case DRMCGRP_TYPE_BANDWIDTH: + if (ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_ACCUM_US] == 0) + seq_puts(sf, "burst_byte_per_us=NaN "); + else + seq_printf(sf, "burst_byte_per_us=%lld ", + ddr->mem_bw_stats[ + DRMCGRP_MEM_BW_ATTR_BYTE_MOVED]/ + ddr->mem_bw_stats[ + DRMCGRP_MEM_BW_ATTR_ACCUM_US]); + + if (ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_TOTAL_ACCUM_US] == 0) + seq_puts(sf, "avg_bytes_per_us=NaN "); + else + seq_printf(sf, "avg_bytes_per_us=%lld ", + ddr->mem_bw_stats[ + DRMCGRP_MEM_BW_ATTR_TOTAL_BYTE_MOVED]/ + ddr->mem_bw_stats[ + DRMCGRP_MEM_BW_ATTR_TOTAL_ACCUM_US]); + + for (i = 0; i < __DRMCGRP_MEM_BW_ATTR_LAST; i++) { + seq_printf(sf, "%s=%lld ", mem_bw_attr_names[i], + ddr->mem_bw_stats[i]); + } + seq_puts(sf, "\n"); + break; default: seq_puts(sf, "\n"); break; @@ -186,9 +260,9 @@ static inline void drmcgrp_print_stats(struct drmcgrp_device_resource *ddr, } static inline void drmcgrp_print_limits(struct drmcgrp_device_resource *ddr, - struct seq_file *sf, enum drmcgrp_res_type type) + struct seq_file *sf, enum drmcgrp_res_type type, int minor) { - if (ddr == NULL) { + if (ddr == NULL || known_drmcgrp_devs[minor] == NULL) { seq_puts(sf, "\n"); return; } @@ -200,6 +274,17 @@ static inline void drmcgrp_print_limits(struct drmcgrp_device_resource *ddr, case DRMCGRP_TYPE_BO_PEAK: seq_printf(sf, "%zu\n", ddr->bo_limits_peak_allocated); break; + case DRMCGRP_TYPE_BANDWIDTH_PERIOD_BURST: + seq_printf(sf, "%lld\n", + known_drmcgrp_devs[minor]->mem_bw_limits_period_in_us); + break; + case DRMCGRP_TYPE_BANDWIDTH: + seq_printf(sf, "%s=%lld %s=%lld\n", + MEM_BW_LIMITS_NAME_BURST, + ddr->mem_bw_limits_bytes_in_period, + MEM_BW_LIMITS_NAME_AVG, + ddr->mem_bw_limits_avg_bytes_per_us); + break; default: seq_puts(sf, "\n"); break; @@ -223,6 +308,17 @@ static inline void drmcgrp_print_default(struct drmcgrp_device *ddev, seq_printf(sf, "%zu\n", ddev->bo_limits_peak_allocated_default); break; + case DRMCGRP_TYPE_BANDWIDTH_PERIOD_BURST: + seq_printf(sf, "%lld\n", + ddev->mem_bw_limits_period_in_us_default); + break; + case DRMCGRP_TYPE_BANDWIDTH: + seq_printf(sf, "%s=%lld %s=%lld\n", + MEM_BW_LIMITS_NAME_BURST, + ddev->mem_bw_bytes_in_period_default, + MEM_BW_LIMITS_NAME_AVG, + ddev->mem_bw_avg_bytes_per_us_default); + break; default: seq_puts(sf, "\n"); break; @@ -251,7 +347,7 @@ int drmcgrp_bo_show(struct seq_file *sf, void *v) drmcgrp_print_stats(ddr, sf, type); break; case DRMCGRP_FTYPE_LIMIT: - drmcgrp_print_limits(ddr, sf, type); + drmcgrp_print_limits(ddr, sf, type, i); break; case DRMCGRP_FTYPE_DEFAULT: drmcgrp_print_default(ddev, sf, type); @@ -317,6 +413,9 @@ ssize_t drmcgrp_bo_limit_write(struct kernfs_open_file *of, char *buf, struct drmcgrp_device_resource *ddr; char *line; char sattr[256]; + char sval[256]; + char *nested; + char *attr; s64 val; s64 p_max; int rc; @@ -381,6 +480,78 @@ ssize_t drmcgrp_bo_limit_write(struct kernfs_open_file *of, char *buf, ddr->bo_limits_peak_allocated = val; break; + case DRMCGRP_TYPE_BANDWIDTH_PERIOD_BURST: + rc = drmcgrp_process_limit_val(sattr, false, + ddev->mem_bw_limits_period_in_us_default, + S64_MAX, + &val); + + if (rc || val < 2000) { + drmcgrp_pr_cft_err(drmcgrp, cft_name, minor); + continue; + } + + ddev->mem_bw_limits_period_in_us= val; + drmcgrp_mem_burst_bw_stats_reset(ddev->dev); + break; + case DRMCGRP_TYPE_BANDWIDTH: + nested = strstrip(sattr); + + while (nested != NULL) { + attr = strsep(&nested, " "); + + if (sscanf(attr, MEM_BW_LIMITS_NAME_BURST"=%s", + sval) == 1) { + p_max = parent == NULL ? S64_MAX : + parent-> + dev_resources[minor]-> + mem_bw_limits_bytes_in_period; + + rc = drmcgrp_process_limit_val(sval, + true, + ddev-> + mem_bw_bytes_in_period_default, + p_max, + &val); + + if (rc || val < 0) { + drmcgrp_pr_cft_err(drmcgrp, + cft_name, + minor); + continue; + } + + ddr->mem_bw_limits_bytes_in_period=val; + continue; + } + + if (sscanf(attr, MEM_BW_LIMITS_NAME_AVG"=%s", + sval) == 1) { + p_max = parent == NULL ? S64_MAX : + parent-> + dev_resources[minor]-> + mem_bw_limits_avg_bytes_per_us; + + rc = drmcgrp_process_limit_val(sval, + true, + ddev-> + mem_bw_avg_bytes_per_us_default, + p_max, + &val); + + if (rc || val < 0) { + drmcgrp_pr_cft_err(drmcgrp, + cft_name, + minor); + continue; + } + + ddr-> + mem_bw_limits_avg_bytes_per_us=val; + continue; + } + } + break; default: break; } @@ -454,6 +625,41 @@ struct cftype files[] = { .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_MEM_PEAK, DRMCGRP_FTYPE_STATS), }, + { + .name = "burst_bw_period_in_us", + .write = drmcgrp_bo_limit_write, + .seq_show = drmcgrp_bo_show, + .flags = CFTYPE_ONLY_ON_ROOT, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BANDWIDTH_PERIOD_BURST, + DRMCGRP_FTYPE_LIMIT), + }, + { + .name = "burst_bw_period_in_us.default", + .seq_show = drmcgrp_bo_show, + .flags = CFTYPE_ONLY_ON_ROOT, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BANDWIDTH_PERIOD_BURST, + DRMCGRP_FTYPE_DEFAULT), + }, + { + .name = "bandwidth.stats", + .seq_show = drmcgrp_bo_show, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BANDWIDTH, + DRMCGRP_FTYPE_STATS), + }, + { + .name = "bandwidth.high", + .write = drmcgrp_bo_limit_write, + .seq_show = drmcgrp_bo_show, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BANDWIDTH, + DRMCGRP_FTYPE_LIMIT), + }, + { + .name = "bandwidth.default", + .seq_show = drmcgrp_bo_show, + .flags = CFTYPE_ONLY_ON_ROOT, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_BANDWIDTH, + DRMCGRP_FTYPE_DEFAULT), + }, { } /* terminate */ }; @@ -476,6 +682,10 @@ int drmcgrp_register_device(struct drm_device *dev) ddev->dev = dev; ddev->bo_limits_total_allocated_default = S64_MAX; ddev->bo_limits_peak_allocated_default = SIZE_MAX; + ddev->mem_bw_limits_period_in_us_default = 200000; + ddev->mem_bw_limits_period_in_us = 200000; + ddev->mem_bw_bytes_in_period_default = S64_MAX; + ddev->mem_bw_avg_bytes_per_us_default = 65536; mutex_init(&ddev->mutex); @@ -652,6 +862,27 @@ void drmcgrp_unchg_mem(struct ttm_buffer_object *tbo) } EXPORT_SYMBOL(drmcgrp_unchg_mem); +static inline void drmcgrp_mem_bw_accum(s64 time_us, + struct drmcgrp_device_resource *ddr) +{ + s64 increment_us = time_us - ddr->mem_bw_stats_last_update_us; + s64 new_credit = ddr->mem_bw_limits_avg_bytes_per_us * increment_us; + + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_ACCUM_US] + += increment_us; + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_TOTAL_ACCUM_US] + += increment_us; + + if ((S64_MAX - new_credit) > + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_BYTE_CREDIT]) + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_BYTE_CREDIT] + += new_credit; + else + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_BYTE_CREDIT] = S64_MAX; + + ddr->mem_bw_stats_last_update_us = time_us; +} + void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict, struct ttm_mem_reg *new_mem) { @@ -661,6 +892,7 @@ void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict, int devIdx = dev->primary->index; int old_mem_type = old_bo->mem.mem_type; int new_mem_type = new_mem->mem_type; + s64 time_us; struct drmcgrp_device_resource *ddr; struct drmcgrp_device *known_dev; @@ -672,6 +904,14 @@ void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict, old_mem_type = old_mem_type > TTM_PL_PRIV ? TTM_PL_PRIV : old_mem_type; new_mem_type = new_mem_type > TTM_PL_PRIV ? TTM_PL_PRIV : new_mem_type; + if (root_drmcgrp->dev_resources[devIdx] != NULL && + root_drmcgrp->dev_resources[devIdx]-> + mem_bw_stats[DRMCGRP_MEM_BW_ATTR_ACCUM_US] >= + known_dev->mem_bw_limits_period_in_us) + drmcgrp_mem_burst_bw_stats_reset(dev); + + time_us = ktime_to_us(ktime_get()); + mutex_lock(&known_dev->mutex); for ( ; drmcgrp != NULL; drmcgrp = parent_drmcgrp(drmcgrp)) { ddr = drmcgrp->dev_resources[devIdx]; @@ -684,7 +924,70 @@ void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict, if (evict) ddr->mem_stats_evict++; + + drmcgrp_mem_bw_accum(time_us, ddr); + + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_BYTE_MOVED] + += move_in_bytes; + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_TOTAL_BYTE_MOVED] + += move_in_bytes; + + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_BYTE_CREDIT] + -= move_in_bytes; } mutex_unlock(&known_dev->mutex); } EXPORT_SYMBOL(drmcgrp_mem_track_move); + +unsigned int drmcgrp_get_mem_bw_period_in_us(struct ttm_buffer_object *tbo) +{ + int devIdx; + + //TODO replace with BUG_ON + if (tbo->bdev->ddev == NULL) + return 0; + + devIdx = tbo->bdev->ddev->primary->index; + + return (unsigned int) known_drmcgrp_devs[devIdx]-> + mem_bw_limits_period_in_us; +} +EXPORT_SYMBOL(drmcgrp_get_mem_bw_period_in_us); + +bool drmcgrp_mem_can_move(struct ttm_buffer_object *tbo) +{ + struct drm_device *dev = tbo->bdev->ddev; + struct drmcgrp *drmcgrp = tbo->drmcgrp; + int devIdx = dev->primary->index; + s64 time_us; + struct drmcgrp_device_resource *ddr; + bool result = true; + + if (root_drmcgrp->dev_resources[devIdx] != NULL && + root_drmcgrp->dev_resources[devIdx]-> + mem_bw_stats[DRMCGRP_MEM_BW_ATTR_ACCUM_US] >= + known_drmcgrp_devs[devIdx]-> + mem_bw_limits_period_in_us) + drmcgrp_mem_burst_bw_stats_reset(dev); + + time_us = ktime_to_us(ktime_get()); + + mutex_lock(&known_drmcgrp_devs[devIdx]->mutex); + for ( ; drmcgrp != NULL; drmcgrp = parent_drmcgrp(drmcgrp)) { + ddr = drmcgrp->dev_resources[devIdx]; + + drmcgrp_mem_bw_accum(time_us, ddr); + + if (result && + (ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_BYTE_MOVED] + >= ddr->mem_bw_limits_bytes_in_period || + ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_BYTE_CREDIT] + <= 0)) { + result = false; + } + } + mutex_unlock(&known_drmcgrp_devs[devIdx]->mutex); + + return result; +} +EXPORT_SYMBOL(drmcgrp_mem_can_move); From patchwork Wed Jun 26 15:05:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho, Kenny" X-Patchwork-Id: 11018061 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 46ACC14C0 for ; Wed, 26 Jun 2019 15:05:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2DB30289A1 for ; Wed, 26 Jun 2019 15:05:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 21C7E2899C; Wed, 26 Jun 2019 15:05:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 622BE287E5 for ; Wed, 26 Jun 2019 15:05:58 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C8FD86E46D; Wed, 26 Jun 2019 15:05:49 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM03-CO1-obe.outbound.protection.outlook.com (mail-eopbgr790048.outbound.protection.outlook.com [40.107.79.48]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8866A6E467; Wed, 26 Jun 2019 15:05:44 +0000 (UTC) Received: from MWHPR12CA0055.namprd12.prod.outlook.com (2603:10b6:300:103::17) by BN6PR12MB1698.namprd12.prod.outlook.com (2603:10b6:404:106::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Wed, 26 Jun 2019 15:05:42 +0000 Received: from DM3NAM03FT041.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e49::201) by MWHPR12CA0055.outlook.office365.com (2603:10b6:300:103::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2008.16 via Frontend Transport; Wed, 26 Jun 2019 15:05:42 +0000 Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.17) by DM3NAM03FT041.mail.protection.outlook.com (10.152.83.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2032.15 via Frontend Transport; Wed, 26 Jun 2019 15:05:41 +0000 Received: from kho-5039A.amd.com (10.180.168.240) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.389.1; Wed, 26 Jun 2019 10:05:34 -0500 From: Kenny Ho To: , , , , , , , , , Subject: [RFC PATCH v3 10/11] drm, cgroup: Add soft VRAM limit Date: Wed, 26 Jun 2019 11:05:21 -0400 Message-ID: <20190626150522.11618-11-Kenny.Ho@amd.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190626150522.11618-1-Kenny.Ho@amd.com> References: <20190626150522.11618-1-Kenny.Ho@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.17; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(39860400002)(396003)(376002)(346002)(136003)(2980300002)(428003)(199004)(189003)(77096007)(26005)(81166006)(4326008)(186003)(14444005)(2870700001)(2906002)(81156014)(72206003)(8936002)(53416004)(76176011)(336012)(8676002)(5660300002)(48376002)(36756003)(50466002)(11346002)(7696005)(51416003)(305945005)(446003)(486006)(126002)(478600001)(68736007)(2616005)(50226002)(426003)(356004)(6666004)(2201001)(53936002)(70206006)(70586007)(110136005)(1076003)(47776003)(476003)(316002)(86362001)(921003)(1121003)(2101003)(83996005); DIR:OUT; SFP:1101; SCL:1; SRVR:BN6PR12MB1698; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; LANG:en; PTR:InfoDomainNonexistent; A:1; MX:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cc379476-ba7b-453b-cff7-08d6fa47c1c4 X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328); SRVR:BN6PR12MB1698; X-MS-TrafficTypeDiagnostic: BN6PR12MB1698: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:849; X-Forefront-PRVS: 00808B16F3 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: AFkfT3amTcCK0u27+u/T7mW3vek0UF5Oa+n9PI8VueRn9h562iN2cT61ZRCpFj2JqCXNOKhHBsSWDWeZaoSrpzWkiUWiuq6QtuHjppTAUZnwMnvkyTAOXiUxJC7nbCaBWnRTl2H/M6LTG863vg/DraU26IK2oj4H72Fj0DoXw9+MHcW4ZJl9/40G3meNwIw0rU+oV7e+MaJHZhfwYPOEEwzr+n1U+ODWjKnr2I3Q5e1UB8odYSpc9lL2B2YDrFoXVkfK22lYt9kzF4XctXKd2HPrmVIEx4K1y/zRyGInCkEX1t0QRnrM9bT8qmIcyfbAlzgAXLzT1tAYW+0Q4KpklThJsV0MjbmeEopZ8+MyFZy09ocnDiyo5kYXjUiV9ZJNjags4n3HOew7RmOs32ka3h8PWCvVcZT927AuKo6D4+c= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2019 15:05:41.4012 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cc379476-ba7b-453b-cff7-08d6fa47c1c4 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR12MB1698 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=oA+woenpRbe1Zgy3xOyhHWwJ0Gxu8eSf8BMZ79JRyzE=; b=OF21V8WaYoat59d72PJA/eZc+0mpdXfSvaVwB6bYCzH1E0v1w6Hoaq46N/fIhzv0CKkCedAnKbjuTOW1wc89sTZf3CqZBamRC977xsZ780RgrfoJ0924JFYgLArub1XguOA8+MHbf/N5XZzO2Qh+ITWAVTUMD2kVCNJ6ymBLrcE= X-Mailman-Original-Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=permerror action=none header.from=amd.com; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kenny Ho Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP The drm resource being limited is the TTM (Translation Table Manager) buffers. TTM manages different types of memory that a GPU might access. These memory types include dedicated Video RAM (VRAM) and host/system memory accessible through IOMMU (GART/GTT). TTM is currently used by multiple drm drivers (amd, ast, bochs, cirrus, hisilicon, maga200, nouveau, qxl, virtio, vmwgfx.) TTM buffers belonging to drm cgroups under memory pressure will be selected to be evicted first. drm.memory.high A read-write nested-keyed file which exists on all cgroups. Each entry is keyed by the drm device's major:minor. The following nested keys are defined. ==== ============================================= vram Video RAM soft limit for a drm device in byte ==== ============================================= Reading returns the following:: 226:0 vram=0 226:1 vram=17768448 226:2 vram=17768448 drm.memory.default A read-only nested-keyed file which exists on the root cgroup. Each entry is keyed by the drm device's major:minor. The following nested keys are defined. ==== =============================== vram Video RAM default limit in byte ==== =============================== Reading returns the following:: 226:0 vram=0 226:1 vram=17768448 226:2 vram=17768448 Change-Id: I7988e28a453b53140b40a28c176239acbc81d491 Signed-off-by: Kenny Ho --- drivers/gpu/drm/ttm/ttm_bo.c | 7 ++ include/drm/drm_cgroup.h | 15 ++++ include/linux/cgroup_drm.h | 2 + kernel/cgroup/drm.c | 145 +++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+) diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c index f06c2b9d8a4a..79c530f4a198 100644 --- a/drivers/gpu/drm/ttm/ttm_bo.c +++ b/drivers/gpu/drm/ttm/ttm_bo.c @@ -806,12 +806,19 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev, struct ttm_mem_type_manager *man = &bdev->man[mem_type]; struct ttm_buffer_object *bo = NULL; bool locked = false; + bool check_drmcgrp; unsigned i; int ret; + check_drmcgrp = drmcgrp_mem_pressure_scan(bdev, mem_type); + spin_lock(&glob->lru_lock); for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { list_for_each_entry(bo, &man->lru[i], lru) { + if (check_drmcgrp && + !drmcgrp_mem_should_evict(bo, mem_type)) + continue; + if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked)) continue; diff --git a/include/drm/drm_cgroup.h b/include/drm/drm_cgroup.h index 9b1dbd6a4eca..360c1e6c809f 100644 --- a/include/drm/drm_cgroup.h +++ b/include/drm/drm_cgroup.h @@ -6,6 +6,7 @@ #include #include +#include #ifdef CONFIG_CGROUP_DRM @@ -25,6 +26,8 @@ void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict, struct ttm_mem_reg *new_mem); unsigned int drmcgrp_get_mem_bw_period_in_us(struct ttm_buffer_object *tbo); bool drmcgrp_mem_can_move(struct ttm_buffer_object *tbo); +bool drmcgrp_mem_pressure_scan(struct ttm_bo_device *bdev, unsigned type); +bool drmcgrp_mem_should_evict(struct ttm_buffer_object *tbo, unsigned type); #else static inline int drmcgrp_register_device(struct drm_device *device) @@ -82,5 +85,17 @@ static inline bool drmcgrp_mem_can_move(struct ttm_buffer_object *tbo) { return true; } + +static inline bool drmcgrp_mem_pressure_scan(struct ttm_bo_device *bdev, + unsigned type) +{ + return false; +} + +static inline bool drmcgrp_mem_should_evict(struct ttm_buffer_object *tbo, + unsigned type) +{ + return true; +} #endif /* CONFIG_CGROUP_DRM */ #endif /* __DRM_CGROUP_H__ */ diff --git a/include/linux/cgroup_drm.h b/include/linux/cgroup_drm.h index 94828da2104a..52ef02eaac70 100644 --- a/include/linux/cgroup_drm.h +++ b/include/linux/cgroup_drm.h @@ -35,6 +35,8 @@ struct drmcgrp_device_resource { s64 mem_stats[TTM_PL_PRIV+1]; s64 mem_peaks[TTM_PL_PRIV+1]; + s64 mem_highs[TTM_PL_PRIV+1]; + bool mem_pressure[TTM_PL_PRIV+1]; s64 mem_stats_evict; s64 mem_bw_stats_last_update_us; diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c index bbc6612200a4..1ce13db36ce9 100644 --- a/kernel/cgroup/drm.c +++ b/kernel/cgroup/drm.c @@ -29,6 +29,8 @@ struct drmcgrp_device { s64 mem_bw_bytes_in_period_default; s64 mem_bw_avg_bytes_per_us_default; + + s64 mem_highs_default[TTM_PL_PRIV+1]; }; #define DRMCG_CTF_PRIV_SIZE 3 @@ -114,6 +116,8 @@ static inline int init_drmcgrp_single(struct drmcgrp *drmcgrp, int minor) /* set defaults here */ if (known_drmcgrp_devs[minor] != NULL) { + int i; + ddr->bo_limits_total_allocated = known_drmcgrp_devs[minor]->bo_limits_total_allocated_default; @@ -125,6 +129,11 @@ static inline int init_drmcgrp_single(struct drmcgrp *drmcgrp, int minor) ddr->mem_bw_limits_avg_bytes_per_us = known_drmcgrp_devs[minor]->mem_bw_avg_bytes_per_us_default; + + for (i = 0; i <= TTM_PL_PRIV; i++) { + ddr->mem_highs[i] = + known_drmcgrp_devs[minor]->mem_highs_default[i]; + } } return 0; @@ -274,6 +283,11 @@ static inline void drmcgrp_print_limits(struct drmcgrp_device_resource *ddr, case DRMCGRP_TYPE_BO_PEAK: seq_printf(sf, "%zu\n", ddr->bo_limits_peak_allocated); break; + case DRMCGRP_TYPE_MEM: + seq_printf(sf, "%s=%lld\n", + ttm_placement_names[TTM_PL_VRAM], + ddr->mem_highs[TTM_PL_VRAM]); + break; case DRMCGRP_TYPE_BANDWIDTH_PERIOD_BURST: seq_printf(sf, "%lld\n", known_drmcgrp_devs[minor]->mem_bw_limits_period_in_us); @@ -308,6 +322,11 @@ static inline void drmcgrp_print_default(struct drmcgrp_device *ddev, seq_printf(sf, "%zu\n", ddev->bo_limits_peak_allocated_default); break; + case DRMCGRP_TYPE_MEM: + seq_printf(sf, "%s=%lld\n", + ttm_placement_names[TTM_PL_VRAM], + ddev->mem_highs_default[TTM_PL_VRAM]); + break; case DRMCGRP_TYPE_BANDWIDTH_PERIOD_BURST: seq_printf(sf, "%lld\n", ddev->mem_bw_limits_period_in_us_default); @@ -552,6 +571,38 @@ ssize_t drmcgrp_bo_limit_write(struct kernfs_open_file *of, char *buf, } } break; + case DRMCGRP_TYPE_MEM: + nested = strstrip(sattr); + + while (nested != NULL) { + attr = strsep(&nested, " "); + + if (sscanf(attr, "vram=%s", + sval) == 1) { + p_max = parent == NULL ? S64_MAX : + parent-> + dev_resources[minor]-> + mem_highs[TTM_PL_VRAM]; + + rc = drmcgrp_process_limit_val(sval, + true, + ddev-> + mem_highs_default[TTM_PL_VRAM], + p_max, + &val); + + if (rc || val < 0) { + drmcgrp_pr_cft_err(drmcgrp, + cft_name, + minor); + continue; + } + + ddr->mem_highs[TTM_PL_VRAM]=val; + continue; + } + } + break; default: break; } @@ -624,6 +675,20 @@ struct cftype files[] = { .seq_show = drmcgrp_bo_show, .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_MEM_PEAK, DRMCGRP_FTYPE_STATS), + }, + { + .name = "memory.default", + .seq_show = drmcgrp_bo_show, + .flags = CFTYPE_ONLY_ON_ROOT, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_MEM, + DRMCGRP_FTYPE_DEFAULT), + }, + { + .name = "memory.high", + .write = drmcgrp_bo_limit_write, + .seq_show = drmcgrp_bo_show, + .private = DRMCG_CTF_PRIV(DRMCGRP_TYPE_MEM, + DRMCGRP_FTYPE_LIMIT), }, { .name = "burst_bw_period_in_us", @@ -674,6 +739,7 @@ struct cgroup_subsys drm_cgrp_subsys = { int drmcgrp_register_device(struct drm_device *dev) { struct drmcgrp_device *ddev; + int i; ddev = kzalloc(sizeof(struct drmcgrp_device), GFP_KERNEL); if (!ddev) @@ -687,6 +753,10 @@ int drmcgrp_register_device(struct drm_device *dev) ddev->mem_bw_bytes_in_period_default = S64_MAX; ddev->mem_bw_avg_bytes_per_us_default = 65536; + for (i = 0; i <= TTM_PL_PRIV; i++) { + ddev->mem_highs_default[i] = S64_MAX; + } + mutex_init(&ddev->mutex); mutex_lock(&drmcgrp_mutex); @@ -991,3 +1061,78 @@ bool drmcgrp_mem_can_move(struct ttm_buffer_object *tbo) return result; } EXPORT_SYMBOL(drmcgrp_mem_can_move); + +static inline void drmcgrp_mem_set_pressure(struct drmcgrp *drmcgrp, + int devIdx, unsigned mem_type, bool pressure_val) +{ + struct drmcgrp_device_resource *ddr; + struct cgroup_subsys_state *pos; + struct drmcgrp *node; + + css_for_each_descendant_pre(pos, &drmcgrp->css) { + node = css_drmcgrp(pos); + ddr = node->dev_resources[devIdx]; + ddr->mem_pressure[mem_type] = pressure_val; + } +} + +static inline bool drmcgrp_mem_check(struct drmcgrp *drmcgrp, int devIdx, + unsigned mem_type) +{ + struct drmcgrp_device_resource *ddr = drmcgrp->dev_resources[devIdx]; + + /* already under pressure, no need to check and set */ + if (ddr->mem_pressure[mem_type]) + return true; + + if (ddr->mem_stats[mem_type] >= ddr->mem_highs[mem_type]) { + drmcgrp_mem_set_pressure(drmcgrp, devIdx, mem_type, true); + return true; + } + + return false; +} + +bool drmcgrp_mem_pressure_scan(struct ttm_bo_device *bdev, unsigned type) +{ + struct drm_device *dev = bdev->ddev; + struct cgroup_subsys_state *pos; + struct drmcgrp *node; + int devIdx; + bool result = false; + + //TODO replace with BUG_ON + if (dev == NULL || type != TTM_PL_VRAM) /* only vram limit for now */ + return false; + + devIdx = dev->primary->index; + + type = type > TTM_PL_PRIV ? TTM_PL_PRIV : type; + + rcu_read_lock(); + drmcgrp_mem_set_pressure(root_drmcgrp, devIdx, type, false); + + css_for_each_descendant_pre(pos, &root_drmcgrp->css) { + node = css_drmcgrp(pos); + result |= drmcgrp_mem_check(node, devIdx, type); + } + rcu_read_unlock(); + + return result; +} +EXPORT_SYMBOL(drmcgrp_mem_pressure_scan); + +bool drmcgrp_mem_should_evict(struct ttm_buffer_object *tbo, unsigned type) +{ + struct drm_device *dev = tbo->bdev->ddev; + int devIdx; + + //TODO replace with BUG_ON + if (dev == NULL) + return true; + + devIdx = dev->primary->index; + + return tbo->drmcgrp->dev_resources[devIdx]->mem_pressure[type]; +} +EXPORT_SYMBOL(drmcgrp_mem_should_evict); From patchwork Wed Jun 26 15:05:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho, Kenny" X-Patchwork-Id: 11018065 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4A0CE14C0 for ; Wed, 26 Jun 2019 15:06:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 042A12882C for ; Wed, 26 Jun 2019 15:06:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EC3D128998; Wed, 26 Jun 2019 15:06:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 652C1289A1 for ; Wed, 26 Jun 2019 15:06:01 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D50946E46E; Wed, 26 Jun 2019 15:05:49 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM03-DM3-obe.outbound.protection.outlook.com (mail-dm3nam03on0601.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe49::601]) by gabe.freedesktop.org (Postfix) with ESMTPS id CB8A96E46B; Wed, 26 Jun 2019 15:05:45 +0000 (UTC) Received: from MWHPR12CA0058.namprd12.prod.outlook.com (2603:10b6:300:103::20) by MWHPR1201MB0031.namprd12.prod.outlook.com (2603:10b6:301:57::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Wed, 26 Jun 2019 15:05:43 +0000 Received: from DM3NAM03FT041.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e49::209) by MWHPR12CA0058.outlook.office365.com (2603:10b6:300:103::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2008.16 via Frontend Transport; Wed, 26 Jun 2019 15:05:43 +0000 Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV02.amd.com (165.204.84.17) by DM3NAM03FT041.mail.protection.outlook.com (10.152.83.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2032.15 via Frontend Transport; Wed, 26 Jun 2019 15:05:43 +0000 Received: from kho-5039A.amd.com (10.180.168.240) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.389.1; Wed, 26 Jun 2019 10:05:35 -0500 From: Kenny Ho To: , , , , , , , , , Subject: [RFC PATCH v3 11/11] drm, cgroup: Allow more aggressive memory reclaim Date: Wed, 26 Jun 2019 11:05:22 -0400 Message-ID: <20190626150522.11618-12-Kenny.Ho@amd.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190626150522.11618-1-Kenny.Ho@amd.com> References: <20190626150522.11618-1-Kenny.Ho@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.17; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(396003)(39860400002)(346002)(136003)(376002)(2980300002)(428003)(199004)(189003)(51416003)(76176011)(4326008)(77096007)(26005)(7696005)(6666004)(305945005)(356004)(70206006)(70586007)(2201001)(5660300002)(316002)(36756003)(110136005)(86362001)(14444005)(8676002)(8936002)(81156014)(81166006)(53416004)(50226002)(2870700001)(53936002)(476003)(486006)(68736007)(50466002)(478600001)(48376002)(2906002)(72206003)(1076003)(11346002)(186003)(47776003)(446003)(426003)(336012)(2616005)(126002)(921003)(83996005)(1121003)(2101003); DIR:OUT; SFP:1101; SCL:1; SRVR:MWHPR1201MB0031; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; LANG:en; PTR:InfoDomainNonexistent; A:1; MX:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: fbefd543-283b-42f1-4815-08d6fa47c2ab X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328); SRVR:MWHPR1201MB0031; X-MS-TrafficTypeDiagnostic: MWHPR1201MB0031: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4125; X-Forefront-PRVS: 00808B16F3 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: 0kuf7AhBCipjPdrqM/uCajaxKOQDlW8M2AA/1yKGyHW/iLEE9qc/Svq8qynkCVqhib2RAOYA47A5Dzl+ElhyAuntRi/fDi/4H43XvfzcrvjnkMdENpryP4dmEA4FyfT/AVdNeMpGcnHiY0UIveFMO6INAsWKPkX+NmTBGZF8pFoFwOmSG35bpR8rOPZBdDyVZ/wmmyb9hiXTW9pZ8jgQQNH+sqTYM7vdOhDy9wOt8oGwVvdg4Gd2iUrSjB8aiPKlumRrGFwzag0L9WGRGf2xMDl3VjXzNyHr03cPzbfZjBIcDaguShpItLmFGhQYoq7yloZVntgssTGdxX/zvqMREeLp6b+DHr+FSw1TV6wPE8oyXa5UAn7rdB5f+VHoOx+EmpXyC3uWeCdKlT5QtYDAdaqJQa0QxG1tWv2pRSO3hhQ= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2019 15:05:43.0257 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fbefd543-283b-42f1-4815-08d6fa47c2ab X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR1201MB0031 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KPdJwmAonW3a1g9HlpEnwq04DKEXm+oNCf0zke7Z7V4=; b=hebrl0QzOfRnTlWa/nhikfzTf0Q8cu6RvZXBVnLUkdsCtE7nQrboN4mBpS5FKiNyG7+4Oh1lC9tSXi+Mb/LH/6vsQa3FlQsBPYHeoBg/2LGzwySeF48WK2iU8DPSeIgzm834RZpl0tI7forOKIs8LgieruyRng/MkCBOLlKiZ9M= X-Mailman-Original-Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=permerror action=none header.from=amd.com; X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kenny Ho Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Allow DRM TTM memory manager to register a work_struct, such that, when a drmcgrp is under memory pressure, memory reclaiming can be triggered immediately. Change-Id: I25ac04e2db9c19ff12652b88ebff18b44b2706d8 Signed-off-by: Kenny Ho --- drivers/gpu/drm/ttm/ttm_bo.c | 47 +++++++++++++++++++++++++++++++++ include/drm/drm_cgroup.h | 14 ++++++++++ include/drm/ttm/ttm_bo_driver.h | 2 ++ kernel/cgroup/drm.c | 33 +++++++++++++++++++++++ 4 files changed, 96 insertions(+) diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c index 79c530f4a198..5fc3bc5bd4c5 100644 --- a/drivers/gpu/drm/ttm/ttm_bo.c +++ b/drivers/gpu/drm/ttm/ttm_bo.c @@ -1509,6 +1509,44 @@ int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type) } EXPORT_SYMBOL(ttm_bo_evict_mm); +static void ttm_bo_reclaim_wq(struct work_struct *work) +{ + struct ttm_operation_ctx ctx = { + .interruptible = false, + .no_wait_gpu = false, + .flags = TTM_OPT_FLAG_FORCE_ALLOC + }; + struct ttm_mem_type_manager *man = + container_of(work, struct ttm_mem_type_manager, reclaim_wq); + struct ttm_bo_device *bdev = man->bdev; + struct dma_fence *fence; + int mem_type; + int ret; + + for (mem_type = 0; mem_type < TTM_NUM_MEM_TYPES; mem_type++) + if (&bdev->man[mem_type] == man) + break; + + BUG_ON(mem_type >= TTM_NUM_MEM_TYPES); + + if (!drmcgrp_mem_pressure_scan(bdev, mem_type)) + return; + + ret = ttm_mem_evict_first(bdev, mem_type, NULL, &ctx); + if (ret) + return; + + spin_lock(&man->move_lock); + fence = dma_fence_get(man->move); + spin_unlock(&man->move_lock); + + if (fence) { + ret = dma_fence_wait(fence, false); + dma_fence_put(fence); + } + +} + int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, unsigned long p_size) { @@ -1543,6 +1581,13 @@ int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, INIT_LIST_HEAD(&man->lru[i]); man->move = NULL; + pr_err("drmcgrp %p type %d\n", bdev->ddev, type); + + if (type <= TTM_PL_VRAM) { + INIT_WORK(&man->reclaim_wq, ttm_bo_reclaim_wq); + drmcgrp_register_device_mm(bdev->ddev, type, &man->reclaim_wq); + } + return 0; } EXPORT_SYMBOL(ttm_bo_init_mm); @@ -1620,6 +1665,8 @@ int ttm_bo_device_release(struct ttm_bo_device *bdev) man = &bdev->man[i]; if (man->has_type) { man->use_type = false; + drmcgrp_unregister_device_mm(bdev->ddev, i); + cancel_work_sync(&man->reclaim_wq); if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) { ret = -EBUSY; pr_err("DRM memory manager type %d is not clean\n", diff --git a/include/drm/drm_cgroup.h b/include/drm/drm_cgroup.h index 360c1e6c809f..134d6e5475f3 100644 --- a/include/drm/drm_cgroup.h +++ b/include/drm/drm_cgroup.h @@ -5,6 +5,7 @@ #define __DRM_CGROUP_H__ #include +#include #include #include @@ -12,6 +13,9 @@ int drmcgrp_register_device(struct drm_device *device); int drmcgrp_unregister_device(struct drm_device *device); +void drmcgrp_register_device_mm(struct drm_device *dev, unsigned type, + struct work_struct *wq); +void drmcgrp_unregister_device_mm(struct drm_device *dev, unsigned type); bool drmcgrp_is_self_or_ancestor(struct drmcgrp *self, struct drmcgrp *relative); void drmcgrp_chg_bo_alloc(struct drmcgrp *drmcgrp, struct drm_device *dev, @@ -40,6 +44,16 @@ static inline int drmcgrp_unregister_device(struct drm_device *device) return 0; } +static inline void drmcgrp_register_device_mm(struct drm_device *dev, + unsigned type, struct work_struct *wq) +{ +} + +static inline void drmcgrp_unregister_device_mm(struct drm_device *dev, + unsigned type) +{ +} + static inline bool drmcgrp_is_self_or_ancestor(struct drmcgrp *self, struct drmcgrp *relative) { diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h index 4cbcb41e5aa9..0956ca7888fc 100644 --- a/include/drm/ttm/ttm_bo_driver.h +++ b/include/drm/ttm/ttm_bo_driver.h @@ -205,6 +205,8 @@ struct ttm_mem_type_manager { * Protected by @move_lock. */ struct dma_fence *move; + + struct work_struct reclaim_wq; }; /** diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c index 1ce13db36ce9..985a89e849d3 100644 --- a/kernel/cgroup/drm.c +++ b/kernel/cgroup/drm.c @@ -31,6 +31,8 @@ struct drmcgrp_device { s64 mem_bw_avg_bytes_per_us_default; s64 mem_highs_default[TTM_PL_PRIV+1]; + + struct work_struct *mem_reclaim_wq[TTM_PL_PRIV]; }; #define DRMCG_CTF_PRIV_SIZE 3 @@ -793,6 +795,31 @@ int drmcgrp_unregister_device(struct drm_device *dev) } EXPORT_SYMBOL(drmcgrp_unregister_device); +void drmcgrp_register_device_mm(struct drm_device *dev, unsigned type, + struct work_struct *wq) +{ + if (dev == NULL || dev->primary->index > max_minor + || type >= TTM_PL_PRIV) + return; + + mutex_lock(&drmcgrp_mutex); + known_drmcgrp_devs[dev->primary->index]->mem_reclaim_wq[type] = wq; + mutex_unlock(&drmcgrp_mutex); +} +EXPORT_SYMBOL(drmcgrp_register_device_mm); + +void drmcgrp_unregister_device_mm(struct drm_device *dev, unsigned type) +{ + if (dev == NULL || dev->primary->index > max_minor + || type >= TTM_PL_PRIV) + return; + + mutex_lock(&drmcgrp_mutex); + known_drmcgrp_devs[dev->primary->index]->mem_reclaim_wq[type] = NULL; + mutex_unlock(&drmcgrp_mutex); +} +EXPORT_SYMBOL(drmcgrp_unregister_device_mm); + bool drmcgrp_is_self_or_ancestor(struct drmcgrp *self, struct drmcgrp *relative) { for (; self != NULL; self = parent_drmcgrp(self)) @@ -1004,6 +1031,12 @@ void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict, ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_BYTE_CREDIT] -= move_in_bytes; + + if (known_dev->mem_reclaim_wq[new_mem_type] != NULL && + ddr->mem_stats[new_mem_type] > + ddr->mem_highs[new_mem_type]) + schedule_work( + known_dev->mem_reclaim_wq[new_mem_type]); } mutex_unlock(&known_dev->mutex); }