Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3309839pxj; Mon, 14 Jun 2021 20:43:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy8vVUSKIIR2Hbh3v7I2Fh4Y29ygfR4gRtT5ISp47TnHzpWsAH0p0bGUrGmVasHoqRGvbqJ X-Received: by 2002:a17:906:b191:: with SMTP id w17mr18982907ejy.10.1623728597382; Mon, 14 Jun 2021 20:43:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623728597; cv=none; d=google.com; s=arc-20160816; b=lCN/tbUxUMM2VCm80NoEYB2aERTA0NZIr1AkhoqLaCxG0uQlSPN0a/oxYoNi8KAO1B hdQ3bvRU/ll+wvMolIawAJyIntgeBuWZPE0mEPUXBklf0wxQzEz6Y0fgFMFJ7i9vKAYe Pm85BXLczzp0Lz4+bcWTJZZE08H3yCX5VwTQAFT5cbnI8ue2VUjNmhn+/E75tJarmrbZ nA+kmL9+12yb1fABbn+TAsMdd9JcJkeBl5ZhB6LeJ5q4RKSF0yGv7yFw/J2HHmRapQBA XhbA1EiITWc0Ouax5li5Sip6FsGd2GVdh9yE04E1CMMFJD0du1Qpq5tb/rKOB0mJ+mhe TIJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=t/YamNGP+ckwga9loSudtTcbfG6KNZMYD9dgrRElUEY=; b=ia3203+cAsHeqd/qhXS913XmtuS6/pi6yiMinGTFdXXtKr3EgbLcuYO1M99NQZcLdg vk0rMlMFMLAA33bfSRb/xwmAW68KscArL+ohF35GTxfzYIBLWqjHKVSUZCfvAQDWmWl2 CL76BxffbCYI/SjLWQlLFMO4TJjf2HQR3LhjT417I28XgjuZpsoIznbD6E8Ltict/Xfj 8rZKtbQqNJ22abgXIXBrvatUlUoSikyB8gBLjk2GAB3lGs6v9qsd46LgVh/+gszVYcij Tv1Py94UX6H2yCqEnhGjXKfl6KPozgSWTSuOCfsUg6cac0IdISf71bHJXeSn3GwJvsgA mX8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=enOTOgUP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cf15si7919691edb.548.2021.06.14.20.42.53; Mon, 14 Jun 2021 20:43:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=enOTOgUP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229931AbhFODnj (ORCPT + 99 others); Mon, 14 Jun 2021 23:43:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229869AbhFODnh (ORCPT ); Mon, 14 Jun 2021 23:43:37 -0400 Received: from mail-yb1-xb34.google.com (mail-yb1-xb34.google.com [IPv6:2607:f8b0:4864:20::b34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 408FEC061574 for ; Mon, 14 Jun 2021 20:41:34 -0700 (PDT) Received: by mail-yb1-xb34.google.com with SMTP id h15so18406900ybm.13 for ; Mon, 14 Jun 2021 20:41:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=t/YamNGP+ckwga9loSudtTcbfG6KNZMYD9dgrRElUEY=; b=enOTOgUPsHecPapW0EVbJYb4dNAsLVbTc7It8jRha4hyxFAsCO7rCIN/4hGnR3aOy6 cDdSp2lR3seoHr8Nm530jsODbJyul5qZA3eXWJULxB0tDVLDU0rRcMN/NU5fTzyXTKKi CbDVz2unkihaqMs6J0/IJsj6KxfY3/69d8NACWUTqppeVkJgiIQPKxgi/y2ef7DwZLH5 2bFxRdEwpnVsgpGE8vQ8NFoACN0C8Sy988+fMrjtSTOBqtiiEqILIPrihaVgJ078w4hq vJViARxaY98Ti0OEHm6kRSd0qxzHao4VklA+Vhw2rUCr/0F8wokrccwoyguF+sXBdPm2 0ZYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=t/YamNGP+ckwga9loSudtTcbfG6KNZMYD9dgrRElUEY=; b=gWM2St+8g73v6yY4xFvGzVGbX1DdOBVVNHy52xkx8EMC35pT37o6V6kkpz4IiOSvZj 4LeYGuTl3ZQ/5JsAdfu+Z5cZTprBrve7T+L3/Qn7JKU5U2zUddOWGqFJ0ywjfCkZf81w 1iGQQg2xG/aXJeDjEslt7rB21tX0on2Re7ZZfVTWMZThrSxEeYhGEdjiMpOQkxD9i1ys ADHQkev4ihz6Cvapj782YLT94JRGzgNzQwTj6ksTniw/dfejxAXvIx2MGDuoIh2INylJ FOXImb5sb2fCBgbaIvbcgqSrhP/r2ORjDBdY3Ctsjizput+UuRO8tHruuUx3YUAoWicR fzvw== X-Gm-Message-State: AOAM531iVynSQOmbZnVK8n5Ni6dMAxBJFxmYSPWN9uaVxUuR6GiehVLU yk8mQAf9VYcTkTSnmPYGVajwMgrAXSlK+G4Hkvo= X-Received: by 2002:a25:b8c:: with SMTP id 134mr29650853ybl.332.1623728493419; Mon, 14 Jun 2021 20:41:33 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Jordan Niethe Date: Tue, 15 Jun 2021 13:41:22 +1000 Message-ID: Subject: Re: [PATCH v2 05/12] powerpc: Do not dereference code as 'struct ppc_inst' (uprobe, code-patching, feature-fixups) To: Christophe Leroy Cc: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , "Naveen N. Rao" , linux-kernel@vger.kernel.org, linuxppc-dev Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 20, 2021 at 11:50 PM Christophe Leroy wrote: > > 'struct ppc_inst' is an internal structure to represent an instruction, > it is not directly the representation of that instruction in text code. > It is not meant to map and dereference code. > > Dereferencing code directly through 'struct ppc_inst' has two main issues: > - On powerpc, structs are expected to be 8 bytes aligned while code is > spread every 4 byte. > - Should a non prefixed instruction lie at the end of the page and the > following page not be mapped, it would generate a page fault. > > In-memory code must be accessed with ppc_inst_read(). > > Signed-off-by: Christophe Leroy > --- > arch/powerpc/kernel/uprobes.c | 2 +- > arch/powerpc/lib/code-patching.c | 8 ++++---- > arch/powerpc/lib/feature-fixups.c | 2 +- > 3 files changed, 6 insertions(+), 6 deletions(-) > > diff --git a/arch/powerpc/kernel/uprobes.c b/arch/powerpc/kernel/uprobes.c > index 186f69b11e94..46971bb41d05 100644 > --- a/arch/powerpc/kernel/uprobes.c > +++ b/arch/powerpc/kernel/uprobes.c > @@ -42,7 +42,7 @@ int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, > return -EINVAL; > > if (cpu_has_feature(CPU_FTR_ARCH_31) && > - ppc_inst_prefixed(auprobe->insn) && > + ppc_inst_prefixed(ppc_inst_read(&auprobe->insn)) && > (addr & 0x3f) == 60) { > pr_info_ratelimited("Cannot register a uprobe on 64 byte unaligned prefixed instruction\n"); > return -EINVAL; > diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c > index 870b30d9be2f..0308429b0d1a 100644 > --- a/arch/powerpc/lib/code-patching.c > +++ b/arch/powerpc/lib/code-patching.c > @@ -329,13 +329,13 @@ static unsigned long branch_iform_target(const struct ppc_inst *instr) > { > signed long imm; > > - imm = ppc_inst_val(*instr) & 0x3FFFFFC; > + imm = ppc_inst_val(ppc_inst_read(instr)) & 0x3FFFFFC; > > /* If the top bit of the immediate value is set this is negative */ > if (imm & 0x2000000) > imm -= 0x4000000; > > - if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0) > + if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0) > imm += (unsigned long)instr; > > return (unsigned long)imm; > @@ -345,13 +345,13 @@ static unsigned long branch_bform_target(const struct ppc_inst *instr) > { > signed long imm; > > - imm = ppc_inst_val(*instr) & 0xFFFC; > + imm = ppc_inst_val(ppc_inst_read(instr)) & 0xFFFC; > > /* If the top bit of the immediate value is set this is negative */ > if (imm & 0x8000) > imm -= 0x10000; > > - if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0) > + if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0) > imm += (unsigned long)instr; > > return (unsigned long)imm; > diff --git a/arch/powerpc/lib/feature-fixups.c b/arch/powerpc/lib/feature-fixups.c > index fe26f2fa0f3f..8905b53109bc 100644 > --- a/arch/powerpc/lib/feature-fixups.c > +++ b/arch/powerpc/lib/feature-fixups.c > @@ -51,7 +51,7 @@ static int patch_alt_instruction(struct ppc_inst *src, struct ppc_inst *dest, > > instr = ppc_inst_read(src); > > - if (instr_is_relative_branch(*src)) { > + if (instr_is_relative_branch(ppc_inst_read(src))) { The above variable instr could be used here, but that is not an issue with this patch. > struct ppc_inst *target = (struct ppc_inst *)branch_target(src); > > /* Branch within the section doesn't need translating */ > -- > 2.25.0 > Reviewed by: Jordan Niethe