From patchwork Sun Jan 8 10:36:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddh Raman Pant X-Patchwork-Id: 13092469 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A86C5C61DB3 for ; Sun, 8 Jan 2023 10:52:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233127AbjAHKwt (ORCPT ); Sun, 8 Jan 2023 05:52:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232979AbjAHKwr (ORCPT ); Sun, 8 Jan 2023 05:52:47 -0500 Received: from sender-of-o50.zoho.in (sender-of-o50.zoho.in [103.117.158.50]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A706B13E33; Sun, 8 Jan 2023 02:52:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673174206; cv=none; d=zohomail.in; s=zohoarc; b=akXCCA4JuxnOgto/J8q1kb+rip57LQ+dko8pa4VesnXQCZtDQZyjwTbUWJ79SoCtrDPAWvD5P3jEAExAq7m2VE4PJ6E9/nEp3oTC0vcpTUZtSRXygu3kXdlLzAI2qEcECMnnf/lSar/eIFpy0YAb4tf9BsYl6lemqLV8NcD5cHg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1673174206; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=i1VfZ1loxuzJE4HpGT3JsDM7dhG8ZhXmhmOet+rOcV0=; b=IqwfeT5R13uy0cuxTkQYXYexmJXDxsn1TKJMQGIBV7cOnon9Y0pskrXlasexqxASoVRIsAUbKKvPa5Mithc714nFOQ3E1kJ2CC67Hzz30lbmHohwEzMWUazRmuYvO+9NtOp0CtTYf1sfiLxloHpBLzSEalRMWGOIibj9eMFUZc4= ARC-Authentication-Results: i=1; mx.zohomail.in; dkim=pass header.i=siddh.me; spf=pass smtp.mailfrom=code@siddh.me; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1673174206; s=zmail; d=siddh.me; i=code@siddh.me; h=From:From:To:To:Cc:Cc:Message-ID:Subject:Subject:Date:Date:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Reply-To; bh=i1VfZ1loxuzJE4HpGT3JsDM7dhG8ZhXmhmOet+rOcV0=; b=amaa5UjyejDEI5LrocbxMM3bBAMy4TBXNeX0QgM10CrpeE5z9uHNfPCgUqm2XkUK 1N9pcnZux/Jej5sYtOO/vYGuaArPchnjuo6VVwPFsXSQemFEhLAHtgX+LqHOMdU3qp7 aillHXO9pvIeAP25d3Ul26C9lyjS9L8G6JPPXrKg= Received: from kampyooter.. (110.226.31.37 [110.226.31.37]) by mx.zoho.in with SMTPS id 1673174205857144.87777906120232; Sun, 8 Jan 2023 16:06:45 +0530 (IST) From: Siddh Raman Pant To: Mauro Carvalho Chehab , Randy Dunlap , David Howells , Jonathan Corbet , "Fabio M. De Francesco" , Eric Dumazet , Christophe JAILLET , Eric Biggers Cc: keyrings , linux-security-module , linux-fsdevel , linux-kernel Message-ID: <97ce37e2fdcfbed29d9467057f0f870359d88b89.1673173920.git.code@siddh.me> Subject: [PATCH v3 1/2] include/linux/watch_queue: Improve documentation Date: Sun, 8 Jan 2023 16:06:31 +0530 X-Mailer: git-send-email 2.39.0 In-Reply-To: References: MIME-Version: 1.0 X-ZohoMailClient: External Precedence: bulk List-ID: Introduce kerneldoc-style comments, and document a couple of things explicitly. Signed-off-by: Siddh Raman Pant --- include/linux/watch_queue.h | 102 ++++++++++++++++++++++++++---------- 1 file changed, 75 insertions(+), 27 deletions(-) diff --git a/include/linux/watch_queue.h b/include/linux/watch_queue.h index fc6bba20273b..7f6eea4a33a6 100644 --- a/include/linux/watch_queue.h +++ b/include/linux/watch_queue.h @@ -18,57 +18,103 @@ struct cred; +/** + * struct watch_type_filter - Filter on watch type + * + * @type: Type of watch_notification + * @subtype_filter: Bitmask of subtypes to filter on + * @info_filter: Filter on watch_notification::info + * @info_mask: Mask of relevant bits in info_filter + */ struct watch_type_filter { enum watch_notification_type type; - __u32 subtype_filter[1]; /* Bitmask of subtypes to filter on */ - __u32 info_filter; /* Filter on watch_notification::info */ - __u32 info_mask; /* Mask of relevant bits in info_filter */ + __u32 subtype_filter[1]; + __u32 info_filter; + __u32 info_mask; }; +/** + * struct watch_filter - Filter on watch + * + * @rcu: RCU head (in union with type_filter) + * @type_filter: Bitmask of accepted types (in union with rcu) + * @nr_filters: Number of filters + * @filters: Array of watch_type_filter + */ struct watch_filter { union { struct rcu_head rcu; - /* Bitmask of accepted types */ DECLARE_BITMAP(type_filter, WATCH_TYPE__NR); }; - u32 nr_filters; /* Number of filters */ + u32 nr_filters; struct watch_type_filter filters[]; }; +/** + * struct watch_queue - General notification queue + * + * @rcu: RCU head + * @filter: Filter to use on watches + * @pipe: The pipe we're using as a buffer + * @watches: Contributory watches + * @notes: Preallocated notifications + * @notes_bitmap: Allocation bitmap for notes + * @usage: Object usage count + * @lock: To serialize accesses and removes + * @nr_notes: Number of notes + * @nr_pages: Number of pages in notes[] + * @defunct: True when queues closed + */ struct watch_queue { struct rcu_head rcu; struct watch_filter __rcu *filter; - struct pipe_inode_info *pipe; /* The pipe we're using as a buffer */ - struct hlist_head watches; /* Contributory watches */ - struct page **notes; /* Preallocated notifications */ - unsigned long *notes_bitmap; /* Allocation bitmap for notes */ - struct kref usage; /* Object usage count */ + struct pipe_inode_info *pipe; + struct hlist_head watches; + struct page **notes; + unsigned long *notes_bitmap; + struct kref usage; spinlock_t lock; - unsigned int nr_notes; /* Number of notes */ - unsigned int nr_pages; /* Number of pages in notes[] */ - bool defunct; /* T when queues closed */ + unsigned int nr_notes; + unsigned int nr_pages; + bool defunct; }; -/* - * Representation of a watch on an object. +/** + * struct watch - Representation of a watch on an object + * + * @rcu: RCU head (in union with info_id) + * @info_id: ID to be OR'd in to info field (in union with rcu) + * @queue: Queue to post events to + * @queue_node: Link in queue->watches + * @watch_list: The watch list containing this watch + * @list_node: Link in watch_list->watchers + * @cred: Creds of the owner of the watch + * @private: Private data for the watched object + * @id: Internal identifier + * @usage: Object usage count */ struct watch { union { struct rcu_head rcu; - u32 info_id; /* ID to be OR'd in to info field */ + u32 info_id; }; - struct watch_queue __rcu *queue; /* Queue to post events to */ - struct hlist_node queue_node; /* Link in queue->watches */ + struct watch_queue __rcu *queue; + struct hlist_node queue_node; struct watch_list __rcu *watch_list; - struct hlist_node list_node; /* Link in watch_list->watchers */ - const struct cred *cred; /* Creds of the owner of the watch */ - void *private; /* Private data for the watched object */ - u64 id; /* Internal identifier */ - struct kref usage; /* Object usage count */ + struct hlist_node list_node; + const struct cred *cred; + void *private; + u64 id; + struct kref usage; }; -/* - * List of watches on an object. +/** + * struct watch_list - List of watches on an object + * + * @rcu: RCU head + * @watchers: List head + * @release_watch: Function to release watch + * @lock: To protect addition and removal of watches */ struct watch_list { struct rcu_head rcu; @@ -118,8 +164,10 @@ static inline void remove_watch_list(struct watch_list *wlist, u64 id) } /** - * watch_sizeof - Calculate the information part of the size of a watch record, - * given the structure size. + * watch_sizeof() - Calculate the information part of the size of a watch + * record, given the structure size. + * + * @STRUCT: The structure whose size is to be given */ #define watch_sizeof(STRUCT) (sizeof(STRUCT) << WATCH_INFO_LENGTH__SHIFT) From patchwork Sun Jan 8 10:36:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddh Raman Pant X-Patchwork-Id: 13092467 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 550C4C54EBC for ; Sun, 8 Jan 2023 10:52:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233069AbjAHKwr (ORCPT ); Sun, 8 Jan 2023 05:52:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231454AbjAHKwq (ORCPT ); Sun, 8 Jan 2023 05:52:46 -0500 Received: from sender-of-o50.zoho.in (sender-of-o50.zoho.in [103.117.158.50]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D85C13E26; Sun, 8 Jan 2023 02:52:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673174207; cv=none; d=zohomail.in; s=zohoarc; b=QbZrTUpSmkOYv7vjHClErIi+mibauz+HcRYC/sNwC102L6vBR7Iq5X2+77N6rRkPvB9Coo03qoiNIYbgZUvLdPOF24KFsZuqWfOthzPU+omDMpzRmJQjugzGc4uGa7B9WDTY8pBJ7Yc4Q7o2yrg1+u+8QZP0YfiQAmHEXXpTyCY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1673174207; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=+jigenlUh+9Y3CbE3GiZGHYIBoFAXIN6GyyVO/YTVrc=; b=V0vjWOsbpi5xmCzh2i6OxFh14YCdxkOx6qNi/ktu59JzvmPve0VdmxVJjolIhX0n69W9NTXm+GfANKvnAfb1MJ7UZxvPgr08ij01bpOjFg5qcCMHFffrmWPB1KjjpNllx4XHzl9uMpRFSdJ7fS1RTMKkBZ975s5zZsVpeywC2ZQ= ARC-Authentication-Results: i=1; mx.zohomail.in; dkim=pass header.i=siddh.me; spf=pass smtp.mailfrom=code@siddh.me; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1673174207; s=zmail; d=siddh.me; i=code@siddh.me; h=From:From:To:To:Cc:Cc:Message-ID:Subject:Subject:Date:Date:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Reply-To; bh=+jigenlUh+9Y3CbE3GiZGHYIBoFAXIN6GyyVO/YTVrc=; b=hwfg/lH5Z7JO4CQq4K81aOEinouUIe9huOOIGpGNr62JtVh8nPKB+ZslVGThEjoJ 4RlZsaGGB5zQe2tZPxerFxq8bgwzOIrkxA669FZU8CPV/GSVQG8LEpubq8OPwcl17ei K1IcSijCT/hmSwzW6UFvfWumU/NbFH0YJgWHhNxU= Received: from kampyooter.. (110.226.31.37 [110.226.31.37]) by mx.zoho.in with SMTPS id 167317420635758.71149531098649; Sun, 8 Jan 2023 16:06:46 +0530 (IST) From: Siddh Raman Pant To: Mauro Carvalho Chehab , Randy Dunlap , David Howells , Jonathan Corbet , "Fabio M. De Francesco" , Eric Dumazet , Christophe JAILLET , Eric Biggers Cc: keyrings , linux-security-module , linux-fsdevel , linux-kernel Message-ID: Subject: [PATCH v3 2/2] kernel/watch_queue: NULL the dangling *pipe, and use it for clear check Date: Sun, 8 Jan 2023 16:06:32 +0530 X-Mailer: git-send-email 2.39.0 In-Reply-To: References: MIME-Version: 1.0 X-ZohoMailClient: External Precedence: bulk List-ID: NULL the dangling pipe reference while clearing watch_queue. If not done, a reference to a freed pipe remains in the watch_queue, as this function is called before freeing a pipe in free_pipe_info() (see line 834 of fs/pipe.c). The sole use of wqueue->defunct is for checking if the watch queue has been cleared, but wqueue->pipe is also NULLed while clearing. Thus, wqueue->defunct is superfluous, as wqueue->pipe can be checked for NULL. Hence, the former can be removed. Signed-off-by: Siddh Raman Pant --- include/linux/watch_queue.h | 4 +--- kernel/watch_queue.c | 12 ++++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/include/linux/watch_queue.h b/include/linux/watch_queue.h index 7f6eea4a33a6..63592c597ec9 100644 --- a/include/linux/watch_queue.h +++ b/include/linux/watch_queue.h @@ -55,7 +55,7 @@ struct watch_filter { * * @rcu: RCU head * @filter: Filter to use on watches - * @pipe: The pipe we're using as a buffer + * @pipe: The pipe we're using as a buffer, NULL when queue is cleared/closed * @watches: Contributory watches * @notes: Preallocated notifications * @notes_bitmap: Allocation bitmap for notes @@ -63,7 +63,6 @@ struct watch_filter { * @lock: To serialize accesses and removes * @nr_notes: Number of notes * @nr_pages: Number of pages in notes[] - * @defunct: True when queues closed */ struct watch_queue { struct rcu_head rcu; @@ -76,7 +75,6 @@ struct watch_queue { spinlock_t lock; unsigned int nr_notes; unsigned int nr_pages; - bool defunct; }; /** diff --git a/kernel/watch_queue.c b/kernel/watch_queue.c index a6f9bdd956c3..6ead921c15c0 100644 --- a/kernel/watch_queue.c +++ b/kernel/watch_queue.c @@ -43,7 +43,7 @@ MODULE_LICENSE("GPL"); static inline bool lock_wqueue(struct watch_queue *wqueue) { spin_lock_bh(&wqueue->lock); - if (unlikely(wqueue->defunct)) { + if (unlikely(!wqueue->pipe)) { spin_unlock_bh(&wqueue->lock); return false; } @@ -105,9 +105,6 @@ static bool post_one_notification(struct watch_queue *wqueue, unsigned int head, tail, mask, note, offset, len; bool done = false; - if (!pipe) - return false; - spin_lock_irq(&pipe->rd_wait.lock); mask = pipe->ring_size - 1; @@ -603,8 +600,11 @@ void watch_queue_clear(struct watch_queue *wqueue) rcu_read_lock(); spin_lock_bh(&wqueue->lock); - /* Prevent new notifications from being stored. */ - wqueue->defunct = true; + /* + * This pipe can be freed by callers like free_pipe_info(). + * Removing this reference also prevents new notifications. + */ + wqueue->pipe = NULL; while (!hlist_empty(&wqueue->watches)) { watch = hlist_entry(wqueue->watches.first, struct watch, queue_node);