From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D515C433FE for ; Thu, 6 Oct 2022 04:06:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B1CAF6B0071; Thu, 6 Oct 2022 00:06:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ACC386B0073; Thu, 6 Oct 2022 00:06:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96CFA8E0001; Thu, 6 Oct 2022 00:06:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 823576B0071 for ; Thu, 6 Oct 2022 00:06:09 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 53E84160376 for ; Thu, 6 Oct 2022 04:06:09 +0000 (UTC) X-FDA: 79989186858.17.EA94A63 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf29.hostedemail.com (Postfix) with ESMTP id 835A612001B for ; Thu, 6 Oct 2022 04:06:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1665029167; x=1696565167; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=odAs4dSBq7mP4T+DKtVrZI0lz9QUelG3wbSlf8i5IJ0=; b=Gp0l6B2Sg4V9p4fHdsWdmdPD07IpUqpUm5U81Q4vznx/NUahObfMmsGS yDB8byR8lITS39KM/R0+vczK9MO0JYj8lrsA0nLq6d41gkjWebLcu4kVJ 1RL1oehhnE9IFYUJ8ID7/cYSiCZqP4kg6b9EX26ky7tgJ+ytg4Q9f1qz+ GPORLTeq2fUcpb+rkGtHA0ZzZwtNdMknXC0UTAngZWYW9raqGnITmo6Bm iPiePk8V+MLOlBpvxyPBxg1EXJzfyrXnWqU96+cQfDvBYCblGTVkZELfY VOICgNzCwwaCdKhRAQ53t1JzBXEPu7pG+55QPLR5tzzRTT3/ZPREVGucM A==; X-IronPort-AV: E=McAfee;i="6500,9779,10491"; a="304916924" X-IronPort-AV: E=Sophos;i="5.95,162,1661842800"; d="scan'208";a="304916924" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Oct 2022 21:06:05 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10491"; a="713685040" X-IronPort-AV: E=Sophos;i="5.95,162,1661842800"; d="scan'208";a="713685040" Received: from iweiny-desk3.amr.corp.intel.com (HELO localhost) ([10.213.182.164]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Oct 2022 21:06:05 -0700 From: ira.weiny@intel.com To: Andrew Morton Cc: Ira Weiny , "Fabio M. De Francesco" , Thomas Gleixner , Christoph Hellwig , kernel test robot , Al Viro , Linus Walleij , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH V2] highmem: Fix kmap_to_page() for kmap_local_page() addresses Date: Wed, 5 Oct 2022 21:05:55 -0700 Message-Id: <20221006040555.1502679-1-ira.weiny@intel.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665029168; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=cccwt4bU/uAcuhZ3KLq+0sdeNa7105bWLYYNMwLmm5g=; b=pFnJgLfQg5T6wtUvwAPr2vVx7vMJpZkDOlkcGMKwS5VJg/h05SDH/rr+7JGmXXwAp/O/pf u8JaA7cdytS5m8EgcfU9cA8Inmnwb5WrS2CKSdMTKRS2c5TUpuLFN8ZnkigIUN0SjJS3wM j5ohCj4QORLlss1n6zRc1ViJdIX3Qj8= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=Gp0l6B2S; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf29.hostedemail.com: domain of ira.weiny@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ira.weiny@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665029168; a=rsa-sha256; cv=none; b=ui/zVbwteOb52mSsoBikey61B2tMZRgFl2rheuLOIAjR00LIdzjwSIdtRdLeCHzi8lDRIZ RC/KnkbTXgSyEROivezDonmcCky2aiuv+ILtSRfVhkOPKUd1Z1clrAqPbwpY/xCW12aITr YHkORPVllbpExBMnDuKYXtSSMGIHXCk= X-Rspamd-Queue-Id: 835A612001B Authentication-Results: imf29.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=Gp0l6B2S; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf29.hostedemail.com: domain of ira.weiny@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ira.weiny@intel.com X-Rspam-User: X-Rspamd-Server: rspam10 X-Stat-Signature: am3pu6npz6su8qtgq3w5fxkpyh6c5ujn X-HE-Tag: 1665029167-448321 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Ira Weiny kmap_to_page() is used to get the page for a virtual address which may be kmap'ed. Unfortunately, kmap_local_page() stores mappings in a thread local array separate from kmap(). These mappings were not checked by the call. Check the kmap_local_page() mappings and return the page if found. Because it is intended to remove kmap_to_page() add a warn on once to the kmap checks to flag potential issues early. NOTE Due to 32bit x86 use of kmap local in iomap atmoic, KMAP_LOCAL does not require HIGHMEM to be set. Therefore the support calls required a new KMAP_LOCAL section to fix 0day build errors. Cc: "Fabio M. De Francesco" Cc: Thomas Gleixner Cc: Christoph Hellwig Cc: Andrew Morton Reported-by: kernel test robot Reported-by: Al Viro Signed-off-by: Ira Weiny --- Changes from V1: Fix 0day build errors. I'm still working toward getting rid of kmap_to_page.[1] But until then this fix should be applied. [1] https://lore.kernel.org/linux-mm/20221002002326.946620-1-ira.weiny@intel.com/ --- mm/highmem.c | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/mm/highmem.c b/mm/highmem.c index c707d7202d5f..529987b28205 100644 --- a/mm/highmem.c +++ b/mm/highmem.c @@ -30,6 +30,17 @@ #include #include +#ifdef CONFIG_KMAP_LOCAL +static inline int kmap_local_calc_idx(int idx) +{ + return idx + KM_MAX_IDX * smp_processor_id(); +} + +#ifndef arch_kmap_local_map_idx +#define arch_kmap_local_map_idx(idx, pfn) kmap_local_calc_idx(idx) +#endif +#endif /* CONFIG_KMAP_LOCAL */ + /* * Virtual_count is not a pure "count". * 0 means that it is not mapped, and has not been mapped @@ -142,14 +153,34 @@ pte_t *pkmap_page_table; struct page *__kmap_to_page(void *vaddr) { + unsigned long base = (unsigned long) vaddr & PAGE_MASK; + struct kmap_ctrl *kctrl = ¤t->kmap_ctrl; unsigned long addr = (unsigned long)vaddr; + int i; - if (addr >= PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP)) { + /* kmap() mappings */ + if (WARN_ON_ONCE(addr >= PKMAP_ADDR(0) && + addr < PKMAP_ADDR(LAST_PKMAP))) { int i = PKMAP_NR(addr); return pte_page(pkmap_page_table[i]); } + /* kmap_local_page() mappings */ + if (WARN_ON_ONCE(base >= __fix_to_virt(FIX_KMAP_END) && + base < __fix_to_virt(FIX_KMAP_BEGIN))) { + for (i = 0; i < kctrl->idx; i++) { + unsigned long base_addr; + int idx; + + idx = arch_kmap_local_map_idx(i, pte_pfn(pteval)); + base_addr = __fix_to_virt(FIX_KMAP_BEGIN + idx); + + if (base_addr == base) + return pte_page(kctrl->pteval[i]); + } + } + return virt_to_page(vaddr); } EXPORT_SYMBOL(__kmap_to_page); @@ -462,10 +493,6 @@ static inline void kmap_local_idx_pop(void) # define arch_kmap_local_post_unmap(vaddr) do { } while (0) #endif -#ifndef arch_kmap_local_map_idx -#define arch_kmap_local_map_idx(idx, pfn) kmap_local_calc_idx(idx) -#endif - #ifndef arch_kmap_local_unmap_idx #define arch_kmap_local_unmap_idx(idx, vaddr) kmap_local_calc_idx(idx) #endif @@ -494,11 +521,6 @@ static inline bool kmap_high_unmap_local(unsigned long vaddr) return false; } -static inline int kmap_local_calc_idx(int idx) -{ - return idx + KM_MAX_IDX * smp_processor_id(); -} - static pte_t *__kmap_pte; static pte_t *kmap_get_pte(unsigned long vaddr, int idx) -- 2.37.2