From patchwork Fri Feb 23 10:19:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "He, Hongbo" X-Patchwork-Id: 10237257 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 E2AA0602A0 for ; Fri, 23 Feb 2018 10:19:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C3AD529086 for ; Fri, 23 Feb 2018 10:19:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B88602950C; Fri, 23 Feb 2018 10:19:49 +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 2468729086 for ; Fri, 23 Feb 2018 10:19:49 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B152C6F0A1; Fri, 23 Feb 2018 10:19:46 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from NAM01-BN3-obe.outbound.protection.outlook.com (mail-bn3nam01on0064.outbound.protection.outlook.com [104.47.33.64]) by gabe.freedesktop.org (Postfix) with ESMTPS id B90B16E0D6 for ; Fri, 23 Feb 2018 10:19: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=PtsPHcvrGwSB4RKdW+o6YvVkbxmGzpeygPuMgvQlPv4=; b=AXhgVc78/ohn+2u9srK9WIhD/whzq4mOAhIr3YyemvYBVRynAFGp8vZCH9fLzfAYgDDi/YkJKX7OM5ntAQb+ThfDzwNc8/HG4Z1FIv0k0CyX4ZG6Mp9RfOTl9WuMODrWHLYwXlKGqbAc41XEgABlGe29sGZUKnl5rK2hWCKgIhI= Received: from MWHPR1201CA0005.namprd12.prod.outlook.com (10.174.253.15) by BY2PR12MB0258.namprd12.prod.outlook.com (10.162.83.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.506.18; Fri, 23 Feb 2018 10:19:42 +0000 Received: from BY2NAM03FT005.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e4a::204) by MWHPR1201CA0005.outlook.office365.com (2603:10b6:301:4a::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.527.15 via Frontend Transport; Fri, 23 Feb 2018 10:19: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 SATLEXCHOV02.amd.com (165.204.84.17) by BY2NAM03FT005.mail.protection.outlook.com (10.152.84.94) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.20.527.18 via Frontend Transport; Fri, 23 Feb 2018 10:19:41 +0000 Received: from roger-build-server.amd.com (10.34.1.3) by SATLEXCHOV02.amd.com (10.181.40.72) with Microsoft SMTP Server id 14.3.382.0; Fri, 23 Feb 2018 04:19:40 -0600 From: Roger He To: Subject: [PATCH] drm/ttm: check if free mem space is under the lower limit Date: Fri, 23 Feb 2018 18:19:36 +0800 Message-ID: <1519381176-28584-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)(376002)(39860400002)(346002)(39380400002)(396003)(2980300002)(428003)(199004)(189003)(16586007)(575784001)(356003)(68736007)(77096007)(59450400001)(36756003)(53936002)(2906002)(7696005)(106466001)(53416004)(305945005)(8676002)(186003)(316002)(478600001)(81156014)(336011)(8936002)(2351001)(5660300001)(72206003)(6666003)(47776003)(50226002)(6916009)(97736004)(81166006)(86362001)(4326008)(51416003)(26005)(48376002)(105586002)(1857600001)(50466002)(54906003)(104016004); DIR:OUT; SFP:1101; SCL:1; SRVR:BY2PR12MB0258; H:SATLEXCHOV02.amd.com; FPR:; SPF:None; PTR:InfoDomainNonexistent; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BY2NAM03FT005; 1:VEjS9hLQgow+2XYiufV1u2JtXEn/3OoJXP7RgFuYNJgfRZuyjTua93bPkJuCA4uDg9bBWiPk9+vfoXkB/Pnmu9ADyE1IUyUFxrWY119X1yTp8UIOFBRtlTxIuiCYBUiZ X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ba4806a5-ddba-4475-d049-08d57aa6f3c4 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(4534165)(4627221)(201703031133081)(201702281549075)(5600026)(4604075)(2017052603307)(7153060); SRVR:BY2PR12MB0258; X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0258; 3:Em8dlMls92/wGMzuUZlFIMbhrnVXfBTogCu2BR0LiH1Nj/cTVr0r1M54Nu86SRteU0qo+Q9ZQG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PKi0tvRh6b/N8OZPTdKjmRElabTEYXGiIitPLOmu5HaW9WfG7YYbBqOnlDxM43gX6NtgW47VlOaKhr8INF4DpMYlaEU3ZgwKcZlfRYbnZMume8zbJDZnw579dsSzT9+XNvZVawOn6Fy1l75ORS/1W7iMgDTRwTwGHoOfYK1py/7csXToLfs19C8+nRAu1GwVyfGC+i3OFqWB8quWykx5feiLSjQLW98OkkN6mbXJxHtzssI2DHcn/WvB17bwiIfle30dfZ6/tJOxdp3qmvGx9HS9gPYHTrDny0Bn+xhqjMbJ/P74mde2l6Hpswx4sXmuCla/uKQhXQza05iNY7w7zvoNMe0nXAA0YngaKzfxNMIkUGkcgUBBfz2Xxn1ZvZ0CQ1DE9T2FnxYhx5Eoaf22wnAu2jWrc X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(8211001083)(6040501)(2401047)(8121501046)(5005006)(3231101)(944501161)(3002001)(10201501046)(93006095)(93003095)(6055026)(6041288)(20161123562045)(20161123564045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(6072148)(201708071742011); SRVR:BY2PR12MB0258; BCL:0; PCL:0; RULEID:; SRVR:BY2PR12MB0258; X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0258; 4:AHVueGgiY+ZgJ+jQi2yofUgAHZBqki5QBTJSFJcxQQf6FslEk7GUskcuK9ymU8PbHnppxq3J0MFllTNPseKEbu7GbS2HN8Vhl6u7vzoMB+T9yAIMPEslASDjLT6usDk22D5BwvwaTC8xezEiVydngp8OXfJziY/BPNRdD/7kVusWH+cEZ/qetqmMvm7LYOQ/XUk+a/neGkSxdnqh9pgOFh/3j1l8fJ08kzW6NUTDi26c9a/OFwq0ubmdMNc/2+Am7YoOJ01QmEQyW4hzvgjWQo76LFkrc/YEpWUS/G7AXWrPDcgV1Sq9IxPofPTl8vzs X-Forefront-PRVS: 0592A9FDE6 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BY2PR12MB0258; 23:pKVCA0DCVbRj/vWUOCyssf0zHtfzlOogesqg974Uj?= =?us-ascii?Q?X3jULHt62I3Jwm7kDVOm16cQfYl06IA5RiOr94XPRIUzMN2sPvJ+8mgtVsNw?= =?us-ascii?Q?SuYJ5ardiT83lnm+HJFoVJugME012giWL34ahI/W0Oz5EtxJZNumLbVrRyw5?= =?us-ascii?Q?3attryneYB5gdlucStWrnLCD6lX2u05eXZ6goNkY+5ScKKgw5yy/dz8LZMcU?= =?us-ascii?Q?lCu1dbxtooNLSVX3qWbOzqr8yfj91Z/UC45CaHXAKavKGeopp2NrURR5I3qY?= =?us-ascii?Q?YNNR8lm6uOyBDpUkB6eDwDSvt5gyFn0eZg2g32fIwqJHrXypos4oVvjejQLK?= =?us-ascii?Q?jxki5lmc1V5AzcbpU2LawdfCtdoXGf8ZZ3YHWKrIbpVN06SJOlXFv0LMycWX?= =?us-ascii?Q?+ntA/2sKkNmx2+ydjwwF3qqRpBsSlDSAcdxUOwM38u3SD7++TbV9fm1vKLpo?= =?us-ascii?Q?JxT6ZvokX2WeHO009wNdifJGruQNt/lEVLj+FVRcJzhE+4c4CmOhwqeKEDNE?= =?us-ascii?Q?Pq1df9VFployLIB41b2Obgq0UWi+XHO7+0oIQ7Wq6JeSuqpYtLEN7pL+E5uv?= =?us-ascii?Q?4kmCrc0sXcuADz7VabLagbBxzD0ZpDmlxoos2O8y1/BxLGyqE2gIeH3OehWC?= =?us-ascii?Q?iXYiJiJxbgxX8jAbFjdL227kOsvtJxQnYTP/hN683uZROAlXuT/eGhbsl33A?= =?us-ascii?Q?vyvAb+cvL+wo1psWkAP6RJX6axbXf/yIcq0InIL4OeXEyPGed63FiDUdnwTc?= =?us-ascii?Q?tHJbl/D7YXkrkG99OrUICAbFV0X2Clc/3CXptxR8QJpjmHMi4jpDpuUgiodG?= =?us-ascii?Q?sWQN/WTgVS5e7KOBqzOZNybKl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dCQpf841LlZ+GBHYwhv0xQ2aZ98uBNjubIdHWvg6lUhoNa7HKw6HDUOs4+A3GLwOAfq2VDs2VCGx+tpCcr9rGk71iD7rN5XkWi9ehDpJQnaKbhHLBAJ0hX6ogduvKoR+Bg/Szq0Wor2r3KuZJJZ1458ieUlkCx+AvVW3OBG3OEt9rtTs19+8enzzYLoTWyno+2GTKMr3tt9kBjpXrOfEPZTOcnK7w81CgOM2hGoMaManJ7i5Lhei7F33 SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0258; 20:fhFshvv2lK4R47/sM6CNYY+2bLmfCSxx/8iCyk+3b32q+oYUK1uwk3ePNajHycEQlkljyyHFlJq9GxeHullVNgUHX3iDFzRSB7bLm/ZsdkLgPgbqwK2oboQDn1b3gpLdZQQxHtu4gmSA2yrwB7dEgDU1NxKSBRAvTll/fFI4ohLmETw2GvPDFoESkL6jl1IkoPWwUxHqPOkZpUWE+/Ic791pD+yeEY8M0QDV/V4HwXc1kyOMQeRJAfflq149T5lM X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Feb 2018 10:19:41.4152 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ba4806a5-ddba-4475-d049-08d57aa6f3c4 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=[SATLEXCHOV02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR12MB0258 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 v6: set lower_mem_limit as 0 to keep original behavior Signed-off-by: Roger He --- drivers/gpu/drm/ttm/ttm_memory.c | 92 ++++++++++++++++++++++++++++++++ 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, 103 insertions(+) diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/ttm/ttm_memory.c index aa0c381..0e192f38 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,53 @@ 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); + /* convert from 4KB to KB */ + 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; + /* convert from KB to 4KB */ + 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 +222,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 +435,9 @@ int ttm_mem_global_init(struct ttm_mem_global *glob) si_meminfo(&si); + /* set it as 0 by default to keep original behavior of OOM */ + glob->lower_mem_limit = 0; + ret = ttm_mem_init_kernel_zone(glob, &si); if (unlikely(ret != 0)) goto out_no_zone; @@ -469,6 +532,35 @@ 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) +{ + 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) + return true; + + return false; +} +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