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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDC5CC433FE for ; Mon, 8 Nov 2021 08:23:25 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6E07961279 for ; Mon, 8 Nov 2021 08:23:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6E07961279 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id F07816B006C; Mon, 8 Nov 2021 03:23:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EB64A6B0078; Mon, 8 Nov 2021 03:23:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA5CE6B007D; Mon, 8 Nov 2021 03:23:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0134.hostedemail.com [216.40.44.134]) by kanga.kvack.org (Postfix) with ESMTP id C8D8E6B006C for ; Mon, 8 Nov 2021 03:23:24 -0500 (EST) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 874AC8249980 for ; Mon, 8 Nov 2021 08:23:24 +0000 (UTC) X-FDA: 78785073528.31.0636625 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf23.hostedemail.com (Postfix) with ESMTP id E5FB9900009B for ; Mon, 8 Nov 2021 08:23:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1636359803; h=from:from: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; bh=X09/Wf7+2r8y3w0MNY/PV0bQTpv1cgLMiBeLAHQWWQ0=; b=I75/zc6nFU+Rvtwe+PmRznwaadNXL9qE9XCiHwk/nnSLUSOB+nThEKW0c8zYadmmeXHxvn NYYhlZmx+pW192QtPwgKq4J7fQz+3XEwKxmLuKR7T9Qgm9EH6+/d3rITcI40iPjbKOyk64 fkqyUjbY6cnxYmNVRFx/nqaIa0Eh784= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-496--JCg9EmyN86OM9KHmPoIzw-1; Mon, 08 Nov 2021 03:23:20 -0500 X-MC-Unique: -JCg9EmyN86OM9KHmPoIzw-1 Received: by mail-wr1-f72.google.com with SMTP id u4-20020a5d4684000000b0017c8c1de97dso3779615wrq.16 for ; Mon, 08 Nov 2021 00:23:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:organization:in-reply-to :content-transfer-encoding; bh=X09/Wf7+2r8y3w0MNY/PV0bQTpv1cgLMiBeLAHQWWQ0=; b=WSscWA4OVesJpxC9ew1xBtGy2rzVh2U2IhlTusJKH9oIsq/l1iy94hp6qUO+pRXcJV cjtXMBTaa+/7qgYqGxqRuOJ1/jX4ohQsGm1D69SE3sF0hCI3aC6WbPIwYhHKYjkDAnmP Hg61WiKIoy8B1e3vlniBF3qP2o+QaSrzwE38TYpl8E4NsZwsx7NxRoRoLsPqdktb+3UB wvzTOtgBOpYQvVWDpPbP7pxXb9diBWc+nz9ZU31btSZxWPeCSfCnQEei6kskXy5uFeew 7mLX0r+2AotYTdlAB4mu7aS9SHtp8sd9hiTAc2W11/MdgXxC3+B6XHAklahHRN6KB5Fj 6NjQ== X-Gm-Message-State: AOAM533sFs31VM8z8V4YWHbuenS/JMSyye1HXt9KKgsBET97REspqNo0 WAJ5aF5tS+y2ygRCrvritZnqA6HkoPnB/ZgQznFOQX1+QuPGkEHZtL8WnjXh5Ryh1a42mdueFjh qJZODgNX0jM4= X-Received: by 2002:a5d:6c67:: with SMTP id r7mr31031041wrz.286.1636359798800; Mon, 08 Nov 2021 00:23:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJxXPUypB/w4+MfLIgKakWtF8xsXW0dqm7QdItXyKpO2KOl7Hi4ZaHDUW312TnrLnZYKTxjv2g== X-Received: by 2002:a5d:6c67:: with SMTP id r7mr31031014wrz.286.1636359798548; Mon, 08 Nov 2021 00:23:18 -0800 (PST) Received: from [192.168.3.132] (p5b0c6108.dip0.t-ipconnect.de. [91.12.97.8]) by smtp.gmail.com with ESMTPSA id j40sm16090857wms.16.2021.11.08.00.23.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 08 Nov 2021 00:23:17 -0800 (PST) Message-ID: <52d4711c-8034-d81f-865f-ff45e4359cad@redhat.com> Date: Mon, 8 Nov 2021 09:23:16 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 Subject: Re: [PATCH] mm/kmemleak: Avoid scanning potential huge holes To: Lang Yu , Andrew Morton Cc: linux-mm@kvack.org, Catalin Marinas , linux-kernel@vger.kernel.org References: <20211105035241.1239751-1-lang.yu@amd.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: E5FB9900009B X-Stat-Signature: qine13h5fn8qjiwrmf5kfrcrjjows5i3 Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="I75/zc6n"; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf23.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com X-HE-Tag: 1636359789-191007 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: On 08.11.21 08:27, Lang Yu wrote: > On Fri, Nov 05, 2021 at 02:14:50PM +0100, David Hildenbrand wrote: >> On 05.11.21 04:52, Lang Yu wrote: >>> When using devm_request_free_mem_region() and >>> devm_memremap_pages() to add ZONE_DEVICE memory, if requested >>> free mem region pfn were huge(e.g., 0x400000000 ,we found >>> on some amd apus, amdkfd svm will request a such free mem region), >>> the node_end_pfn() will be also huge(see move_pfn_range_to_zone()). >>> It creates a huge hole between node_start_pfn() and node_end_pfn(). >>> >>> In such a case, following code snippet acctually was >>> just doing busy test_bit() looping on the huge hole. >>> >>> for (pfn = start_pfn; pfn < end_pfn; pfn++) { >>> struct page *page = pfn_to_online_page(pfn); >>> if (!page) >>> continue; >>> ... >>> } >>> >>> So we got a soft lockup: >>> >>> watchdog: BUG: soft lockup - CPU#6 stuck for 26s! [bash:1221] >>> CPU: 6 PID: 1221 Comm: bash Not tainted 5.15.0-custom #1 >>> RIP: 0010:pfn_to_online_page+0x5/0xd0 >>> Call Trace: >>> ? kmemleak_scan+0x16a/0x440 >>> kmemleak_write+0x306/0x3a0 >>> ? common_file_perm+0x72/0x170 >>> full_proxy_write+0x5c/0x90 >>> vfs_write+0xb9/0x260 >>> ksys_write+0x67/0xe0 >>> __x64_sys_write+0x1a/0x20 >>> do_syscall_64+0x3b/0xc0 >>> entry_SYSCALL_64_after_hwframe+0x44/0xae >>> >>> I did some tests with the patch. >>> >>> (1) amdgpu module unloaded >>> >>> before the patch: >>> >>> real 0m0.976s >>> user 0m0.000s >>> sys 0m0.968s >>> >>> after the patch: >>> >>> real 0m0.981s >>> user 0m0.000s >>> sys 0m0.973s >>> >>> (2) amdgpu module loaded >>> >>> before the patch: >>> >>> real 0m35.365s >>> user 0m0.000s >>> sys 0m35.354s >>> >>> after the patch: >>> >>> real 0m1.049s >>> user 0m0.000s >>> sys 0m1.042s >>> >>> Signed-off-by: Lang Yu >>> --- >>> mm/kmemleak.c | 9 +++++---- >>> 1 file changed, 5 insertions(+), 4 deletions(-) >>> >>> diff --git a/mm/kmemleak.c b/mm/kmemleak.c >>> index b57383c17cf6..d07444613a84 100644 >>> --- a/mm/kmemleak.c >>> +++ b/mm/kmemleak.c >>> @@ -1403,6 +1403,7 @@ static void kmemleak_scan(void) >>> { >>> unsigned long flags; >>> struct kmemleak_object *object; >>> + struct zone *zone; >>> int i; >>> int new_leaks = 0; >>> >>> @@ -1443,9 +1444,9 @@ static void kmemleak_scan(void) >>> * Struct page scanning for each node. >>> */ >>> get_online_mems(); >>> - for_each_online_node(i) { >>> - unsigned long start_pfn = node_start_pfn(i); >>> - unsigned long end_pfn = node_end_pfn(i); >>> + for_each_populated_zone(zone) { >>> + unsigned long start_pfn = zone->zone_start_pfn; >>> + unsigned long end_pfn = zone_end_pfn(zone); >>> unsigned long pfn; >>> >>> for (pfn = start_pfn; pfn < end_pfn; pfn++) { >>> @@ -1455,7 +1456,7 @@ static void kmemleak_scan(void) >>> continue; >>> >>> /* only scan pages belonging to this node */ >>> - if (page_to_nid(page) != i) >>> + if (page_to_nid(page) != zone_to_nid(zone)) >> >> With overlapping zones you might rescan ranges ... instead we should do: >> >> /* only scan pages belonging to this zone */ >> if (zone != page_zone(page)) >> ... >> >> Or alternatively: >> >> /* only scan pages belonging to this node */ >> if (page_to_nid(page) != zone_to_nid(zone)) >> continue; >> /* only scan pages belonging to this zone */ >> if (page_zonenum(page) != zone_idx(zone)) >> continue; > > The original code has covered that, i.e., > only scan pages belonging to this node. > I didn't change that behavior. Again, you can easily have overlapping zones -- ZONE_NORMAL and ZONE_MOVABLE -- in which case, a PFN is spanned by multiple zones, but only belongs to a single zone. The original code would scan each PFN exactly once, as it was iterating the node PFNs. Your changed code might scan a single PFN multiple times, if it's spanned by multiple zones. -- Thanks, David / dhildenb