From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id F1E4FC433F5 for ; Wed, 11 May 2022 16:23:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7BCEC6B007D; Wed, 11 May 2022 12:23:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 76D6C8D0002; Wed, 11 May 2022 12:23:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6352D8D0001; Wed, 11 May 2022 12:23:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 52F9B6B007D for ; Wed, 11 May 2022 12:23:50 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 35B1D223A for ; Wed, 11 May 2022 16:23:50 +0000 (UTC) X-FDA: 79453983420.26.D983B69 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 026CA1C0098 for ; Wed, 11 May 2022 16:23:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652286228; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xzVb9aUm7dZUVaR978hRNFMSk49T/5qOVU61ny8xUgA=; b=OQ95w15itRNkBGw006qixieGo1L+0BPfET2tx0aS7Xd/4QsfAA/W5xguRQ8dc6PE1ho4B4 28RrKKwciBpQ0MzeWVweDmzDPDtUNoC7IJWUvDMQnlAJEqYwAPyosoR/FOlJ7gJqcqfLqf iY0A72x6wvqWpxs4yOTE08JOmm6TDUk= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-655-BngLtFaHNBq864rZfzFuYg-1; Wed, 11 May 2022 12:23:47 -0400 X-MC-Unique: BngLtFaHNBq864rZfzFuYg-1 Received: by mail-wm1-f72.google.com with SMTP id m124-20020a1c2682000000b00393fcd2722dso869902wmm.4 for ; Wed, 11 May 2022 09:23:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:organization:in-reply-to :content-transfer-encoding; bh=xzVb9aUm7dZUVaR978hRNFMSk49T/5qOVU61ny8xUgA=; b=Yc7HrkTUbu0cGvCes9B2TSoiyWTQ+GYk83ON35C6RD7VMoej1dNQMfIb7nQv7NE6fH NwfQUJpov7OCuLsBqahIux/Hb//WeN4XD5/s8pr3+zCiAJLA1/etWzrkYtHJTv3IKU+k 9f00cJRpBlIAQKPyVBblKD7iEtIvIjJkVrvPAtijG9eZ4vBReecS+0CJpWpG58aRMaxY x47m9zumWdBSdKebMbJXdD9TWpU1DbRwphn3OYgrOfORpKKyQ8BttUIxnvdpDgoL4PEU gOOaF2Em+QB1HaVIS5KoAtwQcssWY0p1s0CO9499EZ3QqFK43xpFV50euU7NxebcTtwQ 3hYQ== X-Gm-Message-State: AOAM530yvGgjnJRJwV49NKNd3jSNdyIuE4csS9LC2Gr3BKWtQfGMaFxY MkuzMImnrKBrfxe02rpFIF2CW6JRxWsMNey4NGMg7+LlFS3HAbVigK2OMX9JYIpN7OcPd0icddR g5/X8qlLx9U4= X-Received: by 2002:a5d:5143:0:b0:20c:d517:d321 with SMTP id u3-20020a5d5143000000b0020cd517d321mr8643346wrt.468.1652286226351; Wed, 11 May 2022 09:23:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdiw9S/CTJAMmC+rAXMnyELFp8v1yYDXpfEZqwvfLhPJcSNLTGFikKsKJjOk6LDmY2et/HwA== X-Received: by 2002:a5d:5143:0:b0:20c:d517:d321 with SMTP id u3-20020a5d5143000000b0020cd517d321mr8643318wrt.468.1652286226063; Wed, 11 May 2022 09:23:46 -0700 (PDT) Received: from ?IPV6:2003:cb:c701:700:2393:b0f4:ef08:bd51? (p200300cbc70107002393b0f4ef08bd51.dip0.t-ipconnect.de. [2003:cb:c701:700:2393:b0f4:ef08:bd51]) by smtp.gmail.com with ESMTPSA id o15-20020a5d684f000000b0020c5253d8fesm2109855wrw.74.2022.05.11.09.23.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 11 May 2022 09:23:45 -0700 (PDT) Message-ID: Date: Wed, 11 May 2022 18:23:44 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Subject: Re: [PATCH 1/1] Revert "mm/cma.c: remove redundant cma_mutex lock" To: Dong Aisheng , linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, dongas86@gmail.com, linux-imx@nxp.com, akpm@linux-foundation.org, m.szyprowski@samsung.com, lecopzer.chen@mediatek.com, vbabka@suse.cz, stable@vger.kernel.org, minchan@kernel.org References: <20220509094551.3596244-1-aisheng.dong@nxp.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <20220509094551.3596244-1-aisheng.dong@nxp.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 026CA1C0098 X-Stat-Signature: cwqkge1bwct6su6o55xy8phw1qwj3rw7 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OQ95w15i; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf18.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=david@redhat.com X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1652286218-744327 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: On 09.05.22 11:45, Dong Aisheng wrote: > This reverts commit a4efc174b382fcdb62e2d90d39e78a274a975e38 which > introduced a regression issue that when there're multiple processes > allocating dma memory in parallel by calling dma_alloc_coherent(), it > may fail sometimes as follows: > > Error log: > cma: cma_alloc: linux,cma: alloc failed, req-size: 148 pages, ret: -16 > cma: number of available pages: > 3@125+20@172+12@236+4@380+32@736+17@2287+23@2473+20@36076+99@40477+108@40852+44@41108+20@41196+108@41364+108@41620+ > 108@42900+108@43156+483@44061+1763@45341+1440@47712+20@49324+20@49388+5076@49452+2304@55040+35@58141+20@58220+20@58284+ > 7188@58348+84@66220+7276@66452+227@74525+6371@75549=> 33161 free of 81920 total pages > > When issue happened, we saw there were still 33161 pages (129M) free CMA > memory and a lot available free slots for 148 pages in CMA bitmap that we > want to allocate. > > When dumping memory info, we found that there was also ~342M normal memory, > but only 1352K CMA memory left in buddy system while a lot of pageblocks > were isolated. > > Memory info log: > Normal free:351096kB min:30000kB low:37500kB high:45000kB reserved_highatomic:0KB > active_anon:98060kB inactive_anon:98948kB active_file:60864kB inactive_file:31776kB > unevictable:0kB writepending:0kB present:1048576kB managed:1018328kB mlocked:0kB > bounce:0kB free_pcp:220kB local_pcp:192kB free_cma:1352kB lowmem_reserve[]: 0 0 0 > Normal: 78*4kB (UECI) 1772*8kB (UMECI) 1335*16kB (UMECI) 360*32kB (UMECI) 65*64kB (UMCI) > 36*128kB (UMECI) 16*256kB (UMCI) 6*512kB (EI) 8*1024kB (UEI) 4*2048kB (MI) 8*4096kB (EI) > 8*8192kB (UI) 3*16384kB (EI) 8*32768kB (M) = 489288kB > > The root cause of this issue is that since commit a4efc174b382 > ("mm/cma.c: remove redundant cma_mutex lock"), CMA supports concurrent > memory allocation. It's possible that the memory range process A trying > to alloc has already been isolated by the allocation of process B during > memory migration. > > The problem here is that the memory range isolated during one allocation > by start_isolate_page_range() could be much bigger than the real size we > want to alloc due to the range is aligned to MAX_ORDER_NR_PAGES. > > Taking an ARMv7 platform with 1G memory as an example, when MAX_ORDER_NR_PAGES > is big (e.g. 32M with max_order 14) and CMA memory is relatively small > (e.g. 128M), there're only 4 MAX_ORDER slot, then it's very easy that > all CMA memory may have already been isolated by other processes when > one trying to allocate memory using dma_alloc_coherent(). > Since current CMA code will only scan one time of whole available CMA > memory, then dma_alloc_coherent() may easy fail due to contention with > other processes. > > This patch simply falls back to the original method that using cma_mutex > to make alloc_contig_range() run sequentially to avoid the issue. > > Cc: Andrew Morton > Cc: Marek Szyprowski > Cc: Lecopzer Chen > Cc: David Hildenbrand > Cc: Vlastimil Babka > Cc: Minchan Kim > CC: stable@vger.kernel.org # 5.11+ > Fixes: a4efc174b382 ("mm/cma.c: remove redundant cma_mutex lock") > Signed-off-by: Dong Aisheng > --- > Patch is based on > git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-stable > --- > mm/cma.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/mm/cma.c b/mm/cma.c > index eaa4b5c920a2..4a978e09547a 100644 > --- a/mm/cma.c > +++ b/mm/cma.c > @@ -37,6 +37,7 @@ > > struct cma cma_areas[MAX_CMA_AREAS]; > unsigned cma_area_count; > +static DEFINE_MUTEX(cma_mutex); > > phys_addr_t cma_get_base(const struct cma *cma) > { > @@ -468,9 +469,10 @@ struct page *cma_alloc(struct cma *cma, unsigned long count, > spin_unlock_irq(&cma->lock); > > pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit); > + mutex_lock(&cma_mutex); > ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, > GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0)); > - > + mutex_unlock(&cma_mutex); > if (ret == 0) { > page = pfn_to_page(pfn); > break; Acked-by: David Hildenbrand -- Thanks, David / dhildenb