From patchwork Thu Feb 8 09:06:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "He, Hongbo" X-Patchwork-Id: 10206767 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 CFC1660327 for ; Thu, 8 Feb 2018 09:07:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C4DEF29359 for ; Thu, 8 Feb 2018 09:07:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B926029424; Thu, 8 Feb 2018 09:07:55 +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 1E0DC29359 for ; Thu, 8 Feb 2018 09:07:55 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9CF006E4E8; Thu, 8 Feb 2018 09:06:45 +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-sn1nam01on0086.outbound.protection.outlook.com [104.47.32.86]) by gabe.freedesktop.org (Postfix) with ESMTPS id 74BBF6E503; Thu, 8 Feb 2018 09:06:44 +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=beGF66BpepJk1/pX/OtpYjhetjZKMjj3YEfPDSt8LKk=; b=Nwx2hrOhOi86Ofa+XhkUArgravLH75HcLA1nILDtsAADfAm//UQjfOZI7j5byMbtgCj2u9AfaAFCtbCxpeWJ4smQc8bKeVzFL4TEQ8okbcfwQu/OcZuP4uCVJJZ49U7iE+/9LyIJnRA9YRV1EwVKe6Sh3GTpi6s22ZteKQpa6JM= Received: from CY4PR12CA0030.namprd12.prod.outlook.com (10.175.82.144) by SN1PR12MB0368.namprd12.prod.outlook.com (10.162.4.22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.464.11; Thu, 8 Feb 2018 09:06:42 +0000 Received: from CO1NAM03FT009.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e48::207) by CY4PR12CA0030.outlook.office365.com (2603:10b6:903:129::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.485.10 via Frontend Transport; Thu, 8 Feb 2018 09:06:41 +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 CO1NAM03FT009.mail.protection.outlook.com (10.152.80.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.20.485.12 via Frontend Transport; Thu, 8 Feb 2018 09:06:41 +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.361.1; Thu, 8 Feb 2018 03:06:33 -0600 From: Roger He To: , Subject: [PATCH 4/4] drm/ttm: check if the mem free space is under lower limit Date: Thu, 8 Feb 2018 17:06:01 +0800 Message-ID: <1518080761-12952-4-git-send-email-Hongbo.He@amd.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1518080761-12952-1-git-send-email-Hongbo.He@amd.com> References: <1518080761-12952-1-git-send-email-Hongbo.He@amd.com> 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)(979002)(39380400002)(396003)(346002)(39860400002)(376002)(2980300002)(428003)(199004)(189003)(81166006)(316002)(16586007)(36756003)(4326008)(110136005)(336011)(77096007)(106466001)(81156014)(72206003)(478600001)(50226002)(8676002)(186003)(26005)(76176011)(575784001)(53936002)(7696005)(54906003)(86362001)(105586002)(97736004)(51416003)(104016004)(356003)(47776003)(450100002)(59450400001)(68736007)(305945005)(8936002)(48376002)(50466002)(53416004)(5660300001)(2906002)(6666003)(2950100002)(969003)(989001)(999001)(1009001)(1019001); DIR:OUT; SFP:1101; SCL:1; SRVR:SN1PR12MB0368; H:SATLEXCHOV01.amd.com; FPR:; SPF:None; PTR:InfoDomainNonexistent; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; CO1NAM03FT009; 1:gxtBCOHm+bQ4gzC4UCJBtgI9ultLpWBDO3ACg09agiUQZ6DOjMo/97jakub9AcV8CI5LjEB9z1bJrHkq9itNx6MhoTZwWKaFdGX8rHA/RK+ycZbFvKkOoOPkop9MGFT0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f22b0788-61f9-4a37-1b8f-08d56ed344f2 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(5600026)(4604075)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603307)(7153060); SRVR:SN1PR12MB0368; X-Microsoft-Exchange-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XGL1Iq3z94JXSpaCLuQWLqtEpb+UXmRngA9XrQ0lXcoKFTqwmloYczNM2RTAVL6SJ8MdDO28dO6gaBlM5hUhJOcjRJ9AxdmcmzhWS/9bquxmYM3dz2nObyJ3MJ73X4f0Y9ERIk3MKYjwck1Lk4CYXL4gYGNxLVPwnj1xzebw0MKv/AObbrFZgVKYsXEhuptim9jUp814CWLjHz8tYlAerTTxi2avXQUoaBunmfGi30KQsrM4WRCpUacWPRKJyPDRxPtc2DJO+CMMVnQ0Dzx/34jEVH7Yd1/OLB2iwjCGFT7crOp4zIlYg6yocon+FGXn+F/OUxGrgbgxvHILNzTmsv4o5YKpvhMx+ijkjhOMTKTalHdjMeA3xJ6WWhg1E66wPqL/Ub9iUoQ85f2RrHiUCl6nJ6Aj04n9q1Cm09wZAPtpCEqnjEBHxVspdH65sjmUUnuVH1CmXZl3p3mhWSgmph9ET6H6hT9Gfzn698Wpz+6sw15Xcs X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040501)(2401047)(5005006)(8121501046)(10201501046)(93006095)(93003095)(3231101)(2400082)(944501161)(3002001)(6055026)(6041288)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(20161123558120)(20161123562045)(20161123560045)(6072148)(201708071742011); SRVR:SN1PR12MB0368; BCL:0; PCL:0; RULEID:; SRVR:SN1PR12MB0368; X-Microsoft-Exchange-Diagnostics: 1; SN1PR12MB0368; 4:/VQTHVMSvjjZ1W5ZflI3Wn6lvombmBIqG93RuKAH1AbgBFYOrf97Wp6hnjCyQw5RFVwyLCT/VUqHa/TsSdVgdvdwYTU517lpiYGXyu9ugaJJ7JvXP3v/dF9sBFkBbg1wbuq6UMwRVJ4VOf1ikcbH+flro0GDn1A+41uUIz+6dnoNNJ0zYAcNoL5tca9Wv9ILQwsJeUz6VVoKb2E7AtAghPM+oY5mYsodhe8/fkoJ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ppBfXRVapKNTWs6iYtduBlvuLqF/4D4T3NtDJmB/vlrPt3NAYOfVyQks5ZcG5pdY1sX8xeJmlZbj89M+lkltnwNryB63cheRQDoy25iWHIl63QXyl/ZrB32G/iQF12pynOdKdNXdcexY5tW3mv5RLvN6SOK1s=; 24:Z6xXV3tNocTpBdLu7sn+aCxHv0cAdYMd0Im7ZqFWditHsnT/I+qupG71Mtc/IsMohV0ac8ZJSCiTQlrUGQ6cbVBI206OPrIi2T5cX5brNzA=; 7:sXEjaIh33vBaL7riaoIswGj9ZU2R2nr5ysWz6N8wQq09+gTmJCOIm39JW0r0RbXK/O9PieymPNJEF6mKJXtD++UT9TO4aYur35z4agpljvbwyoLvFQjYvaWf+20mETKGdGHRP3sbt3RqoS2Sz1wKXQXPbKoogclOW6BtS7CLtCLQPDDtrwy5VHwpF63LGsxCRzQLm4NSJsqcva16TUDL9LK6kO6jSYvBlnvHdw13p5MwE4fbrl9pgL+uN/MQEwwn SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; SN1PR12MB0368; 20:5O/VuZde8oRU8s9Ng+i2WTW+qTOQ3XpxY8tQjxQzL3cQ8gFhQHNgEW5Z92v0o7qkq79FhvU29WuKLl14NY8zdJIELIJh/ko8mH2kjrgTXSkwzXg59SCHQIqWaq9egQmhR+8D+Mm5CRldhWCvksIi/kBn6LcLH6gEYsXOCgsmTB2ljbgMAPNecu6j+XDeaF70IlasKQrdjdmD4rb23SJA7Gavh8fvatxiQLutrgpzdaz/j4dCYxzncFN/ZcEe8cKt X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Feb 2018 09:06:41.4790 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f22b0788-61f9-4a37-1b8f-08d56ed344f2 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: SN1PR12MB0368 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 mem free space and lower limit both include two parts: system memory and swap space. For the OOM triggered by TTM, that is the case as below: swap space is full of swapped pages and then system memory will be 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 with ttm->page_flags TTM_PAGE_FLAG_NO_RETRY Signed-off-by: Roger He --- drivers/gpu/drm/ttm/ttm_memory.c | 34 ++++++++++++++++++++++++++++++++ drivers/gpu/drm/ttm/ttm_page_alloc.c | 22 ++++++++++++++------- drivers/gpu/drm/ttm/ttm_page_alloc_dma.c | 31 ++++++++++++++++++----------- include/drm/ttm/ttm_memory.h | 5 +++++ 4 files changed, 73 insertions(+), 19 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/ttm/ttm_memory.c index aa0c381..16ab324 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 @@ -375,6 +376,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 +475,34 @@ void ttm_mem_global_free(struct ttm_mem_global *glob, } EXPORT_SYMBOL(ttm_mem_global_free); +/* + * check if the available mem is under total 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, bool force_alloc) +{ + bool ret = false; + uint64_t available; + + /* always allow allocation, e.g. when serving page fault or suspend */ + if (force_alloc) + return false; + + available = get_nr_swap_pages() + si_mem_available(); + 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..1457a1c 100644 --- a/drivers/gpu/drm/ttm/ttm_page_alloc.c +++ b/drivers/gpu/drm/ttm/ttm_page_alloc.c @@ -1094,7 +1094,8 @@ ttm_pool_unpopulate_helper(struct ttm_tt *ttm, unsigned mem_count_update) int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) { struct ttm_mem_global *mem_glob = ttm->glob->mem_glob; - unsigned i; + unsigned i, unpopulate_count = 0; + bool force_alloc = true; int ret; if (ttm->state != tt_unpopulated) @@ -1102,17 +1103,20 @@ int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags, ttm->caching_state); - if (unlikely(ret != 0)) { - ttm_pool_unpopulate_helper(ttm, 0); - return ret; - } + if (unlikely(ret != 0)) + goto error_populate; + + if (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY) + force_alloc = ctx->flags & TTM_OPT_FLAG_FORCE_ALLOC; + if (ttm_check_under_lowerlimit(mem_glob, force_alloc)) + goto error_populate; for (i = 0; i < ttm->num_pages; ++i) { ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i], PAGE_SIZE, ctx); if (unlikely(ret != 0)) { - ttm_pool_unpopulate_helper(ttm, i); - return -ENOMEM; + unpopulate_count = i; + goto error_populate; } } @@ -1126,6 +1130,10 @@ int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) ttm->state = tt_unbound; return 0; + +error_populate: + ttm_pool_unpopulate_helper(ttm, unpopulate_count); + return -ENOMEM; } EXPORT_SYMBOL(ttm_pool_populate); diff --git a/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c b/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c index 354e0e1..39b2a42 100644 --- a/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +++ b/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c @@ -934,6 +934,7 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev, struct dma_pool *pool; struct dma_page *d_page; enum pool_type type; + bool force_alloc = true; unsigned i; int ret; @@ -943,6 +944,8 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev, INIT_LIST_HEAD(&ttm_dma->pages_list); i = 0; + if (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY) + force_alloc = ctx->flags & TTM_OPT_FLAG_FORCE_ALLOC; type = ttm_to_type(ttm->page_flags, ttm->caching_state); #ifdef CONFIG_TRANSPARENT_HUGEPAGE if (ttm->page_flags & TTM_PAGE_FLAG_DMA32) @@ -964,12 +967,13 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev, if (!d_page) break; + if (ttm_check_under_lowerlimit(mem_glob, force_alloc)) + goto error_populate; + ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i], pool->size, ctx); - if (unlikely(ret != 0)) { - ttm_dma_unpopulate(ttm_dma, dev); - return -ENOMEM; - } + if (unlikely(ret != 0)) + goto error_populate; d_page->vaddr |= VADDR_FLAG_UPDATED_COUNT; for (j = i + 1; j < (i + HPAGE_PMD_NR); ++j) { @@ -996,17 +1000,16 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev, while (num_pages) { d_page = ttm_dma_pool_get_pages(pool, ttm_dma, i); - if (!d_page) { - ttm_dma_unpopulate(ttm_dma, dev); - return -ENOMEM; - } + if (!d_page) + goto error_populate; + + if (ttm_check_under_lowerlimit(mem_glob, force_alloc)) + goto error_populate; ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i], pool->size, ctx); - if (unlikely(ret != 0)) { - ttm_dma_unpopulate(ttm_dma, dev); - return -ENOMEM; - } + if (unlikely(ret != 0)) + goto error_populate; d_page->vaddr |= VADDR_FLAG_UPDATED_COUNT; ++i; @@ -1023,6 +1026,10 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev, ttm->state = tt_unbound; return 0; + +error_populate: + ttm_dma_unpopulate(ttm_dma, dev); + return -ENOMEM; } EXPORT_SYMBOL_GPL(ttm_dma_populate); diff --git a/include/drm/ttm/ttm_memory.h b/include/drm/ttm/ttm_memory.h index 8936285..3aa30d6 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, + bool force_alloc); #endif