From patchwork Thu Feb 22 10:10:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "He, Hongbo" X-Patchwork-Id: 10234977 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 466DC602DC for ; Thu, 22 Feb 2018 10:11:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3F55F28B27 for ; Thu, 22 Feb 2018 10:11:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3394028B2B; Thu, 22 Feb 2018 10:11:06 +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=-4.1 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID 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 1E0BB28B27 for ; Thu, 22 Feb 2018 10:11:04 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 416336ECB8; Thu, 22 Feb 2018 10:11:02 +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-sn1nam01on0066.outbound.protection.outlook.com [104.47.32.66]) by gabe.freedesktop.org (Postfix) with ESMTPS id E52966ECAC for ; Thu, 22 Feb 2018 10:11:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=Xtly9UgKJ6qLO9s7MrTi1wBpGnL3Zlbmak/OrGwbNGM=; b=VRNaJbQDf05c8wI9XbJspgiIF5XuPdDf5IiCB2cISeq4hRZcWk/SArTqL57m+tXxNrX3NMe12auD9C0CxJ69ihviae3vl8Q3FIO0kgcOgJE3Unx3gcWIcpraWIZnw9rmDsIDvuY2JW2O0FL7bbxHVPzD4yL6LTEb7XfpJ4fb4hI= Received: from DM3PR12CA0111.namprd12.prod.outlook.com (10.161.150.159) by BY2PR12MB0259.namprd12.prod.outlook.com (10.162.83.153) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.506.18; Thu, 22 Feb 2018 10:10:57 +0000 Received: from BY2NAM03FT042.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e4a::207) by DM3PR12CA0111.outlook.office365.com (2603:10b6:0:55::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.527.15 via Frontend Transport; Thu, 22 Feb 2018 10:10:57 +0000 Authentication-Results: spf=none (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; lists.freedesktop.org; dkim=none (message not signed) header.d=none;lists.freedesktop.org; dmarc=permerror action=none header.from=amd.com; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV01.amd.com (165.204.84.17) by BY2NAM03FT042.mail.protection.outlook.com (10.152.85.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.506.19 via Frontend Transport; Thu, 22 Feb 2018 10:10:56 +0000 Received: from roger-build-server.amd.com (10.34.1.3) by SATLEXCHOV01.amd.com (10.181.40.71) with Microsoft SMTP Server id 14.3.382.0; Thu, 22 Feb 2018 04:10:55 -0600 From: Roger He To: Subject: [PATCH] drm/ttm: check if free mem space is under the lower limit Date: Thu, 22 Feb 2018 18:10:51 +0800 Message-ID: <1519294251-2297-1-git-send-email-Hongbo.He@amd.com> X-Mailer: git-send-email 2.7.4 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)(39380400002)(376002)(2980300002)(428003)(189003)(199004)(6666003)(6916009)(53936002)(54906003)(68736007)(81166006)(81156014)(575784001)(47776003)(106466001)(53416004)(86362001)(16586007)(36756003)(104016004)(2351001)(50466002)(7696005)(305945005)(8936002)(356003)(1857600001)(48376002)(97736004)(51416003)(316002)(105586002)(4326008)(478600001)(8676002)(77096007)(336011)(2906002)(26005)(186003)(72206003)(59450400001)(5660300001)(50226002); DIR:OUT; SFP:1101; SCL:1; SRVR:BY2PR12MB0259; H:SATLEXCHOV01.amd.com; FPR:; SPF:None; PTR:InfoDomainNonexistent; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BY2NAM03FT042; 1:8GcgxxxKQ8fMwSlZKPdaiduYehc9GeaiJ2FbRV+GtFSsXOJAKFmiBcwjDGuGTd+6579mlrp36h8NWVp1Iv71arf7S9GmKIxT8l0vpr/heUOkyUfuNgJbEb4zxyU0ANeB X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 34d7f027-efd6-4db2-9f0b-08d579dc9050 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(4534165)(4627221)(201703031133081)(201702281549075)(5600026)(4604075)(2017052603307)(7153060); SRVR:BY2PR12MB0259; X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0259; 3:PRl3M7QRAd+u6k0rd5EX8mensC/ux/aFH/nmRj1PXGWIvoEJwqNLVi41F9nIp+PIC5xogqyYGpyBV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hUizVR8M9JI7dFM5B4+jasBUo4qcRrXZZAQO2o3pFhDuzofGwldSwuD2JPGtBBW/QJEEaOTIg85czyb1TTDePbN6Qcz1QG3kq+XbB9meuMgHI8dEUk2hT6njKg3ZOn7H4ejC5t1iFxC8jvRSksqpyqm9IVZe+Bw9Yku8dRyG7Au2SLhQ+XFFkpk5BvF6N+qCdXKI8a8Ju3h6Lz7EPP6Gzns3Q68aVqf6EfkzIQQpFX0FVUT9/LEfVn1/tdjJm748vUZQJ+gRe61vrRJ29sFNnF+oYwwLMoDgF+3YKENQ2TJF3bWqwjUAOx1C0c31CF2hKpnErrm5BAYQlk5+mS19dP9bgn0i60bfa94GRKO5g8k+tuHz4IjHgi6Pzj2JZxWzarsj8E2XQ4Qo3T X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(8211001078)(6040501)(2401047)(5005006)(8121501046)(3231101)(944501161)(93006095)(93003095)(3002001)(10201501046)(6055026)(6041288)(20161123560045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123564045)(6072148)(201708071742011); SRVR:BY2PR12MB0259; BCL:0; PCL:0; RULEID:; SRVR:BY2PR12MB0259; X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0259; 4:F3cZD+oiAdHz7kfdU4bHaTAzXlmpQwg/dfWJbC26siqXom27uJy5x5GHwWMljrxA6kUwBLWSHDCqif2lO1gqbq1pdWbujOG8eguwkZ+8NDz+Vw/Teb2T5VwF63IimCUWv2amJ4/06qSz4NNfXRnWnNmwpqc/X2QCTcauay08bZI5aJJJIhMyuLFok0/Rxo8PtfRL6H4qHhoYO1Q1D8iWl2RUEVzp0b/Z6iZZE/J5g0BxPV9l3bONibVICHseNdXl9kCzypn/Xgd3ZLpv+w/f+nEu//OaGMBI+m5mUhUZXR0drDTp3MHc0kXl8tuA4pEK X-Forefront-PRVS: 059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5k=; 7:68ogSg2SidRwwFMLXwJHpoCxZ4sLEp/l/MQEZhz/kl+3hl111eBn+Oo3wwDhrPJoawOgeKlOElGjO1FO7s62ECeCSwInpJDWVqvPqG3HVSqUnyfSXPxzfm7if10Eqi69yK9a3Fc0mRxtIglZd4MB0DNBd6PEwh+UQGIhG4aI5nX+FMFuvA8BzjMxtiCEvkEWmGg23M030Cwc5BlzVLlxxcrOWhjl+qKZFozejZKGEcCNnmHb6XUGfPylD7IUvWqP SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0259; 20:kjdh0nLmZ6990FvgzRDGFYfnYCW5oplcC4UiRyymzZNlM24TZ53AsHD8fk/DeiMXmAYoaCWaW7Of00tDOthubJG4bJPH16/2fOHn5xO+208NJk/MsqMyTnbj3LtJUM0mx5WtYrOZQMahKWAbFNlSwh11O8a8zNKDTSZffd/8fobwxvzuIxt3i+f7mTlGWMh+gfwXnwbiWf7DpRkRViH/cBbIXY9w1VdwFiSYm8hRvTw5rLnoSefrU2O6vAcUcjoe X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2018 10:10:56.2043 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 34d7f027-efd6-4db2-9f0b-08d579dc9050 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=[SATLEXCHOV01.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR12MB0259 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: Roger He , Christian.Koenig@amd.com Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP the free mem space and the lower limit both include two parts: system memory and swap space. For the OOM triggered by TTM, that is the case as below: first swap space is full of swapped out pages and soon system memory also is filled up with ttm pages. and then any memory allocation request will run into OOM. to cover two cases: a. if no swap disk at all or free swap space is under swap mem limit but available system mem is bigger than sys mem limit, allow TTM allocation; b. if the available system mem is less than sys mem limit but free swap space is bigger than swap mem limit, allow TTM allocation. v2: merge two memory limit(swap and system) into one v3: keep original behavior except ttm_opt_ctx->flags with TTM_OPT_FLAG_FORCE_ALLOC v4: always set force_alloc as tx->flags & TTM_OPT_FLAG_FORCE_ALLOC v5: add an attribute for lower_mem_limit Signed-off-by: Roger He --- drivers/gpu/drm/ttm/ttm_memory.c | 94 ++++++++++++++++++++++++++++++++ drivers/gpu/drm/ttm/ttm_page_alloc.c | 3 + drivers/gpu/drm/ttm/ttm_page_alloc_dma.c | 3 + include/drm/ttm/ttm_memory.h | 5 ++ 4 files changed, 105 insertions(+) diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/ttm/ttm_memory.c index aa0c381..d015e39 100644 --- a/drivers/gpu/drm/ttm/ttm_memory.c +++ b/drivers/gpu/drm/ttm/ttm_memory.c @@ -36,6 +36,7 @@ #include #include #include +#include #define TTM_MEMORY_ALLOC_RETRIES 4 @@ -166,6 +167,52 @@ static struct kobj_type ttm_mem_zone_kobj_type = { .default_attrs = ttm_mem_zone_attrs, }; +static struct attribute ttm_mem_global_lower_mem_limit = { + .name = "lower_mem_limit", + .mode = S_IRUGO | S_IWUSR +}; + +static ssize_t ttm_mem_global_show(struct kobject *kobj, + struct attribute *attr, + char *buffer) +{ + struct ttm_mem_global *glob = + container_of(kobj, struct ttm_mem_global, kobj); + uint64_t val = 0; + + spin_lock(&glob->lock); + val = glob->lower_mem_limit; + spin_unlock(&glob->lock); + + return snprintf(buffer, PAGE_SIZE, "%llu\n", + (unsigned long long) val << 2); +} + +static ssize_t ttm_mem_global_store(struct kobject *kobj, + struct attribute *attr, + const char *buffer, + size_t size) +{ + int chars; + uint64_t val64; + unsigned long val; + struct ttm_mem_global *glob = + container_of(kobj, struct ttm_mem_global, kobj); + + chars = sscanf(buffer, "%lu", &val); + if (chars == 0) + return size; + + val64 = val; + val64 >>= 2; + + spin_lock(&glob->lock); + glob->lower_mem_limit = val64; + spin_unlock(&glob->lock); + + return size; +} + static void ttm_mem_global_kobj_release(struct kobject *kobj) { struct ttm_mem_global *glob = @@ -174,8 +221,20 @@ static void ttm_mem_global_kobj_release(struct kobject *kobj) kfree(glob); } +static struct attribute *ttm_mem_global_attrs[] = { + &ttm_mem_global_lower_mem_limit, + NULL +}; + +static const struct sysfs_ops ttm_mem_global_ops = { + .show = &ttm_mem_global_show, + .store = &ttm_mem_global_store, +}; + static struct kobj_type ttm_mem_glob_kobj_type = { .release = &ttm_mem_global_kobj_release, + .sysfs_ops = &ttm_mem_global_ops, + .default_attrs = ttm_mem_global_attrs, }; static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob, @@ -375,6 +434,11 @@ int ttm_mem_global_init(struct ttm_mem_global *glob) si_meminfo(&si); + /* lower limit of swap space and 256MB is enough */ + glob->lower_mem_limit = 256 << 8; + /* lower limit of ram and keep consistent with each zone->emer_mem */ + glob->lower_mem_limit += si.totalram >> 2; + ret = ttm_mem_init_kernel_zone(glob, &si); if (unlikely(ret != 0)) goto out_no_zone; @@ -469,6 +533,36 @@ void ttm_mem_global_free(struct ttm_mem_global *glob, } EXPORT_SYMBOL(ttm_mem_global_free); +/* + * check if the available mem is under lower memory limit + * + * a. if no swap disk at all or free swap space is under swap_mem_limit + * but available system mem is bigger than sys_mem_limit, allow TTM + * allocation; + * + * b. if the available system mem is less than sys_mem_limit but free + * swap disk is bigger than swap_mem_limit, allow TTM allocation. + */ +bool +ttm_check_under_lowerlimit(struct ttm_mem_global *glob, + uint64_t num_pages, + struct ttm_operation_ctx *ctx) +{ + bool ret = false; + int64_t available; + + if (ctx->flags & TTM_OPT_FLAG_FORCE_ALLOC) + return false; + + available = get_nr_swap_pages() + si_mem_available(); + available -= num_pages; + if (available < glob->lower_mem_limit) + ret = true; + + return ret; +} +EXPORT_SYMBOL(ttm_check_under_lowerlimit); + static int ttm_mem_global_reserve(struct ttm_mem_global *glob, struct ttm_mem_zone *single_zone, uint64_t amount, bool reserve) diff --git a/drivers/gpu/drm/ttm/ttm_page_alloc.c b/drivers/gpu/drm/ttm/ttm_page_alloc.c index 5edcd89..c9d8903 100644 --- a/drivers/gpu/drm/ttm/ttm_page_alloc.c +++ b/drivers/gpu/drm/ttm/ttm_page_alloc.c @@ -1100,6 +1100,9 @@ int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) if (ttm->state != tt_unpopulated) return 0; + if (ttm_check_under_lowerlimit(mem_glob, ttm->num_pages, ctx)) + return -ENOMEM; + ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags, ttm->caching_state); if (unlikely(ret != 0)) { diff --git a/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c b/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c index b122f6e..37e03d9 100644 --- a/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +++ b/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c @@ -940,6 +940,9 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev, if (ttm->state != tt_unpopulated) return 0; + if (ttm_check_under_lowerlimit(mem_glob, num_pages, ctx)) + return -ENOMEM; + INIT_LIST_HEAD(&ttm_dma->pages_list); i = 0; diff --git a/include/drm/ttm/ttm_memory.h b/include/drm/ttm/ttm_memory.h index 8936285..737b5fe 100644 --- a/include/drm/ttm/ttm_memory.h +++ b/include/drm/ttm/ttm_memory.h @@ -49,6 +49,8 @@ * @work: The workqueue callback for the shrink queue. * @lock: Lock to protect the @shrink - and the memory accounting members, * that is, essentially the whole structure with some exceptions. + * @lower_mem_limit: include lower limit of swap space and lower limit of + * system memory. * @zones: Array of pointers to accounting zones. * @num_zones: Number of populated entries in the @zones array. * @zone_kernel: Pointer to the kernel zone. @@ -67,6 +69,7 @@ struct ttm_mem_global { struct workqueue_struct *swap_queue; struct work_struct work; spinlock_t lock; + uint64_t lower_mem_limit; struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES]; unsigned int num_zones; struct ttm_mem_zone *zone_kernel; @@ -90,4 +93,6 @@ extern void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page, uint64_t size); extern size_t ttm_round_pot(size_t size); extern uint64_t ttm_get_kernel_zone_memory_size(struct ttm_mem_global *glob); +extern bool ttm_check_under_lowerlimit(struct ttm_mem_global *glob, + uint64_t num_pages, struct ttm_operation_ctx *ctx); #endif