Received: by 2002:a19:771d:0:0:0:0:0 with SMTP id s29csp1244149lfc; Wed, 1 Jun 2022 12:54:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwCN0Ew7Dzo628ny+9LxwklylRWu3ObOYHIptd+wM6X9VyjYKVan62XEYcK8qyDkTht/MtP X-Received: by 2002:a05:6a00:8c2:b0:510:98ac:96c9 with SMTP id s2-20020a056a0008c200b0051098ac96c9mr68087246pfu.18.1654113262765; Wed, 01 Jun 2022 12:54:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654113262; cv=none; d=google.com; s=arc-20160816; b=FaBzmwlKrGoZaQVScZddsYMVS6cY94z/HCLfSLJ800klgidhLJZiaeTt+asIlG4Qce Y9rbiUgklTmjmpc4mcDMhSveJzk/NpQ7Lmrmp6Lrh+0wQFF/S8ViLYm4HteJzzUPThLE 4kG7I4bIVxZjYvWJXRz5uCDBXkGTOudnjvrUDLLlx4MJXkgMgeAgiBcToM5YSHO8uhVW NLBs0Sa2l2AUq8quZ6KHMy/fzqErVveavABwommPEo0oGY5PxaVb3a4u434O33SZt6Em EefwnHzi+LDD26OdNDRhXFc5o5QNrGF7j6IgP1nv3AlkSi49LyQhXaBNI11rwYqRJCkG QecQ== 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=TNC+q1HYAjRPp62MTH/zi2loKTLmxRGj7rz2onfHvYc=; b=yfTXHRpk/vciwHlfEimziJOqIMcEv4yiXDOiVVCpDukEmxyj0KhbESStccZkGEd8RR h1UOLNQu5Mnf3rHWIBX3/k1yXSrx1qc/YtCr2kdWuWs8reDfA2ypG03kCQIvjmEupzaN 9brCtdkHBy8eVLEn1/Qgn0a3XRabE/dM9Dif3Mt8Xs08Su32rCEvBuWw460W6ai/DVJV mAypbm2PoS8IbQJrD65ndv4CxrOxYJ1KoNQfJ03m0Gas8r0jlao0gQIlxcEurK1JbyHg o7ug/I4oYUy7+51E4+M7W5lHqo3jJPAhG00Flbs+GzzQHip+94GRVFywEmWwOxsg5HLf jjkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GBXq9vHH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id d13-20020a170902b70d00b00161a89c87dasi1991683pls.236.2022.06.01.12.54.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 12:54:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GBXq9vHH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5A94A20C6C4; Wed, 1 Jun 2022 12:15:48 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245419AbiEaKAt (ORCPT + 99 others); Tue, 31 May 2022 06:00:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245425AbiEaKAV (ORCPT ); Tue, 31 May 2022 06:00:21 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2972884A14; Tue, 31 May 2022 03:00:19 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id f21so25481616ejh.11; Tue, 31 May 2022 03:00:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TNC+q1HYAjRPp62MTH/zi2loKTLmxRGj7rz2onfHvYc=; b=GBXq9vHHCJxrvvL36ccyiGaBUxAA4gOuuAE6JJqw7rKR68K9hwr5XNyqXOEIIT7vcs b+n0Pi155O7ZJ3hNSKyRz9mXIkUEKm2lcFNblLxTflNcxgkeUjq/QrKdI4HYMHPMzWAq SVmk2ARrocIRZTeQ2RslzCLdsKehpXg+Kq0kTeKoNXvG6sD16XOQ01MvqTpq1vkjhPtq +sWJbuWYYHK+e5WSj+SIwpEX66Y+xGwye9J3sHcOAM1TU5DbXJA7e3WOh4GWP04ovYuJ BVp7Cx+40rXPVhuLagJwI31hssTpBLyHz/XRKyjdzt8EQGLbOfMOhG6RJdTATBNFmuK2 0Y5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TNC+q1HYAjRPp62MTH/zi2loKTLmxRGj7rz2onfHvYc=; b=uvVaBjCnioWLWAVLQvUWYntrPFExkrnD14h2h4bkKC8KoJQrXUHexMGfADheaxuI4J 62l1PPFu0QDc6FRw8bP/0jXAWet8XH28nTzfj3vdD6isN8Q4cGK/9rdTFvKMbUCzHzi9 KXkyAQcp+cdeUtc/d99oRcFYH15horqmhPF/knz9RkiJllGrada6z646Ee05dWlCrt6D LIMdCBff5sq8QkqENe3L1pEWC1aYPpAHQITyPw8VDajbEMI4SMHKPcksmY5ROWmCouqB 5ZzWOxPGKHo1odJ4f18uno3GzrDR8MJAfYMshoHSgu60F/r02yU1umI6+056AgHrYD/+ QADQ== X-Gm-Message-State: AOAM533Hc8TcyR+b0Igsn2NpGTj+Q3PaFfbsS3ncwu9Ot3gM7pRu/jwA t8bccTr4RXMIqgKQqm7+yizRqwRhI2Pn5Q== X-Received: by 2002:a17:907:1b03:b0:6ff:78d4:c140 with SMTP id mp3-20020a1709071b0300b006ff78d4c140mr8861505ejc.554.1653991217575; Tue, 31 May 2022 03:00:17 -0700 (PDT) Received: from able.fritz.box (p5b0ea02f.dip0.t-ipconnect.de. [91.14.160.47]) by smtp.gmail.com with ESMTPSA id r13-20020a056402018d00b0042617ba6389sm582062edv.19.2022.05.31.03.00.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 03:00:17 -0700 (PDT) From: "=?UTF-8?q?Christian=20K=C3=B6nig?=" X-Google-Original-From: =?UTF-8?q?Christian=20K=C3=B6nig?= To: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, intel-gfx@lists.freedesktop.org, amd-gfx@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-tegra@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: christian.koenig@amd.com, alexander.deucher@amd.com, daniel@ffwll.ch, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, hughd@google.com, andrey.grodzovsky@amd.com Subject: [PATCH 05/13] drm/gem: adjust per file OOM badness on handling buffers Date: Tue, 31 May 2022 11:59:59 +0200 Message-Id: <20220531100007.174649-6-christian.koenig@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220531100007.174649-1-christian.koenig@amd.com> References: <20220531100007.174649-1-christian.koenig@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 From: Andrey Grodzovsky Large amounts of VRAM are usually not CPU accessible, so they are not mapped into the processes address space. But since the device drivers usually support swapping buffers from VRAM to system memory we can still run into an out of memory situation when userspace starts to allocate to much. This patch gives the OOM killer another hint which process is holding references to memory resources. A GEM helper is provided and automatically used for all drivers using the DEFINE_DRM_GEM_FOPS() and DEFINE_DRM_GEM_CMA_FOPS() macros. Signed-off-by: Andrey Grodzovsky --- drivers/gpu/drm/drm_file.c | 19 +++++++++++++++++++ drivers/gpu/drm/drm_gem.c | 5 +++++ include/drm/drm_file.h | 9 +++++++++ include/drm/drm_gem.h | 1 + include/drm/drm_gem_cma_helper.h | 1 + 5 files changed, 35 insertions(+) diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c index ed25168619fc..1959a5b7029e 100644 --- a/drivers/gpu/drm/drm_file.c +++ b/drivers/gpu/drm/drm_file.c @@ -1049,3 +1049,22 @@ unsigned long drm_get_unmapped_area(struct file *file, #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ EXPORT_SYMBOL_GPL(drm_get_unmapped_area); #endif /* CONFIG_MMU */ + + +/** + * drm_oom_badness() - get oom badness for struct drm_file + * @f: struct drm_file to get the badness from + * + * Return how many pages are allocated for this client. + */ +long drm_oom_badness(struct file *f) +{ + + struct drm_file *file_priv = f->private_data; + + if (file_priv) + return atomic_long_read(&file_priv->f_oom_badness); + + return 0; +} +EXPORT_SYMBOL(drm_oom_badness); diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c index eb0c2d041f13..768b28b198cd 100644 --- a/drivers/gpu/drm/drm_gem.c +++ b/drivers/gpu/drm/drm_gem.c @@ -256,6 +256,7 @@ drm_gem_object_release_handle(int id, void *ptr, void *data) drm_gem_remove_prime_handles(obj, file_priv); drm_vma_node_revoke(&obj->vma_node, file_priv); + atomic_long_sub(obj->size >> PAGE_SHIFT, &file_priv->f_oom_badness); drm_gem_object_handle_put_unlocked(obj); return 0; @@ -291,6 +292,8 @@ drm_gem_handle_delete(struct drm_file *filp, u32 handle) idr_remove(&filp->object_idr, handle); spin_unlock(&filp->table_lock); + atomic_long_sub(obj->size >> PAGE_SHIFT, &filp->f_oom_badness); + return 0; } EXPORT_SYMBOL(drm_gem_handle_delete); @@ -399,6 +402,8 @@ drm_gem_handle_create_tail(struct drm_file *file_priv, } *handlep = handle; + + atomic_long_add(obj->size >> PAGE_SHIFT, &file_priv->f_oom_badness); return 0; err_revoke: diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h index e0a73a1e2df7..5926766d79f0 100644 --- a/include/drm/drm_file.h +++ b/include/drm/drm_file.h @@ -366,6 +366,13 @@ struct drm_file { #if IS_ENABLED(CONFIG_DRM_LEGACY) unsigned long lock_count; /* DRI1 legacy lock count */ #endif + + /** + * @f_oom_badness: + * + * How many pages are allocated through this driver connection. + */ + atomic_long_t f_oom_badness; }; /** @@ -430,4 +437,6 @@ unsigned long drm_get_unmapped_area(struct file *file, #endif /* CONFIG_MMU */ +long drm_oom_badness(struct file *f); + #endif /* _DRM_FILE_H_ */ diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h index 9d7c61a122dc..0adf8c2f62e8 100644 --- a/include/drm/drm_gem.h +++ b/include/drm/drm_gem.h @@ -338,6 +338,7 @@ struct drm_gem_object { .read = drm_read,\ .llseek = noop_llseek,\ .mmap = drm_gem_mmap,\ + .oom_badness = drm_oom_badness,\ } void drm_gem_object_release(struct drm_gem_object *obj); diff --git a/include/drm/drm_gem_cma_helper.h b/include/drm/drm_gem_cma_helper.h index fbda4ce5d5fb..455ce1aa6d2c 100644 --- a/include/drm/drm_gem_cma_helper.h +++ b/include/drm/drm_gem_cma_helper.h @@ -273,6 +273,7 @@ unsigned long drm_gem_cma_get_unmapped_area(struct file *filp, .read = drm_read,\ .llseek = noop_llseek,\ .mmap = drm_gem_mmap,\ + .oom_badness = drm_oom_badness,\ DRM_GEM_CMA_UNMAPPED_AREA_FOPS \ } -- 2.25.1