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 DA966C61DA4 for ; Fri, 10 Feb 2023 02:33:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0F8656B00D8; Thu, 9 Feb 2023 21:33:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 082086B00D9; Thu, 9 Feb 2023 21:33:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8C8A6B00DA; Thu, 9 Feb 2023 21:33:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D54EF6B00D8 for ; Thu, 9 Feb 2023 21:33:54 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A644BC11C8 for ; Fri, 10 Feb 2023 02:33:54 +0000 (UTC) X-FDA: 80449811988.29.714A8C4 Received: from out-225.mta1.migadu.com (out-225.mta1.migadu.com [95.215.58.225]) by imf15.hostedemail.com (Postfix) with ESMTP id A80D6A0002 for ; Fri, 10 Feb 2023 02:33:52 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pFJPpcEq; spf=pass (imf15.hostedemail.com: domain of yajun.deng@linux.dev designates 95.215.58.225 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675996433; h=from:from:sender: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:dkim-signature; bh=2qZgslIhozh5iAyrVtnK6cx6++WSjKW0MyBqEgbTn14=; b=WV1L/es5R3XNKnQ3f5uFghOkyZEVD4t3GbnN4LVjxI7/OUAzz+GR+42L+zmo6pIw/Bn6cF kDth/Bsi1lyKaUKxTzfyaaQ4+soYvpnzRP6X+knHs27bWURLt3NL8+lTaflmr6BccAd1+C vkmRHhGs0x7DT62VL4Zs7xGws1yepw0= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pFJPpcEq; spf=pass (imf15.hostedemail.com: domain of yajun.deng@linux.dev designates 95.215.58.225 as permitted sender) smtp.mailfrom=yajun.deng@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675996433; a=rsa-sha256; cv=none; b=CFVwlEwaVuSEF5PqpEBIiFewBaG1jSD5sIfpb5VGLn+Yf9UTWcawXiguW42el/PQxp0O00 sM1AaCSpGvh8mMK0Fq0m88fwXbDn0+Qyfhhj70ayYEjBUTauUmvFF6Xpd7KMfk47hKbzpr SrPLyWdF9wZIk/on3IWSQ4pp6h3kw84= MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1675996430; 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=2qZgslIhozh5iAyrVtnK6cx6++WSjKW0MyBqEgbTn14=; b=pFJPpcEqqYnLoGy1XK3P7gElVhRRf5laAM5WgIBwP9hwmO9oaf/DpunQ6FgZo4APAGYgxJ Yprz1LxbLC/6DdJondvR92Hg1x2Lx1Fsaj3O9nSe84uT2aPdzCQv5cH72yfasWZaoZXxGx g39X2JLL6lCgBG1gdUzC3tX6hvtecXc= Date: Fri, 10 Feb 2023 02:33:47 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: "Yajun Deng" Message-ID: <626a5f4c4996f57631a8e1877c7646e5@linux.dev> Subject: Re: [PATCH v2] mm/page_alloc: optimize find_suitable_fallback() and fallbacks array To: "Zi Yan" Cc: akpm@linux-foundation.org, mgorman@techsingularity.net, david@redhat.com, vbabka@suse.cz, rppt@linux.ibm.com, osalvador@suse.de, rppt@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org In-Reply-To: <494D9F5D-33A4-48B4-911B-9A75CFC9BC67@nvidia.com> References: <494D9F5D-33A4-48B4-911B-9A75CFC9BC67@nvidia.com> <4C196D76-49A9-4B06-A51F-D8A13109DF3B@nvidia.com> <20230209101144.496144-1-yajun.deng@linux.dev> X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A80D6A0002 X-Rspam-User: X-Stat-Signature: gdxmkkbepa3xetqkgz5c8ekgae6311d4 X-HE-Tag: 1675996432-424178 X-HE-Meta: U2FsdGVkX18FeqSbkJ6O1XLEfqAGNIbmmnynVV7Pwc8lftbWy/Pye0itXMeoNAY3CvoskwbCJEkDoAhHxpsYLaV3A/YUrWxy4RnniRb2P73QsFWr50PYtAYtJMr9FaUbXTogNH2gjbCyasWdbGD4w1eVwdVUrZymc5MVkcKp+7/R0hNBzBjo+0zaHhSEjYB8WsWycUGVoZ9SNyHZMuYxP0hUJMMm6UQdMsmp0S8CgQuYDZCwvu8Bn+Dvv8C0PQFzCDzuoaT+xWXGif4/0gB8d4cls2kXxPztkAfSvgDXxA65QVLRHTSqXy4TGx6fIctG0iYtnyKbwqHVulRQDRHvfI3GQnkT+8oPWUzPxA8NtUyWhUdASKhTnk/4A/r/aVi3W02aQ4iBOZjUKE/a4BUPQtWTX1nfyDd7EHncTXhTVmST7x+oLfaSS/w1mjomrZcRNxbxRBjp8t091Mncngu336qO5GSGWd/ZV1qIeJYTBreg04FwycilvOsD425RW8WkkvnES3BprozkDu/eUnGVQRRh7qy/O4SCDt0zcwmvSIkaEYami5eLQk12Qj1fd1C9SBwD+x7B5YFPEfcGIYN/S1GB+bIgWLuA/knWrT7Z2i1T/yZHJZrAdqGZXfaY+YTkZ/9pvj0PRnG/h+UDmwjjS5SbEV4oHuWqrH365plR5p0GCO9uOjcCSwebss6WhEQTu71MStkQdqClj1TpWgkeSwZww7ITB+ymTIY7jeMDgZkvAG/M+mBlUQgAUdSAFDnl6k+CIkWArT887HGYYuFB+dDecGo5xPsYTMCGB80K8bAxam3ZdjGrT89ogsm7lpf31nqX/AuAqZ/oCTeot4h4VMlXIrTxar/LvGZgQLAXJ6SZrMu5QlD6aZx1AYbBOaAhStAFTVMIEjmR5x1wETRMFu/I6ms/hpaJbyvLzbP4ZJyLAI/n9iX9ksy87ammYFHQwO2c0v4QPYwJBIKpTlH CnMTkIUR p8wRzqu+Rn1jQeiiie2LUL+ffmld4NLwyQq4RUFEURAsBowX9v8gFJaHLI5pLVmesHuQDF1QkbziXfmMFNhQHcyA87yq4jvv+lY3P6V+HQuGwUTPzZ8YIcj2JA7X0NV3kkmLJJw2JTTl7r4IVXmZvt/s49wgQttSZNFYFjpmsNtIeLV9A/g4AH16FilqLPwnm43pmlUwKQ9OUkL8/BEm4KwYGZK0FMDDFxqvK 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: February 10, 2023 10:14 AM, "Zi Yan" wrote:=0A=0A> On 9 = Feb 2023, at 20:57, Yajun Deng wrote:=0A> =0A>> February 9, 2023 11:50 PM= , "Zi Yan" wrote:=0A>> =0A>>> On 9 Feb 2023, at 5:11, Ya= jun Deng wrote:=0A>> =0A>> There is no need to execute the next loop if i= t not return in the first=0A>> loop. So add a break at the end of the loo= p.=0A>>> Can you explain why? If it is the case, MIGRATE_UNMOVABLE cannot= fall back=0A>>> to MIGRATE_MOVABLE? And MIGRATE_MOVABLE cannot fall back= to MIGRATE_UNMOVABLE?=0A>>> And MIGRATE_RECLAIMABLE cannot fall back to = MIGRATE_MOVABLE?=0A>> =0A>> The return in the loop is only related to 'or= der', 'migratetype' and 'only_stealable'=0A>> variables. Even if it execu= te the next loop, it can't change the result. So the loop=0A>> can be bro= ken if the first loop can't return.=0A> =0A> OK. Got it. Would the code b= elow look better?=0A> =0A> for (i =3D 0; i < MIGRATE_PCPTYPES - 1 ; i++) = {=0A> fallback_mt =3D fallbacks[migratetype][i];=0A> if (free_area_empty(= area, fallback_mt))=0A> continue;=0A> }=0A> =0A> if (can_steal_fallback(o= rder, migratetype))=0A> *can_steal =3D true;=0A> =0A> if (!only_stealable= || *can_steal)=0A> return fallback_mt;=0A> =0A> return -1;=0A> =0A=0AYes= , I'll submit a v3 patch. =0AThanks.=0A=0A>> At the same time, add !migra= tetype_is_mergeable() before the loop and=0A>> reduce the first index siz= e from MIGRATE_TYPES to MIGRATE_PCPTYPES in=0A>> fallbacks array.=0A>>> Y= ou sent a patch: https://lore.kernel.org/all/20230203100132.1627787-1-yaj= un.deng@linux.dev/T/#u,=0A>>> why not squash this one into that? Why do= =0A>>> we need two separate small patches working on the same code?=0A>> = =0A>> Yes, this is better, but I overlooked this one when I sent the firs= t patch. It is already merged.=0A>> =0A>> As Vlastimil Babka said, reduce= the first index from MIGRATE_TYPES to MIGRATE_PCPTYPES may be=0A>> cause= out of bounds access of the shrinked fallbacks array If we don't judge t= he range of=0A>> migratetype. But this doesn't happen with the second ind= ex.=0A>> =0A>>> Thanks.=0A>> =0A>> Signed-off-by: Yajun Deng =0A>> Acked-by: Vlastimil Babka =0A>> ---=0A>> = include/linux/mmzone.h | 2 +-=0A>> mm/page_alloc.c | 11 +++++------=0A>> = 2 files changed, 6 insertions(+), 7 deletions(-)=0A>> =0A>> diff --git a/= include/linux/mmzone.h b/include/linux/mmzone.h=0A>> index ab94985ee7d9..= 0a817b8c7fb2 100644=0A>> --- a/include/linux/mmzone.h=0A>> +++ b/include/= linux/mmzone.h=0A>> @@ -85,7 +85,7 @@ static inline bool is_migrate_movab= le(int mt)=0A>> * Check whether a migratetype can be merged with another = migratetype.=0A>> *=0A>> * It is only mergeable when it can fall back to = other migratetypes for=0A>> - * allocation. See fallbacks[MIGRATE_TYPES][= 3] in page_alloc.c.=0A>> + * allocation. See fallbacks[][] array in page_= alloc.c.=0A>> */=0A>> static inline bool migratetype_is_mergeable(int mt)= =0A>> {=0A>> diff --git a/mm/page_alloc.c b/mm/page_alloc.c=0A>> index 11= 13483fa6c5..536e8d838fb5 100644=0A>> --- a/mm/page_alloc.c=0A>> +++ b/mm/= page_alloc.c=0A>> @@ -2603,7 +2603,7 @@ struct page *__rmqueue_smallest(s= truct zone *zone, unsigned int order,=0A>> *=0A>> * The other migratetype= s do not have fallbacks.=0A>> */=0A>> -static int fallbacks[MIGRATE_TYPES= ][MIGRATE_PCPTYPES - 1] =3D {=0A>> +static int fallbacks[MIGRATE_PCPTYPES= ][MIGRATE_PCPTYPES - 1] =3D {=0A>> [MIGRATE_UNMOVABLE] =3D { MIGRATE_RECL= AIMABLE, MIGRATE_MOVABLE },=0A>> [MIGRATE_MOVABLE] =3D { MIGRATE_RECLAIMA= BLE, MIGRATE_UNMOVABLE },=0A>> [MIGRATE_RECLAIMABLE] =3D { MIGRATE_UNMOVA= BLE, MIGRATE_MOVABLE },=0A>> @@ -2861,7 +2861,7 @@ int find_suitable_fall= back(struct free_area *area, unsigned int order,=0A>> int i;=0A>> int fal= lback_mt;=0A>> =0A>> - if (area->nr_free =3D=3D 0)=0A>> + if (area->nr_fr= ee =3D=3D 0 || !migratetype_is_mergeable(migratetype))=0A>> return -1;=0A= >> =0A>> *can_steal =3D false;=0A>> @@ -2873,11 +2873,10 @@ int find_suit= able_fallback(struct free_area *area, unsigned int order,=0A>> if (can_st= eal_fallback(order, migratetype))=0A>> *can_steal =3D true;=0A>> =0A>> - = if (!only_stealable)=0A>> - return fallback_mt;=0A>> -=0A>> - if (*can_st= eal)=0A>> + if (!only_stealable || *can_steal)=0A>> return fallback_mt;= =0A>> + else=0A>> + break;=0A>> }=0A>> =0A>> return -1;=0A>> --=0A>> 2.25= .1=0A>>> --=0A>>> Best Regards,=0A>>> Yan, Zi=0A> =0A> --=0A> Best Regard= s,=0A> Yan, Zi