Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp6435074ybf; Thu, 5 Mar 2020 20:52:54 -0800 (PST) X-Google-Smtp-Source: ADFU+vug50R1Jd9q/+ng/y23S5gAnbVSF7E8Si4ULuaCqmbu3/YI1C2WAOzmdUbR7an4IgJeNmLy X-Received: by 2002:a05:6830:616:: with SMTP id w22mr1042637oti.215.1583470374462; Thu, 05 Mar 2020 20:52:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583470374; cv=none; d=google.com; s=arc-20160816; b=Z4+HrBho/SC3XiwvvHmi4rgMkY1BWDnbuBFJoxkNhtSB1ZrDjAsvp2vONM0FDnExjt j0vQW4uDtia54P2qSnx7VgS9Md5TW5hWJ4OCIirNi1N7qwmlZpGi44CMbFq9S0RYOyzm RjTdZ9WhqF7bkqxEcReKV05MlP465B/Bkjbg3LvCwdsysug0deZ0Mw9yoURdiUeSWSwy 3kDsk4Si9n19MAXvyA+ClDH8clnyO8czoSupGQ9d2J92pwKvGTe3Djf7RR7+TO2aKiL9 rrSCbgIomygZtDyor4oBOtm+AA3w15tfFN39wbkxhw7TXNT4YE9y2FuMW5v+KqxcCcSi rz6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=0wipnWw1cldWf5kaoeA+yDqtsFNc3E5tyyjbRgywYFs=; b=BWBV5uV9gQ8iNYK6AiX3d2F3XPAo77l0+7y4xLVKstkbPASFI+eAN2Nf8Cfen5Iv9o Tz5VHWtDBVykahXN0uBE84riY5IzZ2Hd1fsrR8yihQo7wUuzRxw2oGqeqVTFDwsSM0J3 tqi8d+EViFCAp4uhThU2tJ6P1nzEkmABHe+qOj3ts65+highPXiMIJ5gVcXni2w3fPIW qTVx6aHfB8QigWM4PbKYzgD+lKNZFKtUp/Zu3JgNRNCz04wfZhhB45Ti7ho+n5/PB6DG HfPZJ6lO/tXVq71YtvWk1wCacWzH//EXNsq2HZ8Plc6FOI/gUw1AF458bWEj5Y9HKDP1 CZ1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b="e/Ei7Y5I"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g10si686551otn.244.2020.03.05.20.52.41; Thu, 05 Mar 2020 20:52:54 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b="e/Ei7Y5I"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726378AbgCFEuy (ORCPT + 99 others); Thu, 5 Mar 2020 23:50:54 -0500 Received: from mail-qk1-f196.google.com ([209.85.222.196]:37270 "EHLO mail-qk1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726300AbgCFEuy (ORCPT ); Thu, 5 Mar 2020 23:50:54 -0500 Received: by mail-qk1-f196.google.com with SMTP id y126so1188895qke.4 for ; Thu, 05 Mar 2020 20:50:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=0wipnWw1cldWf5kaoeA+yDqtsFNc3E5tyyjbRgywYFs=; b=e/Ei7Y5Ik8WM1Taux+DExv+Jv/Fqylv3LWaQkFS1tZS+VrdFV5zge1M+oSCL4N6nh1 kqFutv0Jhd1kKJrkscv0XC4U8PWfWKvyp3UwW0tl0lYe99dOzId8l7/YWA1Xv+08VLY7 hDJuO8ZOTI4vrG1s7V8krX9kJIqrXheC0YSn2thFWrdtdf55B18kKm6SouQ/651zKGQe w6qrjn9AzZRZiIJD/tYfimUsaWF6T2SnV5KzAUzqTJ6hVwCwgw3BQORlcmjO/INQpYpH /OGE6z0l6tphlsbCSsGA6ecMIRTXWlG6RPo9hVW3aJY8iZn1Laz5XEu2ALoMD9XX1pJD Lltg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=0wipnWw1cldWf5kaoeA+yDqtsFNc3E5tyyjbRgywYFs=; b=OYH4mzsBjtCd9oDPLJxLDkn/EGtUJwo/p1NKbkP8WNbHibuVutcdHcgG6PZxhIiygL +XzioQcUqO2CrRBJZs9yB/zbM1WPEk8r7h97/Pl89zhisXbKiNII2EGCrsFIGriX8n70 /T4QawN1dODurTCTGRnJ041P5GgQZt1cFK3bhM+GSjqAdS9StZeVXDTBU4Wkt1BzRoqC cFXr39tVuNeJkqrq5TZRqHnP0ibHcfPJE5xmEQj8z8zNWcpgdelQvvQsYBqWwssHHr90 44TdysiHioVMcL3AMsEGf9LZebIAcxZMUCFyn+FaUD8iRJMO9wHwB+Ej4CPgS0KgOZ1q X9Gw== X-Gm-Message-State: ANhLgQ08rp283SA2pMZoOVuUOTlAQvKi586PTTnDQBo944v/puJo5eyE nge/viyP+NC5IZolwkepQs6g4Q== X-Received: by 2002:a37:a08b:: with SMTP id j133mr1326092qke.265.1583470253067; Thu, 05 Mar 2020 20:50:53 -0800 (PST) Received: from [192.168.1.153] (pool-71-184-117-43.bstnma.fios.verizon.net. [71.184.117.43]) by smtp.gmail.com with ESMTPSA id b5sm7520197qkk.16.2020.03.05.20.50.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 05 Mar 2020 20:50:52 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.60.0.2.5\)) Subject: Re: [PATCH -next v2] powerpc/64s/pgtable: fix an undefined behaviour From: Qian Cai In-Reply-To: Date: Thu, 5 Mar 2020 23:50:51 -0500 Cc: Michael Ellerman , Andrew Morton , rashmicy@gmail.com, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <1583418759-16105-1-git-send-email-cai@lca.pw> To: Christophe Leroy X-Mailer: Apple Mail (2.3608.60.0.2.5) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Mar 5, 2020, at 2:22 PM, Christophe Leroy = wrote: >=20 >=20 >=20 > Le 05/03/2020 =C3=A0 15:32, Qian Cai a =C3=A9crit : >> Booting a power9 server with hash MMU could trigger an undefined >> behaviour because pud_offset(p4d, 0) will do, >> 0 >> (PAGE_SHIFT:16 + PTE_INDEX_SIZE:8 + H_PMD_INDEX_SIZE:10) >> Fix it by converting pud_offset() and friends to static inline >> functions. >=20 > I was suggesting to convert pud_index() to static inline, because = that's where the shift sits. Is it not possible ? >=20 > Here you seems to fix the problem for now, but if someone reuses = pud_index() in another macro one day, the same problem may happen again. >=20 Sounds reasonable. I send out a v3, https://lore.kernel.org/lkml/20200306044852.3236-1-cai@lca.pw/T/#u > Christophe >=20 >> UBSAN: shift-out-of-bounds in arch/powerpc/mm/ptdump/ptdump.c:282:15 >> shift exponent 34 is too large for 32-bit type 'int' >> CPU: 6 PID: 1 Comm: swapper/0 Not tainted 5.6.0-rc4-next-20200303+ = #13 >> Call Trace: >> dump_stack+0xf4/0x164 (unreliable) >> ubsan_epilogue+0x18/0x78 >> __ubsan_handle_shift_out_of_bounds+0x160/0x21c >> walk_pagetables+0x2cc/0x700 >> walk_pud at arch/powerpc/mm/ptdump/ptdump.c:282 >> (inlined by) walk_pagetables at arch/powerpc/mm/ptdump/ptdump.c:311 >> ptdump_check_wx+0x8c/0xf0 >> mark_rodata_ro+0x48/0x80 >> kernel_init+0x74/0x194 >> ret_from_kernel_thread+0x5c/0x74 >> Suggested-by: Christophe Leroy >> Signed-off-by: Qian Cai >> --- >> arch/powerpc/include/asm/book3s/64/pgtable.h | 20 = ++++++++++++++------ >> 1 file changed, 14 insertions(+), 6 deletions(-) >> diff --git a/arch/powerpc/include/asm/book3s/64/pgtable.h = b/arch/powerpc/include/asm/book3s/64/pgtable.h >> index fa60e8594b9f..4967bc9e25e2 100644 >> --- a/arch/powerpc/include/asm/book3s/64/pgtable.h >> +++ b/arch/powerpc/include/asm/book3s/64/pgtable.h >> @@ -1016,12 +1016,20 @@ static inline bool p4d_access_permitted(p4d_t = p4d, bool write) >> #define pgd_offset(mm, address) ((mm)->pgd + = pgd_index(address)) >> -#define pud_offset(p4dp, addr) \ >> - (((pud_t *) p4d_page_vaddr(*(p4dp))) + pud_index(addr)) >> -#define pmd_offset(pudp,addr) \ >> - (((pmd_t *) pud_page_vaddr(*(pudp))) + pmd_index(addr)) >> -#define pte_offset_kernel(dir,addr) \ >> - (((pte_t *) pmd_page_vaddr(*(dir))) + pte_index(addr)) >> +static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address) >> +{ >> + return (pud_t *)p4d_page_vaddr(*p4d) + pud_index(address); >> +} >> + >> +static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address) >> +{ >> + return (pmd_t *)pud_page_vaddr(*pud) + pmd_index(address); >> +} >> + >> +static inline pte_t *pte_offset_kernel(pmd_t *pmd, unsigned long = address) >> +{ >> + return (pte_t *)pmd_page_vaddr(*pmd) + pte_index(address); >> +} >> #define pte_offset_map(dir,addr) pte_offset_kernel((dir), (addr)) >> =20