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 A2054C43334 for ; Wed, 15 Jun 2022 16:08:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 29B796B0071; Wed, 15 Jun 2022 12:08:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 24AE56B0072; Wed, 15 Jun 2022 12:08:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0EAE56B0074; Wed, 15 Jun 2022 12:08:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id F2F4E6B0071 for ; Wed, 15 Jun 2022 12:08:25 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B295E60CFE for ; Wed, 15 Jun 2022 16:08:25 +0000 (UTC) X-FDA: 79580952570.17.16E0579 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 46876A00B1 for ; Wed, 15 Jun 2022 16:07:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655309258; 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=fOHCvIKVzCA/+PXZllmmH2BoXPXS8bGvjyl5jD0CoBQ=; b=On4IGPCQ6X2B230ehO95qeza+nA4JdsODSG/IBImVffOnr/IHJpe9WX/GclCGPQye5F4et unJ4TCxO4/ILmcyuBU91Gz+iuRE+moil35aQbe7c9afGqFL676X0enWuJWBzPPACtN7zbS yIturxHk2pSRUtW8kfBfuN8PgStnYqI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-601-6wxi91VfPrmdwvZyWyJb_g-1; Wed, 15 Jun 2022 12:07:35 -0400 X-MC-Unique: 6wxi91VfPrmdwvZyWyJb_g-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 A6CAF185A7BA; Wed, 15 Jun 2022 16:07:34 +0000 (UTC) Received: from [10.18.17.215] (dhcp-17-215.bos.redhat.com [10.18.17.215]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6E7B5492C3B; Wed, 15 Jun 2022 16:07:34 +0000 (UTC) Message-ID: <92dd6f80-5124-0b7a-32ed-7703dc9e34ca@redhat.com> Date: Wed, 15 Jun 2022 12:07:34 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: [PATCH v2 3/3] mm/kmemleak: Prevent soft lockup in first object iteration loop of kmemleak_scan() Content-Language: en-US To: Catalin Marinas Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song References: <20220614220359.59282-1-longman@redhat.com> <20220614220359.59282-4-longman@redhat.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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=1655309259; 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=fOHCvIKVzCA/+PXZllmmH2BoXPXS8bGvjyl5jD0CoBQ=; b=2gW+YADM52EiurpXmTHHdxVOYKX9CojfmUB0CK3IfV6c/z/537v40MT+GffSzh1pH+5sag IYIwxkmRg0CqeJy4MKY3vJ5uE57Jg6So/UpvPg72UYg4M6bGUxGIGNj8xeaj9OpFUftYIc cgXOBJ8ftmcT1rEqcNie6XsDpi79Hyc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=On4IGPCQ; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf25.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=1655309259; a=rsa-sha256; cv=none; b=ljGJnkUwfqnX84lupAibYUag/pbtgTgetzhKpFYqOtYEJSSofmp3fviFwyK+HvTJoG+9tf DbzgPM8bkjm3ObvHaWSyHb+ZPjxzjxaFTu/aYlfZJ+vEClpp6r7CR6j9TG2vtlWPBLU4Cu 9W/xnWlHWz77UvlogLsAuo7pLlNCwRs= X-Stat-Signature: 4yo4sjhonz4oir9jk3eestx5z6rtq95j X-Rspamd-Queue-Id: 46876A00B1 X-Rspam-User: X-Rspamd-Server: rspam05 Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=On4IGPCQ; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf25.hostedemail.com: domain of longman@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=longman@redhat.com X-HE-Tag: 1655309258-84275 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 6/15/22 11:11, Catalin Marinas wrote: > On Tue, Jun 14, 2022 at 06:03:59PM -0400, Waiman Long wrote: >> 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; >> + } > With this trick we could probably get rid of rcu_read_lock() and take > the kmemleak_lock instead. But that's for another patch. rcu_read_lock() is cheap unless we use a preempt kernel. > >> + >> + rcu_read_unlock(); >> + cond_resched(); >> + rcu_read_lock(); > cond_resched_rcu() to save a couple of lines? I am not aware of such helper function. Yes, I should have used that instead in case I need to update this patch again. Thanks! > >> + >> + if (!obj_pinned) >> + put_object(object); >> + } > Reviewed-by: Catalin Marinas Thanks, Longman