Skip to content

Commit f4fee21

Browse files
mjguzikjrjohansen
authored andcommitted
apparmor: try to avoid refing the label in apparmor_file_open
If the label is not stale (which is the common case), the fact that the passed file object holds a reference can be leverged to avoid the ref/unref cycle. Doing so reduces performance impact of apparmor on parallel open() invocations. When benchmarking on a 24-core vm using will-it-scale's open1_process ("Separate file open"), the results are (ops/s): before: 6092196 after: 8309726 (+36%) Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent 4b954a0 commit f4fee21

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

security/apparmor/include/cred.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
6363
return aa_get_newest_label(aa_cred_raw_label(cred));
6464
}
6565

66+
static inline struct aa_label *aa_get_newest_cred_label_condref(const struct cred *cred,
67+
bool *needput)
68+
{
69+
struct aa_label *l = aa_cred_raw_label(cred);
70+
71+
if (unlikely(label_is_stale(l))) {
72+
*needput = true;
73+
return aa_get_newest_label(l);
74+
}
75+
76+
*needput = false;
77+
return l;
78+
}
79+
80+
static inline void aa_put_label_condref(struct aa_label *l, bool needput)
81+
{
82+
if (unlikely(needput))
83+
aa_put_label(l);
84+
}
85+
6686
/**
6787
* aa_current_raw_label - find the current tasks confining label
6888
*

security/apparmor/lsm.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ static int apparmor_file_open(struct file *file)
461461
struct aa_file_ctx *fctx = file_ctx(file);
462462
struct aa_label *label;
463463
int error = 0;
464+
bool needput;
464465

465466
if (!path_mediated_fs(file->f_path.dentry))
466467
return 0;
@@ -477,7 +478,7 @@ static int apparmor_file_open(struct file *file)
477478
return 0;
478479
}
479480

480-
label = aa_get_newest_cred_label(file->f_cred);
481+
label = aa_get_newest_cred_label_condref(file->f_cred, &needput);
481482
if (!unconfined(label)) {
482483
struct mnt_idmap *idmap = file_mnt_idmap(file);
483484
struct inode *inode = file_inode(file);
@@ -494,7 +495,7 @@ static int apparmor_file_open(struct file *file)
494495
/* todo cache full allowed permissions set and state */
495496
fctx->allow = aa_map_file_to_perms(file);
496497
}
497-
aa_put_label(label);
498+
aa_put_label_condref(label, needput);
498499

499500
return error;
500501
}

0 commit comments

Comments
 (0)