X-GM-THRID: 6211294151973011456 X-Received: by 10.112.144.105 with SMTP id sl9mr1619212lbb.3.1446206228436; Fri, 30 Oct 2015 04:57:08 -0700 (PDT) X-BeenThere: outreachy-kernel@googlegroups.com Received: by 10.25.160.72 with SMTP id j69ls172073lfe.106.gmail; Fri, 30 Oct 2015 04:57:07 -0700 (PDT) X-Received: by 10.112.142.195 with SMTP id ry3mr1667741lbb.22.1446206227818; Fri, 30 Oct 2015 04:57:07 -0700 (PDT) Return-Path: Received: from mout.kundenserver.de (mout.kundenserver.de. [212.227.17.10]) by gmr-mx.google.com with ESMTPS id g9si78696wmd.2.2015.10.30.04.57.07 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 30 Oct 2015 04:57:07 -0700 (PDT) Received-SPF: neutral (google.com: 212.227.17.10 is neither permitted nor denied by best guess record for domain of arnd@arndb.de) client-ip=212.227.17.10; Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 212.227.17.10 is neither permitted nor denied by best guess record for domain of arnd@arndb.de) smtp.mailfrom=arnd@arndb.de Received: from wuerfel.localnet ([134.3.118.24]) by mrelayeu.kundenserver.de (mreue104) with ESMTPSA (Nemesis) id 0MSa4i-1a20E31k2u-00RclM; Fri, 30 Oct 2015 12:57:07 +0100 From: Arnd Bergmann To: outreachy-kernel@googlegroups.com Cc: Ksenija Stanojevic Subject: Re: [Outreachy kernel] [PATCH v2] Staging: comedi: vmk80xx: Replace semaphore with mutex Date: Fri, 30 Oct 2015 12:57:06 +0100 Message-ID: <4329000.9joMaq9Gbl@wuerfel> User-Agent: KMail/4.11.5 (Linux/3.16.0-10-generic; KDE/4.11.5; x86_64; ; ) In-Reply-To: <1446179606-29779-1-git-send-email-ksenija.stanojevic@gmail.com> References: <1446179606-29779-1-git-send-email-ksenija.stanojevic@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-Provags-ID: V03:K0:4WvrjQ1WhRpmB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static int vmk80xx_auto_attach(struct comedi_device *dev, > if (ret) > return ret; > > - sema_init(&devpriv->limit_sem, 8); > + mutex_init(&devpriv->limit_mut); > > usb_set_intfdata(intf, devpriv); > I missed this one line when I made the recommendation to use a mutex here. I'm sorry about my mistake, but using a mutex will not be a direct replacement as a consequence (and neither would rwsem). We only have a very small number of semaphores in the kernel that are truly counting like this one is: Normal drivers use 'sema_init(&devpriv->limit_sem, 1)' to get mutex behavior where the 'down()' call ensures that the following code can be entered only from one process, whereas this one allows eight concurrent users to enter the function. We should still try to replace the semaphore (as we should do in all drivers), but doing so is now harder. An equivalent replacement would be: - down(&devpriv->limit_sem); + wait_event(devpriv->limit_wq, atomic_add_unless(&devpriv->limit, 1, VMK80XX_LIMIT)); ... - up(&devpriv->limit_sem); + atomic_dec(&devpriv->limit); + wake_up(&devpriv->limit_sem); I have an old patch for the usb-skeleton driver (and I think I never submitted that patch) at http://git.kernel.org/cgit/linux/kernel/git/arnd/playground.git/commit/?h=semaphore-3.2&id=7f3ec7ad361f08317480a632d67fe64928f71fa7 which uses a similar construct (presumably this one was copied from usb-skeleton). If you do this, please research and explain in the changelog the following: - Does the current usage of limit_sem in this driver actually make sense? - If not, do we need a mutex instead, or is the semaphore completely useless here? Arnd