From patchwork Fri Apr 5 11:27:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengguang Xu X-Patchwork-Id: 10887227 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0CDD71575 for ; Fri, 5 Apr 2019 11:28:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EBDF3284D1 for ; Fri, 5 Apr 2019 11:28:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E03AA28AF7; Fri, 5 Apr 2019 11:28:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7B429284D1 for ; Fri, 5 Apr 2019 11:28:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730777AbfDEL2h (ORCPT ); Fri, 5 Apr 2019 07:28:37 -0400 Received: from mout.gmx.net ([212.227.15.18]:43009 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730596AbfDEL2h (ORCPT ); Fri, 5 Apr 2019 07:28:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1554463710; bh=AcH2Y+kPjwVm0m0grrJWswjKGQxCRIPY8vyUMqtB6Ao=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=FK09yYeU3831ykwrE1BQLRjd3vlMlEadDnDfLut5O29xj8Ks0E1sAUl7MQgfzwt5R cdkXOJjrmnWkVeDsVLjXkXlVcmfALOjBS6k0Q/L+tNVoQ7JST436ZSOrHvtSe6irUj BZ8JuqBi9dsW1X8P2QwSOcMKpDCJY/jKIBGO/eiw= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from localhost.localdomain ([218.18.229.179]) by mail.gmx.com (mrgmx003 [212.227.17.184]) with ESMTPSA (Nemesis) id 0Ldq9D-1gTeDW3VcB-00j4a5; Fri, 05 Apr 2019 13:28:30 +0200 From: Chengguang Xu To: gregkh@linuxfoundation.org, dan.carpenter@oracle.com Cc: linux-fsdevel@vger.kernel.org, viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org, Chengguang Xu Subject: [PATCH v3 3/4] chardev: code cleanup for __register_chrdev_region() Date: Fri, 5 Apr 2019 19:27:10 +0800 Message-Id: <20190405112711.25275-3-cgxu519@gmx.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190405112711.25275-1-cgxu519@gmx.com> References: <20190405112711.25275-1-cgxu519@gmx.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:+L48v9102JFn0TemtK4d4YQMjLKPg1HDVNCjtB97oeRWPypJdPV QXO1inBw4D3obyDD2CykZccqFxEltIiCeE+wHaZmkyaNFBENsieqMMZMc/wl3qcx7cgJFep +CKlKCrr/oBQS8HH/cffCN2RnUed01j6KIiRreCSqO5B3jeAFxJV87zrVmE8r6hLZE9sdqv TZICcoI1wjp9Gd1GarfVA== X-UI-Out-Filterresults: notjunk:1;V03:K0:/N4BpP4YNFI=:e+bL/GRUE3t/jwRKdN2X5G G9vTYYeV2gsibcwDCziu7pRg8kXDKy6rp9yNJhu9Ywb+mvQLXub1GoEfO8vHz7GhynoDjVYOP il0wo2ULlbRt2MJNmEQjZ5s+6hgPsFcMqE9slosz2vB0jaDFc+ByaUkaXnvpzHj8rkGmGTTq5 wU0C1cjV9j3+HhjQs0CGDFMiVRjtZ/51lkWqgx5SVwAb90J/dx5+hNSssPdfXHW6fpHpqOKPt M4HZpr30kFFu0YYmVxTff+of1PyviynV7YAGjxOQBvDx6F9zb6gUvDSfPregGUou6dkPgQgo/ bVC5JECsLQzL/IzZolk6kMgaW1+JjfjdScqp3Qm03fxhPFdkIFxnJlkfOxEDzw9kBQkFtmBw7 1TOMIsJ+bpuc0DyH1/PfbsEVRAqMfelKAekh7cMqyYacu2Z7EDynm00+ACpxO5e283qizL8xU x/vuxfxctMRg/2cj71qIcerDccqQdvi0P014uu2i5AY9B1ql5ehaWAmOtztXbICU7u7goYnmK IWCAA3yasKmStBPIOUlL4yyEwNQPbL5+IrIwci1bVfAWkm3o46lIvrffRh9tRjeyDldIXJZAx hIIwek7vVroVbH1/oFGcH2ezvyF7V0kQCYiylxhcCYvUK6L+QTjn1H341qQIFrC8wCsUlC6FR NR5OnR3sRzM3Z7xDlLXIFXNecalvuVqD7kC6sVuDFlr8zymqGmb9OvS60qYEdJC+JS9vIInae qb6rRkrVTLBEGjXKrVa8+UY/PPHYOOVHqqhZkZekRIGcf3UBNWc4kdqMkFHJhLWaFXFpynjQu C7JFCfjuuYSAs5vohIgf+dqTI8BdUUbKTWPVpHUEL77aZHrsxMIep2NKbbiR7d/s7MXdq1mFe 3pQhNbeyRAFXKR5C9IPc65SYHIYLdnyRwQHFvlab9QrZa9Lw7eB2FvH6lEq537 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP It's just code cleanup, not functional change. Signed-off-by: Chengguang Xu --- v1->v2: - Split fix and cleanup patches. - Remove printing minor range in chrdev_show(). v2->v3: - Set variable ret to '-EBUSY' before checking minor range overlap. fs/char_dev.c | 70 +++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 41 deletions(-) -- 2.20.1 diff --git a/fs/char_dev.c b/fs/char_dev.c index 6803e98414f1..47fd0561b03d 100644 --- a/fs/char_dev.c +++ b/fs/char_dev.c @@ -100,10 +100,16 @@ static struct char_device_struct * __register_chrdev_region(unsigned int major, unsigned int baseminor, int minorct, const char *name) { - struct char_device_struct *cd, **cp; - int ret = 0; + struct char_device_struct *cd, *curr, *prev = NULL; + int ret; int i; + if (major >= CHRDEV_MAJOR_MAX) { + pr_err("CHRDEV \"%s\" major requested (%u) is greater than the maximum (%u)\n", + name, major, CHRDEV_MAJOR_MAX-1); + return ERR_PTR(-EINVAL); + } + if (minorct > MINORMASK + 1 - baseminor) { pr_err("CHRDEV \"%s\" minor range requested (%u-%u) is out of range of maximum range (%u-%u) for a single major\n", name, baseminor, baseminor + minorct - 1, 0, MINORMASK); @@ -126,55 +132,37 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor, major = ret; } - if (major >= CHRDEV_MAJOR_MAX) { - pr_err("CHRDEV \"%s\" major requested (%u) is greater than the maximum (%u)\n", - name, major, CHRDEV_MAJOR_MAX-1); - ret = -EINVAL; - goto out; - } - - cd->major = major; - cd->baseminor = baseminor; - cd->minorct = minorct; - strlcpy(cd->name, name, sizeof(cd->name)); - + ret = -EBUSY; i = major_to_index(major); + for (curr = chrdevs[i]; curr; prev = curr, curr = curr->next) { + if (curr->major < major) + continue; - for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next) - if ((*cp)->major > major || - ((*cp)->major == major && - (((*cp)->baseminor >= baseminor) || - ((*cp)->baseminor + (*cp)->minorct > baseminor)))) + if (curr->major > major) break; - /* Check for overlapping minor ranges. */ - if (*cp && (*cp)->major == major) { - int old_min = (*cp)->baseminor; - int old_max = (*cp)->baseminor + (*cp)->minorct - 1; - int new_min = baseminor; - int new_max = baseminor + minorct - 1; + if (curr->baseminor + curr->minorct <= baseminor) + continue; - /* New driver overlaps from the left. */ - if (new_max >= old_min && new_max <= old_max) { - ret = -EBUSY; - goto out; - } + if (curr->baseminor >= baseminor + minorct) + break; - /* New driver overlaps from the right. */ - if (new_min <= old_max && new_min >= old_min) { - ret = -EBUSY; - goto out; - } + goto out; + } - if (new_min < old_min && new_max > old_max) { - ret = -EBUSY; - goto out; - } + cd->major = major; + cd->baseminor = baseminor; + cd->minorct = minorct; + strlcpy(cd->name, name, sizeof(cd->name)); + if (!prev) { + cd->next = curr; + chrdevs[i] = cd; + } else { + cd->next = prev->next; + prev->next = cd; } - cd->next = *cp; - *cp = cd; mutex_unlock(&chrdevs_lock); return cd; out: