From patchwork Wed Oct 19 17:32:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 5787 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp452205wrs; Wed, 19 Oct 2022 10:38:27 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4AtouX40ZvyRxL4tmzgCWf1DWroUkRamhz+ugLqU3c8dfUnNovgQUyaJkoD+aY9lniHCgM X-Received: by 2002:a17:907:2cce:b0:77a:6958:5aaa with SMTP id hg14-20020a1709072cce00b0077a69585aaamr7353699ejc.245.1666201107514; Wed, 19 Oct 2022 10:38:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666201107; cv=none; d=google.com; s=arc-20160816; b=wjZ/fno71CGkys00QzBThjgg34QgTSsi3XexsAQqr27axauJuusH/p0Fo2fXOPLVCF dyYVfywlPSM/o2c4DKnVJ3PgvU3I/A2+aGvc94fLSfWGpnPavJkn829Kdf6vryXdaifc 1qPxqUBxaOwZDV/YlGEqV4FNjRUmsLblsF3IUtED/mWRzl191nSTH33XLbMYeh3J4JjW luR+GxX8TnLIhN356Ylu1XymNsfgNjz3tTnheuGGVXpZd6sjaKz77IeIMW2Lq0UuXhpp f0f1VLTyIY6/3iu5EpKKT3RlMNhOulkQAMPeP95TCK6LaOrR0+IJBQ4DjYzcioyFkjMX hUdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JVb/MsZMIZbXTnt8AbxsrbXN5rmRtMYy8AHTA4ha0A0=; b=lWo4tLVrgp8JEWxMLk9U1BA5JLHmpXKbbfahp2eD6hgOK6ug036X48W3voa9jvuM36 3RpJcP43G13G2dIv3FYJiKV15I7Sw3y1l3vnyYgUEptGXflOlqL/zRWIZDvpHcHd+CSX GJLva67A+Hp0hFC9vjP8/L+uwb1wSnLLuqjPd8xHrYPrrLQW341EvB2F3kLwZ7RPtMO5 li67ckuzAmtzbczajCj6jv9UCp9/RWv4vsFpjyBirS2nqdQ5ahN7CZmR6G4eMTTk6Nqq I9IOVt6caNLqnDGDx8mGMIg4x7oVXg3sGVtOFyhdrUyvqRQ/mpc65VLWBkBPbaQIIfe3 Gaxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="c0C5n/3K"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s17-20020aa7c551000000b0045c9dbe290csi13307724edr.406.2022.10.19.10.38.02; Wed, 19 Oct 2022 10:38:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="c0C5n/3K"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230074AbiJSRec (ORCPT + 99 others); Wed, 19 Oct 2022 13:34:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231310AbiJSReH (ORCPT ); Wed, 19 Oct 2022 13:34:07 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 825801D3473; Wed, 19 Oct 2022 10:34:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666200844; x=1697736844; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iblWtj3oQ+Gzcpob6q0KDZMo8KPs6N0OMQ7W8Hrwr8E=; b=c0C5n/3KLGiKF2usH6erg2PnyHShFnFTJJ1TrCRDdUHGYYK1zO6otd9u SJGwz9NtddkANEhYM1ZwKKCLljxo0mFDQRDWuYuyUSqo3MA8tn7kHM8Z/ TYwukPvK53m9diJmJx+AmiQXAmY2/RTcSJSMB/Biz/DUIn0xVBhfsFUsg 1nBWJ710UZ8O6W5IoNHes1vM8GQOYUlkIKq8LuizsDHd/28/q3FAHkxK5 ZDchScIDOy1rBu+xKlArJvvmUyC9+rg2EYcOMjiI8/L62VnW6rEuRyYad s8YKAnNymScoZ/oY4rhNxrvT5H7ILw09CFBl9Wfpd51ZsWX/WAtsCpi+v Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10505"; a="306474462" X-IronPort-AV: E=Sophos;i="5.95,196,1661842800"; d="scan'208";a="306474462" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Oct 2022 10:33:46 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10505"; a="607204781" X-IronPort-AV: E=Sophos;i="5.95,196,1661842800"; d="scan'208";a="607204781" Received: from mjmcener-mobl1.amr.corp.intel.com (HELO localhost.localdomain) ([10.213.233.40]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Oct 2022 10:33:43 -0700 From: Tvrtko Ursulin To: Intel-gfx@lists.freedesktop.org Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Tejun Heo , Johannes Weiner , Zefan Li , Dave Airlie , Daniel Vetter , Rob Clark , =?utf-8?q?St=C3=A9phane_Marchesin?= , "T . J . Mercier" , Kenny.Ho@amd.com, =?utf-8?q?Chris?= =?utf-8?q?tian_K=C3=B6nig?= , Brian Welty , Tvrtko Ursulin Subject: [RFC 08/17] drm: Allow for migration of clients Date: Wed, 19 Oct 2022 18:32:45 +0100 Message-Id: <20221019173254.3361334-9-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221019173254.3361334-1-tvrtko.ursulin@linux.intel.com> References: <20221019173254.3361334-1-tvrtko.ursulin@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,HK_RANDOM_ENVFROM,HK_RANDOM_FROM, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747138492526477932?= X-GMAIL-MSGID: =?utf-8?q?1747138492526477932?= From: Tvrtko Ursulin Add a helper which allows migrating the tracked client from one process to another. Signed-off-by: Tvrtko Ursulin --- drivers/gpu/drm/drm_cgroup.c | 111 ++++++++++++++++++++++++++++++----- include/drm/drm_clients.h | 7 +++ include/drm/drm_file.h | 1 + 3 files changed, 103 insertions(+), 16 deletions(-) diff --git a/drivers/gpu/drm/drm_cgroup.c b/drivers/gpu/drm/drm_cgroup.c index 7ed9c7150cae..59b730ed1334 100644 --- a/drivers/gpu/drm/drm_cgroup.c +++ b/drivers/gpu/drm/drm_cgroup.c @@ -8,9 +8,21 @@ static DEFINE_XARRAY(drm_pid_clients); +static void +__del_clients(struct drm_pid_clients *clients, struct drm_file *file_priv) +{ + list_del_rcu(&file_priv->clink); + if (atomic_dec_and_test(&clients->num)) { + xa_erase(&drm_pid_clients, (unsigned long)file_priv->cpid); + kfree_rcu(clients, rcu); + } + + put_pid(file_priv->cpid); + file_priv->cpid = NULL; +} + void drm_clients_close(struct drm_file *file_priv) { - unsigned long pid = (unsigned long)file_priv->pid; struct drm_device *dev = file_priv->minor->dev; struct drm_pid_clients *clients; @@ -19,19 +31,32 @@ void drm_clients_close(struct drm_file *file_priv) if (!dev->driver->cg_ops) return; - clients = xa_load(&drm_pid_clients, pid); - list_del_rcu(&file_priv->clink); - if (atomic_dec_and_test(&clients->num)) { - xa_erase(&drm_pid_clients, pid); - kfree_rcu(clients, rcu); + clients = xa_load(&drm_pid_clients, (unsigned long)file_priv->cpid); + if (WARN_ON_ONCE(!clients)) + return; - /* - * FIXME: file_priv is not RCU protected so we add this hack - * to avoid any races with code which walks clients->file_list - * and accesses file_priv. - */ - synchronize_rcu(); + __del_clients(clients, file_priv); + + /* + * FIXME: file_priv is not RCU protected so we add this hack + * to avoid any races with code which walks clients->file_list + * and accesses file_priv. + */ + synchronize_rcu(); +} + +static struct drm_pid_clients *__alloc_clients(void) +{ + struct drm_pid_clients *clients; + + clients = kmalloc(sizeof(*clients), GFP_KERNEL); + if (clients) { + atomic_set(&clients->num, 0); + INIT_LIST_HEAD(&clients->file_list); + init_rcu_head(&clients->rcu); } + + return clients; } int drm_clients_open(struct drm_file *file_priv) @@ -48,12 +73,9 @@ int drm_clients_open(struct drm_file *file_priv) clients = xa_load(&drm_pid_clients, pid); if (!clients) { - clients = kmalloc(sizeof(*clients), GFP_KERNEL); + clients = __alloc_clients(); if (!clients) return -ENOMEM; - atomic_set(&clients->num, 0); - INIT_LIST_HEAD(&clients->file_list); - init_rcu_head(&clients->rcu); new_client = true; } atomic_inc(&clients->num); @@ -69,9 +91,66 @@ int drm_clients_open(struct drm_file *file_priv) } } + file_priv->cpid = get_pid(file_priv->pid); + return 0; } +void drm_clients_migrate(struct drm_file *file_priv) +{ + struct drm_device *dev = file_priv->minor->dev; + struct drm_pid_clients *existing_clients; + struct drm_pid_clients *clients, *spare; + struct pid *pid = task_pid(current); + + if (!dev->driver->cg_ops) + return; + + // TODO: only do this if drmcs level property allows it? + + spare = __alloc_clients(); + if (WARN_ON(!spare)) + return; + + mutex_lock(&dev->filelist_mutex); + rcu_read_lock(); + + existing_clients = xa_load(&drm_pid_clients, (unsigned long)pid); + clients = xa_load(&drm_pid_clients, (unsigned long)file_priv->cpid); + + if (WARN_ON_ONCE(!clients)) + goto out_unlock; + else if (clients == existing_clients) + goto out_unlock; + + __del_clients(clients, file_priv); + smp_mb(); /* hmmm? del_rcu followed by add_rcu? */ + + if (!existing_clients) { + void *xret; + + xret = xa_store(&drm_pid_clients, (unsigned long)pid, spare, + GFP_KERNEL); + if (WARN_ON(xa_err(xret))) + goto out_unlock; + clients = spare; + spare = NULL; + } else { + clients = existing_clients; + } + + atomic_inc(&clients->num); + list_add_tail_rcu(&file_priv->clink, &clients->file_list); + file_priv->cpid = get_pid(pid); + +out_unlock: + rcu_read_unlock(); + mutex_unlock(&dev->filelist_mutex); + + kfree(spare); +} +EXPORT_SYMBOL_GPL(drm_clients_migrate); + unsigned int drm_pid_priority_levels(struct pid *pid, bool *non_uniform) { unsigned int min_levels = UINT_MAX; diff --git a/include/drm/drm_clients.h b/include/drm/drm_clients.h index 10d21138f7af..3a0b1cdb338f 100644 --- a/include/drm/drm_clients.h +++ b/include/drm/drm_clients.h @@ -17,6 +17,8 @@ struct drm_pid_clients { #if IS_ENABLED(CONFIG_CGROUP_DRM) void drm_clients_close(struct drm_file *file_priv); int drm_clients_open(struct drm_file *file_priv); + +void drm_clients_migrate(struct drm_file *file_priv); #else static inline void drm_clients_close(struct drm_file *file_priv) { @@ -26,6 +28,11 @@ static inline int drm_clients_open(struct drm_file *file_priv) { return 0; } + +static inline void drm_clients_migrate(struct drm_file *file_priv) +{ + +} #endif unsigned int drm_pid_priority_levels(struct pid *pid, bool *non_uniform); diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h index a4360e28e2db..2c1e356d3b73 100644 --- a/include/drm/drm_file.h +++ b/include/drm/drm_file.h @@ -280,6 +280,7 @@ struct drm_file { #if IS_ENABLED(CONFIG_CGROUP_DRM) struct list_head clink; + struct pid *cpid; #endif /**