From patchwork Thu Mar 1 23:37:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 880177 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zspnY5rs0z9s75 for ; Fri, 2 Mar 2018 10:40:01 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="W9Zjo3R7"; dkim-atps=neutral Received: from bilbo.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3zspnY3pdtzF1pJ for ; Fri, 2 Mar 2018 10:40:01 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="W9Zjo3R7"; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=rasmusvillemoes.dk (client-ip=2a00:1450:400c:c09::242; helo=mail-wm0-x242.google.com; envelope-from=linux@rasmusvillemoes.dk; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="W9Zjo3R7"; dkim-atps=neutral Received: from mail-wm0-x242.google.com (mail-wm0-x242.google.com [IPv6:2a00:1450:400c:c09::242]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3zspkp1nB9zF1nv for ; Fri, 2 Mar 2018 10:37:33 +1100 (AEDT) Received: by mail-wm0-x242.google.com with SMTP id q83so28555wme.5 for ; Thu, 01 Mar 2018 15:37:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id; bh=llpgdfU+LhhfauyqD8+9fltyjWwGopc6752T8CVLepY=; b=W9Zjo3R7ECCpiHL3xnDXhUbiMYIbKliTaA5PItgPHbjxZ3ldlmrCKw0Ux0WUR020eS 1DADmPb1m6L7jWsk32W8cTd4zFLhq4NgDx5a5jlMXa62Kj0Y7gwluL0pczvYZqL9k9Pu YjbYBP6Fj75iyR4p5sAjGEuxKSHqHKJzC8ujA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=llpgdfU+LhhfauyqD8+9fltyjWwGopc6752T8CVLepY=; b=nhEFU6Mb/gGidusvtdwnFOfTcwYLVK6SS7RuD3iHTyXliemMn9bLVPsq8fU1hfPAT1 Do7RRBBcfpO9JeR0jWj1b+brGOcRLX75WR23dWWvslTuo1wM0+fvzA0vnx9b6l2M3Vvi NSJtinZw0bb6kHWfKrCagO5TfKFyoq44kARFjjh7g8KQoz0JpRNiHfrIHGYBvxCTQTME 0J53e6gwP5tqVAZtVBypWDFQtzg5/ac0AR1AEELzXn6iziUtdJsRjJkY5Ewxa5sY9CBD bvYUpZ+mGrDJ/dPgY6dSO3erqlN5+hTr4NLcHVMR6f+EB7EIKMP4excOHsnFDPVr9+st wOMQ== X-Gm-Message-State: APf1xPAWO9tka6gnDHAmYaS9EBCzJ2mTw4rYmObyHYpdXz+skW9OvrSQ /OdIwCoW3ZRQI2kcxlX7ZuzO/A== X-Google-Smtp-Source: AG47ELuz3gZL5nwkbcdpGID1fl0qtKwVmhKCPYbjABNyljJgt7o6L+wdV/gQ31gbNvTB3LtaNVTauQ== X-Received: by 10.80.137.98 with SMTP id f31mr4886094edf.177.1519947450836; Thu, 01 Mar 2018 15:37:30 -0800 (PST) Received: from prevas-ravi.waoo.dk (dhcp-5-186-126-104.cgn.ip.fibianet.dk. [5.186.126.104]) by smtp.gmail.com with ESMTPSA id e12sm4088274edi.29.2018.03.01.15.37.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 01 Mar 2018 15:37:30 -0800 (PST) From: Rasmus Villemoes To: Jonathan Corbet , Alexander Viro Subject: [RFC 1/5] seq_file: introduce seq_open_data helper Date: Fri, 2 Mar 2018 00:37:20 +0100 Message-Id: <20180301233724.20440-1-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.15.1 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tony Luck , linux-ia64@vger.kernel.org, linux-doc@vger.kernel.org, Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" There are quite a few callers of seq_open that could be simplified by setting the ->private member via the seq_open call instead of fetching file->private_data afterwards. Signed-off-by: Rasmus Villemoes --- I've just included a few examples of possible users of this helper, there are many more similar cases. As a bonus, the first two fix potential NULL derefs (if one believes that seq_open can actually fail). seq_open_private would have been a better name, but that one is already taken... Documentation/filesystems/seq_file.txt | 9 +++++---- fs/seq_file.c | 9 ++++++++- include/linux/seq_file.h | 1 + 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Documentation/filesystems/seq_file.txt b/Documentation/filesystems/seq_file.txt index 9de4303201e1..68571b8275d8 100644 --- a/Documentation/filesystems/seq_file.txt +++ b/Documentation/filesystems/seq_file.txt @@ -234,10 +234,11 @@ Here, the call to seq_open() takes the seq_operations structure we created before, and gets set up to iterate through the virtual file. On a successful open, seq_open() stores the struct seq_file pointer in -file->private_data. If you have an application where the same iterator can -be used for more than one file, you can store an arbitrary pointer in the -private field of the seq_file structure; that value can then be retrieved -by the iterator functions. +file->private_data. If you have an application where the same iterator +can be used for more than one file, you can store an arbitrary pointer +in the private field of the seq_file structure; that value can then be +retrieved by the iterator functions. Using the wrapper seq_open_data() +allows you to set the initial value for that field. There is also a wrapper function to seq_open() called seq_open_private(). It kmallocs a zero filled block of memory and stores a pointer to it in the diff --git a/fs/seq_file.c b/fs/seq_file.c index eea09f6d8830..f2145cb6e23d 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -45,7 +45,7 @@ static void *seq_buf_alloc(unsigned long size) * Note: seq_open() will allocate a struct seq_file and store its * pointer in @file->private_data. This pointer should not be modified. */ -int seq_open(struct file *file, const struct seq_operations *op) +int seq_open_data(struct file *file, const struct seq_operations *op, void *data) { struct seq_file *p; @@ -59,6 +59,7 @@ int seq_open(struct file *file, const struct seq_operations *op) mutex_init(&p->lock); p->op = op; + p->private = data; // No refcounting: the lifetime of 'p' is constrained // to the lifetime of the file. @@ -85,6 +86,12 @@ int seq_open(struct file *file, const struct seq_operations *op) } EXPORT_SYMBOL(seq_open); +int seq_open(struct file *file, const struct seq_operations *op) +{ + return seq_open_data(file, op, NULL); +} +EXPORT_SYMBOL(seq_open_data); + static int traverse(struct seq_file *m, loff_t offset) { loff_t pos = 0, index; diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h index ab437dd2e3b9..f5ff376fa62b 100644 --- a/include/linux/seq_file.h +++ b/include/linux/seq_file.h @@ -107,6 +107,7 @@ void seq_pad(struct seq_file *m, char c); char *mangle_path(char *s, const char *p, const char *esc); int seq_open(struct file *, const struct seq_operations *); +int seq_open_data(struct file *, const struct seq_operations *, void *); ssize_t seq_read(struct file *, char __user *, size_t, loff_t *); loff_t seq_lseek(struct file *, loff_t, int); int seq_release(struct inode *, struct file *); From patchwork Thu Mar 1 23:37:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 880178 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zsprz2XXyz9s75 for ; Fri, 2 Mar 2018 10:42:59 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="D24PaGot"; dkim-atps=neutral Received: from bilbo.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3zspry2VCvzF1V0 for ; Fri, 2 Mar 2018 10:42:58 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="D24PaGot"; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=rasmusvillemoes.dk (client-ip=2a00:1450:400c:c09::244; helo=mail-wm0-x244.google.com; envelope-from=linux@rasmusvillemoes.dk; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="D24PaGot"; dkim-atps=neutral Received: from mail-wm0-x244.google.com (mail-wm0-x244.google.com [IPv6:2a00:1450:400c:c09::244]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3zspkp6ZPSzF1nx for ; Fri, 2 Mar 2018 10:37:35 +1100 (AEDT) Received: by mail-wm0-x244.google.com with SMTP id t3so3405wmc.2 for ; Thu, 01 Mar 2018 15:37:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZaUHUSqLgmeb9zt7BKghCdQOKTJNG8NtyriwsxI1j6Y=; b=D24PaGotwvPlLaRui+7m5+WW6MK0RBarvgba8Z7vf2/YPm0zapDRpodjUe1ZkHwzNr 7x8ZVMFG7CHApVJs1mzbDW9tOCyRbmGB8tOowjzZd2qwwJTQ93gn9K3rxqGYdGNmmcWv y0VeodPrR7W+nrmq6hypemRD4N5BodNY5IZtQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZaUHUSqLgmeb9zt7BKghCdQOKTJNG8NtyriwsxI1j6Y=; b=snnwZ+56cHZcIhv+bur8uS8Kl6aRW2y0lBVB9VLVzWuYd+J1Ic0l9J49ovmsuELNFP sWdCWVDsLtHkLTKClFmg5Hvc1OfEO8uQeaMT9s0/kefRCC/dk4cEAcnLd/pFLSOz8lbg FAdnsoGLWm9YPiFBIdGVoaPR/1LLWohP6iCTd9OCuSzn82FkpYlHNWzkizkSv5pIUrdZ nyKyrCmCGjJADQmXkKFWXGbTwsE+52pkuMOLsJib+xpBHHTeqBCFNM7Vd7Z006jTxeWf ZOOwzzUvCqxL5mJv57+DIjs1H8HBer0hIPcbvgvWo4PNj9rQIdfl+eis4Sv5/ue/GsxW zUlg== X-Gm-Message-State: APf1xPBDPhddP1uclMSfiaBF0odw6HykgVemB+QjtuGL88Atmdkq8nL6 GOuVk2rPe4VXctsTUGj+k4DqIHfytz4= X-Google-Smtp-Source: AG47ELvG5CAzDigVDFnhL9LJ11L4TERcM+gY0LdLo1IAJ//EA89f/I3RPhi3dqZW9Rvm4ePaudojXQ== X-Received: by 10.80.224.205 with SMTP id j13mr4919957edl.304.1519947453225; Thu, 01 Mar 2018 15:37:33 -0800 (PST) Received: from prevas-ravi.waoo.dk (dhcp-5-186-126-104.cgn.ip.fibianet.dk. [5.186.126.104]) by smtp.gmail.com with ESMTPSA id e12sm4088274edi.29.2018.03.01.15.37.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 01 Mar 2018 15:37:32 -0800 (PST) From: Rasmus Villemoes To: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman Subject: [RFC 3/5] powerpc/pseries: use seq_open_data in hcall_inst_seq_open Date: Fri, 2 Mar 2018 00:37:22 +0100 Message-Id: <20180301233724.20440-3-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180301233724.20440-1-linux@rasmusvillemoes.dk> References: <20180301233724.20440-1-linux@rasmusvillemoes.dk> X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linuxppc-dev@lists.ozlabs.org, Rasmus Villemoes , linux-kernel@vger.kernel.org Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" This code should check the return value of seq_open(); if it failed, file->private_data is NULL. But we can avoid the issue entirely and simplify the code by letting seq_open_data() set the ->private member. Signed-off-by: Rasmus Villemoes --- arch/powerpc/platforms/pseries/hvCall_inst.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/arch/powerpc/platforms/pseries/hvCall_inst.c b/arch/powerpc/platforms/pseries/hvCall_inst.c index 89b7ce807e70..05990c9fe264 100644 --- a/arch/powerpc/platforms/pseries/hvCall_inst.c +++ b/arch/powerpc/platforms/pseries/hvCall_inst.c @@ -92,14 +92,7 @@ static const struct seq_operations hcall_inst_seq_ops = { static int hcall_inst_seq_open(struct inode *inode, struct file *file) { - int rc; - struct seq_file *seq; - - rc = seq_open(file, &hcall_inst_seq_ops); - seq = file->private_data; - seq->private = file_inode(file)->i_private; - - return rc; + return seq_open_data(file, &hcall_inst_seq_ops, file_inode(file)->i_private); } static const struct file_operations hcall_inst_seq_fops = {