Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754059AbdCOVfb (ORCPT ); Wed, 15 Mar 2017 17:35:31 -0400 Received: from esa2.hgst.iphmx.com ([68.232.143.124]:44061 "EHLO esa2.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754015AbdCOVfZ (ORCPT ); Wed, 15 Mar 2017 17:35:25 -0400 X-IronPort-AV: E=Sophos;i="5.36,170,1486396800"; d="scan'208";a="99536404" Authentication-Results: spf=pass (sender IP is 74.221.232.54) smtp.mailfrom=sandisk.com; infradead.org; dkim=none (message not signed) header.d=none;infradead.org; dmarc=bestguesspass action=none header.from=sandisk.com; X-AuditID: ac1c2133-99bff7000000c960-a9-58c9b388dfee From: Bart Van Assche To: "tom.leiming@gmail.com" CC: "linux-kernel@vger.kernel.org" , "hch@infradead.org" , "linux-block@vger.kernel.org" , "yizhan@redhat.com" , "axboe@fb.com" , "stable@vger.kernel.org" Subject: Re: [PATCH 1/2] blk-mq: don't complete un-started request in timeout handler Thread-Topic: [PATCH 1/2] blk-mq: don't complete un-started request in timeout handler Thread-Index: AQHSndQBXyeU7CqloE+X1Z4ETgZgFg== Date: Wed, 15 Mar 2017 21:35:03 +0000 Message-ID: <1489613678.2660.9.camel@sandisk.com> References: <1489064578-17305-1-git-send-email-tom.leiming@gmail.com> <1489064578-17305-3-git-send-email-tom.leiming@gmail.com> <1489536441.2676.21.camel@sandisk.com> <20170315121851.GA15807@ming.t460p> <20170315124024.GA16549@ming.t460p> <1489592177.2660.1.camel@sandisk.com> <20170315162158.GA18768@ming.t460p> In-Reply-To: <20170315162158.GA18768@ming.t460p> Accept-Language: en-US, nl-NL Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.28.1.254] Content-Type: text/plain; charset="iso-8859-1" Content-ID: <051AFF7D394CF44BA7A95C164A3A36DE@sandisk.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrNIsWRmVeSWpSXmKPExsWyRobxn27H5pMRBpu3W1n833OMzeL0hEVM FntvaVtc3jWHzWLBxkeMFu9/XGe32HLlLpsDu8fE5nfsHjtn3WX32LxCy+P9vqtsHp83yQWw RnHZpKTmZJalFunbJXBl7L7/na3gVnTFihdvmRoYb/p2MXJwSAiYSLw55NrFyMUhJLCESeJA wyl2COcYo8TlZW0sXYycHGwCRhKzJ+wBs0UEjCU+vX7GCFLELLCKSeLG70ZWkISwQLjEq/Yb jBBFERLvf/5nhbD1JB78vcICso1FQFWiZ3k8SJhXwFDi0/b5LBDLjjBJPGl/zQSS4BQwkDj8 +A4biM0oICuxeHoLWJxZQFzi1pP5YLaEgIDEkj3nmSFsUYmXj/+xQtgKEp9X/GODqNeTuDF1 CpRtJXHw1zUoW1ti2cLXzBBHCEqcnPmEZQKj2CwkK2YhaZ+FpH0WkvZZSNoXMLKuYhQrTkwu zk1PLTA00StOzEvJLM7WS87P3cQIjlJF4x2M/za4H2IU4GBU4uGd4HsyQog1say4MvcQowQH s5IIb/4qoBBvSmJlVWpRfnxRaU5q8SFGaQ4WJXHemNlTI4QE0hNLUrNTUwtSi2CyTBycUg2M ylNmnbt9s6129hy/u38CHi3c82PuGcmmL9+PFDz/eZZvl+PvyPW1m1lb0t/PWOMa2aitHbta 1JfNc9GGvFzOgwsUM3kWT97W8unW74jiRQkbXga9W/tifkuPo8ILIRGVuKWxO7srfptsPN91 3lF4i+mz8j+bfISrJJmSC8PUppRfeZHz69TK60osxRmJhlrMRcWJADr35J3OAgAA X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:74.221.232.54;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10019020)(6009001)(39450400003)(39860400002)(39840400002)(39410400002)(39850400002)(2980300002)(438002)(189002)(377424004)(24454002)(199003)(9170700003)(8936002)(38730400002)(110136004)(54906002)(6246003)(5250100002)(2950100002)(50986999)(2906002)(2501003)(189998001)(53936002)(4326008)(229853002)(36756003)(5640700003)(102836003)(305945005)(356003)(8676002)(8746002)(76176999)(54356999)(106466001)(93886004)(86362001)(2351001)(2900100001)(33646002)(47776003)(3846002)(6916009)(5660300001)(39060400002)(6116002)(50466002)(81166006)(103116003)(7736002)(23756003);DIR:OUT;SFP:1102;SCL:1;SRVR:DM2PR04MB527;H:sacsmgep14.sandisk.com;FPR:;SPF:Pass;MLV:ovrnspm;MX:1;A:1;PTR:InfoDomainNonexistent;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;BN3NAM04FT011;1:a8+eJoK5DebbVq51HQdgS831pSj6MHwiqALwZE5NSW3hP5y2oVpzPEMa/amz071l4KxfYNPo+QNC1dAmJSBJB8NnT5M0Qc1yAOC9aIHtEVpSH8R2IMHEeMUA6ByI1P0YwIIUik30zvpv10RormwTGFSMDr4BqDqdg5BGgUrd2EaDVVR+XvsjzgYseFAqJGp/LYJXZvJkjjfNL11iALrJtF20SJQL7lAO582NMmzA70jjsmx6R14lJ6E/DE93iliha/z9B8pPFKVsj2L6k9a0D/DrOn+NzgoUjitpDgWzzk574ScL0OGSt5og0KrHxJnEe/9GAfS5lxhqvhn/VAJB8KLdrOt6Q+52Dmzf3SvkZ1axicI2sYAfgl4nU3U1qR8qAtLOupYZHdFv3zQeZFkkW2PEStplsCNL5R9gf69jTvo5tQqRs4mc723cbE7zJc+a5s0xiuoO+j7S9xBpbebex1UxsduJyRYFoo7OKx9An8z23KakDEyIk8EBvgItlgSw2fm7ssE0R7TdQIeXWMXotwoDQ7fJbcdXrbDK0qRX0z9mUv7M6t2wSK5FmkFMa8K2 X-MS-Office365-Filtering-Correlation-Id: 95aa12f0-f99e-4f5d-5b41-08d46beb25c3 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(8251501002);SRVR:DM2PR04MB527; X-Microsoft-Exchange-Diagnostics: 1;DM2PR04MB527;3:IXIqZxF5EkietaFyb5TyMwWQfD/1zl6BzY3pONwa6AjULGm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OetbpxXFoJKf6+deFry/lAJHPSk2gFoxZYDCDMhKat9FVw43xjFF;20:VVROLjbfaWQ/Qx1EY3v7wDXcToYeWaDXNwFnjoe9qFBHfJiQWakujmz3b8beK9PtyYmnQakg+MOAwl48KIR7WJB8xKb8A7ZzNPFm6YbFf65TEjcD+hMIEtNf9GSxDezmXA+s/RfOP7Y1Bxh2ruIBTnuWpJ+ETAwsfsoUgrQuC0djDNwDW2bVGtSh4XG9ZSH20ipJvBahX9xM3oiAB8gLXhcV2HvaAdbWwMGU3Q5KxFcdWZ+HPOBUxWh7Vzi4OspmkHIhXMeArU5ccdElG27IbGrhn5RIRMNkD7ObiYcF2OdoLXyYRQv4FqjSgoyNEuF8c6aMnfH18ZibSxOpGwrQr80GWxuTmAzCK5dH07XGkgen5JfxBpUE3WYqldX8vqT8H35DczjO7PSVeDGKRfUvtgEX+Ebt44V66E+cC8b01XcySqLrx4U0lOQfQ6Ss4TWSDCJvQezv4AtLe49rGPYEViwG5E2P5+gtsF0IMWL14QU/GGjBg1XGfFVTmWJ7HJg9 WDCIPOUTBOUND: EOP-TRUE X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040375)(601004)(2401047)(8121501046)(5005006)(13018025)(13016025)(13024025)(13023025)(10201501046)(3002001)(6055026)(6041248)(20161123555025)(20161123560025)(20161123558025)(20161123564025)(20161123562025)(6072148);SRVR:DM2PR04MB527;BCL:0;PCL:0;RULEID:;SRVR:DM2PR04MB527; X-Microsoft-Exchange-Diagnostics: 1;DM2PR04MB527;4:qMKnZKAX9bMEPCy0dAVL06rqAGgp2qwGFCfTTK1TSrB6ZpPuFQsZ6TvRp3VvSq6fIizfRQbF43/dB71x1s4IzzviCzrZpUuUfUrNW5ZO8359cGJ2thdJmfeNeP68/HQnFibj/eGdOxqnm6pciprWDkzGmpFbwIc2NYGj+Jtvz9NYXzFWvtWVGrg0jkwsc0gbcBiO/QtaAGtArSecignhJuaaNRX8ilwzJh+QThPDc8u25Vqe/1wqYZQvQrniVURBl9Az/iB5tbGHRGtTAb7AFG2/KRyNpWCWgbqqu9QprjlF8NaYu+dgsLEFS87oE/ahYua/BYkzDIUqpnSesQbFsM+fk7E1xl7S/jVT9p2iE1f47LQEnoK5D2mgmdWUR7YGqldzDJcWmG1eFSzijQm249YP82MsRMFDFHmVT69HK+EQHqZWMuhJB6MMCYOwf7r/ohEdPDq+65vu67+5hJOFKPk+yV8kBA24zVDYCdooQEeYNckG0FvZJXMutV9siHfN/6u7trVDrSKVO1IFH1hP8Z1LlL6selKnku9Goj3t8HSDxBcstDeaOFN9uh6vIZ2y4WgsF4symYZrdB9FSqIG0PuTD7REAtD5Oy4USd17/IgL5Zxgy/j2paLTOkNgNAH4zXxop/3Vsb7Gud2dq7pQqy40CNM52Jo757tvMnM0YD1LGjIhaOVmm5HVdwPjdhB4 X-Forefront-PRVS: 02475B2A01 X-Microsoft-Exchange-Diagnostics: =?iso-8859-1?Q?1;DM2PR04MB527;23:yMPtkGHpjEHF69J/z0UUkzhjr6O2MS/gzkkGTx56?= =?iso-8859-1?Q?7+hXTSruuIw5g62LMEg2IfswPl53cRDHWKahbi2zAkfu6AjOSvg7mY3p6r?= =?iso-8859-1?Q?1XqtNGXhqLzufSCbQv38suM3QIhwQyNbuHyukENGckH6r53xf6ltX0Kb2b?= =?iso-8859-1?Q?54uPv+1R6yEOa/NhfrRkICVNhFPdHdOlpz+KQOC/xb3iqmlbVD/N7crF9u?= =?iso-8859-1?Q?bWO4UVdItFYOocCQdo5Zn/9fSw4LpiOqWIcP4UgnNicPOw+5TI9jo/QO01?= =?iso-8859-1?Q?Ja2lbyRDnxxdZT866TAYoG+eNafuZlu+p9fIYRhz0xioaloY/bDYYt+LNa?= =?iso-8859-1?Q?sesGsusp8atuFb2yjjxJs2NqDVnA4Q31KRI4IaXI11BcBBQ0egsGU0zSpP?= =?iso-8859-1?Q?Ee+G7Wgv4y+hf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ZJqKFxMKYH6gmnswuNIYSwa7KzvS2HsXHYsvqw0X0UShEJciVBXXTbKg6ilQX6byESjjJ1D4X9JcAFH+lFMi20ehbXdVBOZXlD7PfCe9mkq1hik3wrlsdGvreN4zlE7bvwDBFLCANZFqJCtiA7bcsdsPK2cOW6ETCmk5laZsPw==;20:egmKVIQHTtRwW+fP+D9e69GUSAqFEXnPtwg1ODhfYBmGAkfIJOVqB7+s52E0dhOzFfn2k/TPFe+21i2TRLmtfTXw9l5Oqa26T+vExd7MWvG7ndaQZiLZsgpnW0o8TWzsTeGib+AewxTBIT0wJ6hAH10QCuSAK4Dwpi8dnqjcFFs= X-OriginatorOrg: sandisk.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Mar 2017 21:35:05.3316 (UTC) X-MS-Exchange-CrossTenant-Id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=b61c8803-16f3-4c35-9b17-6f65f441df86;Ip=[74.221.232.54];Helo=[sacsmgep14.sandisk.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR04MB527 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by mail.home.local id v2FLZZIQ026225 Content-Length: 10891 Lines: 298 On Thu, 2017-03-16 at 00:22 +0800, Ming Lei wrote: > On Wed, Mar 15, 2017 at 03:36:31PM +0000, Bart Van Assche wrote: > > Please have another look at __blk_mq_requeue_request(). In that function > > the following code occurs: if (test_and_clear_bit(REQ_ATOM_STARTED, > > &rq->atomic_flags)) { ... } > > > > I think the?REQ_ATOM_STARTED check in blk_mq_check_expired() races with the > > test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags) call in > > __blk_mq_requeue_request(). > > OK, this race should only exist in case that the requeue happens after dispatch > busy, because COMPLETE flag isn't set. And if the requeue is from io completion, > no such race because COMPLETE flag is set. > > One solution I thought of is to call blk_mark_rq_complete() before requeuing > when dispatch busy happened, but that looks a bit silly. Another way is to > set STARTED flag just after .queue_rq returns BLK_MQ_RQ_QUEUE_OK, which looks > reasonable too. Any comments on the 2nd solution? Sorry but I don't think that would be sufficient. There are several other scenarios that have not been mentioned above, e.g. that a tag gets freed and reused after the blk_mq_check_expired() call started and before that function has had the chance to examine any members of struct request. What is needed in blk_mq_check_expired() is the following as a single atomic operation: * Check whether REQ_ATOM_STARTED has been set. * Check whether REQ_ATOM_COMPLETE has not yet been set. * If both conditions have been met, set REQ_ATOM_COMPLETE. I don't think there is another solution than using a single state variable to represent the REQ_ATOM_STARTED and REQ_ATOM_COMPLETE states instead of two independent bits. How about the patch below? Thanks, Bart. [PATCH] blk-mq: Fix request state manipulation races Use a single state variable instead of two separate bits REQ_ATOM_STARTED and REQ_ATOM_COMPLETE. For blk-mq, make __blk_mq_finish_request() perform the transition from "complete" to "not complete" instead of blk_mq_start_request(). Make sure that blk_mq_check_expired() uses a single atomic operation to test the "started" and "complete" states and to set the "complete" state. --- ?block/blk-core.c???????????|??6 +++-- ?block/blk-mq.c?????????????| 67 +++++++++++++++++----------------------------- ?block/blk-timeout.c????????|??2 +- ?block/blk.h????????????????| 21 ++++++++++----- ?drivers/scsi/virtio_scsi.c |??2 +- ?include/linux/blkdev.h?????|??1 + ?6 files changed, 47 insertions(+), 52 deletions(-) diff --git a/block/blk-core.c b/block/blk-core.c index 0eeb99ef654f..dff857d2b86b 100644 --- a/block/blk-core.c +++ b/block/blk-core.c @@ -1305,7 +1305,7 @@ EXPORT_SYMBOL(blk_get_request); ?void blk_requeue_request(struct request_queue *q, struct request *rq) ?{ ? blk_delete_timer(rq); - blk_clear_rq_complete(rq); + atomic_set(&rq->state, REQ_NOT_STARTED); ? trace_block_rq_requeue(q, rq); ? wbt_requeue(q->rq_wb, &rq->issue_stat); ? @@ -2477,7 +2477,9 @@ void blk_start_request(struct request *req) ? wbt_issue(req->q->rq_wb, &req->issue_stat); ? } ? - BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags)); + WARN_ONCE(atomic_read(&req->state) != REQ_NOT_STARTED, + ??"unexpected request state %d != %d\n", + ??atomic_read(&req->state), REQ_NOT_STARTED); ? blk_add_timer(req); ?} ?EXPORT_SYMBOL(blk_start_request); diff --git a/block/blk-mq.c b/block/blk-mq.c index 159187a28d66..fe73d5a1ffc3 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -343,7 +343,7 @@ void __blk_mq_finish_request(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, ? wbt_done(q->rq_wb, &rq->issue_stat); ? rq->rq_flags = 0; ? - clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); + atomic_set(&rq->state, REQ_NOT_STARTED); ? clear_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags); ? if (rq->tag != -1) ? blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag); @@ -479,7 +479,7 @@ EXPORT_SYMBOL(blk_mq_complete_request); ? ?int blk_mq_request_started(struct request *rq) ?{ - return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags); + return atomic_read(&rq->state) == REQ_STARTED; ?} ?EXPORT_SYMBOL_GPL(blk_mq_request_started); ? @@ -505,16 +505,10 @@ void blk_mq_start_request(struct request *rq) ? ?*/ ? smp_mb__before_atomic(); ? - /* - ?* Mark us as started and clear complete. Complete might have been - ?* set if requeue raced with timeout, which then marked it as - ?* complete. So be sure to clear complete again when we start - ?* the request, otherwise we'll ignore the completion event. - ?*/ - if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) - set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); - if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags)) - clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); + WARN_ONCE(atomic_read(&rq->state) != REQ_NOT_STARTED, + ??"unexpected request state %d != %d\n", + ??atomic_read(&rq->state), REQ_NOT_STARTED); + atomic_set(&rq->state, REQ_STARTED); ? ? if (q->dma_drain_size && blk_rq_bytes(rq)) { ? /* @@ -530,12 +524,14 @@ EXPORT_SYMBOL(blk_mq_start_request); ?static void __blk_mq_requeue_request(struct request *rq) ?{ ? struct request_queue *q = rq->q; + enum rq_state prev; ? ? trace_block_rq_requeue(q, rq); ? wbt_requeue(q->rq_wb, &rq->issue_stat); ? blk_mq_sched_requeue_request(rq); ? - if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) { + prev = atomic_xchg(&rq->state, REQ_NOT_STARTED); + if (prev != REQ_NOT_STARTED) { ? if (q->dma_drain_size && blk_rq_bytes(rq)) ? rq->nr_phys_segments--; ? } @@ -661,17 +657,7 @@ void blk_mq_rq_timed_out(struct request *req, bool reserved) ? const struct blk_mq_ops *ops = req->q->mq_ops; ? enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER; ? - /* - ?* We know that complete is set at this point. If STARTED isn't set - ?* anymore, then the request isn't active and the "timeout" should - ?* just be ignored. This can happen due to the bitflag ordering. - ?* Timeout first checks if STARTED is set, and if it is, assumes - ?* the request is active. But if we race with completion, then - ?* we both flags will get cleared. So check here again, and ignore - ?* a timeout event with a request that isn't active. - ?*/ - if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags)) - return; + WARN_ON_ONCE(atomic_read(&req->state) != REQ_COMPLETE); ? ? if (ops->timeout) ? ret = ops->timeout(req, reserved); @@ -682,7 +668,7 @@ void blk_mq_rq_timed_out(struct request *req, bool reserved) ? break; ? case BLK_EH_RESET_TIMER: ? blk_add_timer(req); - blk_clear_rq_complete(req); + atomic_set(&req->state, REQ_STARTED); ? break; ? case BLK_EH_NOT_HANDLED: ? break; @@ -692,27 +678,24 @@ void blk_mq_rq_timed_out(struct request *req, bool reserved) ? } ?} ? +/* + * Check whether or not a request has expired. This function can execute + * concurrently with other functions that change the request state. This can + * result in returning a deadline (blk_mq_timeout_data.next) that occurs + * before a request times out. However, this is harmless because the next + * call of blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data) will + * yield the correct timeout, unless the same race occurs again. + */ ?static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx, ? struct request *rq, void *priv, bool reserved) ?{ ? struct blk_mq_timeout_data *data = priv; ? - if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) { - /* - ?* If a request wasn't started before the queue was - ?* marked dying, kill it here or it'll go unnoticed. - ?*/ - if (unlikely(blk_queue_dying(rq->q))) { - rq->errors = -EIO; - blk_mq_end_request(rq, rq->errors); - } - return; - } - - if (time_after_eq(jiffies, rq->deadline)) { - if (!blk_mark_rq_complete(rq)) - blk_mq_rq_timed_out(rq, reserved); - } else if (!data->next_set || time_after(data->next, rq->deadline)) { + if (time_after_eq(jiffies, rq->deadline) && + ????!blk_mark_rq_complete_if_started(rq)) { + blk_mq_rq_timed_out(rq, reserved); + } else if ((!data->next_set || time_after(data->next, rq->deadline)) && + ???blk_mq_request_started(rq)) { ? data->next = rq->deadline; ? data->next_set = 1; ? } @@ -2821,7 +2804,7 @@ static bool blk_mq_poll_hybrid_sleep(struct request_queue *q, ? ? hrtimer_init_sleeper(&hs, current); ? do { - if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags)) + if (atomic_read(&rq->state) == REQ_COMPLETE) ? break; ? set_current_state(TASK_UNINTERRUPTIBLE); ? hrtimer_start_expires(&hs.timer, mode); diff --git a/block/blk-timeout.c b/block/blk-timeout.c index a30441a200c0..9a8b44ebfb99 100644 --- a/block/blk-timeout.c +++ b/block/blk-timeout.c @@ -94,7 +94,7 @@ static void blk_rq_timed_out(struct request *req) ? break; ? case BLK_EH_RESET_TIMER: ? blk_add_timer(req); - blk_clear_rq_complete(req); + atomic_set(&req->state, REQ_NOT_STARTED); ? break; ? case BLK_EH_NOT_HANDLED: ? /* diff --git a/block/blk.h b/block/blk.h index d1ea4bd9b9a3..8af5fe21e85f 100644 --- a/block/blk.h +++ b/block/blk.h @@ -115,23 +115,32 @@ void blk_account_io_done(struct request *req); ? * Internal atomic flags for request handling ? */ ?enum rq_atomic_flags { - REQ_ATOM_COMPLETE = 0, - REQ_ATOM_STARTED, ? REQ_ATOM_POLL_SLEPT, ?}; ? ?/* + * Request states. Note: REQ_STARTED is only used by the blk-mq code. + */ +enum rq_state { + REQ_NOT_STARTED, + REQ_STARTED, + REQ_COMPLETE, +}; + +/* ? * EH timer and IO completion will both attempt to 'grab' the request, make - * sure that only one of them succeeds + * sure that only one of them succeeds. The return value 0 means that this + * function changed the request state from "not complete" into "complete". ? */ ?static inline int blk_mark_rq_complete(struct request *rq) ?{ - return test_and_set_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); + return atomic_xchg(&rq->state, REQ_COMPLETE) == REQ_COMPLETE; ?} ? -static inline void blk_clear_rq_complete(struct request *rq) +static inline int blk_mark_rq_complete_if_started(struct request *rq) ?{ - clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); + return atomic_cmpxchg(&rq->state, REQ_STARTED, REQ_COMPLETE) != + REQ_STARTED; ?} ? ?/* diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c index 939c47df73fa..136379097131 100644 --- a/drivers/scsi/virtio_scsi.c +++ b/drivers/scsi/virtio_scsi.c @@ -672,7 +672,7 @@ static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd) ? ?* ? ?* In the abort case, sc->scsi_done will do nothing, because ? ?* the block layer must have detected a timeout and as a result - ?* REQ_ATOM_COMPLETE has been set. + ?* rq->state == REQ_COMPLETED. ? ?*/ ? virtscsi_poll_requests(vscsi); ? diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 5a7da607ca04..b286887b095e 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -142,6 +142,7 @@ struct request { ? ? int internal_tag; ? + atomic_t state; ? unsigned long atomic_flags; ? ? /* the following two fields are internal, NEVER access directly */ --? 2.12.0