From patchwork Mon Jan 11 17:28:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ewan Milne X-Patchwork-Id: 8007081 Return-Path: X-Original-To: patchwork-linux-scsi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 60FE1BEEE5 for ; Mon, 11 Jan 2016 17:29:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8F3D4201C8 for ; Mon, 11 Jan 2016 17:29:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9613A201BB for ; Mon, 11 Jan 2016 17:29:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932988AbcAKR3S (ORCPT ); Mon, 11 Jan 2016 12:29:18 -0500 Received: from mx1.redhat.com ([209.132.183.28]:33894 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1760053AbcAKR2b (ORCPT ); Mon, 11 Jan 2016 12:28:31 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id E21F91B4031; Mon, 11 Jan 2016 17:28:30 +0000 (UTC) Received: from emilne.csb (dhcp-25-104.bos.redhat.com [10.18.25.104]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0BHSSip003254; Mon, 11 Jan 2016 12:28:30 -0500 From: "Ewan D. Milne" To: linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org Cc: gregkh@linuxfoundation.org, martin.petersen@oracle.com, hare@suse.com Subject: [PATCH 2/2] scsi_proc: Change /proc/scsi/scsi to use bus device iterator Date: Mon, 11 Jan 2016 12:28:27 -0500 Message-Id: <1452533307-30142-3-git-send-email-emilne@redhat.com> In-Reply-To: <1452533307-30142-1-git-send-email-emilne@redhat.com> References: <1452533307-30142-1-git-send-email-emilne@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: "Ewan D. Milne" This prevents crashing due to accessing a removed element on the list, the iterator will now hold the correct reference. It was not sufficient to rely on the klist's reference on the containing device object. From a patch originally developed by David Jeffery Signed-off-by: Ewan D. Milne Reviewed-by: Hannes Reinecke --- drivers/scsi/scsi_proc.c | 49 ++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c index 251598e..6c1b79d 100644 --- a/drivers/scsi/scsi_proc.c +++ b/drivers/scsi/scsi_proc.c @@ -40,6 +40,11 @@ /* 4K page size, but our output routines, use some slack for overruns */ #define PROC_BLOCK_SIZE (3*1024) +struct scsi_proc_state { + struct klist_iter iter; + int pos; +}; + static struct proc_dir_entry *proc_scsi; /* Protect sht->present and sht->proc_dir */ @@ -370,47 +375,50 @@ static ssize_t proc_scsi_write(struct file *file, const char __user *buf, return err; } -static int always_match(struct device *dev, void *data) -{ - return 1; -} - -static inline struct device *next_scsi_device(struct device *start) -{ - struct device *next = bus_find_device(&scsi_bus_type, start, NULL, - always_match); - put_device(start); - return next; -} - static void *scsi_seq_start(struct seq_file *sfile, loff_t *pos) { + struct scsi_proc_state *state = sfile->private; struct device *dev = NULL; loff_t n = *pos; + int err; + + err = bus_device_iter_init(&state->iter, &scsi_bus_type); + if (err < 0) + return ERR_PTR(err); - while ((dev = next_scsi_device(dev))) { + while ((dev = bus_device_iter_next(&state->iter))) { if (!n--) break; - sfile->private++; + put_device(dev); + state->pos++; } return dev; } static void *scsi_seq_next(struct seq_file *sfile, void *v, loff_t *pos) { + struct scsi_proc_state *state = sfile->private; + (*pos)++; - sfile->private++; - return next_scsi_device(v); + put_device(v); + state->pos++; + + return bus_device_iter_next(&state->iter); } static void scsi_seq_stop(struct seq_file *sfile, void *v) { + struct scsi_proc_state *state = sfile->private; + put_device(v); + bus_device_iter_exit(&state->iter); } static int scsi_seq_show(struct seq_file *sfile, void *dev) { - if (!sfile->private) + struct scsi_proc_state *state = sfile->private; + + if (!state->pos) seq_puts(sfile, "Attached devices:\n"); return proc_print_scsidevice(dev, sfile); @@ -436,7 +444,8 @@ static int proc_scsi_open(struct inode *inode, struct file *file) * We don't really need this for the write case but it doesn't * harm either. */ - return seq_open(file, &scsi_seq_ops); + return seq_open_private(file, &scsi_seq_ops, + sizeof(struct scsi_proc_state)); } static const struct file_operations proc_scsi_operations = { @@ -445,7 +454,7 @@ static const struct file_operations proc_scsi_operations = { .read = seq_read, .write = proc_scsi_write, .llseek = seq_lseek, - .release = seq_release, + .release = seq_release_private, }; /**