Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp4482689rwb; Tue, 6 Sep 2022 08:08:11 -0700 (PDT) X-Google-Smtp-Source: AA6agR5t0C20mpIpJmYYBCt+rftFvhJzCD1k0dxVXDqHtAcdsb4h6RkaDK1POkHcgQz3D60RrDoQ X-Received: by 2002:a05:6402:42c3:b0:43d:682f:c0ca with SMTP id i3-20020a05640242c300b0043d682fc0camr47837027edc.334.1662476891437; Tue, 06 Sep 2022 08:08:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662476891; cv=none; d=google.com; s=arc-20160816; b=MAUSBrC8UB9tPkMrjyL9x7d5L2I5xisr9lFDz3no7Eq/f329X1F6t1p2v3GTJfh3Wd 2PrfF9/HCvdzBdxyK6+bgQXl32n6n8DR04PUs3hW4D67LCxr9DOxr9sXRqHm4xCrWMJQ HjvoHSQ2T+49jzNdkZgCAfzTAJlIGiE3dG4+ex5l/tDoCDUyUKbzNbnJmeu7GEPY+o4c R7HNI1LWwUZO1yHuAE9xshCsOux3411VVYq00lqojW8IDfq25ubwafBJB+Q9MFmOhTug zDW2j37+vTM7RAqRFL/1/scdAn8EAEdmkXQ8GzLB9kljWZNs26ynJOB24QbG1QnKg8PW Hvrw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=cljnZpbJ3tgDrKNtK7kWuZwMdXc8akXhnnVZiJrUqjg=; b=dR/kbhoagUaLMG+Z3vd29qNBrSglKJ1EDeZ+wK1uTTv7LvLQIaWQ6ozsxJUASfsB/7 3eiClF4csWiI/46VZQ6f31a92duNkcFckBdaGzpPEvtP6h6f+iMFN9a1/9OeybjmGh92 lfiNtif2ptoW2KSfLI7m43oqEw8ZUYKTDssJLYnKYTzibpjUYYWynyGGfOE5iMX/GslA Kc3QiEOcGplGcsy1e4fy6RnSskPYxix5t3eQKwBVAGw5thVyVlHf7Pv5iO2Gl6AHIf71 PY42cZ005ppGyITgEQD7MqaRTFZZCM/y53MRZMJauyeVjIlp3Z0N2+MSZPxJ4Nceo5vP PKiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wWUBFgmq; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x97-20020a50baea000000b004482892d410si8569276ede.85.2022.09.06.08.07.46; Tue, 06 Sep 2022 08:08:11 -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=@linuxfoundation.org header.s=korg header.b=wWUBFgmq; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241673AbiIFOSH (ORCPT + 99 others); Tue, 6 Sep 2022 10:18:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241321AbiIFOQM (ORCPT ); Tue, 6 Sep 2022 10:16:12 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AFE9B98; Tue, 6 Sep 2022 06:49:23 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 50A4AB8162F; Tue, 6 Sep 2022 13:48:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BAC9DC433C1; Tue, 6 Sep 2022 13:48:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1662472096; bh=K4dGkykxDeQfgG8iJRdoD0/3YR3v+GjHk/N6AhY71Gw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wWUBFgmqQNzix0ksB7VJbbUlRm6gjdLQYRmLVv31yXOpbIovdGWW94QKi85EefoTV ltxlgcvgm3zEjzh6LtoYzDIMf/Q4ozNZJ+fCebmmwfeHYq0S4GE5pdoifBbAwxKldF hmCWQcFLKbRGDMaZ1O9hMnp64rFvRa9ArWSB/izs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Abhishek Shah , Takashi Iwai Subject: [PATCH 5.19 147/155] ALSA: seq: Fix data-race at module auto-loading Date: Tue, 6 Sep 2022 15:31:35 +0200 Message-Id: <20220906132835.634243627@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220906132829.417117002@linuxfoundation.org> References: <20220906132829.417117002@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,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 From: Takashi Iwai commit 3e7e04b747adea36f349715d9f0998eeebf15d72 upstream. It's been reported that there is a possible data-race accessing to the global card_requested[] array at ALSA sequencer core, which is used for determining whether to call request_module() for the card or not. This data race itself is almost harmless, as it might end up with one extra request_module() call for the already loaded module at most. But it's still better to fix. This patch addresses the possible data race of card_requested[] and client_requested[] arrays by replacing them with bitmask. It's an atomic operation and can work without locks. Reported-by: Abhishek Shah Cc: Link: https://lore.kernel.org/r/CAEHB24_ay6YzARpA1zgCsE7=H9CSJJzux618E=Ka4h0YdKn=qA@mail.gmail.com Link: https://lore.kernel.org/r/20220823072717.1706-2-tiwai@suse.de Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/core/seq/seq_clientmgr.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) --- a/sound/core/seq/seq_clientmgr.c +++ b/sound/core/seq/seq_clientmgr.c @@ -121,13 +121,13 @@ struct snd_seq_client *snd_seq_client_us spin_unlock_irqrestore(&clients_lock, flags); #ifdef CONFIG_MODULES if (!in_interrupt()) { - static char client_requested[SNDRV_SEQ_GLOBAL_CLIENTS]; - static char card_requested[SNDRV_CARDS]; + static DECLARE_BITMAP(client_requested, SNDRV_SEQ_GLOBAL_CLIENTS); + static DECLARE_BITMAP(card_requested, SNDRV_CARDS); + if (clientid < SNDRV_SEQ_GLOBAL_CLIENTS) { int idx; - if (!client_requested[clientid]) { - client_requested[clientid] = 1; + if (!test_and_set_bit(clientid, client_requested)) { for (idx = 0; idx < 15; idx++) { if (seq_client_load[idx] < 0) break; @@ -142,10 +142,8 @@ struct snd_seq_client *snd_seq_client_us int card = (clientid - SNDRV_SEQ_GLOBAL_CLIENTS) / SNDRV_SEQ_CLIENTS_PER_CARD; if (card < snd_ecards_limit) { - if (! card_requested[card]) { - card_requested[card] = 1; + if (!test_and_set_bit(card, card_requested)) snd_request_card(card); - } snd_seq_device_load_drivers(); } }