From patchwork Tue Jun 14 22:03:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 12881650 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 75201C43334 for ; Tue, 14 Jun 2022 22:04:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 002446B0073; Tue, 14 Jun 2022 18:04:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECE1A6B0075; Tue, 14 Jun 2022 18:04:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D5FC46B0074; Tue, 14 Jun 2022 18:04:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C79B86B0072 for ; Tue, 14 Jun 2022 18:04:20 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9D9F8611B2 for ; Tue, 14 Jun 2022 22:04:20 +0000 (UTC) X-FDA: 79578220680.11.F3BE2D5 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf27.hostedemail.com (Postfix) with ESMTP id 244064009E for ; Tue, 14 Jun 2022 22:04:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655244259; 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=x10p2RYJ/7U/x7uwqRvY5a/J+6w+5v/qLg41hZfOPsk=; b=i6KmiBEnWPEzhA9GihlXOX/EswvVDgAI+oJa5bDrY3nY1IF0uFduwEsYzBzRmyOk3dPOEi NONZ0ulj9lka3gPq4cmPitiYmMiBnpPTAVDgJatioFoSBW0l7LLNcwFMdvktPh47c1UdeF h7ihaX7SMvGAMSoVGk1v/xZIH6lZYbs= 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-591-dTAbzv03O6COUgz8sHXMEw-1; Tue, 14 Jun 2022 18:04:15 -0400 X-MC-Unique: dTAbzv03O6COUgz8sHXMEw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 046693C021AA; Tue, 14 Jun 2022 22:04:15 +0000 (UTC) Received: from llong.com (unknown [10.22.33.116]) by smtp.corp.redhat.com (Postfix) with ESMTP id B55A9492CA2; Tue, 14 Jun 2022 22:04:14 +0000 (UTC) From: Waiman Long To: Catalin Marinas , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Waiman Long Subject: [PATCH v2 3/3] mm/kmemleak: Prevent soft lockup in first object iteration loop of kmemleak_scan() Date: Tue, 14 Jun 2022 18:03:59 -0400 Message-Id: <20220614220359.59282-4-longman@redhat.com> In-Reply-To: <20220614220359.59282-1-longman@redhat.com> References: <20220614220359.59282-1-longman@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655244260; 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=x10p2RYJ/7U/x7uwqRvY5a/J+6w+5v/qLg41hZfOPsk=; b=wlKzUPDR5OkPRkLehzIejgGvvDyljGYa6jNN/+9DAje+aaiMH4ZSodsitCzNK314Q/VrAi EFVnr2S9Pw2VfHxKX8NMvAzs38mj+7VrfKtW1bLYEloJJXOzrryjyrloXWZeBdJnkCoB7W UymPSYYz8Wcj/TeoXyE280P9w+GkoKk= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=i6KmiBEn; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf27.hostedemail.com: domain of longman@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=longman@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655244260; a=rsa-sha256; cv=none; b=r1HNuZZNoaQOlsNy/Kq1kxk3FpHkxeVG9tus1UGurDezn3NfJfN74vBcqDRrdTG3uenNmG o0kRgDq6O11wJE5Oz3nKI3+QxEGy9EJQ4K/yMnd60YbJz3Am/JMM2ADXdGNy9Y44EXaX5V Y9G/HAt6J2vUnMlwivTvCD6lSreZ0QU= X-Rspamd-Queue-Id: 244064009E X-Rspam-User: X-Stat-Signature: mgg9ug7by1ra4xbyqg4ay9kdk6mw91zw Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=i6KmiBEn; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf27.hostedemail.com: domain of longman@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=longman@redhat.com X-Rspamd-Server: rspam04 X-HE-Tag: 1655244259-208848 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: The first RCU-based object iteration loop has to modify the object count. So we cannot skip taking the object lock. One way to avoid soft lockup is to insert occasional cond_resched() call into the loop. This cannot be done while holding the RCU read lock which is to protect objects from being freed. However, taking a reference to the object will prevent it from being freed. We can then do a cond_resched() call after every 64k objects safely. Signed-off-by: Waiman Long Reviewed-by: Catalin Marinas --- mm/kmemleak.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 7dd64139a7c7..abba063ae5ee 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -1417,12 +1417,16 @@ static void kmemleak_scan(void) struct zone *zone; int __maybe_unused i; int new_leaks = 0; + int loop1_cnt = 0; jiffies_last_scan = jiffies; /* prepare the kmemleak_object's */ rcu_read_lock(); list_for_each_entry_rcu(object, &object_list, object_list) { + bool obj_pinned = false; + + loop1_cnt++; raw_spin_lock_irq(&object->lock); #ifdef DEBUG /* @@ -1437,10 +1441,32 @@ static void kmemleak_scan(void) #endif /* reset the reference count (whiten the object) */ object->count = 0; - if (color_gray(object) && get_object(object)) + if (color_gray(object) && get_object(object)) { list_add_tail(&object->gray_list, &gray_list); + obj_pinned = true; + } raw_spin_unlock_irq(&object->lock); + + /* + * Do a cond_resched() to avoid soft lockup every 64k objects. + * Make sure a reference has been taken so that the object + * won't go away without RCU read lock. + */ + if (!(loop1_cnt & 0xffff)) { + if (!obj_pinned && !get_object(object)) { + /* Try the next object instead */ + loop1_cnt--; + continue; + } + + rcu_read_unlock(); + cond_resched(); + rcu_read_lock(); + + if (!obj_pinned) + put_object(object); + } } rcu_read_unlock();