Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp1454340rdb; Fri, 19 Jan 2024 22:56:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IH0M1zcMYc9heNgUCyrsrTuUv+F+QvYIo8n55PEongoS0REg1DTPFexwXzJ+ZsuTJdd7dOQ X-Received: by 2002:a0c:df06:0:b0:681:9a5c:7b10 with SMTP id g6-20020a0cdf06000000b006819a5c7b10mr1095903qvl.109.1705733769058; Fri, 19 Jan 2024 22:56:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705733769; cv=pass; d=google.com; s=arc-20160816; b=qUtRP2XunUUkP3j0kU+ZjjW2g4L63YCDrP/riYmSk4Hft3SeoSbP5n3hmh89h0/ebY 8XbHbmmcnhO3FVqTgkMYOWv18JiGP5xJJvzUYg705lsCz8y4Qprxxl9j5CZVMBoJwJyo rAgdzxG5xILZ4Yp13bCQawV7S3q/o951xRkEsBH2weVUfsHtLbHz1Oup87gglRVTNJ8k FmLTUMjjWOm+xHN7qe7ww7mcnFP1J5+lwS/ANte1VMmjIeNGaoZjhkvDR/52Z3/nt2eZ jP4dlyEifFseunvxO0SBcLjoEo6NOquXzUAwH4MWVDuC5vQ2bHjxmg1GGhhW5x32Xa1r Ws6Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=drqLI5iv8tK+6H2RZsx4Mqly9yp+Pt1Pkvi8hiH2284=; fh=A8LfyeIAEqEjRUIFEf6FkShOWhYZjm4NqzioT68cjss=; b=UiL5ucEyLnoUzH1OdTtdHynpeyCKwti3hoFOKN1+je+dslSXuqx0SlrK4gSPLGGdqE AUcnvpEMabyCRM63cmdToMEUr7IyOfTDN9fsbD+r9LGBfKPWEjpcN3qo1F0Fejgwa3na mQ52G3YpQiGoO5TXF24t3d/G9zVZaVopiyGfvvR4m8Ydtqw88biFxeVnqEq/sbDFfLSG UWoqjWzt3lnTMfxmJR9WTXdLXy6rJ9GNfBH5zFJl8RG6O9JeiV2ak4/aPEHQ0592y6cP 0U64IB2v/kzYpRcksiGPvUdrw3SrRHWOrSentFeomcfGYep9xXhrerbah17jEzQh11rS l7Dg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UUBKTSWE; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-31724-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-31724-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id x11-20020a0ce24b000000b0067a29f9abdasi951689qvl.438.2024.01.19.22.56.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 22:56:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-31724-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UUBKTSWE; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-31724-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-31724-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id CAEC11C21B3F for ; Sat, 20 Jan 2024 06:56:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F93320E8; Sat, 20 Jan 2024 06:56:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UUBKTSWE" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 154721858 for ; Sat, 20 Jan 2024 06:55:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705733760; cv=none; b=vFFdPthAZX7CWVtIWaqENPCkirhz/PhnZUSwEI0nRkCLSbNcOpA2IUmEQMHLi0PSGpQ2Yg+vcaBwxn2qx4Rv6O6RMrNdTqTeoOnrkxUrPxYYyqAnJ8WVWH8wbK8X+XQ6uM9A+hT5lGhMVGdTJLvt7LZgrgWKEnCBmHcS0VV1j4Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705733760; c=relaxed/simple; bh=UKO1d22DE5qaYTzjwXgC1y9WVGi1HgsHirzj/ddBBlE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=GMAtSo6MQIsbYPwsQMOGyjuBK3ayuLpI5OQgdJhgedLFyZDCmAVwZ8wvyO6xT4aE7wSbnO+rN6Gg7xQ5QshH4AJ+FyAJG0HdpRKuFKvnOdU1cBni59ZKNwU3Q4fVzl6SkOBGVcar1g0i3aCA4iRnUizDEaxv5CN7HUfG8CDpBiM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=UUBKTSWE; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705733757; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=drqLI5iv8tK+6H2RZsx4Mqly9yp+Pt1Pkvi8hiH2284=; b=UUBKTSWEsKRKbLvKhICVXk1stxFNrRR52Gx8oI0GenV5eI7amGm1ZAv9Rpcre4yrHYXDZB CsZIJkzArY+9INOblYPaJsRUSIF26N++B1sd8na88RruT8mB2jyUhPumi2lBWNMNAbMJHv Bn5y/rVO4VwgX2lAH8V6trtZ14X7TKY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-96-ge-UWV4kMeG87RNIsk1Xjw-1; Sat, 20 Jan 2024 01:55:56 -0500 X-MC-Unique: ge-UWV4kMeG87RNIsk1Xjw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C2B7A3C0ED41; Sat, 20 Jan 2024 06:55:55 +0000 (UTC) Received: from localhost (unknown [10.72.116.42]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0DEA451D5; Sat, 20 Jan 2024 06:55:54 +0000 (UTC) From: Ming Lei To: Andrew Morton Cc: linux-kernel@vger.kernel.org, Thomas Gleixner , Ming Lei , Yury Norov Subject: [PATCH] lib/group_cpus.c: simplify grp_spread_init_one() Date: Sat, 20 Jan 2024 14:55:43 +0800 Message-ID: <20240120065543.739203-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 What the inner loop needs to do is to assign each cpu in `siblmsk & nmsk`. Clean it by using cpumask_next_and(), meantime add helper of grp_assign_cpu(). So grp_spread_init_one() becomes more readable now. Cc: Yury Norov Cc: Thomas Gleixner Signed-off-by: Ming Lei --- Hello Andrew, Please consider to use this one to replace the 1st, 2nd & 4th patches in Yury's V5: - avoid to add new kernel API - avoid to update iterator variable inside loop, which is tricky - fix bug in the 4th patch - add grp_assign_cpu() to make code more readable & clean lib/group_cpus.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/group_cpus.c b/lib/group_cpus.c index ee272c4cefcc..6cbc7379d954 100644 --- a/lib/group_cpus.c +++ b/lib/group_cpus.c @@ -11,32 +11,36 @@ #ifdef CONFIG_SMP +static bool grp_assign_cpu(int cpu, struct cpumask *irqmsk, + struct cpumask *nmsk) +{ + if (cpu >= nr_cpu_ids) + return false; + + cpumask_clear_cpu(cpu, nmsk); + cpumask_set_cpu(cpu, irqmsk); + return true; +} + static void grp_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk, unsigned int cpus_per_grp) { const struct cpumask *siblmsk; - int cpu, sibl; - - for ( ; cpus_per_grp > 0; ) { - cpu = cpumask_first(nmsk); + int cpu = -1, sibl; - /* Should not happen, but I'm too lazy to think about it */ - if (cpu >= nr_cpu_ids) + while (cpus_per_grp > 0) { + cpu = cpumask_next(cpu, nmsk); + if (!grp_assign_cpu(cpu, irqmsk, nmsk)) return; - - cpumask_clear_cpu(cpu, nmsk); - cpumask_set_cpu(cpu, irqmsk); cpus_per_grp--; /* If the cpu has siblings, use them first */ siblmsk = topology_sibling_cpumask(cpu); - for (sibl = -1; cpus_per_grp > 0; ) { - sibl = cpumask_next(sibl, siblmsk); - if (sibl >= nr_cpu_ids) + sibl = cpu; + while (cpus_per_grp > 0) { + sibl = cpumask_next_and(sibl, siblmsk, nmsk); + if (!grp_assign_cpu(sibl, irqmsk, nmsk)) break; - if (!cpumask_test_and_clear_cpu(sibl, nmsk)) - continue; - cpumask_set_cpu(sibl, irqmsk); cpus_per_grp--; } } -- 2.41.0