From patchwork Fri Jan 20 09:03:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Sakamoto X-Patchwork-Id: 13109449 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E081AC05027 for ; Fri, 20 Jan 2023 09:05:09 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 9332C2F66; Fri, 20 Jan 2023 10:04:17 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 9332C2F66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1674205507; bh=ImiYZ6n0nJrpsPKB4+ygXh0w8GjDW3q0HkkeIMvNY8c=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: Cc:From; b=a7LZQH022VEaqlel/kGTWOU4uclcnSSFhndIAhImiESJL2/4penQ8tN7l1I0GqOMm jHXY62vl4akq75p05nWOENK9bh42ejAVq2sMAgdLHJm3RTFcsEwk2/yawnuHaLS7J2 62b9+hGjXoP3bhioaFOsHv41rZI7ALFY/8QH+7Jg= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id C34CBF8024D; Fri, 20 Jan 2023 10:04:05 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 500BDF80543; Fri, 20 Jan 2023 10:04:04 +0100 (CET) Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 90C05F8024C for ; Fri, 20 Jan 2023 10:03:52 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 90C05F8024C Authentication-Results: alsa1.perex.cz; dkim=pass (2048-bit key, unprotected) header.d=sakamocchi.jp header.i=@sakamocchi.jp header.a=rsa-sha256 header.s=fm3 header.b=fUn1G3Ah; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=J6W8fLT7 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 84D955C00C3; Fri, 20 Jan 2023 04:03:51 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Fri, 20 Jan 2023 04:03:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sakamocchi.jp; h=cc:cc:content-transfer-encoding:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1674205431; x= 1674291831; bh=jE/Ddp/JVuzNFe//kFpLgM6MgxEg2yp0BYW9xvltuqQ=; b=f Un1G3AhebUpk57b5I1enrSIHTceCdDYT9pf1wB9L5ioMqtS//jpUrGYuHciPU1NK gxtrHwyICGbzfge/blYuWBfvrlMnCMoSRiCoDqSnDWqQ7Oz5NpiayePcZLVr81OH YJURQg7dmaUFSHIdOo5AbsmjCauuR3q1QX7GQJGnfDmfwbvetvW8C3c0BGYN5ife Uc/vkIUDND4mvYFSqG47uH2YovGvNcOfVFnWx1oWJZR6hvbaCtd32CWb737KusGD ZSfcdL7JbXfcUU9YeRqjbvB7ISnOPpC1/r/3pHTEvcyPXijD4+/UzeOdo6k43OZI kwX/0WoHqFzBQfgKh2MjQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1674205431; x=1674291831; bh=jE/Ddp/JVuzNF e//kFpLgM6MgxEg2yp0BYW9xvltuqQ=; b=J6W8fLT77ClbrQXoJWX7mEnDANUZu b5unNcaow6bD9oAjBrIYp5V29GadliH1jF5XKo0vc9yjPh7rK29dV3OgnE6efHsU TlN4NAye2VpC9f5daVcflvSddR0xlFAW0cMb1dtcECCCJmNQhFH/A6HdmDW7+6Ns MeEpJY2GvyGv4IxofAW23QI5x/0PSnuM1e771zbJlNLFCiFndk7XMPbA3G9gWWjC b1GXcV/S08UR18Poq2qZeKksNLqQCNmKRv7K5c/SuJXbGVHZphxlGY4Z+2EXxKSs uM4cfUPQaLdN1G3pIJ6jZ6kDbehVxmxvJn2Revaetk2Emziq3p5LeNx+Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudduuddguddvlecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhephffvvefufffkofgjfhgggfestd ekredtredttdenucfhrhhomhepvfgrkhgrshhhihcuufgrkhgrmhhothhouceoohdqthgr khgrshhhihesshgrkhgrmhhotggthhhirdhjpheqnecuggftrfgrthhtvghrnhepvdejgf ejuedvgfduudekleevtefgtdevhfdtffefiefgveeuteffiedvffekvddtnecuvehluhhs thgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepohdqthgrkhgrshhhih esshgrkhgrmhhotggthhhirdhjph X-ME-Proxy: Feedback-ID: ie8e14432:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 20 Jan 2023 04:03:49 -0500 (EST) From: Takashi Sakamoto To: tiwai@suse.de, stefanr@s5r6.in-berlin.de Subject: [PATCH 1/3] firewire: core: use kref structure to maintain lifetime of data for fw_request structure Date: Fri, 20 Jan 2023 18:03:42 +0900 Message-Id: <20230120090344.296451-2-o-takashi@sakamocchi.jp> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230120090344.296451-1-o-takashi@sakamocchi.jp> References: <20230120090344.296451-1-o-takashi@sakamocchi.jp> MIME-Version: 1.0 X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alsa-devel@alsa-project.org, linux1394-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" Developers have acknowledged that maintenance of lifetime for fw_transaction structure is effective when handling asynchronous transaction to IEC 61883-1 FCP region, since the core function allows multiples listeners to the region. Some of them needs to access to the payload of request in process context after the callback to listener, while the core function releases the object for the structure just after completing the callbacks to listeners. One of the listeners is character device. Current implementation of the character device duplicates the object for the payload of transaction, while it's a cost in kernel memory consumption. The lifetime management can reduce it. The typical way to maintain the lifetime is reference count. This commit uses kref structure as a first step for the purpose. Signed-off-by: Takashi Sakamoto --- drivers/firewire/core-cdev.c | 4 ++-- drivers/firewire/core-transaction.c | 33 +++++++++++++++++++++++++---- drivers/firewire/core.h | 3 +++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/drivers/firewire/core-cdev.c b/drivers/firewire/core-cdev.c index 958aa4662ccb..93dd80d8d4e1 100644 --- a/drivers/firewire/core-cdev.c +++ b/drivers/firewire/core-cdev.c @@ -826,12 +826,12 @@ static int ioctl_send_response(struct client *client, union ioctl_arg *arg) if (a->length != fw_get_response_length(r->request)) { ret = -EINVAL; - kfree(r->request); + fw_request_put(r->request); goto out; } if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) { ret = -EFAULT; - kfree(r->request); + fw_request_put(r->request); goto out; } fw_send_response(r->card, r->request, a->rcode); diff --git a/drivers/firewire/core-transaction.c b/drivers/firewire/core-transaction.c index af498d767702..83f61cf1aa8f 100644 --- a/drivers/firewire/core-transaction.c +++ b/drivers/firewire/core-transaction.c @@ -617,6 +617,7 @@ void fw_core_remove_address_handler(struct fw_address_handler *handler) EXPORT_SYMBOL(fw_core_remove_address_handler); struct fw_request { + struct kref kref; struct fw_packet response; u32 request_header[4]; int ack; @@ -625,13 +626,33 @@ struct fw_request { u32 data[]; }; +void fw_request_get(struct fw_request *request) +{ + kref_get(&request->kref); +} + +static void release_request(struct kref *kref) +{ + struct fw_request *request = container_of(kref, struct fw_request, kref); + + kfree(request); +} + +void fw_request_put(struct fw_request *request) +{ + kref_put(&request->kref, release_request); +} + static void free_response_callback(struct fw_packet *packet, struct fw_card *card, int status) { - struct fw_request *request; + struct fw_request *request = container_of(packet, struct fw_request, response); - request = container_of(packet, struct fw_request, response); - kfree(request); + // Decrease the reference count since not at in-flight. + fw_request_put(request); + + // Decrease the reference count to release the object. + fw_request_put(request); } int fw_get_response_length(struct fw_request *r) @@ -782,6 +803,7 @@ static struct fw_request *allocate_request(struct fw_card *card, request = kmalloc(sizeof(*request) + length, GFP_ATOMIC); if (request == NULL) return NULL; + kref_init(&request->kref); request->response.speed = p->speed; request->response.timestamp = @@ -809,7 +831,7 @@ void fw_send_response(struct fw_card *card, /* unified transaction or broadcast transaction: don't respond */ if (request->ack != ACK_PENDING || HEADER_DESTINATION_IS_BROADCAST(request->request_header[0])) { - kfree(request); + fw_request_put(request); return; } @@ -821,6 +843,9 @@ void fw_send_response(struct fw_card *card, fw_fill_response(&request->response, request->request_header, rcode, NULL, 0); + // Increase the reference count so that the object is kept during in-flight. + fw_request_get(request); + card->driver->send_response(card, &request->response); } EXPORT_SYMBOL(fw_send_response); diff --git a/drivers/firewire/core.h b/drivers/firewire/core.h index 71d5f16f311c..78c99f1d27fa 100644 --- a/drivers/firewire/core.h +++ b/drivers/firewire/core.h @@ -244,6 +244,9 @@ int fw_get_response_length(struct fw_request *request); void fw_fill_response(struct fw_packet *response, u32 *request_header, int rcode, void *payload, size_t length); +void fw_request_get(struct fw_request *request); +void fw_request_put(struct fw_request *request); + #define FW_PHY_CONFIG_NO_NODE_ID -1 #define FW_PHY_CONFIG_CURRENT_GAP_COUNT -1 void fw_send_phy_config(struct fw_card *card, From patchwork Fri Jan 20 09:03:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Sakamoto X-Patchwork-Id: 13109450 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 444DFC05027 for ; Fri, 20 Jan 2023 09:05:22 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 1D9272F6E; Fri, 20 Jan 2023 10:04:30 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 1D9272F6E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1674205520; bh=jCegmBqRsgW/ZyKCJcoReK/y1MIKheosZgBQcDHWDzU=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: Cc:From; b=tRFxL8GEfy4OXqBE3UkobxO8xjOAfmE48O93h4CUgl6uiEDCHv3IJdk40tcmkdQ48 v3y0VJv3dyE9sw3zqC3F1OWSrLvUzY+EvcvVOYbm1N5dacqiqJQnEwDX50rJ8eTpHz deDgq4o5ptu7F/6bqNKztx3B1WOK+jR8ccMuJlu0= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 7FCD0F80551; Fri, 20 Jan 2023 10:04:06 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id E3362F80495; Fri, 20 Jan 2023 10:04:04 +0100 (CET) Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id C3CB5F8024D for ; Fri, 20 Jan 2023 10:03:54 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz C3CB5F8024D Authentication-Results: alsa1.perex.cz; dkim=pass (2048-bit key, unprotected) header.d=sakamocchi.jp header.i=@sakamocchi.jp header.a=rsa-sha256 header.s=fm3 header.b=b1BRbngq; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=Rct60oA4 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 4B57E5C0154; Fri, 20 Jan 2023 04:03:53 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Fri, 20 Jan 2023 04:03:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sakamocchi.jp; h=cc:cc:content-transfer-encoding:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1674205433; x= 1674291833; bh=ofXt6LA5isnrTwDUYaXALKqLJ5c6lOkyPZo5vc9Gy7g=; b=b 1BRbngqLvU6QNPyFYJx70Ij6oJ6izykcupYX/KcOeJJpgfzlhJS+SaLtPHuvcibt QldRAp52Sdgq+GCfwflthHk+tzXrWyJ/H3fIUKrgT8KGjnFPgLGKDG7n7OlMTL0f VjQ5ARiH0IiLttBHaqbLNJCfUefX1f3fFkZORG3Tjcyov8Dcno3m14/Ly2S614lc Gu/C5SPshpF/VMNByDRsfqXAGoiQJ0VJdsCAs9BihuaCfuCGY5yMyefa7CwCcOcI Sy+Lpd9Vs8MhHknX9frwNB+bfpqCZlRznvW8H6pVSJDou7rFRfa4AqArauPiDKup QWT9q2RM02mY+zY+wZBfQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1674205433; x=1674291833; bh=ofXt6LA5isnrT wDUYaXALKqLJ5c6lOkyPZo5vc9Gy7g=; b=Rct60oA4KKTDoDwWjc0y3rp38AxHY nnXgDupAR1muiISEONGPaLHZjWYPTjDKogJOPPT7Za/Z/6aJoQaKbLyvJ4YrLyW2 M8P/OblK2A0ceHmBdZT3PjV7hzVPlLAvKlBeAYK3SNSsAMfALHeMAcosepZSwdV9 JsbVqN4l90EQFS7HqgdEjN0IpWid9S1rmdKWoKR9MlQv/jzMk+aahk6EpvzxpEdM KV/7q2phb/pv1Tn1lrPx4d0sKnH6v4RgRV+BC+0aQrPiAzQAeEfs9NCgCAeeRNSn Pb1N3325dv3B4q2KS71Z51iKewzSbIXIxT6sdCK2rmeWOqvRHDQnNFZxQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudduuddguddvlecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhephffvvefufffkofgjfhgggfestd ekredtredttdenucfhrhhomhepvfgrkhgrshhhihcuufgrkhgrmhhothhouceoohdqthgr khgrshhhihesshgrkhgrmhhotggthhhirdhjpheqnecuggftrfgrthhtvghrnhepvdejgf ejuedvgfduudekleevtefgtdevhfdtffefiefgveeuteffiedvffekvddtnecuvehluhhs thgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepohdqthgrkhgrshhhih esshgrkhgrmhhotggthhhirdhjph X-ME-Proxy: Feedback-ID: ie8e14432:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 20 Jan 2023 04:03:51 -0500 (EST) From: Takashi Sakamoto To: tiwai@suse.de, stefanr@s5r6.in-berlin.de Subject: [PATCH 2/3] firewire: cdev: obsolete NULL check to detect IEC 61883-1 FCP region Date: Fri, 20 Jan 2023 18:03:43 +0900 Message-Id: <20230120090344.296451-3-o-takashi@sakamocchi.jp> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230120090344.296451-1-o-takashi@sakamocchi.jp> References: <20230120090344.296451-1-o-takashi@sakamocchi.jp> MIME-Version: 1.0 X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alsa-devel@alsa-project.org, linux1394-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" In the character device, the listener to address space should distinguish whether the request is to IEC 61883-1 FCP region or not. The user space application needs to access to the object of request in enough later by read(2), while the core function releases the object of request in the FCP case after completing the callback to handler. The handler guarantees the access safe by some way. It's done by duplication of the object after NULL check to the request, since core function passes NULL in the FCP case. It's inconvenient since the object of request includes some helpful information. It's better to add another way to check whether the request is to FCP region or not. Conveniently the file of transaction layer includes local implementation for the purpose. This commit moves it to module local file and use it instead of the NULL check, then the result of check is stored to per-client data for the inbound transaction so that the result can be referred by later to release the data. Signed-off-by: Takashi Sakamoto --- drivers/firewire/core-cdev.c | 16 +++++++--------- drivers/firewire/core-transaction.c | 20 ++++++++++---------- drivers/firewire/core.h | 6 ++++++ include/linux/firewire.h | 5 ++--- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/drivers/firewire/core-cdev.c b/drivers/firewire/core-cdev.c index 93dd80d8d4e1..7fa49e51bae8 100644 --- a/drivers/firewire/core-cdev.c +++ b/drivers/firewire/core-cdev.c @@ -111,6 +111,7 @@ struct inbound_transaction_resource { struct client_resource resource; struct fw_card *card; struct fw_request *request; + bool is_fcp; void *data; size_t length; }; @@ -643,18 +644,13 @@ static int ioctl_send_request(struct client *client, union ioctl_arg *arg) client->device->max_speed); } -static inline bool is_fcp_request(struct fw_request *request) -{ - return request == NULL; -} - static void release_request(struct client *client, struct client_resource *resource) { struct inbound_transaction_resource *r = container_of(resource, struct inbound_transaction_resource, resource); - if (is_fcp_request(r->request)) + if (r->is_fcp) kfree(r->data); else fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR); @@ -669,6 +665,7 @@ static void handle_request(struct fw_card *card, struct fw_request *request, void *payload, size_t length, void *callback_data) { struct address_handler_resource *handler = callback_data; + bool is_fcp = is_in_fcp_region(offset, length); struct inbound_transaction_resource *r; struct inbound_transaction_event *e; size_t event_size0; @@ -685,10 +682,11 @@ static void handle_request(struct fw_card *card, struct fw_request *request, r->card = card; r->request = request; + r->is_fcp = is_fcp; r->data = payload; r->length = length; - if (is_fcp_request(request)) { + if (is_fcp) { /* * FIXME: Let core-transaction.c manage a * single reference-counted copy? @@ -743,7 +741,7 @@ static void handle_request(struct fw_card *card, struct fw_request *request, kfree(e); kfree(fcp_frame); - if (!is_fcp_request(request)) + if (!is_fcp) fw_send_response(card, request, RCODE_CONFLICT_ERROR); fw_card_put(card); @@ -819,7 +817,7 @@ static int ioctl_send_response(struct client *client, union ioctl_arg *arg) r = container_of(resource, struct inbound_transaction_resource, resource); - if (is_fcp_request(r->request)) { + if (r->is_fcp) { kfree(r->data); goto out; } diff --git a/drivers/firewire/core-transaction.c b/drivers/firewire/core-transaction.c index 83f61cf1aa8f..a9f70c96323e 100644 --- a/drivers/firewire/core-transaction.c +++ b/drivers/firewire/core-transaction.c @@ -535,12 +535,6 @@ const struct fw_address_region fw_unit_space_region = { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, }; #endif /* 0 */ -static bool is_in_fcp_region(u64 offset, size_t length) -{ - return offset >= (CSR_REGISTER_BASE | CSR_FCP_COMMAND) && - offset + length <= (CSR_REGISTER_BASE | CSR_FCP_END); -} - /** * fw_core_add_address_handler() - register for incoming requests * @handler: callback @@ -822,12 +816,18 @@ static struct fw_request *allocate_request(struct fw_card *card, return request; } +/** + * fw_send_response: - send response packet for asynchronous transaction. + * @card: interface to send the response at. + * @request: firewire request data for the transaction. + * @rcode: response code to send. + * + * Submit a response packet into the asynchronous response transmission queue. The @request + * is going to be released when the transmission successfully finishes later. + */ void fw_send_response(struct fw_card *card, struct fw_request *request, int rcode) { - if (WARN_ONCE(!request, "invalid for FCP address handlers")) - return; - /* unified transaction or broadcast transaction: don't respond */ if (request->ack != ACK_PENDING || HEADER_DESTINATION_IS_BROADCAST(request->request_header[0])) { @@ -935,7 +935,7 @@ static void handle_fcp_region_request(struct fw_card *card, rcu_read_lock(); list_for_each_entry_rcu(handler, &address_handler_list, link) { if (is_enclosing_handler(handler, offset, request->length)) - handler->address_callback(card, NULL, tcode, + handler->address_callback(card, request, tcode, destination, source, p->generation, offset, request->data, diff --git a/drivers/firewire/core.h b/drivers/firewire/core.h index 78c99f1d27fa..eafa4eaae737 100644 --- a/drivers/firewire/core.h +++ b/drivers/firewire/core.h @@ -257,4 +257,10 @@ static inline bool is_ping_packet(u32 *data) return (data[0] & 0xc0ffffff) == 0 && ~data[0] == data[1]; } +static inline bool is_in_fcp_region(u64 offset, size_t length) +{ + return offset >= (CSR_REGISTER_BASE | CSR_FCP_COMMAND) && + offset + length <= (CSR_REGISTER_BASE | CSR_FCP_END); +} + #endif /* _FIREWIRE_CORE_H */ diff --git a/include/linux/firewire.h b/include/linux/firewire.h index 980019053e54..56505436d159 100644 --- a/include/linux/firewire.h +++ b/include/linux/firewire.h @@ -278,9 +278,8 @@ typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode, * Otherwise there is a danger of recursion of inbound and outbound * transactions from and to the local node. * - * The callback is responsible that either fw_send_response() or kfree() - * is called on the @request, except for FCP registers for which the core - * takes care of that. + * The callback is responsible that fw_send_response() is called on the @request, except for FCP + * registers for which the core takes care of that. */ typedef void (*fw_address_callback_t)(struct fw_card *card, struct fw_request *request, From patchwork Fri Jan 20 09:03:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Sakamoto X-Patchwork-Id: 13109451 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A9F16C25B50 for ; Fri, 20 Jan 2023 09:05:36 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 926BD2F50; Fri, 20 Jan 2023 10:04:44 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 926BD2F50 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1674205534; bh=dBGYqwl5Ydgd17Nfk5I4glRpR5GO6ggxZ3jGbu6Ti+k=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: Cc:From; b=hsEcala+LJw8Zo/lIbChzoF47RN5k+q+Lnjn5akXWushrWDg6xDJSBHBqzyzWk9st AErcvUTui7N1U6qakuTz8PmdKzNZGZ6VRCjC9pzCfSNZ5iImKK7e7EeVXaWbZbAwz5 VAJ/eunN/WsBazrX1IYZOVBU0DSSdgKoNl+1FpOU= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 1D43EF80558; Fri, 20 Jan 2023 10:04:08 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id E1AA2F80549; Fri, 20 Jan 2023 10:04:05 +0100 (CET) Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 34C6BF80482 for ; Fri, 20 Jan 2023 10:03:56 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 34C6BF80482 Authentication-Results: alsa1.perex.cz; dkim=pass (2048-bit key, unprotected) header.d=sakamocchi.jp header.i=@sakamocchi.jp header.a=rsa-sha256 header.s=fm3 header.b=rlEW288y; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=FO/dK82R Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 056745C0153; Fri, 20 Jan 2023 04:03:55 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Fri, 20 Jan 2023 04:03:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sakamocchi.jp; h=cc:cc:content-transfer-encoding:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1674205435; x= 1674291835; bh=2GbfxkdYITK9cgpb8Yh7Mhw+V5G40/636EJmCJMDeaE=; b=r lEW288yUhUJxwNj2Dyd+rr1T9I2cIY4uxnXjBIDz/UQ6/ruHGlvms7Ga7E0wmgb6 Bf6DXNI4ZwnwXOCTTz/LvCbCJdIaFWz1sTePysRXD165WYm3ll5WxIizkLc1b71J 3a8yBhyzNOY0bWYh4bMXbQKDSAUDEP0hV9iVQiRwtFUg/Tt3ZuEC5vprICncQTKr WOdYyrr3Lbz2aIgHDDt5C2nTEE8iqYMxLFkjmLz04FwbQjcHdmFlyIswU/M5/XWW NzY1P0dPcooAlKpPPCmi4ap50jq6wBB7IHpxce32I/RzaPjAvGDozXbFfnkqZst2 H4MTwMSoNv3j/39OV+Hbw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1674205435; x=1674291835; bh=2GbfxkdYITK9c gpb8Yh7Mhw+V5G40/636EJmCJMDeaE=; b=FO/dK82RiG4h7+gyFZbaq37tqY0b0 lNZw+NatQY74p4do6MdHsjNUBCjgKycRA6ULtOY97yC243uabZ5JiAiLFIgEuHjA NIscmVF7ibI6WkHPZFEeFIkVRdq2ag2NWpfel5R5uHjSAqfENBgFXAmeYOQffMjD db/FFpAKm8tR/iAsVV7mW+yF7EZPK2GkmKVa2uf8YmJC0n0Fp2UuKtXGQHYjt2G5 R8kkNlzFWJzY3jvO4r7PIcK5iqQSN6R80NwpPU3ij2tnB6s3yJXXi5nqE5LJo02i /FemlBPPkID3qNZVCGzEW49GKRjbhb4dd4PSytZU340njD5luDUKp3iag== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudduuddguddvlecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhephffvvefufffkofgjfhgggfestd ekredtredttdenucfhrhhomhepvfgrkhgrshhhihcuufgrkhgrmhhothhouceoohdqthgr khgrshhhihesshgrkhgrmhhotggthhhirdhjpheqnecuggftrfgrthhtvghrnhepvdejgf ejuedvgfduudekleevtefgtdevhfdtffefiefgveeuteffiedvffekvddtnecuvehluhhs thgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepohdqthgrkhgrshhhih esshgrkhgrmhhotggthhhirdhjph X-ME-Proxy: Feedback-ID: ie8e14432:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 20 Jan 2023 04:03:53 -0500 (EST) From: Takashi Sakamoto To: tiwai@suse.de, stefanr@s5r6.in-berlin.de Subject: [PATCH 3/3] firewire: cdev: use single object to dispatch event for request to IEC 61883-1 FCP region Date: Fri, 20 Jan 2023 18:03:44 +0900 Message-Id: <20230120090344.296451-4-o-takashi@sakamocchi.jp> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230120090344.296451-1-o-takashi@sakamocchi.jp> References: <20230120090344.296451-1-o-takashi@sakamocchi.jp> MIME-Version: 1.0 X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alsa-devel@alsa-project.org, linux1394-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" The core function always passes the data of request to the callback of listener in any case. Additionally, the listener can maintain the lifetime of data by reference count. In character device, no need to duplicate the payload of request anymore to copy it to user space. This commit extends the lifetime of data to obsolete duplication of payload for request in character device. Signed-off-by: Takashi Sakamoto --- drivers/firewire/core-cdev.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/drivers/firewire/core-cdev.c b/drivers/firewire/core-cdev.c index 7fa49e51bae8..2c16ee8fd842 100644 --- a/drivers/firewire/core-cdev.c +++ b/drivers/firewire/core-cdev.c @@ -651,7 +651,7 @@ static void release_request(struct client *client, struct inbound_transaction_resource, resource); if (r->is_fcp) - kfree(r->data); + fw_request_put(r->request); else fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR); @@ -669,12 +669,16 @@ static void handle_request(struct fw_card *card, struct fw_request *request, struct inbound_transaction_resource *r; struct inbound_transaction_event *e; size_t event_size0; - void *fcp_frame = NULL; int ret; /* card may be different from handler->client->device->card */ fw_card_get(card); + // Extend the lifetime of data for request so that its payload is safely accessible in + // the process context for the client. + if (is_fcp) + fw_request_get(request); + r = kmalloc(sizeof(*r), GFP_ATOMIC); e = kmalloc(sizeof(*e), GFP_ATOMIC); if (r == NULL || e == NULL) @@ -686,18 +690,6 @@ static void handle_request(struct fw_card *card, struct fw_request *request, r->data = payload; r->length = length; - if (is_fcp) { - /* - * FIXME: Let core-transaction.c manage a - * single reference-counted copy? - */ - fcp_frame = kmemdup(payload, length, GFP_ATOMIC); - if (fcp_frame == NULL) - goto failed; - - r->data = fcp_frame; - } - r->resource.release = release_request; ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC); if (ret < 0) @@ -739,10 +731,11 @@ static void handle_request(struct fw_card *card, struct fw_request *request, failed: kfree(r); kfree(e); - kfree(fcp_frame); if (!is_fcp) fw_send_response(card, request, RCODE_CONFLICT_ERROR); + else + fw_request_put(request); fw_card_put(card); } @@ -818,7 +811,7 @@ static int ioctl_send_response(struct client *client, union ioctl_arg *arg) r = container_of(resource, struct inbound_transaction_resource, resource); if (r->is_fcp) { - kfree(r->data); + fw_request_put(r->request); goto out; }