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 F3645C6FD1D for ; Wed, 15 Mar 2023 06:51:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1ACB26B0072; Wed, 15 Mar 2023 02:51:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 15D028E0001; Wed, 15 Mar 2023 02:51:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F3F476B0075; Wed, 15 Mar 2023 02:51:58 -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 E0E276B0072 for ; Wed, 15 Mar 2023 02:51:58 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9DB3E1C69BA for ; Wed, 15 Mar 2023 06:51:58 +0000 (UTC) X-FDA: 80570212716.14.7C1072D Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by imf16.hostedemail.com (Postfix) with ESMTP id 40B0E18000A for ; Wed, 15 Mar 2023 06:51:56 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=p01TWw+W; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf16.hostedemail.com: domain of quic_zhenhuah@quicinc.com designates 205.220.168.131 as permitted sender) smtp.mailfrom=quic_zhenhuah@quicinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678863116; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=kra+8hgk+4G1Ndw6ydEk7SMxwpkBli/6/VwGY22RaLM=; b=3d5atNvZE6zHpXdWGLdzR0pQ/YCodRXL7bYVnbVkMRwXGNdOsfCjyDTqQM/DZJPritqwbP 0JWgSwD1jyQdHdhfM37+utiG4OaSystfdXxd7muUV8G5gvsZPyHW2jMHWpC5s4spQIb2em uKjTuKy/bDVC25A/anYU59O//CItqok= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=p01TWw+W; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf16.hostedemail.com: domain of quic_zhenhuah@quicinc.com designates 205.220.168.131 as permitted sender) smtp.mailfrom=quic_zhenhuah@quicinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678863116; a=rsa-sha256; cv=none; b=cUl3pytt1fHi8haKwePRSA3Ciy0Ca0e03rmnsLM95MpV23V8kNOpzQqhT9lDGrgVfPsa6/ fz+HxWA7FyGIiQjbvg1E9jukv0J9GCv1ybAuXZER/dX+4x1KsIQcFvTwM4c4WW+EqwXChZ UOdwbVxG+t5tLAKelD6A/FzyasI9K7w= Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 32F5O9FN027420; Wed, 15 Mar 2023 06:51:40 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=message-id : date : mime-version : subject : to : cc : references : from : in-reply-to : content-type : content-transfer-encoding; s=qcppdkim1; bh=kra+8hgk+4G1Ndw6ydEk7SMxwpkBli/6/VwGY22RaLM=; b=p01TWw+Wl1kfEDYyYN1GXlf5IDYNw/GRDwlKU2SrdDjs2Tmi71fjecQcMDKuDShndImc Ojw+YDgWkZSpFRCcY6yKbYbZbjRVjGY+2CHeoWNBf/cnJQepg0bQ18n1dSD1uqEPAKIl tN5Oh9Jd9115McZsNt5Rzr/xxJK7j6PYqfT0nOJWvUR2qOanYWtJh30JQhYpVYjhxqV0 cJr+mGMXEJEs8+OZ4F540u2HDJAYsnvYahy//eCxL1zFp46SNHUZiWMIpPwnCMdlvyAB TELCd/wbLCqti4zxQJ+INQHlrGCN6RJ9RMN0KhAbsvUbbXNx8O2qdKKUGV71Fq4vahD5 jA== Received: from nalasppmta03.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3pb2c98sm6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 15 Mar 2023 06:51:39 +0000 Received: from nalasex01a.na.qualcomm.com (nalasex01a.na.qualcomm.com [10.47.209.196]) by NALASPPMTA03.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32F6pdoq004909 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 15 Mar 2023 06:51:39 GMT Received: from [10.239.132.245] (10.80.80.8) by nalasex01a.na.qualcomm.com (10.47.209.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Tue, 14 Mar 2023 23:51:35 -0700 Message-ID: <3253f502-aa2e-f8c9-b5bd-8eb20e5f6c5e@quicinc.com> Date: Wed, 15 Mar 2023 14:51:32 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: [PATCH v8] mm,kfence: decouple kfence from page granularity mapping judgement Content-Language: en-US To: Pavan Kondeti CC: , , , , , , , , , , , , , , , , References: <1678777502-6933-1-git-send-email-quic_zhenhuah@quicinc.com> <20230314083645.GA556474@hu-pkondeti-hyd.qualcomm.com> <20230314111422.GB556474@hu-pkondeti-hyd.qualcomm.com> From: Zhenhua Huang In-Reply-To: <20230314111422.GB556474@hu-pkondeti-hyd.qualcomm.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01b.na.qualcomm.com (10.46.141.250) To nalasex01a.na.qualcomm.com (10.47.209.196) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: _zmci54ELkPN5x39baaOuMXRVRCzNYNU X-Proofpoint-GUID: _zmci54ELkPN5x39baaOuMXRVRCzNYNU X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-15_02,2023-03-14_02,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 phishscore=0 mlxlogscore=999 clxscore=1015 spamscore=0 mlxscore=0 lowpriorityscore=0 malwarescore=0 bulkscore=0 impostorscore=0 suspectscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2302240000 definitions=main-2303150059 X-Rspamd-Queue-Id: 40B0E18000A X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 516uu5xa7gxdz31b7pegnbkk968aw6b4 X-HE-Tag: 1678863116-320757 X-HE-Meta: U2FsdGVkX1/Iv/LFriOvX/3IjxWuSODm+S/XvRbZzJg/h8mnemXA2dQi5V5iMBkzYbi679QpIDsQ/98jetm/X/8CvVT5hXqgRDoYtIQnZjyXreKtnB5pIQ/f75RvVkEik8o1Xxj/+3TmFc1ls7xJlaPOt5mZ6BymBgyzej0jmi7XXfxq7t4srLA6dx7/PQbNG79fOSkqPO25a2dFtmk3G/g9Z7Q6NonyiVFYzeu3Hgi2F4iW3tUCC77xpP+ZDabHQpoU4jGUgiRoU4qvLm0EBcjvnNWvUMsv8MXAYqnOt1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3geLBp2xCb5+PexSWxIqAoTQ+Gdh+Ofv+7Rk6y90g== 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: Thanks Pavan. On 2023/3/14 19:14, Pavan Kondeti wrote: > On Tue, Mar 14, 2023 at 06:08:07PM +0800, Zhenhua Huang wrote: >> >> >> On 2023/3/14 16:36, Pavan Kondeti wrote: >>> On Tue, Mar 14, 2023 at 03:05:02PM +0800, Zhenhua Huang wrote: >>>> Kfence only needs its pool to be mapped as page granularity, if it is >>>> inited early. Previous judgement was a bit over protected. From [1], Mark >>>> suggested to "just map the KFENCE region a page granularity". So I >>>> decouple it from judgement and do page granularity mapping for kfence >>>> pool only. Need to be noticed that late init of kfence pool still requires >>>> page granularity mapping. >>>> >>>> Page granularity mapping in theory cost more(2M per 1GB) memory on arm64 >>>> platform. Like what I've tested on QEMU(emulated 1GB RAM) with >>>> gki_defconfig, also turning off rodata protection: >>>> Before: >>>> [root@liebao ]# cat /proc/meminfo >>>> MemTotal: 999484 kB >>>> After: >>>> [root@liebao ]# cat /proc/meminfo >>>> MemTotal: 1001480 kB >>>> >>>> To implement this, also relocate the kfence pool allocation before the >>>> linear mapping setting up, arm64_kfence_alloc_pool is to allocate phys >>>> addr, __kfence_pool is to be set after linear mapping set up. >>>> >>>> LINK: [1] https://lore.kernel.org/linux-arm-kernel/Y+IsdrvDNILA59UN@FVFF77S0Q05N/ >>>> Suggested-by: Mark Rutland >>>> Signed-off-by: Zhenhua Huang >>>> --- >>>> arch/arm64/include/asm/kfence.h | 2 ++ >>>> arch/arm64/mm/mmu.c | 44 +++++++++++++++++++++++++++++++++++++++++ >>>> arch/arm64/mm/pageattr.c | 9 +++++++-- >>>> include/linux/kfence.h | 8 ++++++++ >>>> mm/kfence/core.c | 9 +++++++++ >>>> 5 files changed, 70 insertions(+), 2 deletions(-) >>>> >>>> diff --git a/arch/arm64/include/asm/kfence.h b/arch/arm64/include/asm/kfence.h >>>> index aa855c6..f1f9ca2d 100644 >>>> --- a/arch/arm64/include/asm/kfence.h >>>> +++ b/arch/arm64/include/asm/kfence.h >>>> @@ -10,6 +10,8 @@ >>>> #include >>>> +extern phys_addr_t early_kfence_pool; >>>> + >>>> static inline bool arch_kfence_init_pool(void) { return true; } >>>> static inline bool kfence_protect_page(unsigned long addr, bool protect) >>>> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c >>>> index 6f9d889..7fbf2ed 100644 >>>> --- a/arch/arm64/mm/mmu.c >>>> +++ b/arch/arm64/mm/mmu.c >>>> @@ -24,6 +24,7 @@ >>>> #include >>>> #include >>>> #include >>>> +#include >>>> #include >>>> #include >>>> @@ -38,6 +39,7 @@ >>>> #include >>>> #include >>>> #include >>>> +#include >>>> #define NO_BLOCK_MAPPINGS BIT(0) >>>> #define NO_CONT_MAPPINGS BIT(1) >>>> @@ -525,6 +527,33 @@ static int __init enable_crash_mem_map(char *arg) >>>> } >>>> early_param("crashkernel", enable_crash_mem_map); >>>> +#ifdef CONFIG_KFENCE >>>> + >>>> +static phys_addr_t arm64_kfence_alloc_pool(void) >>>> +{ >>>> + phys_addr_t kfence_pool; >>>> + >>>> + if (!kfence_sample_interval) >>>> + return 0; >>>> + >>> >>> Are you sure that kernel commandline param are processed this early? >>> AFAICS, start_kernel()->parse_args() process the kernel arguments. We >>> are here before that. without your patch, mm_init() which takes care of >>> allocating kfence memory is called after parse_args(). >>> >>> Can you check your patch with kfence.sample_interval=0 appended to >>> kernel commandline? >>> >> >> Thanks Pavan. I have tried and you're correct. Previously I thought it's >> parsed by the way: >> setup_arch()->parse_early_param(earlier)->parse_early_options-> >> do_early_param >> Unfortunately seems not take effect. >> >> Then the only way left is we always allocate the kfence pool early? as we >> can't get sample_invertal at this early stage. >> > > That would mean, we would allocate the kfence pool memory even when it > is disabled from commandline. That does not sound good to me. > > Is it possible to free this early allocated memory later in > mm_init()->kfence_alloc_pool()? if that is not possible, can we think of > adding early param for kfence? If we freed that buffer, there may be no chance to get that page granularity mapped buffer again.. as all these allocation/free are through normal buddy allocator. At this stage, seems only additional early param can work.. Marco previously wanted to reuse sample_interval but seems not doable now. Hi Marco, Sorry, Can we thought of the solution again? like ARM64: 1. intercepts early boot arg and gives early alloc memory to KFENCE 2. KFENCE to disable dynamic switch 3. disable page gran and save memory overhead The purpose is in the case of w/o boot arg, it's just same as now.. arch specific kfence buffer will not allocate. And w/ boot arg, we can get expected saving. Thanks, Zhenhua > >>>> + kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE); >>>> + if (!kfence_pool) >>>> + pr_err("failed to allocate kfence pool\n"); >>>> + >>> For whatever reason, if this allocation fails, what should be done? We >>> end up not calling kfence_set_pool(). kfence_alloc_pool() is going to >>> attempt allocation again but we did not setup page granularity. That >>> means, we are enabling KFENCE without meeting pre-conditions. Can you >>> check this? >> >> In this scenario, early_kfence_pool should be false(0) and we will end up >> using page granularity mapping? should be fine IMO. >> > > Right, I missed that hunk in can_set_direct_map(). > > Thanks, > Pavan