From patchwork Mon Aug 5 14:33:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Herring X-Patchwork-Id: 170573 Delivered-To: patch@linaro.org Received: by 2002:a92:512:0:0:0:0:0 with SMTP id q18csp4373772ile; Mon, 5 Aug 2019 07:34:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqy46HEhVr6P2ti0DNZroEfX2KRzcfst79Qul6adTfo1WyeQy6j5pZt79lvGrYt9Nw6tiNJg X-Received: by 2002:a63:d555:: with SMTP id v21mr113358555pgi.179.1565015644637; Mon, 05 Aug 2019 07:34:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565015644; cv=none; d=google.com; s=arc-20160816; b=UE+3jthI5M1+X5/1iTKQF65R0+n30Sz9ReQx8dtVaDpYC6qaP9pNaKr+XuaOARhTmj MjezNGk2kbpnN7dHx/QwMWBTwO/KoHfdqX2MWqgGDUqgHcjrNtoKdHiUHrc9vqwU8o8a JxL6HooPqANdTWFY43dRT55qr2NvsjP/MTetuOtLR1vyv8hIzfM3ezkrh2/HhxFROaiT 4+rWnSqDmJJltbVoXReb++/kte5F3Ky3weysHufrxBumy/pRf4LOkKbOXv0JIeDq7+Un H9nVeiSUdJMf+J807KO9dHQ8omfS6UPNC5r7qhsuIocUQ9XSbABOouSPoSFvxOusHhfb kgVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:mime-version:message-id:date:subject:to:from :delivered-to; bh=+FfjgJPFiusAC0bLg95bAA2wqhHwrm6LslBTXLNKpq8=; b=TDLNr2ae7NFkowHEgHoNghjZN0Hi319EGID8nMubrjbtEZMiEswTEvEXsKwvhRXUjp h7t7qsLbOgPjRehtpG2Sd2MvjmNZkZBrLohMwtTUsNf1vXnWefWZKqn6Bdi4Kdz34Ime eP2bMHJAe0g8dqmyPAai7cX/mvxlrsdZcigSm+/Q2s4LvIxUqkL6qAy/UH9NlhaLzFHZ QCd0kQPp9/7aIekc+Wm/XgSGps4eyH4XOf14Kjyrw9a3zqEbWjUnmuTJekUzIMPnF0H6 2SI0/G7rZkeOLC0MVBUjaplmxDr+h9FY8dG1d97PscPfU05iEowVYwAz05htioIlncXX tnvg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 2610:10:20:722:a800:ff:fe36:1795 as permitted sender) smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from gabe.freedesktop.org (gabe.freedesktop.org. [2610:10:20:722:a800:ff:fe36:1795]) by mx.google.com with ESMTPS id y1si43126903pgg.490.2019.08.05.07.34.04 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 05 Aug 2019 07:34:04 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 2610:10:20:722:a800:ff:fe36:1795 as permitted sender) client-ip=2610:10:20:722:a800:ff:fe36:1795; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 2610:10:20:722:a800:ff:fe36:1795 as permitted sender) smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 684C689E2C; Mon, 5 Aug 2019 14:34:03 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-io1-f68.google.com (mail-io1-f68.google.com [209.85.166.68]) by gabe.freedesktop.org (Postfix) with ESMTPS id 127E589E43 for ; Mon, 5 Aug 2019 14:34:02 +0000 (UTC) Received: by mail-io1-f68.google.com with SMTP id j6so68766ioa.5 for ; Mon, 05 Aug 2019 07:34:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=uSH6HC+fPKJWaHpFvpD9jlBMJi/IsTvSr92waspiYr4=; b=UjxK6nAJ4xsZ3tyjlQ8ZAxszU9h/07YKJJpNrk0EsgtBxbP7w3eq9LGqn2nMrzWkhS FOiX0b2GisqRq6cLckVygdOLrlpJphbsdtgmLQQqX1rIhSoQKqD0vyCrSExDR20BnXR6 D8VWqam5WC/XFkkKqqXdIeJqH43PuBr7GiVwKaMd2MjBwRGcWyxVJAdFdE8Bv/6u9HUs lebuuVl9+6Upm2DsMdEVOXuqviXhX+yxc5xpuXicZnLiJOAud4QGmi72PTuUsilahMwZ NTxzVdJjGdDchckOs22jOaO3sOeQdkltbFvj5bM11hnkpA1ftjo+DUuoudJcSJ+yLECO JwSw== X-Gm-Message-State: APjAAAX8HFt3a/Azav+l4/UmGTvZ556cOeudthQuUseBCJggXATCjQ+4 sYvL/cjRgyOltE/ZvMYbY5r/I8U= X-Received: by 2002:a02:c916:: with SMTP id t22mr16139364jao.24.1565015640689; Mon, 05 Aug 2019 07:34:00 -0700 (PDT) Received: from xps15.herring.priv ([64.188.179.254]) by smtp.googlemail.com with ESMTPSA id y5sm87918337ioc.86.2019.08.05.07.33.58 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 05 Aug 2019 07:33:59 -0700 (PDT) From: Rob Herring To: dri-devel@lists.freedesktop.org Subject: [PATCH 1/2] drm/shmem: Add madvise state and purge helpers Date: Mon, 5 Aug 2019 08:33:57 -0600 Message-Id: <20190805143358.21245-1-robh@kernel.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 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: Rob Clark , Tomeu Vizoso , Maxime Ripard , David Airlie , Steven Price , Alyssa Rosenzweig , Sean Paul Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Add support to the shmem GEM helpers for tracking madvise state and purging pages. This is based on the msm implementation. The BO provides a list_head, but the list management is handled outside of the shmem helpers as there are different locking requirements. Cc: Tomeu Vizoso Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Sean Paul Cc: David Airlie Cc: Daniel Vetter Cc: Eric Anholt Signed-off-by: Rob Herring --- drivers/gpu/drm/drm_gem_shmem_helper.c | 57 ++++++++++++++++++++++++++ include/drm/drm_gem_shmem_helper.h | 15 +++++++ 2 files changed, 72 insertions(+) diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c index 2f64667ac805..4b442576de1c 100644 --- a/drivers/gpu/drm/drm_gem_shmem_helper.c +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c @@ -75,6 +75,7 @@ struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t shmem = to_drm_gem_shmem_obj(obj); mutex_init(&shmem->pages_lock); mutex_init(&shmem->vmap_lock); + INIT_LIST_HEAD(&shmem->madv_list); /* * Our buffers are kept pinned, so allocating them @@ -362,6 +363,62 @@ drm_gem_shmem_create_with_handle(struct drm_file *file_priv, } EXPORT_SYMBOL(drm_gem_shmem_create_with_handle); +/* Update madvise status, returns true if not purged, else + * false or -errno. + */ +int drm_gem_shmem_madvise(struct drm_gem_object *obj, int madv) +{ + struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); + + mutex_lock(&shmem->pages_lock); + + if (shmem->madv >= 0) + shmem->madv = madv; + + madv = shmem->madv; + + mutex_unlock(&shmem->pages_lock); + + return (madv >= 0); +} +EXPORT_SYMBOL(drm_gem_shmem_madvise); + +void drm_gem_shmem_purge_locked(struct drm_gem_object *obj) +{ + struct drm_device *dev = obj->dev; + struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); + + WARN_ON(!drm_gem_shmem_is_purgeable(shmem)); + + drm_gem_shmem_put_pages_locked(shmem); + + shmem->madv = -1; + + drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping); + drm_gem_free_mmap_offset(obj); + + /* Our goal here is to return as much of the memory as + * is possible back to the system as we are called from OOM. + * To do this we must instruct the shmfs to drop all of its + * backing pages, *now*. + */ + shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1); + + invalidate_mapping_pages(file_inode(obj->filp)->i_mapping, + 0, (loff_t)-1); +} +EXPORT_SYMBOL(drm_gem_shmem_purge_locked); + +void drm_gem_shmem_purge(struct drm_gem_object *obj) +{ + struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); + + mutex_lock(&shmem->pages_lock); + drm_gem_shmem_purge_locked(obj); + mutex_unlock(&shmem->pages_lock); +} +EXPORT_SYMBOL(drm_gem_shmem_purge); + /** * drm_gem_shmem_dumb_create - Create a dumb shmem buffer object * @file: DRM file structure to create the dumb buffer for diff --git a/include/drm/drm_gem_shmem_helper.h b/include/drm/drm_gem_shmem_helper.h index 038b6d313447..ce1600fdfc3e 100644 --- a/include/drm/drm_gem_shmem_helper.h +++ b/include/drm/drm_gem_shmem_helper.h @@ -44,6 +44,9 @@ struct drm_gem_shmem_object { */ unsigned int pages_use_count; + int madv; + struct list_head madv_list; + /** * @pages_mark_dirty_on_put: * @@ -121,6 +124,18 @@ void drm_gem_shmem_unpin(struct drm_gem_object *obj); void *drm_gem_shmem_vmap(struct drm_gem_object *obj); void drm_gem_shmem_vunmap(struct drm_gem_object *obj, void *vaddr); +int drm_gem_shmem_madvise(struct drm_gem_object *obj, int madv); + +static inline bool drm_gem_shmem_is_purgeable(struct drm_gem_shmem_object *shmem) +{ + return (shmem->madv > 0) && + !shmem->vmap_use_count && shmem->sgt && + !shmem->base.dma_buf && !shmem->base.import_attach; +} + +void drm_gem_shmem_purge_locked(struct drm_gem_object *obj); +void drm_gem_shmem_purge(struct drm_gem_object *obj); + struct drm_gem_shmem_object * drm_gem_shmem_create_with_handle(struct drm_file *file_priv, struct drm_device *dev, size_t size,