selinux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ondrej Mosnacek <omosnace@redhat.com>
To: Stephen Smalley <sds@tycho.nsa.gov>
Cc: SElinux list <selinux@vger.kernel.org>, Paul Moore <paul@paul-moore.com>
Subject: Re: [PATCH 2/2] selinux: optimize storage of filename transitions
Date: Tue, 11 Feb 2020 22:32:07 +0100	[thread overview]
Message-ID: <CAFqZXNv2ymj9UKr-P=QFaCKGJW2T9ve4gC=1-72Uq_K2rZDhnw@mail.gmail.com> (raw)
In-Reply-To: <19a1cea7-42d5-8cbe-722a-dc05cc6a38a3@tycho.nsa.gov>

On Tue, Feb 11, 2020 at 9:51 PM Stephen Smalley <sds@tycho.nsa.gov> wrote:
>
> On 2/11/20 11:39 AM, Ondrej Mosnacek wrote:
> > In these rules, each rule with the same (target type, target class,
> > filename) values is (in practice) always mapped to the same result type.
> > Therefore, it is much more efficient to group the rules by (ttype,
> > tclass, filename).
> >
> > Thus, this patch drops the stype field from the key and changes the
> > datum to be a linked list of one or more structures that contain a
> > result type and an ebitmap of source types that map the given target to
> > the given result type under the given filename. The size of the hash
> > table is also incremented to 2048 to be more optimal for Fedora policy
> > (which currently has ~2500 unique (ttype, tclass, filename) tuples,
> > regardless of whether the 'unconfined' module is enabled).
> >
> > Not only does this dramtically reduce memory usage when the policy
> > contains a lot of unconfined domains (ergo a lot of filename based
> > transitions), but it also slightly reduces memory usage of strongly
> > confined policies (modeled on Fedora policy with 'unconfined' module
> > disabled) and significantly reduces lookup times of these rules on
> > Fedora (roughly matches the performance of the rhashtable conversion
> > patch [1] posted recently to selinux@vger.kernel.org).
> >
> > An obvious next step is to change binary policy format to match this
> > layout, so that disk space is also saved. However, since that requires
> > more work (including matching userspace changes) and this patch is
> > already beneficial on its own, I'm posting it separately.
> >
> > Performance/memory usage comparison:
> >
> > Kernel           | Policy load | Policy load   | Mem usage | Mem usage     | openbench
> >                   |             | (-unconfined) |           | (-unconfined) | (createfiles)
> > -----------------|-------------|---------------|-----------|---------------|--------------
> > reference        |       1,30s |         0,91s |      90MB |          77MB | 55 us/file
> > rhashtable patch |       0.98s |         0,85s |      85MB |          75MB | 38 us/file
> > this patch       |       0,95s |         0,87s |      75MB |          75MB | 40 us/file
> >
> > (Memory usage is measured after boot. With SELinux disabled the memory
> > usage was ~60MB on the same system.)
> >
> > [1] https://lore.kernel.org/selinux/20200116213937.77795-1-dev@lynxeye.de/T/
> >
> > Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
> > ---
> >   security/selinux/ss/policydb.c | 175 ++++++++++++++++++++-------------
> >   security/selinux/ss/policydb.h |   8 +-
> >   security/selinux/ss/services.c |  16 +--
> >   3 files changed, 120 insertions(+), 79 deletions(-)
> >
> > diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c
> > index 981797bfc547..62283033bb7d 100644
> > --- a/security/selinux/ss/policydb.c
> > +++ b/security/selinux/ss/policydb.c
> > @@ -1882,64 +1884,93 @@ out:
> >
> >   static int filename_trans_read_one(struct policydb *p, void *fp)
> >   {
> <snip>
> > +     exists = false;
> > +     last = NULL;
> > +     datum = hashtab_search(p->filename_trans, &key);
> > +     while (datum) {
> > +             if (ebitmap_get_bit(&datum->stypes, stype - 1)) {
> > +                     exists = true;
> > +                     break;
> > +             }
> > +             if (datum->otype == otype) {
> > +                     last = NULL;
>
> Why set last to NULL here?  Seemingly unused afterward if datum is non-NULL?

Good point. I don't like unnecessary code, so I'll wait a while for
possible other comments and then do a v2.

-- 
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.


      reply	other threads:[~2020-02-11 21:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-11 16:39 [PATCH 0/2] Optimize storage of filename transitions Ondrej Mosnacek
2020-02-11 16:39 ` [PATCH 1/2] selinux: factor out loop body from filename_trans_read() Ondrej Mosnacek
2020-02-11 16:39 ` [PATCH 2/2] selinux: optimize storage of filename transitions Ondrej Mosnacek
2020-02-11 20:52   ` Stephen Smalley
2020-02-11 21:32     ` Ondrej Mosnacek [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFqZXNv2ymj9UKr-P=QFaCKGJW2T9ve4gC=1-72Uq_K2rZDhnw@mail.gmail.com' \
    --to=omosnace@redhat.com \
    --cc=paul@paul-moore.com \
    --cc=sds@tycho.nsa.gov \
    --cc=selinux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).