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 X-Spam-Level: X-Spam-Status: No, score=-1.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53C97C06511 for ; Mon, 1 Jul 2019 14:09:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2C08F2175B for ; Mon, 1 Jul 2019 14:09:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729392AbfGAOJB (ORCPT ); Mon, 1 Jul 2019 10:09:01 -0400 Received: from mx2.suse.de ([195.135.220.15]:49432 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728016AbfGAOJA (ORCPT ); Mon, 1 Jul 2019 10:09:00 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 8653FB016; Mon, 1 Jul 2019 14:08:59 +0000 (UTC) Date: Mon, 01 Jul 2019 16:08:54 +0200 Message-ID: From: Takashi Iwai To: "Evan Green" Cc: , "Thomas Gleixner" , "Amadeusz S*awi*ski" , "Greg Kroah-Hartman" , "Jaroslav Kysela" , Subject: Re: [PATCH v2 1/2] ALSA: hda: Fix widget_mutex incomplete protection In-Reply-To: <20190626212220.239897-2-evgreen@chromium.org> References: <20190626212220.239897-1-evgreen@chromium.org> <20190626212220.239897-2-evgreen@chromium.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/25.3 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 26 Jun 2019 23:22:19 +0200, Evan Green wrote: > > The widget_mutex was introduced to serialize callers to > hda_widget_sysfs_{re}init. However, its protection of the sysfs widget array > is incomplete. For example, it is acquired around the call to > hda_widget_sysfs_reinit(), which actually creates the new array, but isn't > still acquired when codec->num_nodes and codec->start_nid is updated. So > the lock ensures one thread sets up the new array at a time, but doesn't > ensure which thread's value will end up in codec->num_nodes. If a larger > num_nodes wins but a smaller array was set up, the next call to > refresh_widgets() will touch free memory as it iterates over codec->num_nodes > that aren't there. > > The widget_lock really protects both the tree as well as codec->num_nodes, > start_nid, and end_nid, so make sure it's held across that update. It should > also be held during snd_hdac_get_sub_nodes(), so that a very old read from that > function doesn't end up clobbering a later update. OK, right, this fix is needed no matter whether to take my other change to skip hda_widget_sysfs_init() call in hda_widget_sysfs_reinit(). However... > While in there, move the exit mutex call inside the function. This moves the > mutex closer to the data structure it protects and removes a requirement of > acquiring the somewhat internal widget_lock before calling sysfs_exit. ... this doesn't look better from consistency POV. The whole code in hdac_sysfs.c doesn't take any lock in itself. The protection is supposed to be done in the caller side. So, let's keep as is now. Also... > codec->num_nodes = nums; > codec->start_nid = start_nid; > codec->end_nid = start_nid + nums; > + mutex_unlock(&codec->widget_lock); > return 0; > + > +unlock: > + mutex_unlock(&codec->widget_lock); > + return err; There is no need of two mutex_unlock() here. They can be unified, codec->num_nodes = nums; codec->start_nid = start_nid; codec->end_nid = start_nid + nums; unlock: mutex_unlock(&codec->widget_lock); return err; Could you refresh this and resubmit? thanks, Takashi