Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp55050rwb; Wed, 18 Jan 2023 14:03:53 -0800 (PST) X-Google-Smtp-Source: AMrXdXuyGY1Fg4+J7y06lZyTOiNso5WcxGgra6dfGGhciuBCM96iEj2XCrfdhII99toqJsJDRb7x X-Received: by 2002:a17:906:8298:b0:86d:be0:607d with SMTP id h24-20020a170906829800b0086d0be0607dmr8493285ejx.70.1674079433516; Wed, 18 Jan 2023 14:03:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674079433; cv=none; d=google.com; s=arc-20160816; b=KLKrycAk/ecoY8WggHqqUXajOe+e1QClVz/RnihdsZu+/wAxhLKzQjEW3Qp+aHLQl4 FoOL2ZNA40H/EKOUwTH2Dfc70vh3MPILekeE/EPwYpokbGjnChN2nsK+FE8LlzHA0GJ2 VuXCvHpJ8t+BgIeVHZL5kEuJ+w1ziGoJ3V/IA16hxaldMU06zigMLcJizyq92lJeP+Ai YP5Y8lZT8h31Oq2P5Yo9IWUODwzpEZqX0Wr7/5+eJ6HAFg5trVvjBjnb+GRoD5ohKmSj xZGSjHYbatrOssqSc8M8e0K/hDOvopqqKD5Eo6kRJ5oPuYqw5ZqFgPR/GNlWtLknf+kq 26QQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=2Xict1KqehONohpOAa3Ak+Fixs8gdIbG5br506AJAmk=; b=CAXdRGce48MHcU1Rrnh2XS/zy0cMO/siZY/AL4UihfDMZIkylPK+zP9qn6AMsdbWcf BXJLqO3DeItKqjl/wuMXQh0sb4ie7jIiHSQOp2EGd3AI9/Ry9o2+mHnc8f6H8tLfscj+ Iq8LApvED0HpzZkg4JB9baC7Wb4yzzo1Fz0+c93I4k4p3ywcgBq5wr5LK/prRXpHX/Cl ao9+pcWdAvj2fVP2KwJWM/VkWXabBiAYuGvQbMF0Wh+hhvdpGPgJYZkN7TAWQaqWSNq4 tdYE9u9pLMi8xGoiM/2k7v1g9a+Ea05QV65SL8iM1nh1Zzw3zkr3Y5vTBOWQY/2Kw1qY RqPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b="ShoGBD/s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gs44-20020a1709072d2c00b0084d354539a5si39478063ejc.584.2023.01.18.14.03.34; Wed, 18 Jan 2023 14:03:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b="ShoGBD/s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231152AbjARVk1 (ORCPT + 45 others); Wed, 18 Jan 2023 16:40:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231246AbjARVkT (ORCPT ); Wed, 18 Jan 2023 16:40:19 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D56F23C24 for ; Wed, 18 Jan 2023 13:40:18 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 7456E61A51 for ; Wed, 18 Jan 2023 21:40:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A6621C433EF; Wed, 18 Jan 2023 21:40:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1674078017; bh=2tAKOt79uiNmuO0lejd8tlNauoCaC8FnJweuqfL+Ks4=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=ShoGBD/snhYeqkfSgFYNfejuER0iDFtULuGGqQaEiiaaOPA5EIG44pPxP1LKH1bwa EHtHlu9h60nsEK7Lfi/yaBRbE00HwcDRy/DTk93WCDG8JzC1x/8UTwRg9hCBVKBPWJ 1NUtPf3QXe5JmcWCX97ivH5ChO6BRVKRy6mu0xj4= Date: Wed, 18 Jan 2023 13:40:17 -0800 From: Andrew Morton To: Levi Yun Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Laurent Pinchart , Marek Szyprowski , Joonsoo Kim , Minchan Kim Subject: Re: [PATCH] mm/cma: Fix potential memory loss on cma_declare_contiguous_nid Message-Id: <20230118134017.c9b6e1cf0beae8e5c3bdfd8c@linux-foundation.org> In-Reply-To: <20230118080523.44522-1-ppbuk5246@gmail.com> References: <20230118080523.44522-1-ppbuk5246@gmail.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 18 Jan 2023 17:05:23 +0900 Levi Yun wrote: > Suppose, memblock_alloc_ranged_nid with highmem_start is success > when cma_declare_contiguous_nid is called with !fixed > in the system 32-bit system with PHYS_ADDR_T_64BIT enabled > with memblock.bottom_up == false. > > But, Next trial to memblock_alloc_range_nid to allocate in > [SIZE_4G, limits) nullifies former success allocated addr and > it retries to memblock_alloc_ragne_nid again. > > In this situation, first success address area is lost. > > By changing order of allocation (SIZE_4G, high_memory and base) and > checking whether allocated prevents potential memory lost. > The changelog is hard to follow. I redid it as below - please review and correct if needed? From: Levi Yun Subject: mm/cma: fix potential memory loss on cma_declare_contiguous_nid Date: Wed, 18 Jan 2023 17:05:23 +0900 Suppose memblock_alloc_range_nid() with highmem_start succeeds when cma_declare_contiguous_nid is called with !fixed on a 32-bit system with PHYS_ADDR_T_64BIT enabled with memblock.bottom_up == false. But the next trial to memblock_alloc_range_nid() to allocate in [SIZE_4G, limits) nullifies former successfully allocated addr and it retries memblock_alloc_ragne_nid(). In this situation, the first successfully allocated address area is lost. Change the order of allocation (SIZE_4G, high_memory and base) and check whether the allocated succeeded to prevent potential memory loss. Link: https://lkml.kernel.org/r/20230118080523.44522-1-ppbuk5246@gmail.com Signed-off-by: Levi Yun Cc: Laurent Pinchart Cc: Marek Szyprowski Cc: Joonsoo Kim Cc: Minchan Kim Signed-off-by: Andrew Morton --- mm/cma.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) --- a/mm/cma.c~mm-cma-fix-potential-memory-loss-on-cma_declare_contiguous_nid +++ a/mm/cma.c @@ -331,18 +331,6 @@ int __init cma_declare_contiguous_nid(ph phys_addr_t addr = 0; /* - * All pages in the reserved area must come from the same zone. - * If the requested region crosses the low/high memory boundary, - * try allocating from high memory first and fall back to low - * memory in case of failure. - */ - if (base < highmem_start && limit > highmem_start) { - addr = memblock_alloc_range_nid(size, alignment, - highmem_start, limit, nid, true); - limit = highmem_start; - } - - /* * If there is enough memory, try a bottom-up allocation first. * It will place the new cma area close to the start of the node * and guarantee that the compaction is moving pages out of the @@ -359,6 +347,18 @@ int __init cma_declare_contiguous_nid(ph } #endif + /* + * All pages in the reserved area must come from the same zone. + * If the requested region crosses the low/high memory boundary, + * try allocating from high memory first and fall back to low + * memory in case of failure. + */ + if (!addr && base < highmem_start && limit > highmem_start) { + addr = memblock_alloc_range_nid(size, alignment, + highmem_start, limit, nid, true); + limit = highmem_start; + } + if (!addr) { addr = memblock_alloc_range_nid(size, alignment, base, limit, nid, true); _