From patchwork Fri Sep 9 11:16:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Stach X-Patchwork-Id: 12971597 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 880D2ECAAA1 for ; Fri, 9 Sep 2022 11:32:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 27C6C8D0005; Fri, 9 Sep 2022 07:32:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 205758D0002; Fri, 9 Sep 2022 07:32:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0A6228D0005; Fri, 9 Sep 2022 07:32:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id EFA278D0002 for ; Fri, 9 Sep 2022 07:32:07 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CC93F412BC for ; Fri, 9 Sep 2022 11:32:07 +0000 (UTC) X-FDA: 79892333094.23.0301352 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [85.220.165.71]) by imf24.hostedemail.com (Postfix) with ESMTP id 48D1A1800AC for ; Fri, 9 Sep 2022 11:32:07 +0000 (UTC) Received: from dude02.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::28]) by metis.ext.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1oWc04-0002dP-2x; Fri, 09 Sep 2022 13:16:44 +0200 From: Lucas Stach To: linux-mm@kvack.org, dri-devel@lists.freedesktop.org Cc: Daniel Vetter , David Airlie , Andrew Morton , Michal Hocko , =?utf-8?q?Christian_K=C3=B6nig?= , linux-fsdevel@vger.kernel.org, kernel@pengutronix.de Subject: [RFC PATCH 1/5] mm: add MM_DRIVERPAGES Date: Fri, 9 Sep 2022 13:16:36 +0200 Message-Id: <20220909111640.3789791-2-l.stach@pengutronix.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220909111640.3789791-1-l.stach@pengutronix.de> References: <20220909111640.3789791-1-l.stach@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a0a:edc0:0:1101:1d::28 X-SA-Exim-Mail-From: l.stach@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-mm@kvack.org ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf24.hostedemail.com: domain of l.stach@pengutronix.de designates 85.220.165.71 as permitted sender) smtp.mailfrom=l.stach@pengutronix.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662723127; a=rsa-sha256; cv=none; b=iqYC0Y8f156ULUmKIYzdyHo9oWKIObEqaDJibloFxUFBI1dSGGSTkGXlB58cKvyCL3OGM3 2bE45yyD5MdBaYbhkyxPfO+RjSEbk6+i22ZJMpmftIlnnYX/o/ZM2RnSLWZ++SoQu30jaO 1yeBusMuzKL/8F+2cZCAmys4rMrqyX8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662723127; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eaml+Iz7FW9vitLC5tsN/ex2hkOyNtHfirX90JZzfVM=; b=WmxgJonBwSsAZaDOQ7SfhGLAtRuFZmwOAKt+Ln0xQFx30h7M6M9ZxTJv4d3yrmF5EOrRBw roXWk7R+iFt+Ri4iXi9Q7lDKav0BIgrV+WLbRSTrWjUIGOw1Vm2H+eqiCuvsW+0Evkgo9o zhs0PkuoDOY3U1bo6PS+gmZr+iM3MFU= Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf24.hostedemail.com: domain of l.stach@pengutronix.de designates 85.220.165.71 as permitted sender) smtp.mailfrom=l.stach@pengutronix.de X-Rspam-User: X-Rspamd-Server: rspam01 X-Stat-Signature: zpxp4w4ygoferhsaotrcgohomde3f47w X-Rspamd-Queue-Id: 48D1A1800AC X-HE-Tag: 1662723127-93749 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This adds a mm counter for pages allocated by a driver on behalf of a userspace task. Especially with DRM drivers there can be large amounts of pages that are never mapped into userspace and thus are not tracked by the usual ANONPAGES mmap accounting, as those pages are only ever touched by the device. They can make up a significant portion of the tasks resident memory size, but are currently not visible in any of the memory statistics visible to userspace and the OOM handling. Add the counter to allow tracking such memory, which allows to make more sensible decisions in the OOM handling as well as allowing userspace some better insight into the real system memory usage. Signed-off-by: Lucas Stach --- fs/proc/task_mmu.c | 6 ++++-- include/linux/mm.h | 3 ++- include/linux/mm_types_task.h | 1 + kernel/fork.c | 1 + 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index a3398d0f1927..80b095a233bf 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -33,7 +33,8 @@ void task_mem(struct seq_file *m, struct mm_struct *mm) unsigned long text, lib, swap, anon, file, shmem; unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss; - anon = get_mm_counter(mm, MM_ANONPAGES); + anon = get_mm_counter(mm, MM_ANONPAGES) + + get_mm_counter(mm, MM_DRIVERPAGES); file = get_mm_counter(mm, MM_FILEPAGES); shmem = get_mm_counter(mm, MM_SHMEMPAGES); @@ -94,7 +95,8 @@ unsigned long task_statm(struct mm_struct *mm, *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> PAGE_SHIFT; *data = mm->data_vm + mm->stack_vm; - *resident = *shared + get_mm_counter(mm, MM_ANONPAGES); + *resident = *shared + get_mm_counter(mm, MM_ANONPAGES) + + get_mm_counter(mm, MM_DRIVERPAGES); return mm->total_vm; } diff --git a/include/linux/mm.h b/include/linux/mm.h index 3bedc449c14d..2cc014d1ea27 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2003,7 +2003,8 @@ static inline unsigned long get_mm_rss(struct mm_struct *mm) { return get_mm_counter(mm, MM_FILEPAGES) + get_mm_counter(mm, MM_ANONPAGES) + - get_mm_counter(mm, MM_SHMEMPAGES); + get_mm_counter(mm, MM_SHMEMPAGES) + + get_mm_counter(mm, MM_DRIVERPAGES); } static inline unsigned long get_mm_hiwater_rss(struct mm_struct *mm) diff --git a/include/linux/mm_types_task.h b/include/linux/mm_types_task.h index c1bc6731125c..420d88e79906 100644 --- a/include/linux/mm_types_task.h +++ b/include/linux/mm_types_task.h @@ -45,6 +45,7 @@ enum { MM_ANONPAGES, /* Resident anonymous pages */ MM_SWAPENTS, /* Anonymous swap entries */ MM_SHMEMPAGES, /* Resident shared memory pages */ + MM_DRIVERPAGES, /* pages allocated by a driver on behalf of a task */ NR_MM_COUNTERS }; diff --git a/kernel/fork.c b/kernel/fork.c index 90c85b17bf69..74a07a2288ba 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -135,6 +135,7 @@ static const char * const resident_page_types[] = { NAMED_ARRAY_INDEX(MM_ANONPAGES), NAMED_ARRAY_INDEX(MM_SWAPENTS), NAMED_ARRAY_INDEX(MM_SHMEMPAGES), + NAMED_ARRAY_INDEX(MM_DRIVERPAGES), }; DEFINE_PER_CPU(unsigned long, process_counts) = 0; From patchwork Fri Sep 9 11:16:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Stach X-Patchwork-Id: 12971578 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5963ECAAD3 for ; Fri, 9 Sep 2022 11:16:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 66A678D0001; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 560658D0006; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 275A68D0002; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 0F9678D0001 for ; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D973BAADED for ; Fri, 9 Sep 2022 11:16:46 +0000 (UTC) X-FDA: 79892294412.07.359A070 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [85.220.165.71]) by imf22.hostedemail.com (Postfix) with ESMTP id 77D05C00A0 for ; Fri, 9 Sep 2022 11:16:46 +0000 (UTC) Received: from dude02.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::28]) by metis.ext.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1oWc04-0002dP-Ir; Fri, 09 Sep 2022 13:16:44 +0200 From: Lucas Stach To: linux-mm@kvack.org, dri-devel@lists.freedesktop.org Cc: Daniel Vetter , David Airlie , Andrew Morton , Michal Hocko , =?utf-8?q?Christian_K=C3=B6nig?= , linux-fsdevel@vger.kernel.org, kernel@pengutronix.de Subject: [RFC PATCH 2/5] drm/gem: track mm struct of allocating process in gem object Date: Fri, 9 Sep 2022 13:16:37 +0200 Message-Id: <20220909111640.3789791-3-l.stach@pengutronix.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220909111640.3789791-1-l.stach@pengutronix.de> References: <20220909111640.3789791-1-l.stach@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a0a:edc0:0:1101:1d::28 X-SA-Exim-Mail-From: l.stach@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-mm@kvack.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662722206; a=rsa-sha256; cv=none; b=2bFyl9hUby7q7IMS67TxJdo1Sf8fb2odnJdhsuyJ7hVKh9QpkGpQhs8fe93V2ITZ2ZlOUW iU+9uJqslEsesynjyAkZHY8WSJ0TGnlKwZLgQd9nXiDqgR1XdzCp40aQkAmybj115UnnDV LLkHKLaa30V0K27yM5inYNZCx/Aw0GU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; spf=pass (imf22.hostedemail.com: domain of l.stach@pengutronix.de designates 85.220.165.71 as permitted sender) smtp.mailfrom=l.stach@pengutronix.de; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662722206; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=H7S0WIoeL/jW74nVwQoll38jdUlJbQ7EZKZ1IRFnRj8=; b=7/tahXjXfP1p/2zM0F6hSTmV4+NdemBWaoiJD2IFw3UJAaQay3M8geqfknWG43DuXd5PaU FUJMpoR623P8T9A9sErDlfhAWpsqZfW4AbO+eWPGUN9aFV7EqpdAyUSRbt7JONaUzyfH3o //2tzFG+sOkTFNZ3dWx0vkrrfs760lw= X-Rspamd-Server: rspam03 X-Rspam-User: X-Stat-Signature: wjzmccgcqzr7dhn3orsz8p7jn1ewsz5c X-Rspamd-Queue-Id: 77D05C00A0 Authentication-Results: imf22.hostedemail.com; dkim=none; spf=pass (imf22.hostedemail.com: domain of l.stach@pengutronix.de designates 85.220.165.71 as permitted sender) smtp.mailfrom=l.stach@pengutronix.de; dmarc=none X-HE-Tag: 1662722206-445727 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This keeps around a weak reference to the struct mm of the process allocating the GEM object. This allows us to charge/uncharge the process with the allocated backing store memory, even if this is happening from another context. Signed-off-by: Lucas Stach --- drivers/gpu/drm/drm_gem.c | 5 +++++ include/drm/drm_gem.h | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c index 86d670c71286..b882f935cd4b 100644 --- a/drivers/gpu/drm/drm_gem.c +++ b/drivers/gpu/drm/drm_gem.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -157,6 +158,9 @@ void drm_gem_private_object_init(struct drm_device *dev, obj->dev = dev; obj->filp = NULL; + mmgrab(current->mm); + obj->mm = current->mm; + kref_init(&obj->refcount); obj->handle_count = 0; obj->size = size; @@ -949,6 +953,7 @@ drm_gem_object_release(struct drm_gem_object *obj) if (obj->filp) fput(obj->filp); + mmdrop(obj->mm); dma_resv_fini(&obj->_resv); drm_gem_free_mmap_offset(obj); } diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h index 87cffc9efa85..d021a083c282 100644 --- a/include/drm/drm_gem.h +++ b/include/drm/drm_gem.h @@ -234,6 +234,18 @@ struct drm_gem_object { */ struct drm_vma_offset_node vma_node; + /** + * @mm: + * + * mm struct of the process creating the object. Used to account the + * allocated backing store memory. + * + * Note that this is a weak reference created by mmgrab(), so any + * manipulation needs to make sure the address space is still around by + * calling mmget_not_zero(). + */ + struct mm_struct *mm; + /** * @size: * From patchwork Fri Sep 9 11:16:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Stach X-Patchwork-Id: 12971577 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E718FECAAA1 for ; Fri, 9 Sep 2022 11:16:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3BBCB8D0003; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 35FB28D0005; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1FF818D0003; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 107648D0002 for ; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id DE852ABBBB for ; Fri, 9 Sep 2022 11:16:46 +0000 (UTC) X-FDA: 79892294412.03.9F43E27 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [85.220.165.71]) by imf28.hostedemail.com (Postfix) with ESMTP id 83363C00AB for ; Fri, 9 Sep 2022 11:16:46 +0000 (UTC) Received: from dude02.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::28]) by metis.ext.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1oWc05-0002dP-2O; Fri, 09 Sep 2022 13:16:45 +0200 From: Lucas Stach To: linux-mm@kvack.org, dri-devel@lists.freedesktop.org Cc: Daniel Vetter , David Airlie , Andrew Morton , Michal Hocko , =?utf-8?q?Christian_K=C3=B6nig?= , linux-fsdevel@vger.kernel.org, kernel@pengutronix.de Subject: [RFC PATCH 3/5] drm/gem: add functions to account GEM object memory usage Date: Fri, 9 Sep 2022 13:16:38 +0200 Message-Id: <20220909111640.3789791-4-l.stach@pengutronix.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220909111640.3789791-1-l.stach@pengutronix.de> References: <20220909111640.3789791-1-l.stach@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a0a:edc0:0:1101:1d::28 X-SA-Exim-Mail-From: l.stach@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-mm@kvack.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662722206; a=rsa-sha256; cv=none; b=d5mB4o43yia6V7ZB+XUflw5aRqbUPVSv40zplZ627+eYW00OH1ECE3WuYm/Mwu2crhqvLN BxOVSisBqoI10ZljC04S6P9/cAgkrqEb/KcrOS4EYTA2w8qCJxuPKmpQGA+gJB00O2RGL8 PxXOyCScZrK3pGZgntTJtn4jvjdfwYw= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf28.hostedemail.com: domain of l.stach@pengutronix.de designates 85.220.165.71 as permitted sender) smtp.mailfrom=l.stach@pengutronix.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662722206; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gAz3JIKMoJT6m7Xnf6bEU72XdJE+Pv5UXas8WvXrcmI=; b=Z+J3WqucOTAVn/SBgdemwxnNZHXcnGfsSfVvdkIM2j3FwRICZIqNXV9Vgcm7GDyVGYrapI hpP04801/a7Rr7ypmsYTWKljy2NaZzKpjjj/PPNFUkYT71C+Cy2Qq1HWJ6m++DJ6ob+iad asSO29a4+jddntviGAYgUuv5RVK7exk= X-Rspam-User: X-Rspamd-Server: rspam05 Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf28.hostedemail.com: domain of l.stach@pengutronix.de designates 85.220.165.71 as permitted sender) smtp.mailfrom=l.stach@pengutronix.de X-Stat-Signature: m1ankf9dbp1dpu6wwfsf7okm88qe4x4y X-Rspamd-Queue-Id: 83363C00AB X-HE-Tag: 1662722206-50803 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This adds some functions which driver can call to make the MM aware of the resident memory used by the GEM object. As drivers will have different points where memory is made resident/pinned into system memory, this just adds the helper functions and drivers need to make sure to call them at the right points. Signed-off-by: Lucas Stach --- drivers/gpu/drm/drm_gem.c | 37 +++++++++++++++++++++++++++++++++++++ include/drm/drm_gem.h | 3 +++ 2 files changed, 40 insertions(+) diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c index b882f935cd4b..efccd0a1dde7 100644 --- a/drivers/gpu/drm/drm_gem.c +++ b/drivers/gpu/drm/drm_gem.c @@ -1279,3 +1279,40 @@ drm_gem_unlock_reservations(struct drm_gem_object **objs, int count, ww_acquire_fini(acquire_ctx); } EXPORT_SYMBOL(drm_gem_unlock_reservations); + +/** + * drm_gem_add_resident - Account memory used by GEM object to the + * task which called drm_gem_object_init(). Call when the pages are + * made resident in system memory, i.e. pinned for GPU usage. + * + * @obj: GEM buffer object + */ +void drm_gem_add_resident(struct drm_gem_object *obj) +{ + if (!mmget_not_zero(obj->mm)) + return; + + add_mm_counter(obj->mm, MM_DRIVERPAGES, obj->size / PAGE_SIZE); + + mmput(obj->mm); +} +EXPORT_SYMBOL(drm_gem_add_resident) + +/** + * drm_gem_dec_resident - Remove memory used by GEM object accounted + * to the task which called drm_gem_object_init(). Call this when the + * pages backing the GEM object are no longer resident in system memory, + * i.e. when freeing or unpinning the pages. + * + * @obj: GEM buffer object + */ +void drm_gem_dec_resident(struct drm_gem_object *obj) +{ + if (!mmget_not_zero(obj->mm)) + return; + + add_mm_counter(obj->mm, MM_DRIVERPAGES, -(obj->size / PAGE_SIZE)); + + mmput(obj->mm); +} +EXPORT_SYMBOL(drm_gem_dec_resident) diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h index d021a083c282..5951963a2f1a 100644 --- a/include/drm/drm_gem.h +++ b/include/drm/drm_gem.h @@ -374,6 +374,9 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size, struct vm_area_struct *vma); int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma); +void drm_gem_add_resident(struct drm_gem_object *obj); +void drm_gem_dec_resident(struct drm_gem_object *obj); + /** * drm_gem_object_get - acquire a GEM buffer object reference * @obj: GEM buffer object From patchwork Fri Sep 9 11:16:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Stach X-Patchwork-Id: 12971580 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0DC94ECAAD3 for ; Fri, 9 Sep 2022 11:16:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BEE118D0002; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BC3C98D0006; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB21E8D0002; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 9B4318D0006 for ; Fri, 9 Sep 2022 07:16:47 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7AA88C12AD for ; Fri, 9 Sep 2022 11:16:47 +0000 (UTC) X-FDA: 79892294454.13.0764627 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [85.220.165.71]) by imf04.hostedemail.com (Postfix) with ESMTP id 1AB5D40084 for ; Fri, 9 Sep 2022 11:16:46 +0000 (UTC) Received: from dude02.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::28]) by metis.ext.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1oWc05-0002dP-IW; Fri, 09 Sep 2022 13:16:45 +0200 From: Lucas Stach To: linux-mm@kvack.org, dri-devel@lists.freedesktop.org Cc: Daniel Vetter , David Airlie , Andrew Morton , Michal Hocko , =?utf-8?q?Christian_K=C3=B6nig?= , linux-fsdevel@vger.kernel.org, kernel@pengutronix.de Subject: [RFC PATCH 4/5] drm/cma-helper: account memory used by CMA GEM objects Date: Fri, 9 Sep 2022 13:16:39 +0200 Message-Id: <20220909111640.3789791-5-l.stach@pengutronix.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220909111640.3789791-1-l.stach@pengutronix.de> References: <20220909111640.3789791-1-l.stach@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a0a:edc0:0:1101:1d::28 X-SA-Exim-Mail-From: l.stach@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-mm@kvack.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662722207; a=rsa-sha256; cv=none; b=Sc3x891p9d/5/tVDWnesic6yU/Xkp9GddoT5LZKaJbU+aImMlMKCv24hr2ELOX6KaAvboU w2qOczb6iIe93MzjT2CeHJfQr6ETM02fvm5VUA8xuYWgrUW6lpZ0mGdIypTKQ/ueqv4jg7 tZHDB3z9m34tkcSFi9THfU198GB1KXs= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf04.hostedemail.com: domain of l.stach@pengutronix.de designates 85.220.165.71 as permitted sender) smtp.mailfrom=l.stach@pengutronix.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662722207; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vd2hnXVkt8PyL0zKx/WzK3ERGrir9HxLfSp7uQ67S7M=; b=Q4CisKEFZdUNfWEz0ci2AV9KLSdodf36ZnByPwFajIxbNNvD2pADq0N+6VsGBzIrnyZtKQ DbOtXfq0q3iTKlehsfV56jKwSTJ19iNJLQLJ2HMi8FJZU9c+eBq6LZLVy2YBFn61hq6DZK Xm5m9LEpXOhR409lTWuv2P+qGlvPTwA= Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf04.hostedemail.com: domain of l.stach@pengutronix.de designates 85.220.165.71 as permitted sender) smtp.mailfrom=l.stach@pengutronix.de X-Rspam-User: X-Rspamd-Queue-Id: 1AB5D40084 X-Rspamd-Server: rspam09 X-Stat-Signature: 7swiiw6sapeity5hwo1natcnmyfeb8xr X-HE-Tag: 1662722206-820370 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: CMA buffer are pinned into system memory as soon as they are allocated and will only disappear when they are freed. Signed-off-by: Lucas Stach --- drivers/gpu/drm/drm_gem_cma_helper.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/gpu/drm/drm_gem_cma_helper.c b/drivers/gpu/drm/drm_gem_cma_helper.c index 42abee9a0f4f..f0c4e7e6cc33 100644 --- a/drivers/gpu/drm/drm_gem_cma_helper.c +++ b/drivers/gpu/drm/drm_gem_cma_helper.c @@ -162,6 +162,8 @@ struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm, goto error; } + drm_gem_add_resident(&cma_obj->base); + return cma_obj; error: @@ -230,6 +232,8 @@ void drm_gem_cma_free(struct drm_gem_cma_object *cma_obj) struct drm_gem_object *gem_obj = &cma_obj->base; struct iosys_map map = IOSYS_MAP_INIT_VADDR(cma_obj->vaddr); + drm_gem_dec_resident(gem_obj); + if (gem_obj->import_attach) { if (cma_obj->vaddr) dma_buf_vunmap(gem_obj->import_attach->dmabuf, &map); From patchwork Fri Sep 9 11:16:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Stach X-Patchwork-Id: 12971598 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B950ECAAD3 for ; Fri, 9 Sep 2022 11:32:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ABD788D0006; Fri, 9 Sep 2022 07:32:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A45128D0002; Fri, 9 Sep 2022 07:32:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 895758D0006; Fri, 9 Sep 2022 07:32:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7B28A8D0002 for ; Fri, 9 Sep 2022 07:32:09 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4C5CB8131E for ; Fri, 9 Sep 2022 11:32:09 +0000 (UTC) X-FDA: 79892333178.27.BD03386 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [85.220.165.71]) by imf22.hostedemail.com (Postfix) with ESMTP id 05932C009B for ; Fri, 9 Sep 2022 11:32:08 +0000 (UTC) Received: from dude02.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::28]) by metis.ext.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1oWc06-0002dP-1w; Fri, 09 Sep 2022 13:16:46 +0200 From: Lucas Stach To: linux-mm@kvack.org, dri-devel@lists.freedesktop.org Cc: Daniel Vetter , David Airlie , Andrew Morton , Michal Hocko , =?utf-8?q?Christian_K=C3=B6nig?= , linux-fsdevel@vger.kernel.org, kernel@pengutronix.de Subject: [RFC PATCH 5/5] drm/etnaviv: account memory used by GEM buffers Date: Fri, 9 Sep 2022 13:16:40 +0200 Message-Id: <20220909111640.3789791-6-l.stach@pengutronix.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220909111640.3789791-1-l.stach@pengutronix.de> References: <20220909111640.3789791-1-l.stach@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a0a:edc0:0:1101:1d::28 X-SA-Exim-Mail-From: l.stach@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-mm@kvack.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662723129; a=rsa-sha256; cv=none; b=Tc6Sqf2cWKZQd2pDFOuW+YyrADLIU00uoNFOcoY2H5+iFCRkglJhaXZy9eTa8t7WBFtB/H PUK7mFfIMBwdVveP9N8c6VjcXJEi+hxq2OwzNpp6yrdFTUTVzeT/bYEJDxhcN1VBKGjwKY f2F1Rst0+9WDmJXQu1tpKm0P5y9B6bs= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; spf=pass (imf22.hostedemail.com: domain of l.stach@pengutronix.de designates 85.220.165.71 as permitted sender) smtp.mailfrom=l.stach@pengutronix.de; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662723129; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GiYH+wLILHVh2KvkX5YzE3kkzGn1y/KaA+m9GVJgvkw=; b=CB5/yhfTfilICrOmyf/GO1I1XjHpAwcAnuvs4xwuHqIQhOy0BDlhBrR0n/cPBSZfEB1RGC Xd3CZkZQLJz7c5v2GWdxkAVyI5tcfKHRjmO42NpPWHqSSoAv54KbYo2Hh7wIbGEjxm1mZ8 FVPl46kYyh2Y1e5xiShnDjgoTE8Jlr8= X-Stat-Signature: 3j5kwcrcmfwfsf1y7nffeegyfj11den5 X-Rspam-User: X-Rspamd-Queue-Id: 05932C009B X-Rspamd-Server: rspam07 Authentication-Results: imf22.hostedemail.com; dkim=none; spf=pass (imf22.hostedemail.com: domain of l.stach@pengutronix.de designates 85.220.165.71 as permitted sender) smtp.mailfrom=l.stach@pengutronix.de; dmarc=none X-HE-Tag: 1662723128-738709 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Etnaviv GEM buffers are pinned into memory as soon as we allocate the pages backing the object and only disappear when freeing the GEM object as there is no shrinker hooked up for unused buffers. Signed-off-by: Lucas Stach --- drivers/gpu/drm/etnaviv/etnaviv_gem.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gem.c b/drivers/gpu/drm/etnaviv/etnaviv_gem.c index cc386f8a7116..bf3d75b8e154 100644 --- a/drivers/gpu/drm/etnaviv/etnaviv_gem.c +++ b/drivers/gpu/drm/etnaviv/etnaviv_gem.c @@ -67,6 +67,8 @@ static int etnaviv_gem_shmem_get_pages(struct etnaviv_gem_object *etnaviv_obj) etnaviv_obj->pages = p; + drm_gem_add_resident(&etnaviv_obj->base); + return 0; } @@ -79,6 +81,7 @@ static void put_pages(struct etnaviv_gem_object *etnaviv_obj) etnaviv_obj->sgt = NULL; } if (etnaviv_obj->pages) { + drm_gem_dec_resident(&etnaviv_obj->base); drm_gem_put_pages(&etnaviv_obj->base, etnaviv_obj->pages, true, false);