Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2503079imm; Mon, 24 Sep 2018 05:34:19 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZyR3LnX3i9/5aHgdmIPBjppPJ0NaCcg7TFbS1xiwBKfKrLCxYNulyb/1OlS4WDWUf8s6Os X-Received: by 2002:a62:5d89:: with SMTP id n9-v6mr10275007pfj.102.1537792459080; Mon, 24 Sep 2018 05:34:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537792459; cv=none; d=google.com; s=arc-20160816; b=bBRPnK+gO9Zzr1lre6bVnkFUW5L/F9uea+4DuiSefhcSQdJXG2ObYtWCfCgNH2Oi3D mpPi7LQiZkDwJkF9yIm8bgo89TlLXxUKR+DYd9X2PtwX0+/8MkYsDVfgt+ttl5mJza8r aYvdo01Fik6+PjYVgn+nIdFa6qCOQf5Aa6zUm0PZak1rexW2pccXnrifLbZqJ7uy1IED t/uvHR87AMTaS4YLqyOccYxf6EG1rLCXDS6x9UHTL4++hDiY0B3i6Y1/iwiiYuSj2Ib8 CbBBTjJuBwMDZ2twByuv1SwIXU0etrNB829PbG7bi/mW68npBwQeOogdAl/qRzdtmNoy E8fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from; bh=1YVkV2TjOUShmfdZ7kMJM9E9MfE3dhujlUznQn6Mzac=; b=kaVSlek764HtF+NIF558DpItsL2xjMGAMu0ZC/3gteUME2X1LvE52gtRrbZN+hZbGm VOfNL5WTwbnz/kz+WGuobyLZ0DacMIGpdnjm0RZLSCTMKrxZzEePoyWNU/zC5TyYcqoT hODyzwEwyUTuTj46BJds3vtlqWRRCZPmTwNzPPO8QlajSIU64uEsTB9WaHdiddl9AavT 5MJlpd1VCfmDrYkdV7N/SUHckflSBbwLbwCy4M6EtFFBnptIY03lctz0vpPAaugp9m4I J073EqGrIYXmA0DkJLfPKqmcZ4mFHHXaEQpJ1UhU1DLEy+R3CAU+XHsztgCVGkHo1s/l iDkA== 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 b190-v6si35967015pfg.94.2018.09.24.05.34.03; Mon, 24 Sep 2018 05:34:19 -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 S2387900AbeIXSfH (ORCPT + 99 others); Mon, 24 Sep 2018 14:35:07 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:58326 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733238AbeIXSfG (ORCPT ); Mon, 24 Sep 2018 14:35:06 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id D9DE61018; Mon, 24 Sep 2018 12:33:09 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Bin Yang , Kees Cook Subject: [PATCH 4.18 100/235] pstore: Fix incorrect persistent ram buffer mapping Date: Mon, 24 Sep 2018 13:51:26 +0200 Message-Id: <20180924113115.397497790@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180924113103.999624566@linuxfoundation.org> References: <20180924113103.999624566@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Bin Yang commit 831b624df1b420c8f9281ed1307a8db23afb72df upstream. persistent_ram_vmap() returns the page start vaddr. persistent_ram_iomap() supports non-page-aligned mapping. persistent_ram_buffer_map() always adds offset-in-page to the vaddr returned from these two functions, which causes incorrect mapping of non-page-aligned persistent ram buffer. By default ftrace_size is 4096 and max_ftrace_cnt is nr_cpu_ids. Without this patch, the zone_sz in ramoops_init_przs() is 4096/nr_cpu_ids which might not be page aligned. If the offset-in-page > 2048, the vaddr will be in next page. If the next page is not mapped, it will cause kernel panic: [ 0.074231] BUG: unable to handle kernel paging request at ffffa19e0081b000 ... [ 0.075000] RIP: 0010:persistent_ram_new+0x1f8/0x39f ... [ 0.075000] Call Trace: [ 0.075000] ramoops_init_przs.part.10.constprop.15+0x105/0x260 [ 0.075000] ramoops_probe+0x232/0x3a0 [ 0.075000] platform_drv_probe+0x3e/0xa0 [ 0.075000] driver_probe_device+0x2cd/0x400 [ 0.075000] __driver_attach+0xe4/0x110 [ 0.075000] ? driver_probe_device+0x400/0x400 [ 0.075000] bus_for_each_dev+0x70/0xa0 [ 0.075000] driver_attach+0x1e/0x20 [ 0.075000] bus_add_driver+0x159/0x230 [ 0.075000] ? do_early_param+0x95/0x95 [ 0.075000] driver_register+0x70/0xc0 [ 0.075000] ? init_pstore_fs+0x4d/0x4d [ 0.075000] __platform_driver_register+0x36/0x40 [ 0.075000] ramoops_init+0x12f/0x131 [ 0.075000] do_one_initcall+0x4d/0x12c [ 0.075000] ? do_early_param+0x95/0x95 [ 0.075000] kernel_init_freeable+0x19b/0x222 [ 0.075000] ? rest_init+0xbb/0xbb [ 0.075000] kernel_init+0xe/0xfc [ 0.075000] ret_from_fork+0x3a/0x50 Signed-off-by: Bin Yang [kees: add comments describing the mapping differences, updated commit log] Fixes: 24c3d2f342ed ("staging: android: persistent_ram: Make it possible to use memory outside of bootmem") Cc: stable@vger.kernel.org Signed-off-by: Kees Cook Signed-off-by: Greg Kroah-Hartman --- fs/pstore/ram_core.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) --- a/fs/pstore/ram_core.c +++ b/fs/pstore/ram_core.c @@ -429,7 +429,12 @@ static void *persistent_ram_vmap(phys_ad vaddr = vmap(pages, page_count, VM_MAP, prot); kfree(pages); - return vaddr; + /* + * Since vmap() uses page granularity, we must add the offset + * into the page here, to get the byte granularity address + * into the mapping to represent the actual "start" location. + */ + return vaddr + offset_in_page(start); } static void *persistent_ram_iomap(phys_addr_t start, size_t size, @@ -448,6 +453,11 @@ static void *persistent_ram_iomap(phys_a else va = ioremap_wc(start, size); + /* + * Since request_mem_region() and ioremap() are byte-granularity + * there is no need handle anything special like we do when the + * vmap() case in persistent_ram_vmap() above. + */ return va; } @@ -468,7 +478,7 @@ static int persistent_ram_buffer_map(phy return -ENOMEM; } - prz->buffer = prz->vaddr + offset_in_page(start); + prz->buffer = prz->vaddr; prz->buffer_size = size - sizeof(struct persistent_ram_buffer); return 0; @@ -515,7 +525,8 @@ void persistent_ram_free(struct persiste if (prz->vaddr) { if (pfn_valid(prz->paddr >> PAGE_SHIFT)) { - vunmap(prz->vaddr); + /* We must vunmap() at page-granularity. */ + vunmap(prz->vaddr - offset_in_page(prz->paddr)); } else { iounmap(prz->vaddr); release_mem_region(prz->paddr, prz->size);