From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E391DC07E96 for ; Thu, 8 Jul 2021 17:15:27 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5E5EB61626 for ; Thu, 8 Jul 2021 17:15:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5E5EB61626 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=seco.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id ABA73831C0; Thu, 8 Jul 2021 19:15:10 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=secospa.onmicrosoft.com header.i=@secospa.onmicrosoft.com header.b="kI024+Ra"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 99570831C0; Thu, 8 Jul 2021 19:15:05 +0200 (CEST) Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04on0610.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe0e::610]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1DCCD831B0 for ; Thu, 8 Jul 2021 19:14:58 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sean.anderson@seco.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SH9LiJehfEfmj3m+uWpmsxRHF+KzHcthwBlrVuG82m3vJHkBTGum9ogcsUwU9xHfLdyy8UkKQnG5p5uvWNcoia+JJPlksKyKcOonMeBTt1zozbYliLnFOLcwXmhKk86lhvOqfilvPSbN5skQqw7VUysyjhqpLEY/SZuAE+l36J78vVROVG7FVgTfeMIdE1feID9Gt274AGBm8IMtAXv1XmkyHva+iSkg2jMMGUfo9AWjLPS9mWH+h/TGSHaEKHVt8Knl0+I8mCYAunGDX/TYrkXLNf3WQKw7DBFCxNxTQ0jGjcYBwSBLF0R7xNab/rVhQ8dEMfh2vs5aLEL+8mB99w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6UoW03cYGS9vzjdVsyu0Fb5ZPnngvtb88tOOmwjq3S0=; b=cBuh7DhOTY1u/9rbnMKiALUecYbauT4OhH7jrWNb0ezz4XqWKhQhPx9OVIxtNKxHgurQjcM36Zu6Qu5wYqip54j44/mCi9d76mss1jeQL0YYxp3NjFmQ1MTs3t6eacVCh/7MsiEWtOTa5+iR7jaQJs6OWWn6DVBp78uM0XIJNsf/JeiDpb+AhqhY3we9OhIlMauwiG9lE9fIgnu0XhNxV7efjCS2BXxc8qTxv+O3inhg3i7Qz1GkI3Ps/PLKZQH/as6AzmTJENtHsBl2H8CzkdwtpU03l006DItYUv5zMLZ4Hoe7fiCzjVkQpG5GAhHN+wyNClCgU1M/4zxnlIVprg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=seco.com; dmarc=pass action=none header.from=seco.com; dkim=pass header.d=seco.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secospa.onmicrosoft.com; s=selector2-secospa-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6UoW03cYGS9vzjdVsyu0Fb5ZPnngvtb88tOOmwjq3S0=; b=kI024+Ra3t+baAKn7UmHgx+KfSjveSATPJsWHp0V2z3LoZuf6h1p/6z91W5lui0UpZDxCqoehwX7HpYM8efMCQOY0Qc/pEwolcLrTqkpgYSSM3TqdtaZ8LIraMQZeWZUkW9UMregRVeme2r59i6kTAeQiEj6BWx3kuUmk8QbRiw= Authentication-Results: lists.denx.de; dkim=none (message not signed) header.d=none;lists.denx.de; dmarc=none action=none header.from=seco.com; Received: from DB7PR03MB4523.eurprd03.prod.outlook.com (2603:10a6:10:19::27) by DBBPR03MB6762.eurprd03.prod.outlook.com (2603:10a6:10:20b::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19; Thu, 8 Jul 2021 17:14:57 +0000 Received: from DB7PR03MB4523.eurprd03.prod.outlook.com ([fe80::40d5:3554:c709:6b1b]) by DB7PR03MB4523.eurprd03.prod.outlook.com ([fe80::40d5:3554:c709:6b1b%5]) with mapi id 15.20.4264.035; Thu, 8 Jul 2021 17:14:57 +0000 From: Sean Anderson To: u-boot@lists.denx.de, Lukasz Majewski Cc: Maxime Ripard , Jun Li , Andre Przywara , Jagan Teki , Marek Vasut , Marek Szyprowski , Ye Li , Peng Fan , Bryan O'Donoghue , Sean Anderson Subject: [PATCH 2/4] usb: f_mass_storage: Check bh->state in sleep_thread Date: Thu, 8 Jul 2021 13:14:21 -0400 Message-Id: <20210708171424.970928-2-sean.anderson@seco.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210708171424.970928-1-sean.anderson@seco.com> References: <20210708171424.970928-1-sean.anderson@seco.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: MN2PR13CA0005.namprd13.prod.outlook.com (2603:10b6:208:160::18) To DB7PR03MB4523.eurprd03.prod.outlook.com (2603:10a6:10:19::27) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from plantagenet.inhand.com (50.195.82.171) by MN2PR13CA0005.namprd13.prod.outlook.com (2603:10b6:208:160::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4331.12 via Frontend Transport; Thu, 8 Jul 2021 17:14:55 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9eed1207-8eab-4f3d-1ff0-08d94233e8e6 X-MS-TrafficTypeDiagnostic: DBBPR03MB6762: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:291; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9bTWbvPZqzePedXWXVbfU1NUE5YVdPys/No7Y8aZBm4Ump9AlKkRrRDy3TZSllaqDJ2GvECDTYgjlcMMkfDw8lve+z6eWRZDfwXFhtwx9Mh1eQAVl3E5dTRx7mtP4ZXziteDAs7KA0HrCUP2vMq0a29TIZ8ZqLk5Pyyy11WPAquucdlEDu8khGVPbuCGX+WOzWMb2QEGyGzGfB+oeBSd5rkujS++0jhduZIKy5zuN1R7HL/gMEveb9A6wXJ9MmtxEf+GDqxXyPCFlBpkkug1m1Ca/pTkbyzFzvVpQ4vOWQx14xfnIXFiSog9hADrlVJzvldgHt29FDI+nqu/E7aABAj3P1lzZjCXTAG6O5VD5Uo3hgC+PNFovUhYGlNzX7pqVneTlKNlR/VvnIx9Aead4xnoUHJevKufmIJigEIrKW07ZVf4HBv3BBr9PtFix5CCZfdagNu62gDZMurIMK84POXK6pKmKdJTDIeyI2KiQXZPbjJK8v6KxQct3s1/v/tR1HBKV2PwHbnLefSsXgppjsN4iAKDv+ffVcwMfoWnKVzwu5TZlusyfArJGz5k5Tiyeo9vzo+8yxsjxLd1JBegHwGx72eQu30Gu/iBEzwKbQE7ckAzgr5o8WAxjBEAqOjn34UD+/PtRRrrNCk3eHnovD2V3Z5v9+2S5v0RvNKinc1Jsdv1/bIm2CZaa5WL708SPliGBFhwHS8I7VgPpm8kDw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB7PR03MB4523.eurprd03.prod.outlook.com; PTR:; CAT:NONE; SFS:(136003)(366004)(376002)(346002)(396003)(39840400004)(52116002)(54906003)(478600001)(30864003)(316002)(6506007)(6666004)(38350700002)(38100700002)(2906002)(8676002)(4326008)(36756003)(6486002)(8936002)(83380400001)(66946007)(66476007)(66556008)(86362001)(44832011)(1076003)(186003)(7416002)(6512007)(2616005)(107886003)(956004)(26005)(6916009)(5660300002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?5uUg4oJiV0TV8K1jbFJXH7xL3Vy7cTyWbVjxJV0yDT5WIt2gej+6E5HOUPjY?= =?us-ascii?Q?X8wy6ZnGzS5jWHXVfCG+X8YYfwIg7oJcfoIUaiv5p6JIeECTm2KicNAiLX3P?= =?us-ascii?Q?5zXgV2X1MxRrwVmll1LgLdXSGlYeMTgbVEVgaizIL0kxKM4Ph40r9PH7H4LV?= =?us-ascii?Q?4lkNJJzz5JA3c/zi9b5JtlnKnsJoO6P4qGUiBbNssNb+cQGwwis11PG0TR4S?= =?us-ascii?Q?x8JxgN+QpvfAci/xFKzxJXMkq6dhknziZfBRdAI2i8OOhp+6Q2lmxsaFw2b/?= =?us-ascii?Q?u4BiQowqrqNSJEMRahZsVYXh/qbWOv3KkZoEqLVakXLAMkNJ2NC3PZRCPCue?= =?us-ascii?Q?Z5TFl0siih4WR5Fiqwb+5YDjGLJMu46Aml9OQkcHHJjbcOA60hQt9AubZpuU?= =?us-ascii?Q?Ht0KwDppMzFsJB0c8uqePNOwZ1CO+FcC5/0O6wjpgC/2TRv1qL3wTZVRoFVz?= =?us-ascii?Q?ZnGTWusuNtsO2yNY5PsUHnxSIObOURFwG7uEuIul57P1ODjefvjgXA/4fB63?= =?us-ascii?Q?rPe4pQsdN0TmdwNPJMAG/lGxc1ahf3AUgFgB0uxbWmXYIyoZYOWZ+Xzs9Ms+?= =?us-ascii?Q?vBS4vdrU5Kr7YvY8CFny6oXAYM8BNjHGPd8oYrRa9ZbZtCQG4rTquozLX0nF?= =?us-ascii?Q?OfwW/HIr69taRykzgGI1UaBLhmwuvSSCEA1TD15NQsKSs1GDzM6IrGnNvFdw?= =?us-ascii?Q?JgE+NwRvy21oAiyBAoHdDJuHqUF8SEoU/Kagn5wuLNs0UtkCjsnKDLiq1G8e?= =?us-ascii?Q?Lo13aXKyoR6uRaGoHZ7YPEkB/cWP9/KBn/slqqblxDJaHW0JJm2oC7x2jzsa?= =?us-ascii?Q?B4Cssk/YXhG7HJ4OYWzu+hN0UDwEWl/E68+zL3kpkP1n2/ycfigC/NVvgSc9?= =?us-ascii?Q?xjwNyoLWovSk8YocywUgEuSyeGvyrrAav1pHotl/NDYkmhN5vkPyc39isbLM?= =?us-ascii?Q?DIpyETGbbwF6F9ozCxwwYqsZ/mlUfx1PPN/Zo/7439j4fOjADG0SfBBJjsGo?= =?us-ascii?Q?eGnnR8k3VNuVAmx8Li2R4zoGTx3rK9DpMnHzxpLDUDJKR9Cyn7q490fVQq4A?= =?us-ascii?Q?gmeMfAzsK2n4Lcvm5WDOPn1gUNqF/c7twv+tfkmKDWUYIjjKQEdZObC4fsyi?= =?us-ascii?Q?HBObliB6cUChxGOFaPi1RUkkug4qmzWoaro02qgX+ejUmdljlVuNWJvIVw4d?= =?us-ascii?Q?VoHQ7N5riGXg1asaK9knJ9J8IMNeEm2gSRHYUXRd+lZxhGJNM57B6YBhouno?= =?us-ascii?Q?h6hRHeHh4d8W0Z+NB2YkwLvQD0QG0/1XNpzvoL/tcT8jV6jVlNlx8I0DOZ8d?= =?us-ascii?Q?EpCK4gSrvYgABI18ckzimF+d?= X-OriginatorOrg: seco.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9eed1207-8eab-4f3d-1ff0-08d94233e8e6 X-MS-Exchange-CrossTenant-AuthSource: DB7PR03MB4523.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jul 2021 17:14:56.9096 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bebe97c3-6438-442e-ade3-ff17aa50e733 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: kkVkIl5MNDzNcmy2jmzBPaTn50zhurMEEdPPZy1d1weF7BOX3qFAG/HPul7x+CWjja+g1HaGpXs67asWv21mbg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR03MB6762 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Every caller of sleep_thread except one wraps it in a second loop which waits for a buffer head to be filled. Since sleep_thread is already (infinitely) looping, we can move this check down. Some parts of the code have been reorganized to better take advantage of this property and and be structured closer to their Linux counterparts. In addition, sleep_thread now returns -EINTR if we need to wake up, mirroring Linux. This takes advantage of the existing logic regarding sleep_thread, which typically returns immediately on error. With this change, any exception causes the current operation to be backed out of immediately. Lastly, we only clear thread_wakeup_needed in handle_exception, mirroring Linux's signal-handling logic. Any subsequent calls to sleep_thread will still wake up. While these changes are conceptually different, their implementation is interdependent, so they are all performed at once. These changes are primarily inspired by Linux commit 225785aec726 ("USB: f_mass_storage: improve memory barriers and synchronization"). Signed-off-by: Sean Anderson --- drivers/usb/gadget/f_mass_storage.c | 225 ++++++++++++---------------- drivers/usb/gadget/storage_common.c | 2 +- 2 files changed, 100 insertions(+), 127 deletions(-) diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c index 45f0504b6e..99935fe9a3 100644 --- a/drivers/usb/gadget/f_mass_storage.c +++ b/drivers/usb/gadget/f_mass_storage.c @@ -652,15 +652,17 @@ static void busy_indicator(void) state = 0; } -static int sleep_thread(struct fsg_common *common) +static int sleep_thread(struct fsg_common *common, struct fsg_buffhd *bh) { - int rc = 0; int i = 0, k = 0; - /* Wait until a signal arrives or we are woken up */ - for (;;) { - if (common->thread_wakeup_needed) - break; + /* + * Wait until a signal arrives, we are woken up, or the buffer head is + * full. + */ + while (!bh || bh->state == BUF_STATE_BUSY) { + if (exception_in_progress(common)) + return -EINTR; if (++i == 20000) { busy_indicator(); @@ -682,8 +684,7 @@ static int sleep_thread(struct fsg_common *common) usb_gadget_handle_interrupts(controller_index); } - common->thread_wakeup_needed = 0; - return rc; + return 0; } /*-------------------------------------------------------------------------*/ @@ -744,11 +745,9 @@ static int do_read(struct fsg_common *common) /* Wait for the next buffer to become available */ bh = common->next_buffhd_to_fill; - while (bh->state != BUF_STATE_EMPTY) { - rc = sleep_thread(common); - if (rc) - return rc; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; /* If we were asked to read past the end of file, * end with an empty buffer. */ @@ -922,67 +921,64 @@ static int do_write(struct fsg_common *common) bh = common->next_buffhd_to_drain; if (bh->state == BUF_STATE_EMPTY && !get_some_more) break; /* We stopped early */ - if (bh->state == BUF_STATE_FULL) { - common->next_buffhd_to_drain = bh->next; - bh->state = BUF_STATE_EMPTY; - /* Did something go wrong with the transfer? */ - if (bh->outreq->status != 0) { - curlun->sense_data = SS_COMMUNICATION_FAILURE; - curlun->info_valid = 1; - break; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; - amount = bh->outreq->actual; + common->next_buffhd_to_drain = bh->next; + bh->state = BUF_STATE_EMPTY; - /* Perform the write */ - rc = ums[common->lun].write_sector(&ums[common->lun], - file_offset / SECTOR_SIZE, - amount / SECTOR_SIZE, - (char __user *)bh->buf); - if (!rc) - return -EIO; - nwritten = rc * SECTOR_SIZE; + /* Did something go wrong with the transfer? */ + if (bh->outreq->status != 0) { + curlun->sense_data = SS_COMMUNICATION_FAILURE; + curlun->info_valid = 1; + break; + } - VLDBG(curlun, "file write %u @ %llu -> %d\n", amount, - (unsigned long long) file_offset, - (int) nwritten); + amount = bh->outreq->actual; - if (nwritten < 0) { - LDBG(curlun, "error in file write: %d\n", - (int) nwritten); - nwritten = 0; - } else if (nwritten < amount) { - LDBG(curlun, "partial file write: %d/%u\n", - (int) nwritten, amount); - nwritten -= (nwritten & 511); - /* Round down to a block */ - } - file_offset += nwritten; - amount_left_to_write -= nwritten; - common->residue -= nwritten; - - /* If an error occurred, report it and its position */ - if (nwritten < amount) { - printf("nwritten:%zd amount:%u\n", nwritten, - amount); - curlun->sense_data = SS_WRITE_ERROR; - curlun->info_valid = 1; - break; - } + /* Perform the write */ + rc = ums[common->lun].write_sector(&ums[common->lun], + file_offset / SECTOR_SIZE, + amount / SECTOR_SIZE, + (char __user *)bh->buf); + if (!rc) + return -EIO; + nwritten = rc * SECTOR_SIZE; - /* Did the host decide to stop early? */ - if (bh->outreq->actual != bh->outreq->length) { - common->short_packet_received = 1; - break; - } - continue; + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount, + (unsigned long long) file_offset, + (int) nwritten); + + if (nwritten < 0) { + LDBG(curlun, "error in file write: %d\n", + (int) nwritten); + nwritten = 0; + } else if (nwritten < amount) { + LDBG(curlun, "partial file write: %d/%u\n", + (int) nwritten, amount); + nwritten -= (nwritten & 511); + /* Round down to a block */ } + file_offset += nwritten; + amount_left_to_write -= nwritten; + common->residue -= nwritten; - /* Wait for something to happen */ - rc = sleep_thread(common); - if (rc) - return rc; + /* If an error occurred, report it and its position */ + if (nwritten < amount) { + printf("nwritten:%zd amount:%u\n", nwritten, + amount); + curlun->sense_data = SS_WRITE_ERROR; + curlun->info_valid = 1; + break; + } + + /* Did the host decide to stop early? */ + if (bh->outreq->actual != bh->outreq->length) { + common->short_packet_received = 1; + break; + } } return -EIO; /* No default reply */ @@ -1430,13 +1426,10 @@ static int pad_with_zeros(struct fsg_dev *fsg) bh->state = BUF_STATE_EMPTY; /* For the first iteration */ fsg->common->usb_amount_left = nkeep + fsg->common->residue; while (fsg->common->usb_amount_left > 0) { - /* Wait for the next buffer to be free */ - while (bh->state != BUF_STATE_EMPTY) { - rc = sleep_thread(fsg->common); - if (rc) - return rc; - } + rc = sleep_thread(fsg->common, bh); + if (rc) + return rc; nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN); memset(bh->buf + nkeep, 0, nsend - nkeep); @@ -1461,23 +1454,7 @@ static int throw_away_data(struct fsg_common *common) bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; bh = common->next_buffhd_to_drain) { - /* Throw away the data in a filled buffer */ - if (bh->state == BUF_STATE_FULL) { - bh->state = BUF_STATE_EMPTY; - common->next_buffhd_to_drain = bh->next; - - /* A short packet or an error ends everything */ - if (bh->outreq->actual != bh->outreq->length || - bh->outreq->status != 0) { - raise_exception(common, - FSG_STATE_ABORT_BULK_OUT); - return -EINTR; - } - continue; - } - /* Try to submit another request if we need one */ - bh = common->next_buffhd_to_fill; if (bh->state == BUF_STATE_EMPTY && common->usb_amount_left > 0) { amount = min(common->usb_amount_left, FSG_BUFLEN); @@ -1494,13 +1471,24 @@ static int throw_away_data(struct fsg_common *common) return -EIO; common->next_buffhd_to_fill = bh->next; common->usb_amount_left -= amount; - continue; } - /* Otherwise wait for something to happen */ - rc = sleep_thread(common); + /* Wait for the data to be received */ + rc = sleep_thread(common, bh); if (rc) return rc; + + /* Throw away the data in a filled buffer */ + bh->state = BUF_STATE_EMPTY; + common->next_buffhd_to_drain = bh->next; + + /* A short packet or an error ends everything */ + if (bh->outreq->actual != bh->outreq->length || + bh->outreq->status != 0) { + raise_exception(common, + FSG_STATE_ABORT_BULK_OUT); + return -EINTR; + } } return 0; } @@ -1613,11 +1601,9 @@ static int send_status(struct fsg_common *common) /* Wait for the next buffer to become available */ bh = common->next_buffhd_to_fill; - while (bh->state != BUF_STATE_EMPTY) { - rc = sleep_thread(common); - if (rc) - return rc; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; if (curlun) sd = curlun->sense_data; @@ -1790,11 +1776,9 @@ static int do_scsi_command(struct fsg_common *common) /* Wait for the next buffer to become available for data or status */ bh = common->next_buffhd_to_fill; common->next_buffhd_to_drain = bh; - while (bh->state != BUF_STATE_EMPTY) { - rc = sleep_thread(common); - if (rc) - return rc; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; common->phase_error = 0; common->short_packet_received = 0; @@ -2118,11 +2102,9 @@ static int get_next_command(struct fsg_common *common) /* Wait for the next buffer to become available */ bh = common->next_buffhd_to_fill; - while (bh->state != BUF_STATE_EMPTY) { - rc = sleep_thread(common); - if (rc) - return rc; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; /* Queue a request to read a Bulk-only CBW */ set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN); @@ -2137,11 +2119,9 @@ static int get_next_command(struct fsg_common *common) * next_buffhd_to_fill. */ /* Wait for the CBW to arrive */ - while (bh->state != BUF_STATE_FULL) { - rc = sleep_thread(common); - if (rc) - return rc; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; bh->state = BUF_STATE_EMPTY; @@ -2291,6 +2271,7 @@ static void handle_exception(struct fsg_common *common) struct fsg_lun *curlun; unsigned int exception_req_tag; + common->thread_wakeup_needed = 0; /* Cancel all the pending transfers */ if (common->fsg) { for (i = 0; i < FSG_NUM_BUFFERS; ++i) { @@ -2300,18 +2281,9 @@ static void handle_exception(struct fsg_common *common) if (bh->outreq_busy) usb_ep_dequeue(common->fsg->bulk_out, bh->outreq); - } - /* Wait until everything is idle */ - for (;;) { - int num_active = 0; - for (i = 0; i < FSG_NUM_BUFFERS; ++i) { - bh = &common->buffhds[i]; - num_active += bh->inreq_busy + bh->outreq_busy; - } - if (num_active == 0) - break; - if (sleep_thread(common)) + /* Wait until the transfer is idle */ + if (sleep_thread(common, bh)) return; } @@ -2403,15 +2375,16 @@ int fsg_main_thread(void *common_) } if (!common->running) { - ret = sleep_thread(common); - if (ret) + ret = sleep_thread(common, NULL); + if (ret != -EINTR) return ret; - continue; } ret = get_next_command(common); - if (ret) + if (ret == -EINTR) + continue; + else if (ret) return ret; if (!exception_in_progress(common)) diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c index 5674e8fe49..723549ef09 100644 --- a/drivers/usb/gadget/storage_common.c +++ b/drivers/usb/gadget/storage_common.c @@ -318,7 +318,7 @@ static struct fsg_lun *fsg_lun_from_dev(struct device *dev) enum fsg_buffer_state { BUF_STATE_EMPTY = 0, BUF_STATE_FULL, - BUF_STATE_BUSY + BUF_STATE_BUSY, }; struct fsg_buffhd { -- 2.25.1