From patchwork Thu Aug 6 02:56:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Goldsworthy X-Patchwork-Id: 11702721 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D04A3138C for ; Thu, 6 Aug 2020 02:57:16 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A35A72177B for ; Thu, 6 Aug 2020 02:57:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=mg.codeaurora.org header.i=@mg.codeaurora.org header.b="ZK6HzhBy" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A35A72177B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E2AF78D0001; Wed, 5 Aug 2020 22:57:15 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E00166B0006; Wed, 5 Aug 2020 22:57:15 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3D418D0001; Wed, 5 Aug 2020 22:57:15 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0130.hostedemail.com [216.40.44.130]) by kanga.kvack.org (Postfix) with ESMTP id C02CC6B0005 for ; Wed, 5 Aug 2020 22:57:15 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 81AEE180AD804 for ; Thu, 6 Aug 2020 02:57:15 +0000 (UTC) X-FDA: 77118632430.12.cow24_2103dcd26fb4 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id 58D7018015986 for ; Thu, 6 Aug 2020 02:57:15 +0000 (UTC) X-Spam-Summary: 1,0,0,2d3369a2c0655e84,d41d8cd98f00b204,bounce+d06763.be9e4a-linux-mm=kvack.org@mg.codeaurora.org,,RULES_HIT:41:355:379:541:800:960:966:973:988:989:1260:1345:1359:1434:1437:1535:1543:1711:1730:1747:1777:1792:2196:2199:2393:2553:2559:2562:2689:2693:2898:3138:3139:3140:3141:3142:3355:3865:3866:3867:3868:3870:3871:3874:4031:4117:4250:4321:4385:5007:6261:6653:7903:8660:10004:11026:11218:11232:11658:11914:12291:12294:12296:12297:12438:12517:12519:12555:13138:13148:13161:13229:13230:13231:14096:14181:14394:14721:21080:21433:21451:21627:21740:21939:21972:21990:22119:30012:30054:30070:30090,0,RBL:69.72.43.7:@mg.codeaurora.org:.lbl8.mailshell.net-66.100.201.201 62.2.0.100;04yg1316ihb53uikpbwe45gewm1xnopkxyqq45xmaszwj4tz3irnugkrzmrgq4d.dyxdph9nr7rumg3t15mt6dkye7yrm5twddj4be45szxd3541sw75w84u9xs7qf9.h-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_S UMMARY:n X-HE-Tag: cow24_2103dcd26fb4 X-Filterd-Recvd-Size: 6224 Received: from m43-7.mailgun.net (m43-7.mailgun.net [69.72.43.7]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Thu, 6 Aug 2020 02:57:12 +0000 (UTC) DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1596682634; h=References: In-Reply-To: Message-Id: Date: Subject: Cc: To: From: Sender; bh=GSqGnDAtFz2eN+4a/6d10DfESlZhpkDU0wGAOPxxqGE=; b=ZK6HzhByj7luHSsWAPh2gsJyVuBrMa5B+GbmegTndP7wYkmOv6Arnl3nITCLx2hiEs/cHYzG YXeFphN6r08knPxQ/eSVAtZewSS37jqLLoyBMsVHvIIVvfFfDyDWHMpXIHjtDtj1SJY37ycP v3qaOlrZ53GLyAiUq96UiKF1gno= X-Mailgun-Sending-Ip: 69.72.43.7 X-Mailgun-Sid: WyIwY2Q3OCIsICJsaW51eC1tbUBrdmFjay5vcmciLCAiYmU5ZTRhIl0= Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by smtp-out-n16.prod.us-east-1.postgun.com with SMTP id 5f2b7163c431c2aa348063f4 (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Thu, 06 Aug 2020 02:56:35 GMT Received: by smtp.codeaurora.org (Postfix, from userid 1001) id CEF85C43391; Thu, 6 Aug 2020 02:56:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=2.0 tests=ALL_TRUSTED,SPF_NONE, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from cgoldswo-linux.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: cgoldswo) by smtp.codeaurora.org (Postfix) with ESMTPSA id 37839C433CA; Thu, 6 Aug 2020 02:56:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 37839C433CA Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; spf=none smtp.mailfrom=cgoldswo@codeaurora.org From: Chris Goldsworthy To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, pratikp@codeaurora.org, pdaly@codeaurora.org, sudraja@codeaurora.org, iamjoonsoo.kim@lge.com, Chris Goldsworthy , Susheel Khiani , Vinayak Menon Subject: [PATCH] mm: cma: retry allocations in cma_alloc Date: Wed, 5 Aug 2020 19:56:22 -0700 Message-Id: <1596682582-29139-2-git-send-email-cgoldswo@codeaurora.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596682582-29139-1-git-send-email-cgoldswo@codeaurora.org> References: <1596682582-29139-1-git-send-email-cgoldswo@codeaurora.org> X-Rspamd-Queue-Id: 58D7018015986 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: CMA allocations will fail if 'pinned' pages are in a CMA area, since we cannot migrate pinned pages. The _refcount of a struct page being greater than _mapcount for that page can cause pinning for anonymous pages. This is because try_to_unmap(), which (1) is called in the CMA allocation path, and (2) decrements both _refcount and _mapcount for a page, will stop unmapping a page from VMAs once the _mapcount for a page reaches 0. This implies that after try_to_unmap() has finished successfully for a page where _recount > _mapcount, that _refcount will be greater than 0. Later in the CMA allocation path in migrate_page_move_mapping(), we will have one more reference count than intended for anonymous pages, meaning the allocation will fail for that page. One example of where _refcount can be greater than _mapcount for a page we would not expect to be pinned is inside of copy_one_pte(), which is called during a fork. For ptes for which pte_present(pte) == true, copy_one_pte() will increment the _refcount field followed by the _mapcount field of a page. If the process doing copy_one_pte() is context switched out after incrementing _refcount but before incrementing _mapcount, then the page will be temporarily pinned. So, inside of cma_alloc(), instead of giving up when alloc_contig_range() returns -EBUSY after having scanned a whole CMA-region bitmap, perform retries with sleeps to give the system an opportunity to unpin any pinned pages. Signed-off-by: Chris Goldsworthy Co-developed-by: Susheel Khiani Signed-off-by: Susheel Khiani Co-developed-by: Vinayak Menon Signed-off-by: Vinayak Menon --- mm/cma.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/mm/cma.c b/mm/cma.c index 7f415d7..7b85fe6 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include "cma.h" @@ -418,6 +419,8 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align, size_t i; struct page *page = NULL; int ret = -ENOMEM; + int num_attempts = 0; + int max_retries = 5; if (!cma || !cma->count || !cma->bitmap) return NULL; @@ -442,8 +445,25 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align, bitmap_maxno, start, bitmap_count, mask, offset); if (bitmap_no >= bitmap_maxno) { - mutex_unlock(&cma->lock); - break; + if ((num_attempts < max_retries) && (ret == -EBUSY)) { + mutex_unlock(&cma->lock); + + /* + * Page may be momentarily pinned by some other + * process which has been scheduled out, e.g. + * in exit path, during unmap call, or process + * fork and so cannot be freed there. Sleep + * for 100ms and retry the allocation. + */ + start = 0; + ret = -ENOMEM; + msleep(100); + num_attempts++; + continue; + } else { + mutex_unlock(&cma->lock); + break; + } } bitmap_set(cma->bitmap, bitmap_no, bitmap_count); /*