Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp876097imj; Fri, 15 Feb 2019 08:14:23 -0800 (PST) X-Google-Smtp-Source: AHgI3IY1R4QDVlLSQIL8yP8LbWXUtC7id2IZCTjNV4o6osfIeZGWZa4LbL5/nVIZqpaaozhGbwIn X-Received: by 2002:a62:70c9:: with SMTP id l192mr1341299pfc.207.1550247263107; Fri, 15 Feb 2019 08:14:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550247263; cv=none; d=google.com; s=arc-20160816; b=vp0858oYGOuHin5vFmFwDUYr9nYLIAjByZGdHdn0uapwgUXDmpM1fQfYJpkA1vUto1 ozgiQmukwjvRG4Y1TsBQmPZlj8Yg6pU14Dwa3av0DgcxR/Ni4e9Avw05DkoK44oHQdJV RG8P+x50LmVFNMtSf8JTbya+aym0TqAMu50WC2vLAWpGurish2mmASYnE7J5RcA5dBv5 yG1nPNbbEx6Ltc0mhcTYdqEKH1sX4C1pCjcnuLHmb6/COVRk2g4nb47UXI17ilNchtfF DvRM5BIuWvBlCe0pXOhvnOXYzBjL00kn/RrK2McX58hkp/AQNOrXE5ENVITTlH5+GCXj Jg9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=zCrBAzT5R7V3OjNN334At1GxtGTw4Q49jrzFzGhVh6M=; b=jx3MgIfDv+43lRXSEgzeWCUhEdOFqhg4Qf9Gzwv81SzGsX8SC1XBoLzMrQginfXrfA vWk9/v7BvT5lWlW6nH3cB4tOfECA1we15GCCwmKtrjERKRANE20ue4iKNwjYvUSL6rUY tfcGrStzGPWl/ecrf1RnQw+7c05gjIIC+VLSn8gKs/8OjHniLdUC2PfVfTfGmbkTVCQF 422UKJcEAnbVHCV6Hb7N3XOS4vPtI/dryMTwf2xNbtM+ZTdeNHU+9HBMH8rkPOGlIPkG QDt4+DsKWrWM4AkW6P7tH9mrEicuKDsluqMSdHeOONMk/0J/q0VpMJGIK2Nzw77R6f8K Zjig== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s11si5649788pgk.344.2019.02.15.08.14.07; Fri, 15 Feb 2019 08:14:23 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2436967AbfBOM22 (ORCPT + 99 others); Fri, 15 Feb 2019 07:28:28 -0500 Received: from mout.gmx.net ([212.227.17.20]:35221 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729332AbfBOM21 (ORCPT ); Fri, 15 Feb 2019 07:28:27 -0500 Received: from localhost.localdomain ([218.18.229.179]) by mail.gmx.com (mrgmx103 [212.227.17.174]) with ESMTPSA (Nemesis) id 0MHH3P-1gptlr0ISZ-00E7bi; Fri, 15 Feb 2019 13:28:24 +0100 From: Chengguang Xu To: gregkh@linuxfoundation.org Cc: viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org, Chengguang Xu Subject: [PATCH v2 3/4] chardev: code cleanup for __register_chrdev_region() Date: Fri, 15 Feb 2019 20:27:13 +0800 Message-Id: <20190215122714.28842-3-cgxu519@gmx.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190215122714.28842-1-cgxu519@gmx.com> References: <20190215122714.28842-1-cgxu519@gmx.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Provags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k3VN3fEr8qElhbd6NzKPx7RYrPy4mp/yVhRe d8htBRSxWnMEKGgYph3pOYNtaRr7uA8lRZvv6oopuMi2KCEcYfC92+uWaIQjW8JFpag2WgQDj DDc0gaRFrXRsEezfeHr+xJ8zxK5OcO8mzgZPu2ylj1zkKu5nBqAZOwD8BTyB4c Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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(). fs/char_dev.c | 69 +++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/fs/char_dev.c b/fs/char_dev.c index 6803e98414f1..bd2bf1fd847e 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 = -EBUSY; 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,36 @@ __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)); - 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: -- 2.20.1