From patchwork Wed Feb 15 03:27:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tahsin Erdogan X-Patchwork-Id: 9573317 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 E1D7F601D8 for ; Wed, 15 Feb 2017 03:28:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CBDD3208C2 for ; Wed, 15 Feb 2017 03:28:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BE00B2841E; Wed, 15 Feb 2017 03:28:37 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BAEE2208C2 for ; Wed, 15 Feb 2017 03:28:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751869AbdBOD2W (ORCPT ); Tue, 14 Feb 2017 22:28:22 -0500 Received: from mail-pf0-f181.google.com ([209.85.192.181]:33240 "EHLO mail-pf0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751868AbdBOD2V (ORCPT ); Tue, 14 Feb 2017 22:28:21 -0500 Received: by mail-pf0-f181.google.com with SMTP id c73so24339670pfb.0 for ; Tue, 14 Feb 2017 19:28:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=yuH8TAdIaEOtsDRZJLzcAxyhPpXflhaQYNbPHCD03Gw=; b=JL4GaJ2EuEFTqPnHzR7zdLBcnflOu6O9vu9Q6JZfsYqkv25yLhEoaJt/Eib0WXF4uK sy+Np6DtQEXDd0RPovjIWGhN+4rfZN3yVOM98o9v36EtxVIoOQarVz8HyjpBwOy0edQz 3ggvZDjcKzZI8bqQDHp1cNpw706HqnSe0cvUXbK9VPhhOrycS9EnLrDeC5QVclj/25PT ZETzOfqfye9l2WEAdPW/HFPsB/eVCfWhpUcxnj+aFU/zM+UGrAw+4uXwAEQ1jLoRVw/m muylvLIt5cFT/VGxo+OOatux31+9l9lqTtZH8B1q2tc1u/t4T+3bd0xGOGkxTwzYCzoX ZLxQ== 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=yuH8TAdIaEOtsDRZJLzcAxyhPpXflhaQYNbPHCD03Gw=; b=dIE8/e8h1Gh/eZosBChTCR/PYPGABW2LVX+zQSGqs4ju6dttTjUaPSgg44OrT4UKYW hAbue+5TdAaK5PW44FgYTAAaWBkcs2d0DBXHnTuZ1VvGjthzGegTR5+u4MgR9qo0JrIy qER/wTVuV6jQ3Etau9Efud4Iy4yw04xnIpVcQrWaBKCSqYpu4aYv7RsRIWW8rAE9NAsS gagBfDwzTxQtwVAOZ4UysDwYnBFdlT5jMzHSlGtppFQDsa5d8QLxmGQsg4Y/lN4Rfvpa QaBYdAXbaO+PMMgIqxzfAdinjxY2jxwNxXbwennERcmVYFFtQKI1TC3P7yYiG5lpBDfl 3bdw== X-Gm-Message-State: AMke39m8pIZAYBI+Z5+QB+Ruru9C5Ty/k/NLZZFvIKH6gs31yL6mK7tLuD/EQHZQ/nJvV1Rg X-Received: by 10.84.128.33 with SMTP id 30mr40750276pla.128.1487129295006; Tue, 14 Feb 2017 19:28:15 -0800 (PST) Received: from tahsin1.mtv.corp.google.com ([100.99.140.90]) by smtp.gmail.com with ESMTPSA id s64sm3750073pfe.27.2017.02.14.19.28.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 14 Feb 2017 19:28:14 -0800 (PST) From: Tahsin Erdogan To: Jens Axboe , linux-block@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Tahsin Erdogan Subject: [PATCH] block: do not allow updates through sysfs until registration completes Date: Tue, 14 Feb 2017 19:27:38 -0800 Message-Id: <20170215032738.22689-1-tahsin@google.com> X-Mailer: git-send-email 2.11.0.483.g087da7b7c-goog Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When a new disk shows up, sysfs queue directory is created before elevator is registered. This allows a user to attempt a scheduler switch even though the initial registration hasn't completed yet. In one scenario, blk_register_queue() calls elv_register_queue() and right before cfq_registered_queue() is called, another process executes elevator_switch() and replaces q->elevator with deadline scheduler. When cfq_registered_queue() executes it interprets e->elevator_data as struct cfq_data even though it is actually struct deadline_data. Grab q->sysfs_lock in blk_register_queue() to synchronize with sysfs callers. Signed-off-by: Tahsin Erdogan --- block/blk-sysfs.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c index 070d81bae1d5..002af836aa87 100644 --- a/block/blk-sysfs.c +++ b/block/blk-sysfs.c @@ -902,17 +902,20 @@ int blk_register_queue(struct gendisk *disk) if (ret) return ret; + if (q->mq_ops) + blk_mq_register_dev(dev, q); + + /* Prevent changes through sysfs until registration is completed. */ + mutex_lock(&q->sysfs_lock); + ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue"); if (ret < 0) { blk_trace_remove_sysfs(dev); - return ret; + goto unlock; } kobject_uevent(&q->kobj, KOBJ_ADD); - if (q->mq_ops) - blk_mq_register_dev(dev, q); - blk_wb_init(q); if (q->request_fn || (q->mq_ops && q->elevator)) { @@ -922,11 +925,13 @@ int blk_register_queue(struct gendisk *disk) kobject_del(&q->kobj); blk_trace_remove_sysfs(dev); kobject_put(&dev->kobj); - return ret; + goto unlock; } } - - return 0; + ret = 0; +unlock: + mutex_unlock(&q->sysfs_lock); + return ret; } void blk_unregister_queue(struct gendisk *disk)