Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp888948ybt; Fri, 10 Jul 2020 15:21:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwp/G+2p2294Br8he62biEhXrWwglo38LixhKbSXvJuKzITG98Wl+N5fK28nf6s/vWDAkol X-Received: by 2002:a17:906:4086:: with SMTP id u6mr67427975ejj.9.1594419677228; Fri, 10 Jul 2020 15:21:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594419677; cv=none; d=google.com; s=arc-20160816; b=RQA2z2Y4PACjCSY1UWan6u6shbISXFc/oh4oHZjkMdz1c9iAdLUMDfHpjaI698CWtf Ky6p9fxA/e/SmPWNicXy+mbcbee4e7wxNrTMtYoYloWDiW4xHZL7X59t7sA/VurEoH7c y9TMOhHTM+kTFVvHQpE5EiWragaXGpqkG0SpY2kulJ0BhXFuU17FZfmzkGwOF5qfg/dy XDqEGHsdWPUr63lEsgrVRwraSdMOJl81anNRJpw1Bj0hvYDTVzXUtuXLwafhl6s8i0j1 Qd0CxcXzaRJg9wobsNRnYRitejG6cyof4zmjSZuDcie8iV3HhAne3jXP/UTmbJ49PInB fcyw== 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:dkim-signature; bh=RPlXxwxC87uaq64nnqw5MANvLol+4JOrpjsVdPBxD3o=; b=RjW5BbjzoW7uhCK+3VfujbkytErdgcwHBaN98gGQ1rsQRJPI5Mp+LOgTC3W/MgOlkG dHzFcLtNuAJZUFmbZ7TsQCtzaA8VZbO29aod2lK97ZmKsBnOyNIvGcoJ14UXmMy/7seE SlTNw88cOJswiaUA+i14MKKejFE3DvlPNPoQ2mMePZsb/3FaMh+jKRM1NJXrTUW64RuF vJlUo9R8ojusL9qTduLrLjscCQE1Ke40oqTsHGWD6pdCInns87ec0zpQXzgD0sjwU55X dnnc7CUByu0xik/Tl2SQ3pgrkaibEeYL7jZO0FenkU0noLxGy30eycT2Yu9u0xPN3dk1 sp8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=igXknrAx; 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 bu2si5328337edb.423.2020.07.10.15.20.54; Fri, 10 Jul 2020 15:21: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=igXknrAx; 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 S1726803AbgGJWU2 (ORCPT + 99 others); Fri, 10 Jul 2020 18:20:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726772AbgGJWUZ (ORCPT ); Fri, 10 Jul 2020 18:20:25 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E45E4C08C5DC; Fri, 10 Jul 2020 15:20:24 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id j18so7599114wmi.3; Fri, 10 Jul 2020 15:20:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=RPlXxwxC87uaq64nnqw5MANvLol+4JOrpjsVdPBxD3o=; b=igXknrAx+ieLXkXBcEVFyoWfoYs+N5dQQI59AiDzZ1RrQb2nMVrxJB3zRBeI5nLuP0 xiQ+T1T30ezIW8OnSvFK8+//wKnlQDz7NjoE+g3+5UWx9KODFbIMb6vTsi8Y4tRi1wDH CmWoJfru5cZ/yAaIZc7sNTHKl8keBtYbmZLuHZRNyV6v/0R05rkDitki9YMfdSOTG61j Ru/MJmcLzXsKbMe9gRKUnFajnt/kNutKehlRM2ZGzB63l85mPpADGL9tXAt1oTS4hb8O MeHpRsE86DJf/E5NSkfQWwLoI8gNcwh2hwt8Y8r5+g2KZ0moALU1wlXRl0H4ZkZiMsS9 WY5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=RPlXxwxC87uaq64nnqw5MANvLol+4JOrpjsVdPBxD3o=; b=ThnCTcTi+034XwAsX0Lq76Tt+jHnvbHxrm11curFgsPnuh1gzW4rat88hFpAnzLxA/ DYTQMTstOb1mNB/hLOwXOdEVmtVIR4mC7TiXKyu4rWjYqwbSRK+bgXdF24/JhPjsoLBw 27kWEWO4x/f/8sdf2KirfQBINSfvzIsacLuhFpWJpcq9n3DqCfZUhqB8OfVJhzpNqzBG Pso+gSshQijHXHWcR2yiosXxE2aXACGlKYqua7n47acBeLO0vD6ZTw1xHq38x77vkCSi 7bwjDh8FmGhjbDBV0QBqSPPzbdgw95Wgsd7DYYm96wxvwviAbc+v0wICBIb9qchURPHT 5Wyw== X-Gm-Message-State: AOAM532wK6NVjnSpGIinsvKSpVMBBuMFHSbenSHda2/iVCnWrQwnJtIe juHn0p4auIHtlDosWzHr+HY= X-Received: by 2002:a7b:ca52:: with SMTP id m18mr7036373wml.92.1594419623608; Fri, 10 Jul 2020 15:20:23 -0700 (PDT) Received: from net.saheed (54007186.dsl.pool.telekom.hu. [84.0.113.134]) by smtp.gmail.com with ESMTPSA id l18sm12170281wrm.52.2020.07.10.15.20.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jul 2020 15:20:23 -0700 (PDT) From: Saheed Olayemi Bolarinwa To: helgaas@kernel.org Cc: Bolarinwa Olayemi Saheed , bjorn@helgaas.com, skhan@linuxfoundation.org, linux-pci@vger.kernel.org, linux-kernel-mentees@lists.linuxfoundation.org, linux-kernel@vger.kernel.org Subject: [PATCH 13/14] PCI/ASPM: Check the return value of pcie_capability_read_*() Date: Fri, 10 Jul 2020 23:20:25 +0200 Message-Id: <20200710212026.27136-14-refactormyself@gmail.com> X-Mailer: git-send-email 2.18.2 In-Reply-To: <20200710212026.27136-1-refactormyself@gmail.com> References: <20200710212026.27136-1-refactormyself@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bolarinwa Olayemi Saheed On failure pcie_capability_read_dword() sets it's last parameter, val to 0. However, with Patch 14/14, it is possible that val is set to ~0 on failure. This would introduce a bug because (x & x) == (~0 & x). This bug can be avoided if the return value of pcie_capability_read_dword is checked to confirm success. Check the return value of pcie_capability_read_dword() to ensure success. Suggested-by: Bjorn Helgaas Signed-off-by: Bolarinwa Olayemi Saheed --- drivers/pci/pcie/aspm.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c index b17e5ffd31b1..32aa9d57672a 100644 --- a/drivers/pci/pcie/aspm.c +++ b/drivers/pci/pcie/aspm.c @@ -176,7 +176,7 @@ static void pcie_set_clkpm(struct pcie_link_state *link, int enable) static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist) { - int capable = 1, enabled = 1; + int ret, capable = 1, enabled = 1; u32 reg32; u16 reg16; struct pci_dev *child; @@ -184,14 +184,14 @@ static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist) /* All functions should have the same cap and state, take the worst */ list_for_each_entry(child, &linkbus->devices, bus_list) { - pcie_capability_read_dword(child, PCI_EXP_LNKCAP, ®32); - if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) { + ret = pcie_capability_read_dword(child, PCI_EXP_LNKCAP, ®32); + if (ret || !(reg32 & PCI_EXP_LNKCAP_CLKPM)) { capable = 0; enabled = 0; break; } - pcie_capability_read_word(child, PCI_EXP_LNKCTL, ®16); - if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN)) + ret = pcie_capability_read_word(child, PCI_EXP_LNKCTL, ®16); + if (ret || !(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN)) enabled = 0; } link->clkpm_enabled = enabled; @@ -205,6 +205,7 @@ static bool pcie_retrain_link(struct pcie_link_state *link) struct pci_dev *parent = link->pdev; unsigned long end_jiffies; u16 reg16; + int ret; pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16); reg16 |= PCI_EXP_LNKCTL_RL; @@ -222,8 +223,8 @@ static bool pcie_retrain_link(struct pcie_link_state *link) /* Wait for link training end. Break out after waiting for timeout */ end_jiffies = jiffies + LINK_RETRAIN_TIMEOUT; do { - pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16); - if (!(reg16 & PCI_EXP_LNKSTA_LT)) + ret = pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16); + if (ret || !(reg16 & PCI_EXP_LNKSTA_LT)) break; msleep(1); } while (time_before(jiffies, end_jiffies)); @@ -237,7 +238,7 @@ static bool pcie_retrain_link(struct pcie_link_state *link) */ static void pcie_aspm_configure_common_clock(struct pcie_link_state *link) { - int same_clock = 1; + int ret, same_clock = 1; u16 reg16, parent_reg, child_reg[8]; struct pci_dev *child, *parent = link->pdev; struct pci_bus *linkbus = parent->subordinate; @@ -249,24 +250,24 @@ static void pcie_aspm_configure_common_clock(struct pcie_link_state *link) BUG_ON(!pci_is_pcie(child)); /* Check downstream component if bit Slot Clock Configuration is 1 */ - pcie_capability_read_word(child, PCI_EXP_LNKSTA, ®16); - if (!(reg16 & PCI_EXP_LNKSTA_SLC)) + ret = pcie_capability_read_word(child, PCI_EXP_LNKSTA, ®16); + if (ret || !(reg16 & PCI_EXP_LNKSTA_SLC)) same_clock = 0; /* Check upstream component if bit Slot Clock Configuration is 1 */ - pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16); - if (!(reg16 & PCI_EXP_LNKSTA_SLC)) + ret = pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16); + if (ret || !(reg16 & PCI_EXP_LNKSTA_SLC)) same_clock = 0; /* Port might be already in common clock mode */ - pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16); - if (same_clock && (reg16 & PCI_EXP_LNKCTL_CCC)) { + ret = pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16); + if (!ret && same_clock && (reg16 & PCI_EXP_LNKCTL_CCC)) { bool consistent = true; list_for_each_entry(child, &linkbus->devices, bus_list) { - pcie_capability_read_word(child, PCI_EXP_LNKCTL, + ret = pcie_capability_read_word(child, PCI_EXP_LNKCTL, ®16); - if (!(reg16 & PCI_EXP_LNKCTL_CCC)) { + if (ret || !(reg16 & PCI_EXP_LNKCTL_CCC)) { consistent = false; break; } -- 2.18.2