From: Dan Carpenter <dan.carpenter@oracle.com>
To: Namjae Jeon <namjae.jeon@samsung.com>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-cifs@vger.kernel.org,
linux-cifsd-devel@lists.sourceforge.net, smfrench@gmail.com,
senozhatsky@chromium.org, hyc.lee@gmail.com,
viro@zeniv.linux.org.uk, hch@lst.de, hch@infradead.org,
ronniesahlberg@gmail.com, aurelien.aptel@gmail.com,
aaptel@suse.com, sandeen@sandeen.net, colin.king@canonical.com,
rdunlap@infradead.org,
Sergey Senozhatsky <sergey.senozhatsky@gmail.com>,
Steve French <stfrench@microsoft.com>
Subject: Re: [PATCH 3/5] cifsd: add file operations
Date: Mon, 22 Mar 2021 10:04:47 +0300 [thread overview]
Message-ID: <20210322070447.GE1667@kadam> (raw)
In-Reply-To: <20210322051344.1706-4-namjae.jeon@samsung.com>
On Mon, Mar 22, 2021 at 02:13:42PM +0900, Namjae Jeon wrote:
> +void *ksmbd_alloc(size_t size)
> +{
> + return kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
This patch adds a bunch of wrappers around kvmalloc(). Don't do that.
Just use kvmalloc() directly instead. They just make the code hard to
read. kvmalloc() is not appropriate for small allocations. This
wrapper hides a GFP_KERNEL which may lead to scheduling in atomic bugs
and the secret ZEROing makes the code hard to read.
> +}
> +
> +void ksmbd_free(void *ptr)
> +{
> + kvfree(ptr);
> +}
> +
> +static struct wm *wm_alloc(size_t sz, gfp_t flags)
> +{
> + struct wm *wm;
> + size_t alloc_sz = sz + sizeof(struct wm);
^^^^^^^^^^^^^^^^^^^^^^
Check for integer overflow.
> +
> + wm = kvmalloc(alloc_sz, flags);
> + if (!wm)
> + return NULL;
> + wm->sz = sz;
> + return wm;
> +}
> +
> +static int register_wm_size_class(size_t sz)
> +{
> + struct wm_list *l, *nl;
> +
> + nl = kvmalloc(sizeof(struct wm_list), GFP_KERNEL);
Just use kmalloc() for small allocations.
> + if (!nl)
> + return -ENOMEM;
> +
> + nl->sz = sz;
> + spin_lock_init(&nl->wm_lock);
> + INIT_LIST_HEAD(&nl->idle_wm);
> + INIT_LIST_HEAD(&nl->list);
> + init_waitqueue_head(&nl->wm_wait);
> + nl->avail_wm = 0;
> +
> + write_lock(&wm_lists_lock);
> + list_for_each_entry(l, &wm_lists, list) {
> + if (l->sz == sz) {
> + write_unlock(&wm_lists_lock);
> + kvfree(nl);
> + return 0;
> + }
> + }
> +
> + list_add(&nl->list, &wm_lists);
> + write_unlock(&wm_lists_lock);
> + return 0;
> +}
> +
> +static struct wm_list *match_wm_list(size_t size)
> +{
> + struct wm_list *l, *rl = NULL;
> +
> + read_lock(&wm_lists_lock);
> + list_for_each_entry(l, &wm_lists, list) {
> + if (l->sz == size) {
> + rl = l;
> + break;
> + }
> + }
> + read_unlock(&wm_lists_lock);
> + return rl;
> +}
> +
> +static struct wm *find_wm(size_t size)
> +{
> + struct wm_list *wm_list;
> + struct wm *wm;
> +
> + wm_list = match_wm_list(size);
> + if (!wm_list) {
> + if (register_wm_size_class(size))
> + return NULL;
> + wm_list = match_wm_list(size);
> + }
> +
> + if (!wm_list)
> + return NULL;
> +
> + while (1) {
> + spin_lock(&wm_list->wm_lock);
> + if (!list_empty(&wm_list->idle_wm)) {
> + wm = list_entry(wm_list->idle_wm.next,
> + struct wm,
> + list);
> + list_del(&wm->list);
> + spin_unlock(&wm_list->wm_lock);
> + return wm;
> + }
> +
> + if (wm_list->avail_wm > num_online_cpus()) {
> + spin_unlock(&wm_list->wm_lock);
> + wait_event(wm_list->wm_wait,
> + !list_empty(&wm_list->idle_wm));
> + continue;
> + }
> +
> + wm_list->avail_wm++;
I don't think we should increment this until after the allocation
succeeds?
> + spin_unlock(&wm_list->wm_lock);
> +
> + wm = wm_alloc(size, GFP_KERNEL);
> + if (!wm) {
> + spin_lock(&wm_list->wm_lock);
> + wm_list->avail_wm--;
> + spin_unlock(&wm_list->wm_lock);
> + wait_event(wm_list->wm_wait,
> + !list_empty(&wm_list->idle_wm));
> + continue;
> + }
> + break;
> + }
> +
> + return wm;
> +}
regards,
dan carpenter
next prev parent reply other threads:[~2021-03-22 7:06 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <CGME20210322052203epcas1p21fe2d04c4df5396c466c38f4d57d8bb8@epcas1p2.samsung.com>
2021-03-22 5:13 ` [PATCH 0/5] cifsd: introduce new SMB3 kernel server Namjae Jeon
2021-03-22 5:13 ` [PATCH 1/5] cifsd: add server handler and tranport layers Namjae Jeon
2021-03-22 22:18 ` Matthew Wilcox
2021-03-23 3:01 ` Namjae Jeon
2021-03-23 3:12 ` Matthew Wilcox
2021-03-23 3:16 ` Namjae Jeon
2021-03-22 5:13 ` [PATCH 2/5] cifsd: add server-side procedures for SMB3 Namjae Jeon
2021-03-22 6:47 ` Dan Carpenter
2021-03-22 6:50 ` Christoph Hellwig
2021-03-22 13:25 ` [Linux-cifsd-devel] " Stefan Metzmacher
2021-03-22 23:20 ` Namjae Jeon
2021-03-22 23:17 ` Namjae Jeon
2021-03-23 7:19 ` Dan Carpenter
2021-03-25 5:25 ` Sebastian Gottschall
2021-03-22 8:34 ` Matthew Wilcox
2021-03-22 10:27 ` Sergey Senozhatsky
2021-03-22 13:12 ` Matthew Wilcox
2021-03-22 5:13 ` [PATCH 3/5] cifsd: add file operations Namjae Jeon
2021-03-22 6:55 ` Al Viro
2021-03-23 0:12 ` Namjae Jeon
2021-03-22 7:02 ` Al Viro
2021-03-22 9:26 ` Sergey Senozhatsky
2021-03-22 7:04 ` Dan Carpenter [this message]
2021-03-22 9:39 ` Sergey Senozhatsky
2021-03-22 8:15 ` Matthew Wilcox
2021-03-22 9:03 ` Sergey Senozhatsky
2021-03-22 13:02 ` Matthew Wilcox
2021-03-22 13:57 ` Christoph Hellwig
2021-03-22 14:40 ` Matthew Wilcox
2021-03-22 17:09 ` Matthew Wilcox
2021-03-23 0:05 ` Sergey Senozhatsky
2021-03-22 16:16 ` Schaufler, Casey
2021-03-23 0:21 ` Namjae Jeon
2021-03-22 5:13 ` [PATCH 4/5] cifsd: add Kconfig and Makefile Namjae Jeon
2021-03-22 5:13 ` [PATCH 5/5] MAINTAINERS: add cifsd kernel server Namjae Jeon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210322070447.GE1667@kadam \
--to=dan.carpenter@oracle.com \
--cc=aaptel@suse.com \
--cc=aurelien.aptel@gmail.com \
--cc=colin.king@canonical.com \
--cc=hch@infradead.org \
--cc=hch@lst.de \
--cc=hyc.lee@gmail.com \
--cc=linux-cifs@vger.kernel.org \
--cc=linux-cifsd-devel@lists.sourceforge.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=namjae.jeon@samsung.com \
--cc=rdunlap@infradead.org \
--cc=ronniesahlberg@gmail.com \
--cc=sandeen@sandeen.net \
--cc=senozhatsky@chromium.org \
--cc=sergey.senozhatsky@gmail.com \
--cc=smfrench@gmail.com \
--cc=stfrench@microsoft.com \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox