hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
kernel/fs/proc/base.c
....@@ -1882,7 +1882,7 @@
18821882 put_pid(pid);
18831883 }
18841884
1885
-struct inode *proc_pid_make_inode(struct super_block * sb,
1885
+struct inode *proc_pid_make_inode(struct super_block *sb,
18861886 struct task_struct *task, umode_t mode)
18871887 {
18881888 struct inode * inode;
....@@ -1911,11 +1911,6 @@
19111911
19121912 /* Let the pid remember us for quick removal */
19131913 ei->pid = pid;
1914
- if (S_ISDIR(mode)) {
1915
- spin_lock(&pid->lock);
1916
- hlist_add_head_rcu(&ei->sibling_inodes, &pid->inodes);
1917
- spin_unlock(&pid->lock);
1918
- }
19191914
19201915 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
19211916 security_task_to_inode(task, inode);
....@@ -1926,6 +1921,39 @@
19261921 out_unlock:
19271922 iput(inode);
19281923 return NULL;
1924
+}
1925
+
1926
+/*
1927
+ * Generating an inode and adding it into @pid->inodes, so that task will
1928
+ * invalidate inode's dentry before being released.
1929
+ *
1930
+ * This helper is used for creating dir-type entries under '/proc' and
1931
+ * '/proc/<tgid>/task'. Other entries(eg. fd, stat) under '/proc/<tgid>'
1932
+ * can be released by invalidating '/proc/<tgid>' dentry.
1933
+ * In theory, dentries under '/proc/<tgid>/task' can also be released by
1934
+ * invalidating '/proc/<tgid>' dentry, we reserve it to handle single
1935
+ * thread exiting situation: Any one of threads should invalidate its
1936
+ * '/proc/<tgid>/task/<pid>' dentry before released.
1937
+ */
1938
+static struct inode *proc_pid_make_base_inode(struct super_block *sb,
1939
+ struct task_struct *task, umode_t mode)
1940
+{
1941
+ struct inode *inode;
1942
+ struct proc_inode *ei;
1943
+ struct pid *pid;
1944
+
1945
+ inode = proc_pid_make_inode(sb, task, mode);
1946
+ if (!inode)
1947
+ return NULL;
1948
+
1949
+ /* Let proc_flush_pid find this directory inode */
1950
+ ei = PROC_I(inode);
1951
+ pid = ei->pid;
1952
+ spin_lock(&pid->lock);
1953
+ hlist_add_head_rcu(&ei->sibling_inodes, &pid->inodes);
1954
+ spin_unlock(&pid->lock);
1955
+
1956
+ return inode;
19291957 }
19301958
19311959 int pid_getattr(const struct path *path, struct kstat *stat,
....@@ -3345,7 +3373,8 @@
33453373 {
33463374 struct inode *inode;
33473375
3348
- inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3376
+ inode = proc_pid_make_base_inode(dentry->d_sb, task,
3377
+ S_IFDIR | S_IRUGO | S_IXUGO);
33493378 if (!inode)
33503379 return ERR_PTR(-ENOENT);
33513380
....@@ -3510,7 +3539,8 @@
35103539 }
35113540
35123541 static const struct inode_operations proc_tid_comm_inode_operations = {
3513
- .permission = proc_tid_comm_permission,
3542
+ .setattr = proc_setattr,
3543
+ .permission = proc_tid_comm_permission,
35143544 };
35153545
35163546 /*
....@@ -3643,7 +3673,8 @@
36433673 struct task_struct *task, const void *ptr)
36443674 {
36453675 struct inode *inode;
3646
- inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3676
+ inode = proc_pid_make_base_inode(dentry->d_sb, task,
3677
+ S_IFDIR | S_IRUGO | S_IXUGO);
36473678 if (!inode)
36483679 return ERR_PTR(-ENOENT);
36493680