Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2988263imu; Sun, 9 Dec 2018 14:23:58 -0800 (PST) X-Google-Smtp-Source: AFSGD/XfhP4Cte3iw3poTxIfgDtVw+OUjAbOKZ5NxyD7Rydg1UHYiAahTmTh6HT3aUeDeiCfPEsT X-Received: by 2002:a63:9501:: with SMTP id p1mr8945795pgd.149.1544394238860; Sun, 09 Dec 2018 14:23:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544394238; cv=none; d=google.com; s=arc-20160816; b=lNSVLVR3JXdojtst3GyVWsaPvWjg6JdZAnjR1KVI5LnIhN2u8Bm91xI4hfPjhXxGlK 6cY8eFZLnjCl2saL6A8O0u4bi3WDvoaEmDTX4++ncvI+SJIojLbQ7dcAWNARsRxQgfBV kFnCFgRUls5KiD7x7rMOkKSoSdzHtF8nLxMddCKpnp/TtsvHHzMj7byVjKAl2jUTNzqa 3sKjYXYw9o3vijYCn4MgQT/C37QSxY3vHBZarAdWA21TyVfaIJY2qPHOxebzGCzUVpzg NN/RxJ0VVxhngsn1pUlhNuW4fwdU7eQ9xpkhpZdpHngjTCyqdr1yA2yJe/oifZyhRFa2 pJlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=/ME97iBsiPZMYhN+X4QWPjh+fecJNA2jbuZwQpkJDBo=; b=LwO2L0PisUDzdFc4H6yCeGrgpgLa7P57ittpewIQHqYTRe9ldY8TLbWc3BIq5/HkuW Kuoxtq/GP5Ctg1+M0BrsV/8SFhGUo6M2tN6Qx0nctJ4yj7EyDd8bBufUXQeKNkuJ9nr4 U4IYFGq8q+OnSEe+AcRNyEKmaihKEMGpjhJYLGn2qFgxFwogsjroVIg1sCxUoPnMZLZJ PcHL+D2LoXZFrWE4V5G8xBY66/SrwV79joJXlmRrgLC1Z1D6by7Br668+4fEA22m/M2E H00hreodU35Jg4NeT59/B+EYmnwt3eZ4yCRk/ID9roDzDjzClHFDeIec+hZkFwYg9Rd1 fGFQ== 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 w12si8868704pfn.212.2018.12.09.14.23.43; Sun, 09 Dec 2018 14:23:58 -0800 (PST) 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 S1726839AbeLIWW1 (ORCPT + 99 others); Sun, 9 Dec 2018 17:22:27 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:35236 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726525AbeLIVzV (ORCPT ); Sun, 9 Dec 2018 16:55:21 -0500 Received: from pub.yeoldevic.com ([81.174.156.145] helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gW72l-0002if-9X; Sun, 09 Dec 2018 21:55:19 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gW72i-0003ZW-07; Sun, 09 Dec 2018 21:55:16 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Bin Yang" , "Kees Cook" Date: Sun, 09 Dec 2018 21:50:33 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 239/328] pstore: Fix incorrect persistent ram buffer mapping In-Reply-To: X-SA-Exim-Connect-IP: 81.174.156.145 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.62-rc1 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") Signed-off-by: Kees Cook Signed-off-by: Ben Hutchings --- 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 @@ -376,7 +376,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, @@ -395,6 +400,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; } @@ -415,7 +425,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; @@ -461,7 +471,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);