From patchwork Fri Jul 20 10:29:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 10536423 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 816856029B for ; Fri, 20 Jul 2018 10:30:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 72F012953D for ; Fri, 20 Jul 2018 10:30:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 66DC429540; Fri, 20 Jul 2018 10:30:20 +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=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C8EFD2953F for ; Fri, 20 Jul 2018 10:30:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BDB866B0003; Fri, 20 Jul 2018 06:30:18 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B63736B0005; Fri, 20 Jul 2018 06:30:18 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A04AF6B0006; Fri, 20 Jul 2018 06:30:18 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by kanga.kvack.org (Postfix) with ESMTP id 3F9786B0003 for ; Fri, 20 Jul 2018 06:30:18 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id c2-v6so4503331edi.20 for ; Fri, 20 Jul 2018 03:30:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id; bh=ef1IinSsDXnP0FyRH/XIS6U/LSy2GMjNOyjFuFG7su4=; b=Vlu0qPT6xqAD21Udlqs6skf6/wRcvifwIUcSZYbh+QUfEBYWLlJeXaLFsThuPgZJ0/ NgP1ZkhXbK6Vh5zhQoMNLX6sq1kdclk7xBnXspm8W0R32pBGXeC5VtbetX0Vpnq7OACo 1Rb+66PlDGnAo3m5RTU8VXuQ6ZyBJoRN+8f3F0KeTnkpfKhVG06bJzjDa4TfJSJoe9or WuMhoAJhe/TJPCYcdWZwA2Sd/P+yo0FfyZkcDruqgn0DEHlB3NFrrsAOEdSwK88dz4H0 gNyCY5VzJIrJ9V6MGdEF+TDn3K1EcVBB8JFEZk0OXp+zmCNTg0sVOLkd/OeEJmLD42sH zkEQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of vbabka@suse.cz designates 195.135.220.15 as permitted sender) smtp.mailfrom=vbabka@suse.cz X-Gm-Message-State: AOUpUlGfTV72fKGTvUmzmyxRzZ/6SsiBZrUOq9dC7wHKzkjV0rrm88iW 1cjUKVWIxbXpujvIjQ6Xs83cS5XN6pFY5qRHF1AdXAx3W0dw5d5lMcCPeKl3LVeFb838Ivkux4B K+pXKxbiYduiG4HZsOb46+d9pD+dTAwJPVy5xNxhlHGa6xIiB8XySqCmJzjbrx0uLBg== X-Received: by 2002:a50:e146:: with SMTP id i6-v6mr2011186edl.204.1532082617791; Fri, 20 Jul 2018 03:30:17 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdEMUOscA9ru/H6eQOzS8dFPK2/nyZvwc3a4bafwqjDvNdgz6aObUUOrOn5sCV1PT9iH8H9 X-Received: by 2002:a50:e146:: with SMTP id i6-v6mr2011118edl.204.1532082616684; Fri, 20 Jul 2018 03:30:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532082616; cv=none; d=google.com; s=arc-20160816; b=fC3LI4q9SXfMpzxEzVGUIKV/H2HsEahoJTtk/4DZ/z/+Hc0KRP4BdpVNZKAXKn0j2H qzs+AKx7A1aGroFstJERx7AmmfomSsNAgD31BGz9kmhUIB1qvQ/7pT53v6w3K6FneNXj NyVf51Ykz7MhV9kxT7orKVYTZW/msRAAbU4C5nU3d4Rm1sfQbdf+37IeuCdop9LvphFD Qra6KE7RW/QRzJE91fbMk2yaBLaDsH1OghpPrwYpGiD7mDs/om+Mlaa5TgAmcD7lKvih yHXDB4Crgkf5hGE3LATP4lr0qs9nPSc3LCPhq6ObIBWhiVzpEODU4knH2vMnBoeZWrup mg6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:arc-authentication-results; bh=ef1IinSsDXnP0FyRH/XIS6U/LSy2GMjNOyjFuFG7su4=; b=zDnKCntCD06Tk1BTPFMcHYyk1iAKtkV7pxX/uf4570EHT2GY8UfzntYpBKmuIX4nxd mVkAl4/JS9pwKujg0Xk/K01/7O6xkqPsWs0AgqyeNy3lkshDwtrLjypEDUlJUL2aoIe0 rNMybJpsOcjdMej32PdIkFAgErOdS9XR11s4+C3CCCutpSCz2OzhQ3KWbIx/4ZVR/wRe CAga3fdR5/Xb78EsnYeef0cZLyBPRsoQ5QGRxPgeKcP0oo5JTnLX3wBrEkrTjO4Csw8X tdFug9JpPdH9fSceejRNM11W1qJD2ncrcarwAyGnxFrxt55M+rUu9+aUb6C+g8hVlL98 DCSQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of vbabka@suse.cz designates 195.135.220.15 as permitted sender) smtp.mailfrom=vbabka@suse.cz Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id x18-v6si1318099edb.460.2018.07.20.03.30.16 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 20 Jul 2018 03:30:16 -0700 (PDT) Received-SPF: pass (google.com: domain of vbabka@suse.cz designates 195.135.220.15 as permitted sender) client-ip=195.135.220.15; Authentication-Results: mx.google.com; spf=pass (google.com: domain of vbabka@suse.cz designates 195.135.220.15 as permitted sender) smtp.mailfrom=vbabka@suse.cz X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id D4963ADA7; Fri, 20 Jul 2018 10:30:15 +0000 (UTC) From: Vlastimil Babka To: Alexander Viro Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Vlastimil Babka , Matthew Wilcox Subject: [PATCH] fs/seq_file: remove kmalloc(ops) for single_open seqfiles Date: Fri, 20 Jul 2018 12:29:52 +0200 Message-Id: <20180720102952.30935-1-vbabka@suse.cz> X-Mailer: git-send-email 2.18.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP single_open() currently allocates seq_operations with kmalloc(). This is suboptimal, because that's four pointers, of which three are constant, and only the 'show' op differs. We also have to be careful to use single_release() to avoid leaking the ops structure. Instead of this we can have a fixed single_show() function and constant ops structure for these seq_files. We can store the pointer to the 'show' op as a new field of struct seq_file. That's also not terribly elegant, because the field is there also for non-single_open() seq files, but it's a single pointer in an already existing (and already relatively large) structure instead of an extra kmalloc of four pointers, so the tradeoff is OK. Suggested-by: Matthew Wilcox Signed-off-by: Vlastimil Babka --- Documentation/filesystems/seq_file.txt | 5 +++- fs/seq_file.c | 40 ++++++++++++-------------- include/linux/seq_file.h | 5 ++-- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Documentation/filesystems/seq_file.txt b/Documentation/filesystems/seq_file.txt index 9de4303201e1..ed61495abee8 100644 --- a/Documentation/filesystems/seq_file.txt +++ b/Documentation/filesystems/seq_file.txt @@ -335,4 +335,7 @@ When output time comes, the show() function will be called once. The data value given to single_open() can be found in the private field of the seq_file structure. When using single_open(), the programmer should use single_release() instead of seq_release() in the file_operations structure -to avoid a memory leak. +to avoid a memory leak. Note that the implementation has changed and current +kernels will not leak anymore, but it's better to keep using single_release() +in case the implementation details change again. + diff --git a/fs/seq_file.c b/fs/seq_file.c index 4cc090b50cc5..3fd2ded04d93 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -563,22 +563,27 @@ static void single_stop(struct seq_file *p, void *v) { } +static int single_show(struct seq_file *p, void *v) +{ + return p->single_show_op(p, v); +} + +static const struct seq_operations single_seq_op = { + .start = single_start, + .next = single_next, + .stop = single_stop, + .show = single_show +}; + int single_open(struct file *file, int (*show)(struct seq_file *, void *), void *data) { - struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT); - int res = -ENOMEM; - - if (op) { - op->start = single_start; - op->next = single_next; - op->stop = single_stop; - op->show = show; - res = seq_open(file, op); - if (!res) - ((struct seq_file *)file->private_data)->private = data; - else - kfree(op); + int res; + + res = seq_open(file, &single_seq_op); + if (!res) { + ((struct seq_file *)file->private_data)->private = data; + ((struct seq_file *)file->private_data)->single_show_op = show; } return res; } @@ -602,15 +607,6 @@ int single_open_size(struct file *file, int (*show)(struct seq_file *, void *), } EXPORT_SYMBOL(single_open_size); -int single_release(struct inode *inode, struct file *file) -{ - const struct seq_operations *op = ((struct seq_file *)file->private_data)->op; - int res = seq_release(inode, file); - kfree(op); - return res; -} -EXPORT_SYMBOL(single_release); - int seq_release_private(struct inode *inode, struct file *file) { struct seq_file *seq = file->private_data; diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h index a121982af0f5..c9a70c584a7d 100644 --- a/include/linux/seq_file.h +++ b/include/linux/seq_file.h @@ -24,9 +24,10 @@ struct seq_file { u64 version; struct mutex lock; const struct seq_operations *op; - int poll_event; + int (*single_show_op)(struct seq_file *, void *); const struct file *file; void *private; + int poll_event; }; struct seq_operations { @@ -140,7 +141,7 @@ int seq_path_root(struct seq_file *m, const struct path *path, int single_open(struct file *, int (*)(struct seq_file *, void *), void *); int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t); -int single_release(struct inode *, struct file *); +#define single_release seq_release void *__seq_open_private(struct file *, const struct seq_operations *, int); int seq_open_private(struct file *, const struct seq_operations *, int); int seq_release_private(struct inode *, struct file *);