Re: [RFC V2 3/6] sh/mm: Stop using pte_ERROR()

Anshuman Khandual <[email protected]>
Newsgroups dev.linux.lists.loongarch,org.infradead.lists.linux-arm-kernel,org.infradead.lists.linux-riscv,org.infradead.lists.linux-snps-arc,org.infradead.lists.linux-um,org.kernel.vger.linux-alpha,org.kernel.vger.linux-arch,org.kernel.vger.linux-csky,org.kernel.vger.linux-hexagon,org.kernel.vger.linux-kernel,org.kernel.vger.linux-m68k,org.kernel.vger.linux-mips,org.kernel.vger.linux-openrisc,org.kernel.vger.linux-parisc,org.kernel.vger.linux-s390,org.kernel.vger.linux-sh,org.kernel.vger.sparclinux,org.kvack.linux-mm,org.ozlabs.lists.linuxppc-dev
Message-ID <azo46amarknescr2uotz77khpt6zakwex7sb52krsvakrzfdlq@qu2t3ahd4m5t>
On Wed, Aug 12, 2026 at 01:28:23PM +0200, David Hildenbrand (Arm) wrote:
> On 8/11/26 06:21, Anshuman Khandual wrote:
> > Directly use pr_err() in set_pte_phys() and drop pte_ERROR() which helps in
> > eventually dropping pte_ERROR() macro across the tree. In this new printing
> > __FILE__ and __LINE__ has been dropped because they are always the same and
> > don't really add any value.
> > 
> > Cc: Yoshinori Sato <[email protected]>
> > Cc: Rich Felker <[email protected]>
> > Cc: John Paul Adrian Glaubitz <[email protected]>
> > Cc: [email protected]
> > Cc: [email protected]
> > Signed-off-by: Anshuman Khandual <[email protected]>
> > ---
> >  arch/sh/include/asm/pgtable_32.h |  5 -----
> >  arch/sh/mm/init.c                | 11 ++++++++++-
> >  2 files changed, 10 insertions(+), 6 deletions(-)
> > 
> > diff --git a/arch/sh/include/asm/pgtable_32.h b/arch/sh/include/asm/pgtable_32.h
> > index 5f51af18997b..c8eb9a7a4c4c 100644
> > --- a/arch/sh/include/asm/pgtable_32.h
> > +++ b/arch/sh/include/asm/pgtable_32.h
> > @@ -401,14 +401,9 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd)
> >  #define pmd_page(pmd)		(virt_to_page(pmd_val(pmd)))
> >  
> >  #ifdef CONFIG_X2TLB
> > -#define pte_ERROR(e) \
> > -	printk("%s:%d: bad pte %p(%08lx%08lx).\n", __FILE__, __LINE__, \
> > -	       &(e), (e).pte_high, (e).pte_low)
> >  #define pgd_ERROR(e) \
> >  	printk("%s:%d: bad pgd %016llx.\n", __FILE__, __LINE__, pgd_val(e))
> >  #else
> > -#define pte_ERROR(e) \
> > -	printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
> >  #define pgd_ERROR(e) \
> >  	printk("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
> >  #endif
> > diff --git a/arch/sh/mm/init.c b/arch/sh/mm/init.c
> > index 110308bdef01..b3c7fd84ceb4 100644
> > --- a/arch/sh/mm/init.c
> > +++ b/arch/sh/mm/init.c
> > @@ -78,13 +78,22 @@ static pte_t *__get_pte_phys(unsigned long addr)
> >  	return pte_offset_kernel(pmd, addr);
> >  }
> >  
> > +static void print_bad_pte(pte_t pte)
> > +{
> > +#ifdef CONFIG_X2TLB
> > +	printk("bad pte %p(%08lx%08lx).\n", &pte, pte.pte_high, pte.pte_low);
> > +#else
> > +	printk("bad pte %08lx.\n", pte_val(pte));
> > +#endif
> 
> I'd avoid that helper if possible, so we don't encourage new users.
> 
> Shouldn't we just switch to pr_err() right away?

Yes we could move from printk() to pr_err(). The above helper
just prevented an ugly #ifdef block inside if(!pte_none(*pte))
conditional block, nothing more. Will just drop it then.

> 
> > +}
> > +
> >  static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
> >  {
> >  	pte_t *pte;
> >  
> >  	pte = __get_pte_phys(addr);
> >  	if (!pte_none(*pte)) {
> > -		pte_ERROR(*pte);
> > +		print_bad_pte(*pte);
> 
> Maybe similar to my reply to patch #1, actually print something more useful than
> just "bad pte" ? And possibly also just use ptval_to_str().

Probably #ifdef could be dropped here as ptval_to_str() should be able to
handle pte with two unsiged long elements in there i.e pte_high, pte_low
along with standard single unsiged long pte_low format.

#ifdef CONFIG_X2TLB
typedef struct { unsigned long pte_low, pte_high; } pte_t;
#define pte_val(x) \
        ((x).pte_low | ((unsigned long long)(x).pte_high << 32))
#define __pte(x) \
        ({ pte_t __pte = {(x), ((unsigned long long)(x)) >> 32}; __pte; })
#else
typedef struct { unsigned long pte_low; } pte_t;
#define pte_val(x)      ((x).pte_low)
#define __pte(x)        ((pte_t) { (x) } )
#endif

static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
{
        pte_t *pte;

        pte = __get_pte_phys(addr);
        if (!pte_none(*pte)) {
                char str[PTVAL_STR_MAX];

                ptval_to_str(str, pte_val(*pte));
                pr_err("unexpected set PTE at %lx in set_pte_phys: bad pte %p(%s).\n",
                        addr, pte, str);
                return;
        }

        set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
        local_flush_tlb_one(get_asid(), addr);

        if (pgprot_val(prot) & _PAGE_WIRED)
                tlb_wire_entry(NULL, addr, *pte);
}

But if this does not work revert back printing both pte formats separately
like before.

#ifdef CONFIG_X2TLB
               pr_err("unexpected set PTE at %lx in set_pte_phys: bad pte %p(%08lx%08lx).\n",
                       addr, pte, pte->pte_high, pte->pte_low);
#else
               char str[PTVAL_STR_MAX];

               ptval_to_str(str, pte_val(*pte));
               pr_err("unexpected set PTE at %lx in set_pte_phys: bad pte %s.\n", addr, str);
#endif

> 
> -- 
> Cheers,
> 
> David
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.