From patchwork Wed Jun 26 16:26:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Garry X-Patchwork-Id: 167839 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp1158065ilk; Wed, 26 Jun 2019 09:28:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqzeBJyrOnPlphl7pNV3rd+jw9RP7+gH0zc17EKQCsDDmS1TY/icEvIan8rh4I+VwZSiHKUk X-Received: by 2002:a17:90a:d14b:: with SMTP id t11mr1032664pjw.79.1561566515937; Wed, 26 Jun 2019 09:28:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561566515; cv=none; d=google.com; s=arc-20160816; b=iwks5CcQAy/Pq21AYY09SGIOFr2Dav0EjXwk19VidwRDU78pfluCFm37YBClTqSORk nZi1shs3SEEOUxo9xOYhE0NPvshDK3cqnGdz1uP9hm1r5lp2s+xZFblAU+JB6GtAkSyj xdMspZIsCZlc3S6NvzISz/SsOkp99fkntTE5iSCtfyaDrGA1tRPtkCgrenzrsvzLYMe9 U/zVzlamsyMAVdIeJXb72h5B+PDI9UYIGDGBA2Oia5rMYmDLpeLi6OwoQf1xELU/F85p tBgp/w27ME4QvCqsiVUdd66mN5XvXszJKgxGBpW0P1qWg+U0DwGlmtn4K6K3ztzEIwsU fDJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=OpJU7uJo3GX7afAAcEYz3oDNKK2QelsMLaehetq1p+o=; b=rWzBa4Fx1ZD6r8+lLAPOexMgxQ7HptGl/9M7NY56/w2PL8SxbGxT+/Kr0cPEc+ZVZ8 yKVTt91nV+ct3s0NBelLgsUQhyAiNp0YbEd+EfLYTfzq/ku0VrP2H64ATEPcW1ZgGeCi vTHb+LPRSpvjf72bLFI+0LOogvXeryDCiAw3SS0npgP07ycUPkSCDNsQ04VNr06m2W3L nzc9W1RNZC5Tcxr/mAvpq3IPWedtQXoCO0H98MaZuNxBtt8l5Y8fN1CdKoVFmDzOkc40 fU7NnO6k+XDm2Wjozj538HZ2k1kLw3C49XyDQxJVCnB4bXSaOh2OBMMl4zJ/Z4zmgjhU ZTnQ== 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 bc8si3558738plb.322.2019.06.26.09.28.35; Wed, 26 Jun 2019 09:28:35 -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 S1726523AbfFZQ2e (ORCPT + 30 others); Wed, 26 Jun 2019 12:28:34 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:19085 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726014AbfFZQ2d (ORCPT ); Wed, 26 Jun 2019 12:28:33 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 5BE6637B57A44D2D3B5C; Thu, 27 Jun 2019 00:28:31 +0800 (CST) Received: from localhost.localdomain (10.67.212.75) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.439.0; Thu, 27 Jun 2019 00:28:25 +0800 From: John Garry To: CC: , , , , , John Garry Subject: [PATCH v3 1/6] lib: logic_pio: Fix RCU usage Date: Thu, 27 Jun 2019 00:26:53 +0800 Message-ID: <1561566418-22714-2-git-send-email-john.garry@huawei.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1561566418-22714-1-git-send-email-john.garry@huawei.com> References: <1561566418-22714-1-git-send-email-john.garry@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.212.75] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The traversing of io_range_list with list_for_each_entry_rcu() is not properly protected by rcu_read_lock() and rcu_read_unlock(), so add them. These functions mark the critical section scope where the list is protected for the reader, it cannot be "reclaimed". Any updater - in this case, the logical PIO registration functions - cannot update the list until the reader exits this critical section. In addition, the list traversing used in logic_pio_register_range() does not need to use the rcu variant. This is because we are already using io_range_mutex to guarantee mutual exclusion from mutating the list. Fixes: 031e3601869c ("lib: Add generic PIO mapping method") Signed-off-by: John Garry --- lib/logic_pio.c | 49 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) -- 2.17.1 diff --git a/lib/logic_pio.c b/lib/logic_pio.c index feea48fd1a0d..761296376fbc 100644 --- a/lib/logic_pio.c +++ b/lib/logic_pio.c @@ -46,7 +46,7 @@ int logic_pio_register_range(struct logic_pio_hwaddr *new_range) end = new_range->hw_start + new_range->size; mutex_lock(&io_range_mutex); - list_for_each_entry_rcu(range, &io_range_list, list) { + list_for_each_entry(range, &io_range_list, list) { if (range->fwnode == new_range->fwnode) { /* range already there */ goto end_register; @@ -108,26 +108,38 @@ int logic_pio_register_range(struct logic_pio_hwaddr *new_range) */ struct logic_pio_hwaddr *find_io_range_by_fwnode(struct fwnode_handle *fwnode) { - struct logic_pio_hwaddr *range; + struct logic_pio_hwaddr *range, *found_range = NULL; + rcu_read_lock(); list_for_each_entry_rcu(range, &io_range_list, list) { - if (range->fwnode == fwnode) - return range; + if (range->fwnode == fwnode) { + found_range = range; + break; + } } - return NULL; + rcu_read_unlock(); + + return found_range; } /* Return a registered range given an input PIO token */ static struct logic_pio_hwaddr *find_io_range(unsigned long pio) { - struct logic_pio_hwaddr *range; + struct logic_pio_hwaddr *range, *found_range = NULL; + rcu_read_lock(); list_for_each_entry_rcu(range, &io_range_list, list) { - if (in_range(pio, range->io_start, range->size)) - return range; + if (in_range(pio, range->io_start, range->size)) { + found_range = range; + break; + } } - pr_err("PIO entry token %lx invalid\n", pio); - return NULL; + rcu_read_unlock(); + + if (!found_range) + pr_err("PIO entry token 0x%lx invalid\n", pio); + + return found_range; } /** @@ -180,14 +192,23 @@ unsigned long logic_pio_trans_cpuaddr(resource_size_t addr) { struct logic_pio_hwaddr *range; + rcu_read_lock(); list_for_each_entry_rcu(range, &io_range_list, list) { if (range->flags != LOGIC_PIO_CPU_MMIO) continue; - if (in_range(addr, range->hw_start, range->size)) - return addr - range->hw_start + range->io_start; + if (in_range(addr, range->hw_start, range->size)) { + unsigned long cpuaddr; + + cpuaddr = addr - range->hw_start + range->io_start; + + rcu_read_unlock(); + return cpuaddr; + } } - pr_err("addr %llx not registered in io_range_list\n", - (unsigned long long) addr); + rcu_read_unlock(); + + pr_err("addr %pa not registered in io_range_list\n", &addr); + return ~0UL; }