Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3779522pxj; Mon, 7 Jun 2021 21:09:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyMZy+NnhWUJu+6Ks8Sh4lottNB0FtaBKNl7iF31+uyG1hGneluoW8G8vJpo6BbWuLg2BF X-Received: by 2002:a05:6402:49a:: with SMTP id k26mr8560626edv.279.1623125341119; Mon, 07 Jun 2021 21:09:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623125341; cv=none; d=google.com; s=arc-20160816; b=P6IYZqLqra4afjMvehzu02uRlkVTYtMDnQbVAls5T9xRfALJD1OdjII+5O3VuxtNKi N5UaCFDkLLqigfEz38wOSXNkq8NTTLax3yGnsrR920hI0A6oV1Bzad6+Nq8jyerX1FiR ZIrznzT7Ve/ejHVtC80pkc9rAj6CkR7M19gUw/xCnwd40n2YjSVb0gh6+qj41qt59yKL ZbLyqI+WGFhLEzpdY4pav+K9wmxxNwrpvHw62/XnA+TqObaqsWUu9koMWhyl/8wa2v7O EeSka1/I2Vl8/JLc87ML7CmhcKrxgoqxUz7eHeQzC/WeVcVLzZ2lktt2VMFusCvYN6XE ENTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:references:cc:to:subject :from; bh=Nxg98DfYCKxh6oGawhWFvgWtRau4t1tvIJusFHo/lYE=; b=qsjG9Ve9LDts3DGnaQ4hAY2FyO8jk4eToo6W8llMQvELbJLVbemjixwhlTy1934FpQ 2wxG3II8MIQYPnKKtcjD+XI4YYrCXzQQdR46lYkAQokfH9euWQKkDKHcA73XHTGmKUDu /0cBqQGRGXrefXJGuBorGMa1boz3SmiRn5UUnTci+4t2+sN7RJxY3xOG1AIZjmzIst3m RiJNLycn1jln82nIfjTx4n1vISjbIsx54D1mqGPW1U6VGFQ5exYU6taOM+ka3YRVBPRv RoAanHTkW5ht809nXF41w+eocTDpr4cEhlFoULVuGayfqiOlo6W1XW0XeBEPGScycZVb 43iw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p7si14033209ejn.515.2021.06.07.21.08.37; Mon, 07 Jun 2021 21:09:01 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231253AbhFHEGO (ORCPT + 99 others); Tue, 8 Jun 2021 00:06:14 -0400 Received: from out30-42.freemail.mail.aliyun.com ([115.124.30.42]:60661 "EHLO out30-42.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231238AbhFHEGN (ORCPT ); Tue, 8 Jun 2021 00:06:13 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=alimailimapcm10staff010182156082;MF=yaohuiwang@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0Ubixn0M_1623125043; Received: from Dillions-MBP-16.local(mailfrom:yaohuiwang@linux.alibaba.com fp:SMTPD_---0Ubixn0M_1623125043) by smtp.aliyun-inc.com(127.0.0.1); Tue, 08 Jun 2021 12:04:19 +0800 From: Yaohui Wang Subject: Re: [PATCH] mm: fix pfn calculation mistake in __ioremap_check_ram To: Dave Hansen , dave.hansen@linux.intel.com Cc: luto@kernel.org, peterz@infradead.org, linux-kernel@vger.kernel.org, yaohuiwang@linux.alibaba.com, luoben@linux.alibaba.com References: <20210607091938.47960-1-yaohuiwang@linux.alibaba.com> Message-ID: <0d1a308b-4d0e-d91a-52a7-6456ec6713f8@linux.alibaba.com> Date: Tue, 8 Jun 2021 12:04:03 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/6/7 21:55, Dave Hansen wrote: > On 6/7/21 2:19 AM, Yaohui Wang wrote: >> According to the source code in function >> arch/x86/mm/ioremap.c:__ioremap_caller, after __ioremap_check_mem, if the >> mem range is IORES_MAP_SYSTEM_RAM, then __ioremap_caller should fail. But >> because of the pfn calculation problem, __ioremap_caller can success >> on IORES_MAP_SYSTEM_RAM region when the @size parameter is less than >> PAGE_SIZE. This may cause misuse of the ioremap function and raise the >> risk of performance issues. For example, ioremap(phys, PAGE_SIZE-1) may >> cause the direct memory mapping of @phys to be uncached, and iounmap won't >> revert this change. This patch fixes this issue. >> >> In arch/x86/mm/ioremap.c:__ioremap_check_ram, start_pfn should wrap down >> the res->start address, and end_pfn should wrap up the res->end address. >> This makes the check more strict and should be more reasonable. > > Was this found by inspection, or was there a real-world bug which this > patch addresses? > I did a performance test for linux kernel in many aspects. One of my scripts is to test the performance influence of ioremap. I found that applying ioremap on normal RAM may cause terrible performance issues. To avoid memory cache behavior aliasing, ioremap will call memtype_kernel_map_sync to sync the cache attribute in the directing mapping, which causes: 1. If the phys addr is in a huge page in the directing mapping, then ioremap will split the huge page into 4K pages. 2. It will set the PCD bit in the directing mapping pte. Both the above behaviors will downgrade the performance of the machine, especially when there is important code/data which is accessed frequently. What's worse, iounmap won't clear the PCD bit in the directing mapping pte, and I need to call ioremap_cache to clear the PCD bit. All these should be avoided. Another thing also confuses me: From __ioremap_caller, we can see that __ioremap_caller don't allow us to remap normal RAM. In my understanding, direct mapping only maps normal RAM. So if the remap behavior is not allowed on normal RAM, it should be unnecessary to call memtype_kernel_map_sync. Is this right?