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 9845BC433EF for ; Mon, 13 Jun 2022 16:47:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F164B8D01A8; Mon, 13 Jun 2022 12:47:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC6418D01A1; Mon, 13 Jun 2022 12:47:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8D308D01A8; Mon, 13 Jun 2022 12:47:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id CB4068D01A1 for ; Mon, 13 Jun 2022 12:47:10 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 05B0660EEA for ; Mon, 13 Jun 2022 16:47:05 +0000 (UTC) X-FDA: 79573792410.15.C3CCEA8 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf05.hostedemail.com (Postfix) with ESMTP id 6569F10015A for ; Mon, 13 Jun 2022 16:44:11 +0000 (UTC) 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 05CF861511 for ; Mon, 13 Jun 2022 16:32:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6233C341CA for ; Mon, 13 Jun 2022 16:32:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1655137941; bh=/KQ4avZANuW/a1vawyxsqtceAy8vWzHJKM7FSDpEDE0=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=CRO3rNDWqZhKGr4aQHLjTeuRFW5RSt9ubBvyYxcc/oRg90s1EhUInLtA5NGYH4Yhn jcDQYGYqdu6UjHgcgHNaEQt/ZJA0VkzrzUQVbmBsr0rY/ENDsiTs22rr9GaIdCOz1P V6VBDg1MkjFGzrjyndssLgJ3toRWzTTo/guQxdBtcWByel19dAdcHB5uQiuOm7r4Df Hy3t9H84eQaACkzWaMH/Rps1eNA/XBAUZm5GemUcnXKjgjfIAtpNb0VHc5ShxpDioG ioU/mV2o0P0vd5q/lC1svKvhVf14Hyy1ceddYDG8JTCk260OSBHMrFqGDPlNJQEmoB FKiTiTeNdWxiQ== Received: by mail-vs1-f52.google.com with SMTP id r4so3332124vsf.10 for ; Mon, 13 Jun 2022 09:32:21 -0700 (PDT) X-Gm-Message-State: AJIora8kcn7ACT69qLRjpEsWu5jibxd65vwImejzsHv/YRXZi8M3UMN1 uKFZk1esnod+wp2oIS2scWOyrO96WW8GDE0PVyw= X-Google-Smtp-Source: AGRyM1tfN7ClzqlZh0CySrsdANEe6kYoaXK42AQPb7y+xZBud115bHG6pqdlowvl3n8yP0fu9ar3HNRATklqupla3mQ= X-Received: by 2002:a05:6102:184:b0:34b:9e95:14cf with SMTP id r4-20020a056102018400b0034b9e9514cfmr97455vsq.59.1655137940769; Mon, 13 Jun 2022 09:32:20 -0700 (PDT) MIME-Version: 1.0 References: <20220613131046.3009889-1-xianting.tian@linux.alibaba.com> <0262A4FB-5A9B-47D3-8F1A-995509F56279@nvidia.com> In-Reply-To: <0262A4FB-5A9B-47D3-8F1A-995509F56279@nvidia.com> From: Guo Ren Date: Tue, 14 Jun 2022 00:32:09 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RESEND PATCH] mm: page_alloc: validate buddy before check the migratetype To: Zi Yan Cc: Xianting Tian , Andrew Morton , Vlastimil Babka , Linux-MM , Linux Kernel Mailing List , stable@vger.kernel.org, huanyi.xj@alibaba-inc.com, zjb194813@alibaba-inc.com, tianhu.hh@alibaba-inc.com, Hanjun Guo , Joonsoo Kim , Laura Abbott Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655138658; a=rsa-sha256; cv=none; b=TC8X/HO/0efIpxngNpxJLY359ODtMb77bdkTgsL0Dotv3WXxPWNXwP1EsaSB41KiCSD248 GD2h3YL34hKDt5GmFm7kHDjHA3fB94OorB4TRbBPPKvIXKBB57HZwXThoQdWw5o1EdBJbY Q0xkEIwRQsUnfodSCbEJGm/1uW5j7y4= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=CRO3rNDW; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of guoren@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=guoren@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655138658; 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=ScDYhOcGb7b563ZbX/QRHQybBFZuOTBa5d9m58YJcxI=; b=4fRQHDvRzX0+zzPnYoouzba9AoL03Nzm4q0XMg6Ehpff2b1XXHokF04VUeGYYH0kQI6Jys /6RqTkvZRFIiRPsPSf2IpNB3oVRnGsJpsztl2+4AB+m3J9lQqcAkR9cnwwoDQ9XZBc6ulq LHhJpsYbg6xpOBZawtivQrLhN6Xnpdk= X-Rspamd-Queue-Id: 6569F10015A X-Rspam-User: Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=CRO3rNDW; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of guoren@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=guoren@kernel.org X-Rspamd-Server: rspam06 X-Stat-Signature: c193sxomx689dmaumnp6ye6eo5httbmd X-HE-Tag: 1655138651-108232 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 Mon, Jun 13, 2022 at 11:23 PM Zi Yan wrote: > > Hi Xianting, > > Thanks for your patch. > > On 13 Jun 2022, at 9:10, Xianting Tian wrote: > > > Commit 787af64d05cd ("mm: page_alloc: validate buddy before check its m= igratetype.") > > added buddy check code. But unfortunately, this fix isn't backported to > > linux-5.17.y and the former stable branches. The reason is it added wro= ng > > fixes message: > > Fixes: 1dd214b8f21c ("mm: page_alloc: avoid merging non-fallbackab= le > > pageblocks with others") > > No, the Fixes tag is right. The commit above does need to validate buddy. I think Xianting is right. The =E2=80=9CFixes:" tag is not accurate and the page_is_buddy() is necessary here. This patch could be applied to the early version of the stable tree (eg: Linux-5.10.y, not the master tree) > > > Actually, this issue is involved by commit: > > commit d9dddbf55667 ("mm/page_alloc: prevent merging between isola= ted and other pageblocks") > > > > For RISC-V arch, the first 2M is reserved for sbi, so the start PFN is = 512, > > but it got buddy PFN 0 for PFN 0x2000: > > 0 =3D 0x2000 ^ (1 << 12) > > With the illegal buddy PFN 0, it got an illegal buddy page, which cause= d > > crash in __get_pfnblock_flags_mask(). > > It seems that the RISC-V arch reveals a similar bug from d9dddbf55667. > Basically, this bug will only happen when PFN=3D0x2000 is merging up and > there are some isolated pageblocks. Not PFN=3D0x2000, it's PFN=3D0x1000, I guess. RISC-V's first 2MB RAM could reserve for opensbi, so it would have riscv_pfn_base=3D512 and mem_map began with 512th PFN when CONFIG_FLATMEM=3Dy. (Also, csky has the same issue: a non-zero pfn_base in some scenarios.) But __find_buddy_pfn algorithm thinks the start address is 0, it could get 0 pfn or less than the pfn_base value. We need another check to prevent that. > > BTW, what does first reserved 2MB imply? All 4KB pages from first 2MB are > set to PageReserved? > > > > > With the patch, it can avoid the calling of get_pageblock_migratetype()= if > > it isn't buddy page. > > You might miss the __find_buddy_pfn() caller in unset_migratetype_isolate= () > from mm/page_isolation.c, if you are talking about linux-5.17.y and forme= r > version. There, page_is_buddy() is also not called and is_migrate_isolate= _page() > is called, which calls get_pageblock_migratetype() too. > > > > > Fixes: d9dddbf55667 ("mm/page_alloc: prevent merging between isolated a= nd other pageblocks") > > Cc: stable@vger.kernel.org > > Reported-by: zjb194813@alibaba-inc.com > > Reported-by: tianhu.hh@alibaba-inc.com > > Signed-off-by: Xianting Tian > > --- > > mm/page_alloc.c | 3 +++ > > 1 file changed, 3 insertions(+) > > > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > > index b1caa1c6c887..5b423caa68fd 100644 > > --- a/mm/page_alloc.c > > +++ b/mm/page_alloc.c > > @@ -1129,6 +1129,9 @@ static inline void __free_one_page(struct page *p= age, > > > > buddy_pfn =3D __find_buddy_pfn(pfn, order); > > buddy =3D page + (buddy_pfn - pfn); > > + > > + if (!page_is_buddy(page, buddy, order)) > > + goto done_merging; > > buddy_mt =3D get_pageblock_migratetype(buddy); > > > > if (migratetype !=3D buddy_mt > > -- > > 2.17.1 > > -- > Best Regards, > Yan, Zi --=20 Best Regards Guo Ren ML: https://lore.kernel.org/linux-csky/