Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp1297349iol; Sun, 12 Jun 2022 11:42:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxtDMqA10+4HgM/iodDtZGOWmqXyvXJcIGxN5UYpuU1BrkR8lPyB3ICb5s6sBJ6qZpbDF1i X-Received: by 2002:a05:6402:4248:b0:431:1133:aecb with SMTP id g8-20020a056402424800b004311133aecbmr47938401edb.222.1655059336661; Sun, 12 Jun 2022 11:42:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655059336; cv=none; d=google.com; s=arc-20160816; b=VqaDFZYAupECAhOe5aLle2Q+LAW64LjtwpHQeYRXqHYpaFUjdc2uXMfUrimTsXONuw 0ahZnFJYkUV38lbcn5tODus2CWg649OUZZs8a/wo7+GV4G8accengejeHVdFRGBGA1oy slmk6WQ7Y5/tR6hzI5Fzea18RZsqluygL4mFdSec/kdwGsIWE4T0Aeag5oGzFxmVj0x4 8s3d/FqK2vmiXkHaX0e/iNkWJNenEwCgfLKVVrwmzVxa7dmj6X9/TIjjTjAuXLiFprnY H6dl0LM8Q0EithXWFfNZH3Gp6tmIglcZ/3l938YelbBicKfMCiPgXYApbVY7P0/EALsA yaRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XR7KB+0VWMpvWvXRkdA4FDcU+xomfv79CCDTuY4yKdw=; b=rRJMpBp5L6UY/eZasWy6gHUBYN1igtvQYqK8uIDZ+Y5LnxVgTjfnXzstaf5WWYBR+F kNPf3iGJJSGoKYcCV1u5Vgtzo5Quo1v98w+rhtPc35k62Mat5436sIcfytUaU4wdYV9i qdlSzi+0zLExKpVroPn4XK+s5TVdEcHn4lKBmsU4GQw5v3lrF73/jzpDMGv7p3T78uMT vNBBwU8zzEkbSF5t83S+1frY/Eq+jQWL8nj0tIV5rpjCzySzX3hHGJamantYtlI+dIt/ YQdiRCCZckkpveIE1u65Dnb38QcIURpb4rS3WW+61D633W04WmaIT1v3p3j7Gq/WB2Nv RW/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CJbD2Zg1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gv43-20020a1709072beb00b006fefb7a6361si4401245ejc.164.2022.06.12.11.41.52; Sun, 12 Jun 2022 11:42:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CJbD2Zg1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233247AbiFLSdq (ORCPT + 99 others); Sun, 12 Jun 2022 14:33:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232911AbiFLSdO (ORCPT ); Sun, 12 Jun 2022 14:33:14 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D7A92DAF for ; Sun, 12 Jun 2022 11:33:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655058792; 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=XR7KB+0VWMpvWvXRkdA4FDcU+xomfv79CCDTuY4yKdw=; b=CJbD2Zg1VsdjtH6iIZRnG+/Yu6+MzU8ZqF1HmjJUM4fcUjgT7TjwEUPQJhlWZAdS5WdmQg 8cwAdEwo7B0/ZQ/F6xfSZDZisLgFKzGv1q56S5JrYNX/8wbHUjRQAFC8916qjhyKatQMEo sjPUrslsqY3HnheAkmxF6aLB1KDgYaY= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-373-XB4a8fA1NSuW2fTRQfxo-Q-1; Sun, 12 Jun 2022 14:33:09 -0400 X-MC-Unique: XB4a8fA1NSuW2fTRQfxo-Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2B9133C02B7F; Sun, 12 Jun 2022 18:33:09 +0000 (UTC) Received: from llong.com (unknown [10.22.8.63]) by smtp.corp.redhat.com (Postfix) with ESMTP id E76E440C1247; Sun, 12 Jun 2022 18:33:08 +0000 (UTC) From: Waiman Long To: Catalin Marinas , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Waiman Long Subject: [PATCH 2/3] mm/kmemleak: Skip unlikely objects in kmemleak_scan() without taking lock Date: Sun, 12 Jun 2022 14:33:00 -0400 Message-Id: <20220612183301.981616-3-longman@redhat.com> In-Reply-To: <20220612183301.981616-1-longman@redhat.com> References: <20220612183301.981616-1-longman@redhat.com> MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are 3 RCU-based object iteration loops in kmemleak_scan(). Because of the need to take RCU read lock, we can't insert cond_resched() into the loop like other parts of the function. As there can be millions of objects to be scanned, it takes a while to iterate all of them. The kmemleak functionality is usually enabled in a debug kernel which is much slower than a non-debug kernel. With sufficient number of kmemleak objects, the time to iterate them all may exceed 22s causing soft lockup. watchdog: BUG: soft lockup - CPU#3 stuck for 22s! [kmemleak:625] In this particular bug report, the soft lockup happen in the 2nd iteration loop. In the 2nd and 3rd loops, most of the objects are checked and then skipped under the object lock. Only a selected fews are modified. Those objects certainly need lock protection. However, the lock/unlock operation is slow especially with interrupt disabling and enabling included. We can actually do some basic check like color_white() without taking the lock and skip the object accordingly. Of course, this kind of check is racy and may miss objects that are being modified concurrently. The cost of missed objects, however, is just that they will be discovered in the next scan instead. The advantage of doing so is that iteration can be done much faster especially with LOCKDEP enabled in a debug kernel. With a debug kernel running on a 2-socket 96-thread x86-64 system (HZ=1000), the 2nd and 3rd iteration loops speedup with this patch on the first kmemleak_scan() call after bootup is shown in the table below. Before patch After patch Loop # # of objects Elapsed time # of objects Elapsed time ------ ------------ ------------ ------------ ------------ 2 2,599,850 2.392s 2,596,364 0.266s 3 2,600,176 2.171s 2,597,061 0.260s This patch reduces loop iteration times by about 88%. This will greatly reduce the chance of a soft lockup happening in the 2nd or 3rd iteration loops. Signed-off-by: Waiman Long --- mm/kmemleak.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index dad9219c972c..7dd64139a7c7 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -1508,6 +1508,13 @@ static void kmemleak_scan(void) */ rcu_read_lock(); list_for_each_entry_rcu(object, &object_list, object_list) { + /* + * This is racy but we can save the overhead of lock/unlock + * calls. The missed objects, if any, should be caught in + * the next scan. + */ + if (!color_white(object)) + continue; raw_spin_lock_irq(&object->lock); if (color_white(object) && (object->flags & OBJECT_ALLOCATED) && update_checksum(object) && get_object(object)) { @@ -1535,6 +1542,13 @@ static void kmemleak_scan(void) */ rcu_read_lock(); list_for_each_entry_rcu(object, &object_list, object_list) { + /* + * This is racy but we can save the overhead of lock/unlock + * calls. The missed objects, if any, should be caught in + * the next scan. + */ + if (!color_white(object)) + continue; raw_spin_lock_irq(&object->lock); if (unreferenced_object(object) && !(object->flags & OBJECT_REPORTED)) { -- 2.31.1