Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp6014267imm; Sat, 19 May 2018 15:08:21 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpTLZCqmHZu1FbDuTyp4Vydf9vDOdQba7DDUVB+ni3re2o1SDlth6WaVog/Sck/WTST5ztv X-Received: by 2002:a63:a84f:: with SMTP id i15-v6mr11603028pgp.367.1526767701330; Sat, 19 May 2018 15:08:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526767701; cv=none; d=google.com; s=arc-20160816; b=yrOlYph6C0ln2BdZRamEAKYTg2dgpbIZCHcvSGFADmj+yfA1aGcevzLf7Bokezhcv5 Qp0ibn2OwP1+ce1kuxnTB9RjcTpek6CBv84fg4c4orv2sPB2Xxcr9sSMtHFL28JdoqgB azMP2tdXjIW0mDlk1CqXcEPDFRy2TQTcsBVeDm3+EhU04hantRZn8DkGkOVVJ1rIP0Oa x8hTZuxC67li0kD0cuR6Z1/fRcEJwUYOBkRHXAGR67qRqmagLF+KcNjomz514JrUDnkp RHheIUDg3byuKzKH8LIy/EWJ3s/5ZAxNccUUzkmo3yirciqBo/MMVdrHTIYJ9xPH+7Qy ft2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=71TsBQgJDQMOpBgeCnHe8W1EKcPOkMSh6WcCFuAvb6g=; b=Vaht4L5LJhbRTHVU//DHzIvfRreokFL6hCHelH3ucxZD/QxQffSIEGayosAu/Rq4gC PloGk9SDoM2JpjqMzkcaWslKvDfY4uhDoket7h3kor+DyhYqN7C9ks4vH7GBzwP1NBfQ FcVrMRIJc0cb2/GMwPthCnxRjaT/g0EqLeJzhnbh3TsZemSJqHtUGRP0Vxe2/tGk1IdO 2huFcwVZVcXrSTcMZK6+30Izcqb279X2bzzLMnmw0W6+JFyhpJembi9YEVtIlzb/m/jO giOl4DX2QSz9mBnmZevaomHy7zkGy3aCuOJ+m6vjGAF7w1fbDs7Pb0oQtrOgyrB18Lps Zx6g== ARC-Authentication-Results: i=1; mx.google.com; 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 o1-v6si3594224pga.261.2018.05.19.15.08.05; Sat, 19 May 2018 15:08:21 -0700 (PDT) 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; 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 S1752626AbeESWHu (ORCPT + 99 others); Sat, 19 May 2018 18:07:50 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:49434 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752553AbeESWHs (ORCPT ); Sat, 19 May 2018 18:07:48 -0400 Received: by vps-vb.mhejs.net with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1fKA0Z-0001Ot-My; Sun, 20 May 2018 00:07:23 +0200 From: "Maciej S. Szmigiero" To: Borislav Petkov Cc: Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 4/8] x86/microcode/AMD: Split status from data to skip in verify_and_add_patch() Date: Sun, 20 May 2018 00:07:18 +0200 Message-Id: X-Mailer: git-send-email 2.17.0 In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org verify_and_add_patch() returned a single "int" value which encoded both this function error status and also a length of microcode container data to skip. Unfortunately, ranges of these two values collide: the length of data to skip can be any value between 1 and UINT_MAX, so, for example, error status of -EINVAL maps to a valid return value of 4294967274 bytes. That's why these two values need to be split. Let's keep the common convention that a function zero return value means success while a negative value means an error while moving the returned length of microcode container data to skip to a separate output parameter. Signed-off-by: Maciej S. Szmigiero --- arch/x86/kernel/cpu/microcode/amd.c | 33 +++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c index f4c7479a961c..f8bd74341ed8 100644 --- a/arch/x86/kernel/cpu/microcode/amd.c +++ b/arch/x86/kernel/cpu/microcode/amd.c @@ -730,40 +730,41 @@ static void cleanup(void) } /* - * We return the current size even if some of the checks failed so that - * we can skip over the next patch. If we return a negative value, we - * signal a grave error like a memory allocation has failed and the - * driver cannot continue functioning normally. In such cases, we tear - * down everything we've used up so far and exit. + * We return zero (success) and the current patch data size in @crnt_size + * even if some of the checks failed so that we can skip over the next patch. + * If we return a negative value, we signal a grave error like a memory + * allocation has failed and the driver cannot continue functioning normally. + * In such cases, we tear down everything we've used up so far and exit. */ -static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover) +static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover, + unsigned int *crnt_size) { struct microcode_header_amd *mc_hdr; struct ucode_patch *patch; - unsigned int patch_size, crnt_size, ret; + unsigned int patch_size, ret; u32 proc_fam; u16 proc_id; patch_size = *(u32 *)(fw + 4); - crnt_size = patch_size + SECTION_HDR_SIZE; + *crnt_size = patch_size + SECTION_HDR_SIZE; mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE); proc_id = mc_hdr->processor_rev_id; proc_fam = find_cpu_family_by_equiv_cpu(proc_id); if (!proc_fam) { pr_err("No patch family for equiv ID: 0x%04x\n", proc_id); - return crnt_size; + return 0; } /* check if patch is for the current family */ proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff); if (proc_fam != family) - return crnt_size; + return 0; if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) { pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n", mc_hdr->patch_id); - return crnt_size; + return 0; } /* @@ -774,7 +775,7 @@ static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover) ret = verify_patch_size(family, patch_size, leftover - SECTION_HDR_SIZE); if (!ret) { pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id); - return crnt_size; + return 0; } patch = kzalloc(sizeof(*patch), GFP_KERNEL); @@ -800,7 +801,7 @@ static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover) /* ... and add to cache. */ update_cache(patch); - return crnt_size; + return 0; } static enum ucode_state __load_microcode_amd(u8 family, const u8 *data, @@ -809,7 +810,6 @@ static enum ucode_state __load_microcode_amd(u8 family, const u8 *data, enum ucode_state ret = UCODE_ERROR; unsigned int leftover; u8 *fw = (u8 *)data; - int crnt_size = 0; int offset; offset = install_equiv_cpu_table(data); @@ -827,8 +827,9 @@ static enum ucode_state __load_microcode_amd(u8 family, const u8 *data, } while (leftover) { - crnt_size = verify_and_add_patch(family, fw, leftover); - if (crnt_size < 0) + unsigned int crnt_size; + + if (verify_and_add_patch(family, fw, leftover, &crnt_size) < 0) return ret; fw += crnt_size;